Payment.php 1.4 KB

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