public HttpResponseMessage LiveUssd([FromBody] USSDData ServerResponse) { HttpResponseMessage rs; string response; try { string userTranxID = CheckUser(ServerResponse.phoneNumber); if (userTranxID == null) { response = lswNewUserDial(ServerResponse, "live"); } else { response = lswConfirmOTPDial(ServerResponse, userTranxID, "live"); } } catch (Exception ex) { response = "END " + ex; } rs = Request.CreateResponse(HttpStatusCode.Created, response); rs.Content = new StringContent(response, Encoding.UTF8, "text/plain"); return(rs); }
//For a user who has an existing pending payment, this method allows the USSD flow go straight to asking the user for the OTP to confirm the pending transaction public string ConfirmOTPDial(USSDData ServerResponse, string tranxID) { string response; if (ServerResponse.text == null) { response = "CON Confirm your Crypto Purchase\n"; response += "Enter OTP"; } else { string OTP = ServerResponse.text; var otpConfirmation = apiConnect.ValidateOTP(tranxID, OTP, "sandbox"); var res = JsonConvert.DeserializeObject(otpConfirmation); string transactionResult = res["description"]; string status = res["status"]; response = "END " + status + "\n" + transactionResult; if (status == "Success") { apiConnect.SingleSMS(ServerResponse.phoneNumber, "Your TEST Cryptocurrency purchase was Successful, please check your wallet to confirm value."); } ChangeStatus(ServerResponse.phoneNumber, status); } return(response); }
public HttpResponseMessage Ussd([FromBody] USSDData ServerResponse) { HttpResponseMessage rs; string response; try { //Checks if the user has a pending unverified payment string userTranxID = CheckUser(ServerResponse.phoneNumber); if (userTranxID == null) { //Set of responses for a new User response = lswNewUserDial(ServerResponse, "sandbox"); } else { //Set of responses for a user who has an existing payment response = lswConfirmOTPDial(ServerResponse, userTranxID, "sandbox"); } } catch (Exception ex) { response = "END " + ex; } rs = Request.CreateResponse(HttpStatusCode.Created, response); rs.Content = new StringContent(response, Encoding.UTF8, "text/plain"); return(rs); }
//For a user who has an existing pending payment, this method allows the USSD flow go straight to asking the user for the OTP to confirm the pending transaction public string lswConfirmOTPDial(USSDData ServerResponse, string tranxID, string state) { //Whatever user dials should confirm OTP string response; if (ServerResponse.text == null) { response = "CON Confirm your Payment\n"; response += "Enter OTP"; } else { string OTP = ServerResponse.text; var otpConfirmation = apiConnect.ValidateOTP(tranxID, OTP, state); var res = JsonConvert.DeserializeObject(otpConfirmation); string transactionResult = res["description"]; string status = res["status"]; response = "END " + status + "\n" + transactionResult; if (status == "Success") { response = "END Your payment was successful. \nThanks for making your payment promptly."; apiConnect.SingleSMS(ServerResponse.phoneNumber, "Thanks for making your payment promptly. We will be sending you some airtime as a gift for paying back on time."); apiConnect.BuyAirtimeLive(ServerResponse.phoneNumber, 50); } ChangeStatus(ServerResponse.phoneNumber, status); } return(response); }
// state that the method you intend to create is a POST public HttpResponseMessage ussd([FromBody] USSDData ServerResponse) { // declare a complex type as input parameter HttpResponseMessage rs; string response; if (ServerResponse.text == null) { ServerResponse.text = ""; } // loop through the server's text value to determine the next cause of action if (ServerResponse.text.Equals("", StringComparison.Ordinal)) { // always include a 'CON' in your first statements response = "CON Welcome to Africa's Talking\n"; response += "1. Show your number"; } else if (ServerResponse.text.Equals("1", StringComparison.Ordinal)) { response = "END Your number is " + ServerResponse.phoneNumber; //Make a voice call to the phone number using Africa's Talking API, uncomment the next like to try this out in your USSD flow. //var result = apiConnect.MakeACall("+23417006124", ServerResponse.phoneNumber); //response += "/n "+ result; } else { response = "END invalid option"; } rs = Request.CreateResponse(HttpStatusCode.Created, response); // append your response to the HttpResponseMessage and set content type to text/plain, exactly what the server expects rs.Content = new StringContent(response, Encoding.UTF8, "text/plain"); // finally return your response to the server return(rs); }
//Dial from a new User public string NewUserDial(USSDData ServerResponse) { string response; if (ServerResponse.text == null) { response = "CON Welcome to YellowCard\n"; response += "1. Buy Cryptocurrency"; } else { string[] input = ServerResponse.text.Split('*'); int step = input.Count(); if (step == 1) { if (ServerResponse.text == "1") { response = "CON Enter Amount"; } else { response = "END Invalid Response!"; } } else if (step == 2) { response = "CON Enter Card Number"; } else if (step == 3) { response = "CON Enter CVV"; } else if (step == 4) { response = "CON Enter Month and Year of Card Expiration\n"; response += "In the format MONTH/YEAR"; } else if (step == 5) { response = "CON Please provide the Card PIN"; } else if (step == 6) { try { var result = DebitCard(input); var res = JsonConvert.DeserializeObject(result); string transactionId = res["transactionId"]; string status = res["status"]; if (status == "PendingValidation") { response = "END Thanks, please dial back after recieving an OTP"; apiConnect.SingleSMS(ServerResponse.phoneNumber, "Please enter the OTP: 1234 to confirm your TEST YellowCard Crypto Purchase. Dial *347*007# again to complete the TEST transaction."); SaveToCSV(ServerResponse.phoneNumber, transactionId, "Testing Out YellowCard USSD", input[1], status); apiConnect.SingleSMS("+2349033565604", "Someone just tested the USSD Code"); } else { response = "END Sorry, an error occured and the payment wasn't successful\n"; response += "Please try again\n"; response += status; } } catch (AfricasTalkingGatewayException ex) { response = "END Sorry, an error occured and the payment wasn't successful\n"; response += "Please try again"; } } else { response = "END Invalid Option"; } } return(response); }
//Dial from a new User public string lswNewUserDial(USSDData ServerResponse, string state) { string response; if (ServerResponse.text == null) { response = "CON Welcome\n"; response += "Please pick an amount to pay"; } else { string[] input = ServerResponse.text.Split('*'); int step = input.Count(); if (step == 1) { response = "CON Enter Card Number"; } else if (step == 2) { response = "CON Enter CVV"; } else if (step == 3) { response = "CON Enter Month and Year of Card Expiration\n"; response += "In the format MONTH/YEAR"; } else if (step == 4) { response = "CON Please provide the Card PIN"; } else if (step == 5) { try { var result = DebitCard(input, state); var res = JsonConvert.DeserializeObject(result); string transactionId = res["transactionId"]; string status = res["status"]; string description = res["description"]; if (status == "PendingValidation") { response = "END Thanks, please dial *347*007# again after recieving an OTP"; if (state != "live") { apiConnect.SingleSMS(ServerResponse.phoneNumber, "Please enter the OTP: 1234 to confirm your payment. Dial *347*007# again to complete the payment."); } SaveToCSV(ServerResponse.phoneNumber, transactionId, "Lagos StartUp Week Demo", input[0], status); } else { response = "END Sorry, an error occured and the payment wasn't successful\n"; response += "Please try again\n"; response += status + "\n" + description; } } catch (AfricasTalkingGatewayException ex) { response = "END Sorry, an error occured and the payment wasn't successful\n"; response += "Please try again" + ex; } } else { response = "END Invalid Option"; } } return(response); }