Voice-Enabled Payment System Using Python
Overview
- Voice Input Handling: Capture user voice input using a microphone.
- Voice-to-Text Conversion: Convert the captured voice input into text using Automatic Speech Recognition (ASR).
- Intent Recognition: Use Natural Language Understanding (NLU) to determine the user's intent.
- Response Generation: Generate appropriate responses and convert them back to speech.
- Payment Processing: Integrate with a payment platform to handle transactions.
- Hardware Integration: Implement the system on a hardware platform like Raspberry Pi.
Setting Up Your Environment
You'll need the following tools and libraries:
- Python: The programming language used for this project.
- Amazon Lex: For creating the voice chatbot.
- Boto3: AWS SDK for Python to interact with Amazon Lex.
- Requests: For making HTTP requests to the Eyowo API.
- SpeechRecognition: For capturing voice input.
- Pyttsx3: For text-to-speech conversion.
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:
- 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>- 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>- 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
- Obtain API Credentials:
- Register with the payment service and obtain necessary API credentials (API key, token, etc.).
- Integrate Payment API:
- Use the service’s API documentation to integrate payment functionality into your application.
- Handle Payment Responses:
- Process the response from the payment API to confirm the transaction and handle any errors.
- Test Thoroughly:
- Test the payment integration in a sandbox or test environment provided by the payment service before going live.
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:
- Install Raspbian OS on the Raspberry Pi.
- Connect USB Microphone and Speakers: Use the USB microphone for input and speakers for output.
- Run Python Script: Execute the Python script to capture voice input, interact with Amazon Lex, and handle payments.
- Case Design: Design and 3D print a case to house the Raspberry Pi, microphone, and speakers.
References
- 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