예제 #1
0
        public async Task PaymentDeduct(PaymentDeductCommand paymentDeductCommand)
        {
            try
            {
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await _authHandler.GetAuthAccessToken());
                var response = await _httpClient.PostAsJsonAsync <PaymentDeductCommand>("api/Stripe/PaymentDeduct", paymentDeductCommand);

                if (response.IsSuccessStatusCode)
                {
                    var commandResult = await response.Content.ReadFromJsonAsync <PaymentDeductCommandResult>();

                    _apiCallResultSubject.OnNext(new ApiCallResult <string>()
                    {
                        IsSucceed = true,
                        Operation = "PaymentDeduct",
                    });
                }

                else
                {
                    _apiCallResultSubject.OnNext(new ApiCallResult <string>()
                    {
                        IsSucceed    = false,
                        Operation    = "PaymentDeduct",
                        ErrorMessage = "Response is not succeeded from server."
                    });
                }
            }

            catch (Exception ex)
            {
                _apiCallResultSubject.OnNext(new ApiCallResult <string>()
                {
                    IsSucceed    = false,
                    Operation    = "PaymentDeduct",
                    ErrorMessage = ex.Message
                });
            }
        }
        public async Task <IActionResult> PaymentDeduction(PaymentDeductCommand command)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(base.BadRequest(ModelState));
                }

                var result = await _mediator.Send <PaymentDeductCommandResult>(command);

                return(base.Ok(result));
            }

            catch (NotFoundException)
            {
                return(base.NotFound());
            }

            catch (InvalidOperationException ex)
            {
                return(base.Conflict(ex.Message));
            }
        }
        public async Task <bool> PaymentDeduct(PaymentDeductCommand command)
        {
            try
            {
                //var amountt=long.Parse(amount);
                var methodOptions = new PaymentMethodListOptions
                {
                    Customer = command.StripeCustomerId,
                    Type     = "card",
                };

                var methodService  = new PaymentMethodService();
                var paymentMethods = methodService.List(methodOptions);

                //To get the first payment method
                var payment = paymentMethods.ToList().FirstOrDefault();

                var service = new PaymentIntentService();
                var options = new PaymentIntentCreateOptions
                {
                    Amount        = command.Amount,
                    Currency      = "usd",
                    Customer      = command.StripeCustomerId,
                    PaymentMethod = payment.Id,
                    Confirm       = true,
                    OffSession    = true,
                };
                var paymentIntent = service.Create(options);
                return(true);
            }


            catch (StripeException e)
            {
                string errorMessage = "";
                switch (e.StripeError.Error)
                {
                case "card_error":
                    errorMessage = $"Card Error occurred on {e.StripeError.PaymentIntent.Id}, Error: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                case "api_error":
                    errorMessage = $"API Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                case "api_connection_error":
                    errorMessage = $"API Connection Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                case "invalid_request_error	":
                    errorMessage = $"Invalid request Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                default:
                    errorMessage = $"Some Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;
                }

                throw new InvalidOperationException(errorMessage);
            }
        }