/// <summary> /// Gets a single use token that can be used in place of a credit card details. /// The token can be used once for creating a new charge. /// </summary> /// <param name="creditCard"></param> /// <param name="address"></param> /// <param name="settings"></param> /// <returns></returns> public static string GetCardToken(CreditCardFormData creditCard, IAddress address, StripeProcessorSettings settings) { var requestParams = new NameValueCollection(); requestParams.Add("card[number]", creditCard.CardNumber); requestParams.Add("card[exp_month]", creditCard.ExpireMonth); requestParams.Add("card[exp_year]", creditCard.ExpireYear); requestParams.Add("card[cvc]", creditCard.CardCode); requestParams.Add("card[name]", creditCard.CardholderName); if (address != null) { requestParams.Add("card[address_line1]", address.Address1); requestParams.Add("card[address_line2]", address.Address2); requestParams.Add("card[address_city]", address.Locality); if (!string.IsNullOrEmpty(address.Region)) { requestParams.Add("card[address_state]", address.Region); } requestParams.Add("card[address_zip]", address.PostalCode); if (!string.IsNullOrEmpty(address.CountryCode)) { requestParams.Add("card[address_country]", address.CountryCode); } } string postData = requestParams.AllKeys.Aggregate("", (current, key) => current + (key + "=" + HttpUtility.UrlEncode(requestParams[key]) + "&")) .TrimEnd('&'); // https://stripe.com/docs/api#create_card_token var response = StripeHelper.MakeStripeApiRequest("https://api.stripe.com/v1/tokens", "POST", requestParams, settings); string apiResponse = null; using (var reader = new StreamReader(response.GetResponseStream())) { apiResponse = reader.ReadToEnd(); } JObject responseJson = JObject.Parse(apiResponse); switch (response.StatusCode) { case HttpStatusCode.OK: // 200 return((string)responseJson["id"]); default: throw new Exception("Stripe error"); } }
public IPaymentResult PriorAuthorizeCapturePayment(IInvoice invoice, IPayment payment) { string stripeChargeId = payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.StripeChargeId); string url = string.Format("https://api.stripe.com/v1/charges/{0}/capture", stripeChargeId); try { var response = StripeHelper.MakeStripeApiRequest(url, "POST", null, _settings); return(GetCapturePaymentResult(invoice, payment, response)); } catch (WebException ex) { return(GetCapturePaymentResult(invoice, payment, (HttpWebResponse)ex.Response)); } }
/// <summary> /// Processes the Authorize and AuthorizeAndCapture transactions /// </summary> /// <param name="invoice">The <see cref="IInvoice" /> to be paid</param> /// <param name="payment">The <see cref="IPayment" /> record</param> /// <param name="transactionMode">Authorize or AuthorizeAndCapture</param> /// <param name="amount">The money amount to be processed</param> /// <param name="creditCard">The <see cref="CreditCardFormData" /></param> /// <returns>The <see cref="IPaymentResult" /></returns> public IPaymentResult ProcessPayment(IInvoice invoice, IPayment payment, TransactionMode transactionMode, decimal amount, CreditCardFormData creditCard) { if (!IsValidCurrencyCode(invoice.CurrencyCode())) { return(new PaymentResult(Attempt <IPayment> .Fail(payment, new Exception("Invalid currency")), invoice, false)); } // The minimum amount is $0.50 (or equivalent in charge currency). // Test that the payment meets the minimum amount (for USD only). if (invoice.CurrencyCode() == "USD") { if (amount < 0.5m) { return (new PaymentResult( Attempt <IPayment> .Fail(payment, new Exception("Invalid amount (less than 0.50 USD)")), invoice, false)); } } else { if (amount < 1) { return (new PaymentResult( Attempt <IPayment> .Fail(payment, new Exception("Invalid amount (less than 1 " + invoice.CurrencyCode() + ")")), invoice, false)); } } var requestParams = StripeHelper.PreparePostDataForProcessPayment(invoice.GetBillingAddress(), transactionMode, ConvertAmount(invoice, amount), invoice.CurrencyCode(), creditCard, invoice.PrefixedInvoiceNumber(), string.Format("Full invoice #{0}", invoice.PrefixedInvoiceNumber())); // https://stripe.com/docs/api#create_charge try { var response = StripeHelper.MakeStripeApiRequest("https://api.stripe.com/v1/charges", "POST", requestParams, _settings); return(GetProcessPaymentResult(invoice, payment, response)); } catch (WebException ex) { return(GetProcessPaymentResult(invoice, payment, (HttpWebResponse)ex.Response)); } }
public IPaymentResult VoidPayment(IInvoice invoice, IPayment payment) { // Stripe does not seem to have a Void method, so we do a full refund string stripeChargeId = payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.StripeChargeId); if (!payment.Authorized || string.IsNullOrEmpty(stripeChargeId)) { return (new PaymentResult( Attempt <IPayment> .Fail(payment, new InvalidOperationException("Payment is not Authorized or Stripe charge id not present")), invoice, false)); } string url = string.Format("https://api.stripe.com/v1/charges/{0}/refunds", stripeChargeId); var response = StripeHelper.MakeStripeApiRequest(url, "POST", null, _settings); return(GetRefundPaymentResult(invoice, payment, response)); }
public IPaymentResult RefundPayment(IInvoice invoice, IPayment payment, decimal amount) { string stripeChargeId = payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.StripeChargeId); if (!payment.Authorized || string.IsNullOrEmpty(stripeChargeId)) { return (new PaymentResult( Attempt <IPayment> .Fail(payment, new InvalidOperationException("Payment is not Authorized or Stripe charge id not present")), invoice, false)); } string url = string.Format("https://api.stripe.com/v1/charges/{0}/refunds", stripeChargeId); var requestParams = new NameValueCollection(); requestParams.Add("amount", ConvertAmount(invoice, amount)); var response = StripeHelper.MakeStripeApiRequest(url, "POST", requestParams, _settings); return(GetRefundPaymentResult(invoice, payment, response)); }