|
@@ -1,107 +0,0 @@
|
|
|
-#coding=utf-8
|
|
|
-
|
|
|
-from django.db import models
|
|
|
-from django.utils import timezone
|
|
|
-from django.conf import settings
|
|
|
-
|
|
|
-
|
|
|
-from apps.package_order.models import PackageOrder
|
|
|
-from apps.vehicle_order.models import VehicleOrder
|
|
|
-from apps.vehicle_model.models import Model
|
|
|
-from apps.package.models import Package
|
|
|
-from apps.commission_order.models import CommissionOrder, CommissionOrderPackage
|
|
|
-from apps.tenant.models import Tenant
|
|
|
-from apps.WechatApplet.models import WechatApplet
|
|
|
-from apps.customer.models import CustomerWechat
|
|
|
-from apps.customer.models import Customer
|
|
|
-from apps.activity.models import Order
|
|
|
-from apps.account.models import Branch
|
|
|
-
|
|
|
-from util.exceptions import CustomError
|
|
|
-from util.wechatpay import WechatPay
|
|
|
-
|
|
|
-
|
|
|
-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
|