| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2023/2/20
- * Time: 15:39
- */
- namespace app\common\models;
- use app\common\modules\refund\RefundPayAdapter;
- /**
- * Class PayCallbackException
- * @property string pay_sn
- * @property integer frequency
- * @property integer pay_type_id
- * @property integer error_code
- * @property string error_msg
- * @property array result
- * @property array response
- * @property integer status
- * @package app\common\models
- */
- class PayCallbackException extends BaseModel
- {
- public $table = 'yz_pay_callback_exception';
- protected $guarded = ['id'];
- protected $hidden = ['updated_at','deleted_at'];
- protected $dates = ['record_at'];
- protected $appends = ['status_name','pay_type_name'];
- protected $attributes = [
- 'status' => 0,
- ];
- protected $casts = [
- 'result' => 'json',
- 'response' => 'json',
- ];
- const ORDER_CLOSE = 1;
- const INITIAL = 0;
- const STATUS_SUCCESS = 1;
- const STATUS_FAIL = -1;
- public static function getList($search = [])
- {
- $model = self::uniacid();
- if ($search['pay_sn']) {
- $model->where('pay_sn', trim($search['pay_sn']));
- }
- if ($search['order_sn']) {
- $pay_sn_array = OrderPay::whereHas('orders', function ($order) use ($search) {
- $order->where('order_sn', trim($search['order_sn']));
- })->pluck('pay_sn')->unique()->toArray();
- $model->whereIn('pay_sn', $pay_sn_array);
- }
- if (isset($search['status']) && is_numeric($search['status'])) {
- $model->where('status', $search['status']);
- }
- if ($search['start_time'] && $search['end_time']) {
- $range = [strtotime($search['start_time']), strtotime($search['end_time'])];
- $model->whereBetween('record_at', $range);
- }
- return $model;
- }
- /**
- * @return array
- */
- public function refund()
- {
- $payAdapter = new RefundPayAdapter($this->pay_type_id);
- $totalmoney = $this->orderPay->amount; //订单总金额
- try {
- $result = $payAdapter->pay($this->pay_sn, $totalmoney, $totalmoney);
- if ($result['status']) {
- $this->orderPay->updateRefund($this->orderPay->id);
- $this->status = self::STATUS_SUCCESS;
- $this->save();
- } else {
- $this->status = self::STATUS_FAIL;
- $this->handle_msg = $result['msg'];
- $this->save();
- }
- return $result;
- } catch (\Exception $e) {
- \Log::debug('支付回调异常退款失败:'.$e->getMessage(), $result);
- return ['status' => 0, 'msg' => $e->getMessage()];
- }
- }
- public function getCodeNameAttribute()
- {
- switch ($this->error_code) {
- case self::ORDER_CLOSE:
- $name = '订单已关闭';
- break;
- default:
- $name = '默认';
- break;
- }
- return $name;
- }
- public function getStatusNameAttribute()
- {
- switch ($this->status) {
- case 1:
- $name = '已处理';
- break;
- case -1:
- $name = '退款失败';
- break;
- default:
- $name = '未处理';
- break;
- }
- return $name;
- }
- /**
- * 支付类型汉字
- * @return string
- */
- public function getPayTypeNameAttribute()
- {
- if ($this->pay_type_id == PayType::CREDIT) {
- $set = \Setting::get('shop.shop');
- return ($set['credit'] ?: '余额');
- }
- return $this->hasOnePayType->name;
- }
- public function hasOnePayType()
- {
- return $this->hasOne(PayType::class,'id', 'pay_type_id');
- }
- public function orderPay()
- {
- return $this->hasOne(OrderPay::class,'pay_sn', 'pay_sn');
- }
- }
|