private bool PayBillRequestValid(BillingSubmission billingSubmission) { bool isValid = true; if (string.IsNullOrEmpty(billingSubmission.CreditCardNum) || string.IsNullOrEmpty(billingSubmission.ExpMonth) || string.IsNullOrEmpty(billingSubmission.ExpYear) || string.IsNullOrEmpty(billingSubmission.SecurityCode)) { isValid = false; } return(isValid); }
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); }