Fix for expired tokens.
authorRadek Czajka <rczajka@rczajka.pl>
Wed, 20 Apr 2022 23:09:44 +0000 (01:09 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Wed, 20 Apr 2022 23:09:44 +0000 (01:09 +0200)
src/club/migrations/0039_auto_20220421_0109.py [new file with mode: 0644]
src/club/models.py
src/club/payu/models.py

diff --git a/src/club/migrations/0039_auto_20220421_0109.py b/src/club/migrations/0039_auto_20220421_0109.py
new file mode 100644 (file)
index 0000000..2436a77
--- /dev/null
@@ -0,0 +1,18 @@
+# Generated by Django 2.2.27 on 2022-04-20 23:09
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('club', '0038_payuorder_created_at'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='payuorder',
+            name='status',
+            field=models.CharField(blank=True, choices=[('PENDING', 'Pending'), ('WAITING_FOR_CONFIRMATION', 'Waiting for confirmation'), ('COMPLETED', 'Completed'), ('CANCELED', 'Canceled'), ('REJECTED', 'Rejected'), ('ERR-INVALID_TOKEN', 'Invalid token')], max_length=128),
+        ),
+    ]
index 0d75ec4..c5057a7 100644 (file)
@@ -316,7 +316,7 @@ class PayUOrder(payu_models.Order):
         if self.status == 'COMPLETED':
             self.schedule.set_payed()
 
-        elif self.status == 'CANCELED':
+        elif self.status == 'CANCELED' or self.status.startswith('ERR-'):
             if self.is_recurring() and self.schedule.expires_at:
                 self.schedule.send_email_failed_recurring()
             
index 6305e2e..10dd60a 100644 (file)
@@ -36,6 +36,8 @@ class Order(models.Model):
         ('COMPLETED', _('Completed')),
         ('CANCELED', _('Canceled')),
         ('REJECTED', _('Rejected')),
+
+        ('ERR-INVALID_TOKEN', _('Invalid token')),
     ])
     created_at = models.DateTimeField(null=True, blank=True, auto_now_add=True)
     completed_at = models.DateTimeField(null=True, blank=True)
@@ -128,11 +130,17 @@ class Order(models.Model):
             # else?
 
         if 'orderId' not in response:
-            raise ValueError("Expecting dict with `orderId` key, got: %s" % response)
-        self.order_id = response['orderId']
-        self.save()
+            code = response.get('status', {}).get('codeLiteral', '')
+            if code:
+                self.status = 'ERR-' + str(code)
+                self.save()
+                self.status_updated()
+            else:
+                raise ValueError("Expecting dict with `orderId` key, got: %s" % response)
+        else:
+            self.order_id = response['orderId']
+            self.save()
 
-        
         return response.get('redirectUri', self.schedule.get_thanks_url())