PayCallbackException.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2023/2/20
  8. * Time: 15:39
  9. */
  10. namespace app\common\models;
  11. use app\common\modules\refund\RefundPayAdapter;
  12. /**
  13. * Class PayCallbackException
  14. * @property string pay_sn
  15. * @property integer frequency
  16. * @property integer pay_type_id
  17. * @property integer error_code
  18. * @property string error_msg
  19. * @property array result
  20. * @property array response
  21. * @property integer status
  22. * @package app\common\models
  23. */
  24. class PayCallbackException extends BaseModel
  25. {
  26. public $table = 'yz_pay_callback_exception';
  27. protected $guarded = ['id'];
  28. protected $hidden = ['updated_at','deleted_at'];
  29. protected $dates = ['record_at'];
  30. protected $appends = ['status_name','pay_type_name'];
  31. protected $attributes = [
  32. 'status' => 0,
  33. ];
  34. protected $casts = [
  35. 'result' => 'json',
  36. 'response' => 'json',
  37. ];
  38. const ORDER_CLOSE = 1;
  39. const INITIAL = 0;
  40. const STATUS_SUCCESS = 1;
  41. const STATUS_FAIL = -1;
  42. public static function getList($search = [])
  43. {
  44. $model = self::uniacid();
  45. if ($search['pay_sn']) {
  46. $model->where('pay_sn', trim($search['pay_sn']));
  47. }
  48. if ($search['order_sn']) {
  49. $pay_sn_array = OrderPay::whereHas('orders', function ($order) use ($search) {
  50. $order->where('order_sn', trim($search['order_sn']));
  51. })->pluck('pay_sn')->unique()->toArray();
  52. $model->whereIn('pay_sn', $pay_sn_array);
  53. }
  54. if (isset($search['status']) && is_numeric($search['status'])) {
  55. $model->where('status', $search['status']);
  56. }
  57. if ($search['start_time'] && $search['end_time']) {
  58. $range = [strtotime($search['start_time']), strtotime($search['end_time'])];
  59. $model->whereBetween('record_at', $range);
  60. }
  61. return $model;
  62. }
  63. /**
  64. * @return array
  65. */
  66. public function refund()
  67. {
  68. $payAdapter = new RefundPayAdapter($this->pay_type_id);
  69. $totalmoney = $this->orderPay->amount; //订单总金额
  70. try {
  71. $result = $payAdapter->pay($this->pay_sn, $totalmoney, $totalmoney);
  72. if ($result['status']) {
  73. $this->orderPay->updateRefund($this->orderPay->id);
  74. $this->status = self::STATUS_SUCCESS;
  75. $this->save();
  76. } else {
  77. $this->status = self::STATUS_FAIL;
  78. $this->handle_msg = $result['msg'];
  79. $this->save();
  80. }
  81. return $result;
  82. } catch (\Exception $e) {
  83. \Log::debug('支付回调异常退款失败:'.$e->getMessage(), $result);
  84. return ['status' => 0, 'msg' => $e->getMessage()];
  85. }
  86. }
  87. public function getCodeNameAttribute()
  88. {
  89. switch ($this->error_code) {
  90. case self::ORDER_CLOSE:
  91. $name = '订单已关闭';
  92. break;
  93. default:
  94. $name = '默认';
  95. break;
  96. }
  97. return $name;
  98. }
  99. public function getStatusNameAttribute()
  100. {
  101. switch ($this->status) {
  102. case 1:
  103. $name = '已处理';
  104. break;
  105. case -1:
  106. $name = '退款失败';
  107. break;
  108. default:
  109. $name = '未处理';
  110. break;
  111. }
  112. return $name;
  113. }
  114. /**
  115. * 支付类型汉字
  116. * @return string
  117. */
  118. public function getPayTypeNameAttribute()
  119. {
  120. if ($this->pay_type_id == PayType::CREDIT) {
  121. $set = \Setting::get('shop.shop');
  122. return ($set['credit'] ?: '余额');
  123. }
  124. return $this->hasOnePayType->name;
  125. }
  126. public function hasOnePayType()
  127. {
  128. return $this->hasOne(PayType::class,'id', 'pay_type_id');
  129. }
  130. public function orderPay()
  131. {
  132. return $this->hasOne(OrderPay::class,'pay_sn', 'pay_sn');
  133. }
  134. }