
import requests
from . import _constants as const


class aamarPay:
    isSandbox: bool
    storeID: str
    successUrl: str
    failUrl: str
    cancelUrl: str
    transactionID: str
    transactionAmount: int
    signature: str
    description: str
    customerName: str
    customerEmail: str
    customerMobile: str
    customerAddress1: str
    customerAddress2: str
    customerCity: str
    customerState: str
    customerPostCode: str

    def __init__(self, isSandbox=True, storeID=const.storeID, successUrl=const.succesUrl, failUrl=const.failUrl, cancelUrl=const.cancelUrl, transactionID='testTrId', transactionAmount='100', signature=const.signature, description='Description', customerName='Test user', customerEmail='sandbox@email.com', customerMobile='0111111111', customerAddress1='', customerAddress2='', customerCity='', customerState='', customerPostCode='') -> None:
        self.isSandbox = isSandbox
        self.storeID = storeID
        self.successUrl = successUrl
        self.failUrl = failUrl
        self.cancelUrl = cancelUrl
        self.transactionID = transactionID
        self.transactionAmount = transactionAmount
        self.signature = signature
        self.description = description
        self.customerName = customerName
        self.customerEmail = customerEmail
        self.customerMobile = customerMobile
        self.customerAddress1 = customerAddress1
        self.customerAddress2 = customerAddress2
        self.customerCity = customerCity
        self.customerState = customerState
        self.customerPostCode = customerPostCode

    def payment(self):

        payload = {
            "store_id": const.storeID,
            "tran_id": self.transactionID,
            "success_url": self.successUrl,
            "fail_url": self.failUrl,
            "cancel_url": self.cancelUrl,
            "amount": self.transactionAmount,
            "currency": "BDT",
            "signature_key": self.signature,
            "desc": self.description,
            "cus_name": self.customerName,
            "cus_email": self.customerEmail,
            "cus_add1":
            self.customerAddress1,
            "cus_add2":
            self.customerAddress2,
            "cus_city": self.customerCity,
            "cus_state":
            self.customerState,
            "cus_postcode": self.customerPostCode,
            "cus_country": "Bangladesh",
            "cus_phone": self.customerMobile
        }
        response = requests.post(
            const.sandBoxUrl if self.isSandbox else const.productionUrl, payload)
        if response.status_code == 200:
            start = response.text.index('action="')
            return const.sandboxReturnUrl + response.text[start+8:start+45] if self.isSandbox else const.productionReturnUrl+response.text[start+8:start+45]

        return response.text
