diff --git a/apps/pg/models.py b/apps/pg/models.py index 46d89ab..aac7600 100644 --- a/apps/pg/models.py +++ b/apps/pg/models.py @@ -156,6 +156,7 @@ class PNA(BasePGModel): client = PNAClient() def make_request(self): + self.payment_request.change_state(StateChoices.CREATED, StateChoices.IPG_PENDING, 'can not change state') amount = self.payment_request.amount description = 'from addwin ipg' callback_url = urllib.parse.urljoin( @@ -164,12 +165,19 @@ class PNA(BasePGModel): order_id = self.id phone_number = self.payment_request.phone_number - response = self.client.request_normal_sale(amount, description, callback_url, order_id, phone_number) - self.pna_status_code = response['status'] - self.pna_status_message = response.get('title') + try: + response = self.client.request_normal_sale(amount, description, callback_url, order_id, phone_number) + self.pna_status_code = response['status'] + self.pna_status_message = response.get('title') - if response['status'] == 0: - self.pna_token = response['token'] + if response['status'] == 0: + self.pna_token = response['token'] + + else: + self.payment_request.change_state(self.payment_request.state, StateChoices.FAILED, + 'error in pardakht novin gateway') + except Exception as e: + self.payment_request.change_state(self.payment_request.state, StateChoices.FAILED, 'error in pardakht novin gateway') self.save() @@ -178,6 +186,10 @@ class PNA(BasePGModel): return self.client.pg_base_url.format(token=self.pna_token) def confirm_request(self, request=None): + if self.payment_request.state not in [StateChoices.IPG_PENDING]: + messages.error(request, "وضعیت تراکنش در حالت مناسبی نیست.") + return + print(dict(request.POST)) post_dict = dict(request.POST) if post_dict.get('status') in [0, '0', [0], ['0']]: @@ -193,6 +205,8 @@ class PNA(BasePGModel): self.details = response if response['status'] == 0: self.pna_status = StateChoices.SUCCESS.value + self.payment_request.change_state(StateChoices.IPG_PENDING, StateChoices.IPG_SUCCESS, + 'error in change state') self.save() self.payment_request.charge_wallet_balance()