Payment.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\OrderPayService;
  4. use think\Log;
  5. use think\Model;
  6. /**
  7. * @property int pay_type
  8. * @property string order_no
  9. */
  10. class Payment extends Model
  11. {
  12. protected $autoWriteTimestamp=true;
  13. protected $type=[
  14. 'params'=>'json',
  15. ];
  16. public static function pay(User $user,$pay_type,$amount,$payment_id,$body="订单付款",$payment_type=''){
  17. $payment=self::create([
  18. 'pay_type'=>$pay_type,
  19. 'order_no'=>order_no(),
  20. 'amount'=>$amount,
  21. 'user_id'=>$user['id'],
  22. 'payment_type'=>$payment_type,
  23. 'payment_id'=>$payment_id,
  24. ]);
  25. $service=new OrderPayService();
  26. $service->setPayment($payment);
  27. $service->setBody($body);
  28. $service->setExpire($payment->getExpire());
  29. return $service->pay();
  30. }
  31. public function user(){
  32. return $this->belongsTo(User::class);
  33. }
  34. public function payed(){
  35. $this['pay_time']=time();
  36. $this->save();
  37. switch ($this['payment_type']){
  38. case (new Orders)->getTable():
  39. Orders::makePayed($this);
  40. break;
  41. }
  42. }
  43. public function getExpire(){
  44. switch ($this['payment_type']){
  45. case (new Orders)->getTable():
  46. return time()+Orders::EXP_PAY-60;
  47. default:
  48. return time()+3600;
  49. }
  50. }
  51. }