1from flask import Flask, request, session, redirect
2import requests
3import json
4
5app = Flask(__name__)
6app.secret_key = "your_secret_key_here"
7
8API_BASE = "https://coinpay.ir/api/v1"
9API_TOKEN = "YOUR_MERCHANT_API_TOKEN"
10
11@app.route('/create-payment')
12def create_payment():
13 payload = {
14 "amount": 10000,
15 "client_ref_id": "order-123",
16 "name": "John Doe",
17 "description": "Buying goods",
18 }
19
20 headers = {
21 "Content-Type": "application/json",
22 "Authorization": f"Bearer {API_TOKEN}",
23 }
24
25 response = requests.post(f"{API_BASE}/coin-pay/payment", json=payload, headers=headers)
26 data = response.json()
27
28 if data.get("status"):
29 session["transaction_id"] = data["transaction_id"]
30 return redirect(data["url"])
31 else:
32 return f"<h1>Error: {data.get('message', 'Unknown error')}</h1>"
33
34@app.route('/verify')
35def verify_transaction():
36 transaction_id = session.get("transaction_id")
37 if not transaction_id:
38 return "<h1>No transaction found</h1>"
39
40 headers = {
41 "Content-Type": "application/json",
42 "Authorization": f"Bearer {API_TOKEN}",
43 }
44
45 response = requests.get(f"{API_BASE}/coin-pay", params={"transaction_id": transaction_id}, headers=headers)
46 data = response.json()
47
48 if data.get("payment_status") == "completed":
49 return "<h1>Transaction was successful</h1>"
50 else:
51 reason = data.get("reason", "Unknown")
52 return f"<h1>Transaction failed: {reason}</h1>"
53
54if __name__ == '__main__':
55 app.run(debug=True)