|
| 1 | +import requests |
| 2 | +import datetime |
| 3 | +from .auth import generate_setu_headers |
| 4 | +from .errors import handle_setu_errors |
| 5 | + |
| 6 | + |
| 7 | +class URLS: |
| 8 | + class Sandbox: |
| 9 | + url = "https://sandbox.setu.co/api" |
| 10 | + |
| 11 | + class Prod: |
| 12 | + url = "https://prod.setu.co/api" |
| 13 | + |
| 14 | + |
| 15 | +class Deeplink: |
| 16 | + def __init__(self, schemeId, secret, productInstance, production=False): |
| 17 | + self.schemeId = schemeId |
| 18 | + self.secret = secret |
| 19 | + self.productInstance = productInstance |
| 20 | + self.url = URLS.Sandbox.url if not production else URLS.Prod.url |
| 21 | + |
| 22 | + def generate_link(self, amount, expiresInDays, payeeName, refId, exactness="EXACT"): |
| 23 | + expiryDate = datetime.datetime.now() + datetime.timedelta(days=expiresInDays) |
| 24 | + path = "/payment-links" |
| 25 | + payload = { |
| 26 | + "amount": {"currencyCode": "INR", "value": amount}, |
| 27 | + "amountExactness": exactness, |
| 28 | + "billerBillID": refId, |
| 29 | + "dueDate": "{}Z".format(expiryDate.isoformat("T")), |
| 30 | + "expiryDate": "{}Z".format(expiryDate.isoformat("T")), |
| 31 | + "name": payeeName, |
| 32 | + } |
| 33 | + |
| 34 | + if exactness == "EXACT_UP": |
| 35 | + payload["validationRules"] = {"amount": {"maximum": 0, "minimum": amount}} |
| 36 | + elif exactness == "EXACT_DOWN": |
| 37 | + payload["validationRules"] = {"amount": {"maximum": amount, "minimum": 0}} |
| 38 | + |
| 39 | + headers = generate_setu_headers( |
| 40 | + self.schemeId, self.secret, self.productInstance |
| 41 | + ) |
| 42 | + response = requests.post(self.url + path, json=payload, headers=headers) |
| 43 | + handle_setu_errors(response) |
| 44 | + data = response.json() |
| 45 | + self.platformBillID = data["data"]["platformBillID"] |
| 46 | + return data["data"] |
| 47 | + |
| 48 | + def check_status(self, platformBillID=None): |
| 49 | + bill_id = self.platformBillID |
| 50 | + if platformBillID: |
| 51 | + bill_id = platformBillID |
| 52 | + path = "/payment-links/{}".format(bill_id) |
| 53 | + headers = generate_setu_headers( |
| 54 | + self.schemeId, self.secret, self.productInstance |
| 55 | + ) |
| 56 | + response = requests.get(self.url + path, headers=headers) |
| 57 | + data = response.json() |
| 58 | + print(data) |
| 59 | + return data["data"] |
| 60 | + |
| 61 | + def mock_payment(self, amount, upiId): |
| 62 | + path = "/triggers/funds/addCredit" |
| 63 | + payload = { |
| 64 | + "amount": 0, |
| 65 | + "destinationAccount": {"accountID": 'Biller-66147538470438-001', "ifsc": "EXTERNALBRANCH001"}, |
| 66 | + "sourceAccount": {"accountID": "123123123", "ifsc": "1231231"}, |
| 67 | + "type": "UPI", |
| 68 | + } |
| 69 | + |
| 70 | + headers = generate_setu_headers( |
| 71 | + self.schemeId, self.secret, self.productInstance |
| 72 | + ) |
| 73 | + print("Coming soon!") |
| 74 | + # response = requests.post(self.url + path,json=payload, headers=headers) |
| 75 | + # data= response.json() |
| 76 | + # print('mock----',data) |
0 commit comments