public async Task <IActionResult> AddPayment(PaymentCreateBindingModel data) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var result = await _stripeService.ProcessPayment(data); if (result.Item1.ServiceStatus == ServiceStatus.Error) { foreach (var error in result.Item1.Errors) { ModelState.AddModelError(error.Item1, error.Item2); } return(BadRequest(ModelState)); } else { return(Ok(result.Item2)); } } catch (Exception ex) { throw ex; } }
public async Task <(ResponseModel, Stripe.Charge)> ProcessPayment(PaymentCreateBindingModel model) { var responseModel = new ResponseModel() { ServiceStatus = Enums.ServiceStatus.Error }; try { StripeConfiguration.ApiKey = "sk_test_kRWpvCZmKMlhNd5BOMK10CfV"; var metaData = new Dictionary <string, string> { { nameof(model.CardholderName), model.CardholderName } }; var myCharge = new Stripe.ChargeCreateOptions { Amount = (int)(model.Amount * 100), Currency = "AUD", Metadata = metaData, Source = model.StripeToken, Description = $"$ {model.Amount} paid for customer" }; var stripeCharge = await _stripeChargeService.CreateAsync(myCharge); responseModel.ServiceStatus = Enums.ServiceStatus.Success; return(responseModel, stripeCharge); } catch (Stripe.StripeException e) { responseModel.Errors.Add((e.HttpStatusCode.ToString(), e.Message)); return(responseModel, null); } catch (Exception ex) { throw ex; } }