12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace app\common\model;
- use think\Log;
- use think\Model;
- /**
- * @property string order_no
- * @property MobileOrder order
- */
- class Payment extends Model
- {
- protected $autoWriteTimestamp=true;
- protected $type=[
- 'params'=>'json',
- 'settle_data'=>'json',
- 'settle_query_data'=>'json',
- 'refund_data'=>'json',
- 'refund_query_data'=>'json',
- ];
- public static function pay(User $user,$amount,$class,$func,$params,$body="订单付款"){
- $payment=self::create([
- 'class'=>$class,
- 'func'=>$func,
- 'params'=>$params,
- 'order_no'=>order_no(),
- 'amount'=>$amount,
- 'user_id'=>$user['id'],
- ]);
- try {
- $data=payment($user)->order->unify([
- 'body' => $body,
- 'out_trade_no' => $payment['order_no'],
- 'total_fee' => (int)($payment['amount']*100),
- 'notify_url' => request()->root(true)."/index/payment/index/order_no/".$payment['order_no'],
- 'trade_type' => 'JSAPI',
- 'openid' => $user['openid'],
- ]);
- if(empty($data['return_code']) || $data['return_code']!='SUCCESS' || $data['result_code']!='SUCCESS'){
- Log::error("支付错误:".json_encode($data));
- throw_user("支付错误:".($data['err_code_des']??$data['return_msg']??''));
- }
- $p=payment($user)->jssdk->sdkConfig($data['prepay_id']);
- return $p;
- }catch (\Exception $e){
- throw_user($e->getMessage());
- }
- }
- public function user(){
- return $this->belongsTo(User::class);
- }
- public function payed(){
- $user=$this->user;
- return payment($user)->handlePaidNotify(function ($message, $fail){
- Log::info("支付通知:".json_encode($message));
- if($this['pay_time']){
- return true;
- }
- $this['pay_time']=date('Y-m-d H:i:s');
- $class=new $this['class'];
- $class->{$this['func']}($this['params'],$this);
- return true;
- });
- }
- protected static function init()
- {
- self::beforeInsert(function (self $payment){
- });
- }
- public function refundSuccess(){
- $this['refund_status']=2;
- $this['refund_at']=date('Y-m-d H:i:s');
- $this->save();
- }
- public function settleSuccess($success,$queryData=null){
- $this['refund_query_data']=$queryData;
- if($success) {
- $this['settle_status'] = 2;
- $this['settle_at'] = date('Y-m-d H:i:s');
- }else{
- $this['settle_status']=3;
- }
- $this->save();
- }
- }
|