private PaymentProcessingResult ProcessPaymentCapture(IOrderGroup orderGroup, ICreditCardPayment creditCardPayment, IOrderAddress billingAddress) { if (string.IsNullOrEmpty(creditCardPayment.ProviderPaymentId)) { return(Charge(orderGroup, creditCardPayment, billingAddress)); } try { var capture = _stripeChargeService.Capture(creditCardPayment.ProviderPaymentId, new StripeChargeCaptureOptions()); if (!string.IsNullOrEmpty(capture.FailureCode)) { return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate(capture.Outcome.Reason))); } creditCardPayment.ProviderPaymentId = capture.Id; creditCardPayment.ProviderTransactionID = capture.BalanceTransactionId; return(PaymentProcessingResult.CreateSuccessfulResult("")); } catch (StripeException e) { switch (e.StripeError.ErrorType) { case "card_error": return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate(e.StripeError.Code))); default: return(PaymentProcessingResult.CreateUnsuccessfulResult(e.StripeError.Message)); } } }
private PaymentProcessingResult ProcessPaymentRefund(IOrderGroup orderGroup, ICreditCardPayment creditCardPayment) { var refundAmount = creditCardPayment.Amount; var purchaseOrder = (IPurchaseOrder)orderGroup; if (purchaseOrder == null || refundAmount <= 0 || string.IsNullOrEmpty(creditCardPayment.ProviderPaymentId)) { return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate("RefundError"))); } try { var refundOptions = new StripeRefundCreateOptions() { Amount = (int)refundAmount * GetMultiplier(orderGroup.Currency), Reason = StripeRefundReasons.RequestedByCustomer }; var refund = _stripeRefundService.Create(creditCardPayment.ProviderPaymentId, refundOptions); // Extract the response details. creditCardPayment.TransactionID = refund.Id; var message = $"[{creditCardPayment.PaymentMethodName}] [RefundTransaction-{refund.Id}] " + $"Response: {refund.Status} at Timestamp={refund.Created.ToString()}: {refund.Amount}{refund.Currency}"; // add a new order note about this refund AddNoteToPurchaseOrder("REFUND", message, purchaseOrder.CustomerId, purchaseOrder); _orderRepository.Service.Save(purchaseOrder); return(PaymentProcessingResult.CreateSuccessfulResult(message)); } catch (StripeException e) { switch (e.StripeError.ErrorType) { case "card_error": return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate(e.StripeError.Code))); default: return(PaymentProcessingResult.CreateUnsuccessfulResult(e.StripeError.Message)); } } }
private PaymentProcessingResult Charge(IOrderGroup cart, ICreditCardPayment creditCardPayment, IOrderAddress billingAddress) { var contact = CustomerContext.Current.GetContactById(cart.CustomerId); StripeCustomer stripeCustomer = null; try { if (contact != null) { if (!string.IsNullOrEmpty(contact?["StripeId"]?.ToString())) { stripeCustomer = _stripeCustomerService.Get(contact["StripeId"].ToString()); } if (stripeCustomer == null) { stripeCustomer = _stripeCustomerService.Create(new StripeCustomerCreateOptions { Email = contact.Email, SourceToken = creditCardPayment.CreditCardNumber }); contact["StripeId"] = stripeCustomer.Id; contact.SaveChanges(); } } Enum.TryParse(creditCardPayment.TransactionType, out TransactionType transactionType); var options = new StripeChargeCreateOptions { Amount = (int)creditCardPayment.Amount * GetMultiplier(cart.Currency), Description = "Ecommerce Charge", Currency = cart.Currency.ToString().ToLower(), Capture = transactionType == TransactionType.Capture || transactionType == TransactionType.CaptureOnly }; if (stripeCustomer != null) { options.CustomerId = stripeCustomer.Id; } else { options.SourceTokenOrExistingSourceId = creditCardPayment.CreditCardNumber; } var charge = _stripeChargeService.Create(options); if (!string.IsNullOrEmpty(charge.FailureCode)) { return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate(charge.Outcome.Reason))); } creditCardPayment.ProviderPaymentId = charge.Id; creditCardPayment.ProviderTransactionID = charge.BalanceTransactionId; creditCardPayment.Properties["stripe_CustomerId"] = stripeCustomer?.Id; return(PaymentProcessingResult.CreateSuccessfulResult("")); } catch (StripeException e) { switch (e.StripeError.ErrorType) { case "card_error": return(PaymentProcessingResult.CreateUnsuccessfulResult(Translate(e.StripeError.Code))); default: return(PaymentProcessingResult.CreateUnsuccessfulResult(e.StripeError.Message)); } } }
public Purchase(ICreditCardPayment creditCardPayment, IAddressProvider addressProvider, IItemCatalog itemCatalog) { this.creditCardPayment = creditCardPayment; this.addressProvider = addressProvider; this.itemCatalog = itemCatalog; }