Voice-Enabled Payment System Using Python

Overview

  1. Voice Input Handling: Capture user voice input using a microphone.
  2. Voice-to-Text Conversion: Convert the captured voice input into text using Automatic Speech Recognition (ASR).
  3. Intent Recognition: Use Natural Language Understanding (NLU) to determine the user's intent.
  4. Response Generation: Generate appropriate responses and convert them back to speech.
  5. Payment Processing: Integrate with a payment platform to handle transactions.
  6. Hardware Integration: Implement the system on a hardware platform like Raspberry Pi.

Setting Up Your Environment

You'll need the following tools and libraries:

Configuring Amazon Lex

Create a chatbot using Amazon Lex with the following steps:

Create a New Bot: Log in to the AWS Management Console and navigate to Amazon Lex. Create a new bot, configuring intents, utterances, and slots as required. Define Intents: Set up intents for various actions like checking balance and making payments. Integrate Lambda Functions: Create Lambda functions for handling intent logic and interacting with Eyowo's API.

Example of a basic intent configuration for checking balance:


{
    "name": "CheckBalance",
    "sampleUtterances": [
        "What is my balance?",
        "Check my balance",
        "Get my account balance"
    ],
    "slots": [],
    "fulfillmentActivity": {
        "type": "CodeHook",
        "codeHook": {
            "uri": "arn:aws:lambda:your-region:your-account-id:function:your-function",
            "messageVersion": "1.0"
        }
    }
}

## Capturing Voice Input

import speech_recognition as sr

def capture_voice():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = recognizer.listen(source)
    try:
        text = recognizer.recognize_google(audio)
        return text
    except sr.UnknownValueError:
        return "Sorry, I did not understand that."
    except sr.RequestError:
        return "Sorry, there was an error with the speech recognition service."

Converting Text to Speech

Use pyttsx3 to convert text responses from Amazon Lex back to speech:

import pyttsx3

def speak_text(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

Handling Payment Transactions

Integrating Google Pay (Client-Side Integration) For Google Pay, you'd typically handle payments through client-side integration. Here’s a high-level overview of how you might set up Google Pay for web applications:

  1. Add Google Pay API to Your Web Page: Include the Google Pay API JavaScript library in your HTML:
<script async src="https://pay.google.com/gp/p/js/pay.js"></script>
  1. Configure Google Pay in JavaScript:Initialize Google Pay and create a payment request:
<script>
  const paymentsClient = new google.payments.api.PaymentsClient({
    environment: 'TEST' // or 'PRODUCTION'
  });

  const paymentDataRequest = {
    // Your Google Pay API configuration
    apiVersion: 2,
    apiVersionMinor: 0,
    allowedPaymentMethods: [
      {
        type: 'CARD',
        parameters: {
          allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
          allowedNetworks: ['AMEX', 'DISCOVER', 'MASTERCARD', 'VISA']
        },
        tokenizationSpecification: {
          type: 'PAYMENT_GATEWAY',
          parameters: {
            gateway: 'example',
            gatewayMerchantId: 'exampleGatewayMerchantId'
          }
        }
      }
    ],
    merchantInfo: {
      merchantId: 'exampleMerchantId',
      merchantName: 'Example Merchant'
    },
    transactionInfo: {
      totalPriceStatus: 'FINAL',
      totalPriceLabel: 'Total',
      totalPrice: '100.00',
      currencyCode: 'USD',
      countryCode: 'US'
    },
    shippingAddressRequired: true
  };

  function onGooglePayButtonClicked() {
    paymentsClient.loadPaymentData(paymentDataRequest)
      .then(function(paymentData) {
        // Handle the response
        console.log(paymentData);
      })
      .catch(function(err) {
        console.error('Error:', err);
      });
  }
</script>
  1. Add a Google Pay Button: Add a button to your HTML that triggers the payment:
<button id="google-pay-button" onclick="onGooglePayButtonClicked()">
  Pay with Google Pay
</button>

General Steps for Using Any Payment Service API

  1. Obtain API Credentials:
  1. Integrate Payment API:
  1. Handle Payment Responses:
  1. Test Thoroughly:

References

For detailed documentation and integration guides, refer to the respective payment service provider’s API documentation:

Google Pay API Documentation: https://developers.google.com/pay/api/web/overview

Or Integrate with the Eyowo API to handle payments. First, obtain your Eyowo API credentials and use them to make HTTP requests:

import requests

def make_payment(phone_number, amount, pin):
    url = "https://api.eyowo.com/payment"
    payload = {
        'phone_number': phone_number,
        'amount': amount,
        'pin': pin
    }
    response = requests.post(url, json=payload)
    return response.json()

#3 Integrating Hardware: Raspberry Pi

For a complete hardware-based implementation, you can use a Raspberry Pi with the following setup:

  1. Install Raspbian OS on the Raspberry Pi.
  2. Connect USB Microphone and Speakers: Use the USB microphone for input and speakers for output.
  3. Run Python Script: Execute the Python script to capture voice input, interact with Amazon Lex, and handle payments.
  4. Case Design: Design and 3D print a case to house the Raspberry Pi, microphone, and speakers.

References

  1. Isaac Samuel, Fiyinfoba A. Ogunkeye. "Development of a Voice Chatbot for Payment Using Amazon Lex Service with ‘Eyowo’ as the Payment Platform," Department of Electrical and Information Engineering, Covenant University, Ota, Nigeria.

This is just an attempt to help you through how would a voice assissted payment system look like and could be made. There may be several other methods but I came up with this floorplan after my research