A standard end-to-end workflow involves two main steps:
- Create an Order: Generate a unique order in the system.
- Initiate Device Checkout: Use the order ID to trigger the physical POS device to start the payment process.
Finally, it is good practice to call close_checkout to clean up the session.
import razorpay
import time
class POSIntegration:
def __init__(self, key_id, key_secret, base_url=None):
self.client = razorpay.Client(auth=(key_id, key_secret), base_url=base_url)
def complete_pos_checkout(self, device_id, amount, device_mode="wired"):
try:
# Step 1: Create Order
order = self.client.order.create({
"amount": amount,
"currency": "INR",
"receipt": f"pos_order_{int(time.time())}",
"notes": {"device_id": device_id}
}, device_mode=device_mode)
# Step 2: Initiate Device Checkout
activity = self.client.device_activity.create({
"device_id": device_id,
"action": "initiate_checkout",
"notes": {"order_id": order['id']},
"initiate_checkout": {
"name": "Acme Corp",
"amount": amount,
"currency": "INR",
"description": "POS Transaction",
"type": "in_person",
"order_id": order['id'],
"prefill": {
"name": "Customer Name",
"email": "customer@example.com",
"contact": "9000090000",
"method": "upi"
}
}
}, device_mode=device_mode)
self.close_checkout(device_id, device_mode)
return {"success": True, "order": order, "activity": activity}
except Exception as e:
return {"success": False, "error": str(e)}
def close_checkout(self, device_id, device_mode):
try:
return self.client.device_activity.create({
"device_id": device_id,
"action": "close_checkout"
}, device_mode=device_mode)
except Exception as e:
print(f"Failed to close checkout: {e}")
return None
# Usage
pos = POSIntegration('your_key_id', 'your_key_secret')
result = pos.complete_pos_checkout(device_id="2841158834", amount=19900, device_mode="wired")