|
@@ -9,7 +9,6 @@ from django.conf import settings
|
|
from apps.exceptions import CustomError
|
|
from apps.exceptions import CustomError
|
|
from apps.account.models import Branch
|
|
from apps.account.models import Branch
|
|
from apps.customer.models import Customer
|
|
from apps.customer.models import Customer
|
|
-from apps.pay.models import Pay
|
|
|
|
from util.wechatpay import WechatPay, WeChatResponse
|
|
from util.wechatpay import WechatPay, WeChatResponse
|
|
|
|
|
|
|
|
|
|
@@ -68,6 +67,92 @@ class Activity(models.Model):
|
|
return instance
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
+class Pay(models.Model):
|
|
|
|
+
|
|
|
|
+ WAIT = 1
|
|
|
|
+ CONFIRM = 2
|
|
|
|
+ CLOSED = 3
|
|
|
|
+ STATUS_CHOICES = (
|
|
|
|
+ (WAIT, u'待付款'),
|
|
|
|
+ (CONFIRM, u'已付款'),
|
|
|
|
+ (CLOSED, u'已关闭'),
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ branch = models.ForeignKey(Branch, verbose_name=u"门店", on_delete=models.PROTECT, editable=False)
|
|
|
|
+ no = models.CharField(max_length=64, verbose_name=u"单号")
|
|
|
|
+ create_time = models.DateTimeField(verbose_name=u"创建时间", default=timezone.now)
|
|
|
|
+ customer = models.ForeignKey(Customer, verbose_name=u'客户', on_delete=models.PROTECT)
|
|
|
|
+ status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u"支付状态")
|
|
|
|
+ precreate_amount = models.BigIntegerField(verbose_name=u"预支付金额")
|
|
|
|
+ amount = models.BigIntegerField(verbose_name=u"实际支付金额", null=True)
|
|
|
|
+
|
|
|
|
+ class Meta:
|
|
|
|
+ db_table = "pay"
|
|
|
|
+ verbose_name = u"支付"
|
|
|
|
+ ordering = ('-id',)
|
|
|
|
+ index_together = (
|
|
|
|
+ 'create_time',
|
|
|
|
+ 'status',
|
|
|
|
+ )
|
|
|
|
+ unique_together = (
|
|
|
|
+ 'no',
|
|
|
|
+ )
|
|
|
|
+ default_permissions = ()
|
|
|
|
+
|
|
|
|
+ def payClosed(self):
|
|
|
|
+ if self.status != Pay.WAIT:
|
|
|
|
+ return
|
|
|
|
+ self.status = Pay.CLOSED
|
|
|
|
+ self.save()
|
|
|
|
+
|
|
|
|
+ def payConfirm(self, amount):
|
|
|
|
+ if self.status != Pay.WAIT:
|
|
|
|
+ return
|
|
|
|
+
|
|
|
|
+ self.status = Pay.CONFIRM
|
|
|
|
+ self.amount = amount
|
|
|
|
+ self.save()
|
|
|
|
+ order = Order.objects.filter(pay=self).first()
|
|
|
|
+ if order:
|
|
|
|
+ order.status = Order.FINISH
|
|
|
|
+ order.amount = self.amount
|
|
|
|
+ order.save()
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def getByNo(no):
|
|
|
|
+ instance = Pay.objects.filter(no=no).first()
|
|
|
|
+ if not instance:
|
|
|
|
+ raise CustomError(u'未找到相应的支付单!')
|
|
|
|
+ return instance
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def wechatPay(branch, customer, amount, openid):
|
|
|
|
+ instance = Pay._addnew(branch, customer, amount)
|
|
|
|
+ return instance, instance._wechatUnifiedOrder(openid)
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def _addnew(branch, customer, amount):
|
|
|
|
+ if amount <= 0:
|
|
|
|
+ raise CustomError(u'无效的付款金额!')
|
|
|
|
+
|
|
|
|
+ no = timezone.now().strftime('%y%m%d%H%M%S') + str(customer.id)
|
|
|
|
+ instance = Pay.objects.create(
|
|
|
|
+ branch=branch,
|
|
|
|
+ no=no,
|
|
|
|
+ customer=customer,
|
|
|
|
+ type=type,
|
|
|
|
+ status=Pay.WAIT,
|
|
|
|
+ precreate_amount=amount
|
|
|
|
+ )
|
|
|
|
+ return instance
|
|
|
|
+
|
|
|
|
+ def _wechatUnifiedOrder(self, openid):
|
|
|
|
+ wechatpay = WechatPay(settings.APPID, settings.AGENT_NUM, settings.AGENT_KEY)
|
|
|
|
+ wechatpay.unifiedOrder(self.no, self.precreate_amount, openid)
|
|
|
|
+ data = wechatpay.getAppString()
|
|
|
|
+ return data
|
|
|
|
+
|
|
|
|
+
|
|
class Order(models.Model):
|
|
class Order(models.Model):
|
|
DEFAULT = 0
|
|
DEFAULT = 0
|
|
FINISH = 1
|
|
FINISH = 1
|
|
@@ -116,8 +201,6 @@ class Order(models.Model):
|
|
return data
|
|
return data
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
class Coupon(models.Model):
|
|
class Coupon(models.Model):
|
|
FIXED_DATE = 0
|
|
FIXED_DATE = 0
|
|
RECEIVE_TIMING = 1
|
|
RECEIVE_TIMING = 1
|