# Import the helper gateway class from africastalking.AfricasTalkingGateway import ( AfricasTalkingGateway, AfricasTalkingGatewayException) #create a gateway instance gateway = AfricasTalkingGateway( "jani", "680fdfa9eae83b8649c7d3884ad0679b827c7393140bc68b85e0e5b0a31dcb68") productName = "Nerd Payments" phoneNumber = "+254708415904,+254702456399" currencyCode = "KES" amount = 10.50 metadata = {"agentId": "654", "productId": "321"} gateway.initiateMobilePaymentCheckout("Nerd Payments", "+254727487476", "KES", 10.50, { "agentId": "654", "productId": "321" })
def mobile_payment(request, ticket_id): ''' Function that carries out the payment process ''' # Get ticket with a given id ticket = Ticket.get_single_ticket(ticket_id) # Get the route and convert to string bus_route = str((ticket.schedule.bus.bus_organisation.name)) print(type(bus_route)) # Get the phone number phone_number = ticket.phone_number print(type(phone_number)) # Get the ticket price and convert Decimal to int ticket_price = float(ticket.schedule.price) print(type(ticket_price)) # Africas Talking Set Up # Specify your credentials # username = "******" username = "******" apiKey = config('API_KEY_AFRICAS_TALKING') # Create an instance of our awesome gateway class and pass your credentials gateway = AfricasTalkingGateway(username, apiKey, "sandbox") #************************************************************************************* # NOTE: If connecting to the sandbox: # # 1. Use "sandbox" as the username # 2. Use the apiKey generated from your sandbox application # https://account.africastalking.com/apps/sandbox/settings/key # 3. Add the "sandbox" flag to the constructor # # gateway = AfricasTalkingGateway(username, apiKey, "sandbox"); #************************************************************************************** # Specify the name of your Africa's Talking payment product productName = bus_route # The phone number of the customer checking out phoneNumber = phone_number # The 3-Letter ISO currency code for the checkout amount currencyCode = "KES" # The checkout amount amount = ticket_price print(amount) # Any metadata that you would like to send along with this request # This metadata will be included when we send back the final payment notification metadata = {"agentId": "654", "productId": "321"} try: # Initiate the checkout. If successful, you will get back a transactionId transaction_id = gateway.initiateMobilePaymentCheckout( productName, phoneNumber, currencyCode, amount, metadata) print("The transactionId is " + transaction_id) ticket.transaction_code = transaction_id ticket.save() print(ticket.transaction_code) return redirect('/ticket/' + str(ticket_id)) except AfricasTalkingGatewayException as e: print("Received error response: %s" % str(e))
# Import the helper gateway class from africastalking.AfricasTalkingGateway import ( AfricasTalkingGateway, AfricasTalkingGatewayException) #create a gateway instance gateway = AfricasTalkingGateway( "jani", "d46192b5e6c1bdf6e24ae3760f5d49cde42e8b09d53f01fc929eec205996f5ce", "sandbox") productName = "Good Morning" phoneNumber = "+254708415904" currencyCode = "KES" amount = 100.50 metadata = {"agentId": "654", "productId": "321"} gateway.initiateMobilePaymentCheckout(productName, phoneNumber, currencyCode, amount, metadata)