public ActionResult HandleCardPaymentComplete(StripePayment model)
        {
            StripeConfiguration.ApiKey = ConfigurationManager.AppSettings["Cashier:Stripe:Secret"];

            var paymentIntent = PaymentService.GetPaymentIntentByTransactionRef(model.TransactionReference);
            var service       = new PaymentIntentService();

            ExStripe.PaymentIntent stripePI = null;
            if (paymentIntent.MotoMode == true)
            {
                //if it's a moto payment, we need to create the payment intent from
                var servicePM     = new PaymentMethodService();
                var paymentMethod = servicePM.Get(model.StripePaymentIntentId);
                var piCreate      = new PaymentIntentCreateOptions
                {
                    Amount               = (long)paymentIntent.Amount * 100,
                    Currency             = paymentIntent.Currency,
                    Description          = paymentIntent.Description,
                    Confirm              = true,
                    PaymentMethod        = model.StripePaymentIntentId,
                    PaymentMethodOptions = new PaymentIntentPaymentMethodOptionsOptions
                    {
                        Card = new PaymentIntentPaymentMethodOptionsCardOptions
                        {
                            Moto = true
                        }
                    }
                };
                piCreate.Metadata = new Dictionary <string, string>
                {
                    { "TransactionReference", paymentIntent.TransactionReference }
                };
                try
                {
                    stripePI = service.Create(piCreate);
                }
                catch (StripeException ex)
                {
                    stripePI = ex.StripeError.PaymentIntent;
                }

                model.StripePaymentIntentId = stripePI.Id;
            }


            stripePI = stripePI ?? service.Get(model.StripePaymentIntentId);

            if (stripePI.Status == "succeeded" && stripePI.Metadata["TransactionReference"] == model.TransactionReference)
            {
                PaymentService.UpdatePaymentStatus(model.TransactionReference, model.StripePaymentIntentId, PaymentStatus.Succeeded);


                return(Redirect(paymentIntent.ConfirmationPageUrl));
            }

            PaymentService.UpdatePaymentStatus(model.TransactionReference, model.StripePaymentIntentId, PaymentStatus.Failed);

            return(Redirect(paymentIntent.FailurePageUrl));
        }
Example #2
0
        protected override async Task <IPaymentResponse> CreateCharge(IStripeCharge stripeChargeParameters)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(stripeChargeParameters.Token))
                {
                    if (stripeChargeParameters.IsIntentConfirm || stripeChargeParameters.Token.Contains("~"))
                    {
                        var    splitToken      = stripeChargeParameters.Token.Split('~');
                        string paymentMethodId = splitToken[1];
                        string paymentIntentId = splitToken[0];

                        var service        = new PaymentIntentService();
                        var confirmOptions = new PaymentIntentConfirmOptions();

                        PaymentIntent paymentIntent = service.Confirm(paymentIntentId, confirmOptions, new RequestOptions
                        {
                            ApiKey = GetStripeToken(stripeChargeParameters)
                        });
                        if (paymentIntent.Status == "succeeded")
                        {
                            if (paymentIntent.Status == "succeeded")
                            {
                                Transaction transactionResult = _transactionStatusUpdater.UpdateTranscationStatus(Convert.ToInt64(stripeChargeParameters.TransactionId));
                                if (stripeChargeParameters.ChannelId == Channels.Feel)
                                {
                                    try
                                    {
                                        var redemptionOrder = new Redemption_OrderDetails
                                        {
                                            AltId              = Guid.NewGuid(),
                                            ApprovedBy         = null,
                                            ApprovedUtc        = null,
                                            CreatedBy          = transactionResult.CreatedBy,
                                            CreatedUtc         = DateTime.UtcNow,
                                            IsEnabled          = false,
                                            OrderCompletedDate = null,
                                            OrderStatusId      = (int)FIL.Contracts.Enums.ApproveStatus.Pending,
                                            TransactionId      = Convert.ToInt64(stripeChargeParameters.TransactionId),
                                            UpdatedBy          = null,
                                            UpdatedUtc         = null
                                        };
                                        _orderDetailsRepository.Save(redemptionOrder);
                                        _zoomMeetingProvider.CreateMeeting(transactionResult);
                                    }
                                    catch (Exception e)
                                    {
                                        _logger.Log(LogCategory.Error, new Exception("Failed to save the redemption order", e));
                                    }
                                }
                                else if (stripeChargeParameters.ChannelId == Channels.Website)
                                {
                                    _zoomMeetingProvider.CreateMeeting(transactionResult);
                                }
                            }

                            _transactionPaymentDetailRepository.Save(new TransactionPaymentDetail
                            {
                                TransactionId    = Convert.ToInt64(stripeChargeParameters.TransactionId),
                                PaymentOptionId  = PaymentOptions.CreditCard,
                                PaymentGatewayId = PaymentGateway.Stripe,
                                RequestType      = "Charge Recieved",
                                Amount           = stripeChargeParameters.Amount.ToString(),
                                PayConfNumber    = paymentIntent.Id,
                                PaymentDetail    = "{\"Response\":" + Newtonsoft.Json.JsonConvert.SerializeObject(paymentIntent) + "}",
                            });
                            return(GetPaymentResponse(true, PaymentGatewayError.None));
                        }
                        else if ((paymentIntent.Status == "requires_source_action" || paymentIntent.Status == "requires_action") && paymentIntent.NextAction.Type == "use_stripe_sdk")
                        {
                            return(GetPaymentResponse(false, PaymentGatewayError.RequireSourceAction, PaymentGateway.Stripe, paymentIntent.ClientSecret));
                        }
                        return(GetPaymentResponse(false, GetPaymentGatewayErrorCode(paymentIntent.CancellationReason)));
                    }

                    Stripe.PaymentIntent stripeCharge = CreateStripeCharge(stripeChargeParameters);
                    if (stripeCharge != null)
                    {
                        var paymentConfirmationNumber = stripeCharge.Id;
                        if ((stripeCharge.Status == "requires_source_action" || stripeCharge.Status == "requires_action") && stripeCharge.NextAction.Type == "use_stripe_sdk")
                        {
                            return(GetPaymentResponse(false, PaymentGatewayError.RequireSourceAction, PaymentGateway.Stripe, stripeCharge.ClientSecret));
                        }

                        if (stripeCharge.Status == "succeeded")
                        {
                            Transaction transactionResult = _transactionStatusUpdater.UpdateTranscationStatus(Convert.ToInt64(stripeChargeParameters.TransactionId));
                            if (stripeChargeParameters.ChannelId == Channels.Feel)
                            {
                                try
                                {
                                    var redemptionOrder = new Redemption_OrderDetails
                                    {
                                        AltId              = Guid.NewGuid(),
                                        ApprovedBy         = null,
                                        ApprovedUtc        = null,
                                        CreatedBy          = transactionResult.CreatedBy,
                                        CreatedUtc         = DateTime.UtcNow,
                                        IsEnabled          = false,
                                        OrderCompletedDate = null,
                                        OrderStatusId      = (int)FIL.Contracts.Enums.ApproveStatus.Pending,
                                        TransactionId      = Convert.ToInt64(stripeChargeParameters.TransactionId),
                                        UpdatedBy          = null,
                                        UpdatedUtc         = null
                                    };
                                    _orderDetailsRepository.Save(redemptionOrder);
                                    _zoomMeetingProvider.CreateMeeting(transactionResult);
                                }
                                catch (Exception e)
                                {
                                    _logger.Log(LogCategory.Error, new Exception("Failed to save the redemption order", e));
                                }
                            }
                            else if (stripeChargeParameters.ChannelId == Channels.Website)
                            {
                                _zoomMeetingProvider.CreateMeeting(transactionResult);
                            }
                        }

                        _transactionPaymentDetailRepository.Save(new TransactionPaymentDetail
                        {
                            TransactionId    = Convert.ToInt64(stripeChargeParameters.TransactionId),
                            PaymentOptionId  = PaymentOptions.CreditCard,
                            PaymentGatewayId = PaymentGateway.Stripe,
                            RequestType      = "Charge Recieved",
                            Amount           = stripeChargeParameters.Amount.ToString(),
                            PayConfNumber    = stripeCharge.Id,
                            PaymentDetail    = "{\"Response\":" + Newtonsoft.Json.JsonConvert.SerializeObject(stripeCharge) + "}",
                        });

                        return(stripeCharge.Status == "succeeded" ? GetPaymentResponse(true, PaymentGatewayError.None) : GetPaymentResponse(false, GetPaymentGatewayErrorCode(stripeCharge.CancellationReason)));
                    }
                    else
                    {
                        return(GetPaymentResponse(false, PaymentGatewayError.Unknown));
                    }
                }
                else
                {
                    return(GetPaymentResponse(false, PaymentGatewayError.InvalidToken));
                }
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("Your card was declined"))
                {
                    _logger.Log(LogCategory.Error, new Exception("Failed to create charge", ex));
                }
                _transactionPaymentDetailRepository.Save(new TransactionPaymentDetail
                {
                    TransactionId    = Convert.ToInt64(stripeChargeParameters.TransactionId),
                    PaymentOptionId  = PaymentOptions.CreditCard,
                    PaymentGatewayId = PaymentGateway.Stripe,
                    RequestType      = "Charge Recieved",
                    Amount           = stripeChargeParameters.Amount.ToString(),
                    PayConfNumber    = "",
                    PaymentDetail    = "{\"Response\":" + ex.Message + "}",
                });
                return(GetPaymentResponse(false, GetPaymentGatewayErrorCode(ex.Message)));
            }
        }