1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace app\common\model;
- use think\Log;
- use think\Model;
- class Payment extends Model
- {
- protected $autoWriteTimestamp=true;
- protected $type=[
- 'params'=>'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;
- });
- }
- }
|