File size: 2,417 Bytes
c1b0793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()