Payments / app.py
jbot7984's picture
Create app.py
c1b0793 verified
import gradio as gr
from square.client import Client
# Initialize Square client
square_client = Client(
access_token='YOUR_SQUARE_ACCESS_TOKEN',
environment='sandbox' # Use 'production' for live transactions
)
# Define meat products and their prices (in cents)
PRODUCTS = {
"Ground Beef": 799,
"Ribeye Steak": 1599,
"Pork Chops": 899,
"Chicken Breast": 699
}
def process_payment(name, email, product, quantity, card_number, exp_month, exp_year, cvc):
try:
# Calculate total amount
amount = PRODUCTS[product] * int(quantity)
# Create a nonce (normally, you'd use Square.js on the frontend for this)
card_nonce = square_client.cards.create_card_nonce(
body={
"card": {
"number": card_number,
"expiration_month": exp_month,
"expiration_year": exp_year,
"cvv": cvc
}
}
).body['card_nonce']
# Create a payment
result = square_client.payments.create_payment(
body={
"source_id": card_nonce,
"amount_money": {
"amount": amount,
"currency": "USD"
},
"idempotency_key": f"{name}-{email}-{product}-{quantity}",
"note": f"Payment for {quantity} {product}(s)"
}
)
if result.is_success():
return f"Payment successful! Charged ${amount/100:.2f} for {quantity} {product}(s). Receipt sent to {email}."
else:
return f"Payment failed: {result.errors}"
except Exception as e:
return f"Payment failed: {str(e)}"
# Create the Gradio interface
iface = gr.Interface(
fn=process_payment,
inputs=[
gr.Textbox(label="Full Name"),
gr.Textbox(label="Email"),
gr.Dropdown(choices=list(PRODUCTS.keys()), label="Product"),
gr.Number(label="Quantity", minimum=1, maximum=10, step=1),
gr.Textbox(label="Card Number"),
gr.Number(label="Expiration Month", minimum=1, maximum=12),
gr.Number(label="Expiration Year", minimum=2023, maximum=2030),
gr.Textbox(label="CVV")
],
outputs="text",
title="Meat Processing Company Payment",
description="Enter your details to make a payment for meat products."
)
# Launch the app
iface.launch()