public async Task <IActionResult> OnPostAsync() { int id = (int)HttpContext.Session.GetInt32(SD.UserSessionId); PayBillResponse response = await stripeHelper.PayBill(billingSubmission); if (response.responseMessage.StatusCode != HttpStatusCode.OK) { return(RedirectToPage("./Billing/PaymentFailed")); } else { StudentPayment studentPayment = new StudentPayment(); studentPayment.StudentId = id; studentPayment.StripeTokenId = response.studentPayments.StripeTokenId; studentPayment.Payment = response.studentPayments.Payment; studentPayment.CreatedOn = DateTime.UtcNow; await _context.StudentPayments.AddAsync(studentPayment); await _context.SaveChangesAsync(); return(RedirectToPage("./Billing/PaymentSuccessful")); } }
public async Task <PayBillResponse> PayBill(BillingSubmission billingSubmission) { HttpStatusCode statusCode = new HttpStatusCode(); PayBillResponse payBillResponse = new PayBillResponse(); try { if (!PayBillRequestValid(billingSubmission)) { statusCode = HttpStatusCode.BadRequest; } else { var cardValues = new Dictionary <string, string> { { "card[number]", billingSubmission.CreditCardNum }, { "card[exp_month]", billingSubmission.ExpMonth }, { "card[exp_year]", billingSubmission.ExpYear }, { "card[cvc]", billingSubmission.SecurityCode } }; var formUrlEncodedContent = new FormUrlEncodedContent(cardValues); //Post request to Stripe var response = await httpClient.PostAsync(BaseURL + CreateURL, formUrlEncodedContent); //Parse the json var responsejson = await response.Content.ReadAsStringAsync(); //Parse the token id string token = JObject.Parse(responsejson).SelectToken("id").ToString(); var chargeValues = new Dictionary <string, string> { { "amount", billingSubmission.Amount.ToString() }, { "currency", "usd" }, { "description", "Test Charge Through Learning Management System" }, { "source", token } }; var formUrlEncodedContent2 = new FormUrlEncodedContent(chargeValues); var response2 = await httpClient.PostAsync(BaseURL + ChargeURL, formUrlEncodedContent2); var responsejson2 = await response.Content.ReadAsStringAsync(); if (response2.StatusCode == HttpStatusCode.OK) { StudentPayment payment = new StudentPayment(); payment.StripeTokenId = JObject.Parse(responsejson2).SelectToken("id").ToString(); payment.Payment = billingSubmission.Amount; payment.CreatedOn = DateTime.UtcNow; payBillResponse.studentPayments = payment; payBillResponse.responseMessage = response2; } } } catch (Exception e) { payBillResponse = new PayBillResponse(); } return(payBillResponse); }