|
|
import gradio as gr |
|
|
from square.client import Client |
|
|
|
|
|
|
|
|
square_client = Client( |
|
|
access_token='YOUR_SQUARE_ACCESS_TOKEN', |
|
|
environment='sandbox' |
|
|
) |
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
amount = PRODUCTS[product] * int(quantity) |
|
|
|
|
|
|
|
|
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'] |
|
|
|
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
|
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." |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch() |