fix
[wolnelektury.git] / src / club / payu / models.py
index 6305e2e..937d32a 100644 (file)
@@ -36,6 +36,8 @@ class Order(models.Model):
         ('COMPLETED', _('Completed')),
         ('CANCELED', _('Canceled')),
         ('REJECTED', _('Rejected')),
         ('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)
     ])
     created_at = models.DateTimeField(null=True, blank=True, auto_now_add=True)
     completed_at = models.DateTimeField(null=True, blank=True)
@@ -75,6 +77,9 @@ class Order(models.Model):
     def get_notify_url(self):
         raise NotImplementedError
 
     def get_notify_url(self):
         raise NotImplementedError
 
+    def get_thanks_url(self):
+        raise NotImplementedError
+
     def status_updated(self):
         pass
 
     def status_updated(self):
         pass
 
@@ -83,6 +88,11 @@ class Order(models.Model):
     def get_pos(self):
         return POSS[self.pos_id]
 
     def get_pos(self):
         return POSS[self.pos_id]
 
+    def get_continue_url(self):
+        return "https://{}{}".format(
+            Site.objects.get_current().domain,
+            self.get_thanks_url())
+
     def get_representation(self, token=None):
         rep = {
             "notifyUrl": self.get_notify_url(),
     def get_representation(self, token=None):
         rep = {
             "notifyUrl": self.get_notify_url(),
@@ -128,12 +138,18 @@ class Order(models.Model):
             # else?
 
         if 'orderId' not in response:
             # 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())
+        return response.get('redirectUri', self.get_thanks_url())
 
 
 class Notification(models.Model):
 
 
 class Notification(models.Model):