public static TransactionResult StopSubscription(TransactionRequest request, StripeSettings stripeSettings, ILogger logger) { var subscriptionId = request.GetParameterAs <string>("subscriptionId"); InitStripe(stripeSettings, true); var subscriptionService = new SubscriptionService(); var subscription = subscriptionService.Cancel(subscriptionId, new SubscriptionCancelOptions()); var stopResult = new TransactionResult() { TransactionGuid = Guid.NewGuid().ToString(), ResponseParameters = new Dictionary <string, object>() { { "canceledAt", subscription.CanceledAt }, }, OrderGuid = request.Order.Guid, TransactionCurrencyCode = request.Order.CurrencyCode, }; if (subscription.Status == "canceled") { stopResult.Success = true; stopResult.NewStatus = PaymentStatus.Complete; return(stopResult); } else { logger.Log <TransactionResult>(LogLevel.Warning, "The subscription cancellation request for Order#" + stopResult.Order.Id + " by stripe failed." + subscription.StripeResponse.Content); stopResult.Success = false; stopResult.Exception = new Exception("An error occurred while processing refund"); return(stopResult); } }
public static TransactionResult ProcessCapture(TransactionRequest captureRequest, SquareSettings squareSettings, ILogger logger) { var order = captureRequest.Order; var config = GetConfiguration(squareSettings); var paymentId = captureRequest.GetParameterAs <string>("paymentId"); var paymentsApi = new PaymentsApi(config); //perform the call var paymentResponse = paymentsApi.CompletePayment(paymentId); var transactionResult = new TransactionResult() { OrderGuid = order.Guid, TransactionGuid = Guid.NewGuid().ToString(), }; if (paymentResponse != null) { var payment = paymentResponse.Payment; transactionResult.Success = true; transactionResult.TransactionAmount = (decimal)(payment.AmountMoney.Amount ?? 0) / 100; transactionResult.ResponseParameters = new Dictionary <string, object>() { { "PaymentId", payment.Id }, { "ReferenceId", payment.ReferenceId } }; if (payment.Status == "COMPLETED") { transactionResult.NewStatus = PaymentStatus.Complete; } else { transactionResult.NewStatus = PaymentStatus.Failed; var errors = string.Join(",", paymentResponse.Errors); logger.Log <TransactionResult>(LogLevel.Warning, "The capture for Order#" + order.Id + " by square failed." + errors); transactionResult.Exception = new Exception("An error occurred while capturing payment. Error Details: " + errors); transactionResult.Success = false; } } else { logger.Log <TransactionResult>(LogLevel.Warning, "The capture for Order#" + order.Id + " by square failed. No response received."); transactionResult.Success = false; transactionResult.Exception = new Exception("An error occurred while capturing payment"); } return(transactionResult); }
public static TransactionResult ProcessRefund(TransactionRequest refundRequest, StripeSettings stripeSettings, ILogger logger) { InitStripe(stripeSettings, true); var refundService = new RefundService(); var order = refundRequest.Order; var address = order.BillingAddressSerialized.To <Address>(); GetFinalAmountDetails(refundRequest.Amount ?? refundRequest.Order.OrderTotal, refundRequest.Order.CurrencyCode, address, out _, out var amount); var refundOptions = new RefundCreateOptions { Charge = refundRequest.GetParameterAs <string>("chargeId"), Amount = 100 * (long)(amount), }; var refund = refundService.Create(refundOptions); var refundResult = new TransactionResult() { TransactionGuid = Guid.NewGuid().ToString(), ResponseParameters = new Dictionary <string, object>() { { "sourceTransferReversalId", refund.SourceTransferReversalId }, { "balanceTransactionId", refund.BalanceTransactionId }, { "chargeId", refund.ChargeId }, { "receiptNumber", refund.ReceiptNumber }, }, OrderGuid = refundRequest.Order.Guid, TransactionCurrencyCode = refund.Currency, TransactionAmount = (decimal)refund.Amount / 100 }; if (refund.Status == "failed") { logger.Log <TransactionResult>(LogLevel.Warning, "The refund for Order#" + refundRequest.Order.Id + " by stripe failed." + refund.FailureReason); refundResult.Success = false; refundResult.Exception = new Exception("An error occurred while processing refund"); return(refundResult); } if (refund.Status == "succeeded") { refundResult.NewStatus = refundRequest.IsPartialRefund ? PaymentStatus.RefundedPartially : PaymentStatus.Refunded; refundResult.Success = true; } return(refundResult); }
public static TransactionResult ProcessCapture(TransactionRequest captureRequest, StripeSettings stripeSettings, ILogger logger) { InitStripe(stripeSettings, true); var chargeService = new ChargeService(); var chargeId = captureRequest.GetParameterAs <string>("chargeId"); var charge = chargeService.Capture(chargeId, new ChargeCaptureOptions() { Amount = 100 * (long)(captureRequest.Amount ?? captureRequest.Order.OrderTotal) }); var captureResult = new TransactionResult() { ResponseParameters = new Dictionary <string, object>() { { "chargeId", charge.Id }, { "balanceTransactionId", charge.BalanceTransactionId }, { "disputeId", charge.DisputeId }, { "invoiceId", charge.InvoiceId }, }, OrderGuid = captureRequest.Order.Guid, TransactionAmount = (decimal)charge.Amount / 100 }; if (charge.Status == "failed") { logger.Log <TransactionResult>(LogLevel.Warning, "The capture for Order#" + captureRequest.Order.Id + " by stripe failed."); captureResult.Success = false; captureResult.Exception = new Exception("An error occurred while processing capture"); return(captureResult); } if (charge.Captured.GetValueOrDefault()) { captureResult.NewStatus = PaymentStatus.Complete; captureResult.Success = true; } return(captureResult); }
public static TransactionResult ProcessPayment(TransactionRequest request, SquareSettings squareSettings, ILogger logger) { var order = request.Order; var config = GetConfiguration(squareSettings); var nonce = request.GetParameterAs <string>("nonce"); var location = GetApplicationLocations(squareSettings, logger).FirstOrDefault(x => x.Id == squareSettings.LocationId); var billingAddress = order.BillingAddressSerialized.To <EvenCart.Data.Entity.Addresses.Address>(); var shippingAddress = order.ShippingAddressSerialized?.To <EvenCart.Data.Entity.Addresses.Address>(); var customerId = order.User.GetPropertyValueAs <string>(SquareCustomerIdKey); if (customerId.IsNullEmptyOrWhiteSpace()) { var createCustomerRequest = new CreateCustomerRequest(EmailAddress: order.User.Email, GivenName: order.User.Name); var customerApi = new CustomersApi(config); var customerCreateResponse = customerApi.CreateCustomer(createCustomerRequest); customerId = customerCreateResponse.Customer.Id; } var paymentRequest = new CreatePaymentRequest(SourceId: nonce, IdempotencyKey: Guid.NewGuid().ToString(), LocationId: location?.Id, CustomerId: customerId, AmountMoney: new Money() { Amount = (long)((request.Amount ?? order.OrderTotal) * 100), Currency = order.CurrencyCode }, AppFeeMoney: new Money() { Amount = (long)(100 * order.PaymentMethodFee), Currency = order.CurrencyCode }, BillingAddress: new Address(billingAddress.Address1, billingAddress.Address2, FirstName: billingAddress.Name, Locality: billingAddress.Landmark, PostalCode: billingAddress.ZipPostalCode), BuyerEmailAddress: order.User.Email ); if (shippingAddress != null) { paymentRequest.ShippingAddress = new Address(shippingAddress.Address1, shippingAddress.Address2, FirstName: shippingAddress.Name, Locality: shippingAddress.Landmark, PostalCode: shippingAddress.ZipPostalCode); } if (squareSettings.AuthorizeOnly) { paymentRequest.Autocomplete = false; //authorize only } var paymentsApi = new PaymentsApi(config); var paymentResponse = paymentsApi.CreatePayment(paymentRequest); var transactionResult = new TransactionResult() { OrderGuid = order.Guid, TransactionGuid = Guid.NewGuid().ToString(), }; if (paymentResponse != null) { var payment = paymentResponse.Payment; transactionResult.Success = true; transactionResult.TransactionAmount = (decimal)(payment.AmountMoney.Amount ?? 0) / 100; transactionResult.ResponseParameters = new Dictionary <string, object>() { { "paymentId", payment.Id }, { "referenceId", payment.ReferenceId }, { "orderId", payment.OrderId }, { "cardDetails", payment.CardDetails } }; if (payment.Status == "APPROVED") { transactionResult.NewStatus = PaymentStatus.Authorized; } else if (payment.Status == "COMPLETED") { transactionResult.NewStatus = PaymentStatus.Complete; } else { transactionResult.NewStatus = PaymentStatus.Failed; var errors = string.Join(",", paymentResponse.Errors); logger.Log <TransactionResult>(LogLevel.Warning, "The payment for Order#" + order.Id + " by square failed." + errors); transactionResult.Exception = new Exception("An error occurred while processing payment. Error Details: " + errors); transactionResult.Success = false; } } else { logger.Log <TransactionResult>(LogLevel.Warning, "The payment for Order#" + order.Id + " by square failed. No response received."); transactionResult.Success = false; transactionResult.Exception = new Exception("An error occurred while processing payment"); } return(transactionResult); }
public static TransactionResult ProcessRefund(TransactionRequest refundRequest, SquareSettings squareSettings, ILogger logger) { var order = refundRequest.Order; var config = GetConfiguration(squareSettings); var paymentId = refundRequest.GetParameterAs <string>("paymentId"); var refundsApi = new RefundsApi(config); var refundResponse = refundsApi.RefundPayment(new RefundPaymentRequest() { IdempotencyKey = Guid.NewGuid().ToString(), AmountMoney = new Money() { Amount = (long)((refundRequest.IsPartialRefund ?refundRequest.Amount : order.OrderTotal) * 100) }, PaymentId = paymentId, Reason = "Refunded by administrator " + ApplicationEngine.CurrentUser.Name }); //perform the call var transactionResult = new TransactionResult() { OrderGuid = order.Guid, TransactionGuid = Guid.NewGuid().ToString(), }; if (refundResponse != null) { var refund = refundResponse.Refund; transactionResult.Success = true; transactionResult.TransactionAmount = (decimal)(refund.AmountMoney.Amount ?? 0) / 100; transactionResult.ResponseParameters = new Dictionary <string, object>() { { "refundId", refund.Id }, }; if (refund.Status == "PENDING") { transactionResult.NewStatus = PaymentStatus.RefundPending; } else if (refund.Status == "COMPLETE") { transactionResult.NewStatus = refundRequest.IsPartialRefund ? PaymentStatus.RefundedPartially : PaymentStatus.Refunded; } else { var errors = string.Join(",", refundResponse.Errors); logger.Log <TransactionResult>(LogLevel.Warning, "The refund for Order#" + order.Id + " by square failed." + errors); transactionResult.Exception = new Exception("An error occurred while refunding payment. Error Details: " + errors); transactionResult.Success = false; } } else { logger.Log <TransactionResult>(LogLevel.Warning, "The refund for Order#" + order.Id + " by square failed. No response received."); transactionResult.Success = false; transactionResult.Exception = new Exception("An error occurred while refunding payment"); } if (transactionResult.NewStatus == PaymentStatus.RefundPending) { //request again to get exact status var refund = refundsApi.GetPaymentRefund(transactionResult.GetParameterAs <string>("refundId")); if (refund.Refund.Status == "COMPLETE") { transactionResult.NewStatus = refundRequest.IsPartialRefund ? PaymentStatus.RefundedPartially : PaymentStatus.Refunded; } } return(transactionResult); }