当前位置: 首页 > 后端技术 > PHP

visualuniapp集成thinkphp6EasyWeChat实现微信小程序支付

时间:2023-03-29 19:24:25 PHP

Visualuniapp集成thinkphp6EasyWeChat实现微信小程序和政务支付,调用tp6接口返回微信相关订单参数。开源地址:https://gitee.com/diygw/diygw...//小程序支付相关方法varPay={asyncpay(param){letpage=getApp().globalData.currentPageletsession=page.$会话;if(!session.getToken()||!session.getUser().openid){//如果参数自带openid,则跳过验证if(!param.openid){page.showToast('请先登录')返回;}}if(!param.total){page.showToast('请配置价格参数total')return;}letdata=awaitpage.$http.post(param.url||'/api/wepay/order',{total:param.total,body:param.body,openid:param.openid||session.getUser().openid,},{},'json')if(data.code!=200){page.showToast(data.msg)返回;}if(this[param.paytype]){this[param.paytype](Object.assign(data,param))}else{page.showToast('请使用微信打开')return;}},//微信支付weixin(params={}){uni.requestPayment({provider:'wxpay',timeStamp:params.data.timeStamp,//支付签名时间戳,注意微信jssdk中使用的时间戳字段全部小写但是最新版支付后台生成签名时使用的timeStamp字段名需要大写。S字符nonceStr:params.data.nonceStr,//支付签名随机字符串,不超过32位package:params.data.package,//统一支付接口返回的prepay_id参数值,提交格式如下:prepay_id=\*\*\*)signType:params.data.signType,//签名方式,默认为'SHA1',新版支付需要传入'MD5'paySign:params.data.paySign,//支付签名成功:res=>{if(typeofparams.success=='function'){params.success(res)}else{console.log('配置支付回调成功方法')}},fail:res=>{if(typeofparams.fail=='function'){params.fail(res)}else{console.log('配置支付回调失败方法')}}})}}exportdefaultPaywepayApp=Factory::payment($paymentConfig);}/***用户下单*@return\think\response\Json*@throws\EasyWeChat\Kernel\Exceptions\InvalidArgumentException*@throws\EasyWeChat\Kernel\Exceptions\InvalidConfigException*@throws\GuzzleHttp\Exception\GuzzleException*@throws\think\exception\DbException*/publicfunctionorder(){$userModel=DiyUserModel::where(['id'=>$this->request->userId])->find();if(!$userModel){return$this->error('请先登录'.$this->request->;用户ID);$user=$userModel->toArray();if(empty($user['openid'])){return$this->error('请先登录');}//生成订单信息$data=$this->request->param();$data['orderNo']=getOrderNo();$数据['状态']=0;$data['payStatus']=0;$data['openid']=$user['openid'];$data['userId']=$this->request->userId;$model=newDiyOrderModel();$data=$model->add($data);$notify_url=url('api/wepay/notify')->suffix('html')->domain($this->request->domain())->build();//开始微信支付$payData=['body'=>$data['body'],'out_trade_no'=>$data['orderNo'],'total_fee'=>(float)($data['total']*100),'notify_url'=>$notify_url,//支付结果通知URL,不设置则使用配置中的默认地址'trade_type'=>'JSAPI',//请替换为值与您的付款方式“openid”相对应的类型'=>$data['openid'],];$result=$this->wepayApp->order->unify($payData);如果($result['return_code']==='SUCCESS'){$jssdk=$this->wepayApp->jssdk;$config=$jssdk->bridgeConfig($result['prepay_id'],false);//返回数组return$this->successData($config);}else{返回$this->errorData($result);}}/***支付回复*@returnmixed*/publicfunctionnotify(){$response=$this->wepayApp->handlePaidNotify(function($message,$error){$order=DiyOrderModel::where(['order_no'=>$message['out_trade_no']])->find();if(!$order||$order['status']=='1')returntrue;if($message['return_code']==='SUCCESS'){if($message['result_code']==='SUCCESS'){$order['status']='1';}elseif($message['result_code']==='失败'){$order['status']='2';}}else{return$error('通讯失败,请稍后通知我');}if($order->save()){返回真;}返回假;});$send=$response->send();返回$发送;}}wexcxApp=Factory::miniProgram($minConfig);}/***获取用户登录信息*@return\think\response\Json*@throws\think\Exception*@throws\think\db\exception\DataNotFoundException*@throws\think\db\exception\DbException*@throws\think\db\exception\ModelNotFoundException*/publicfunctionlogin(){$userInfo=json_decode($this->request->post('userInfo'),true);$code=$this->request->post('code');$auth=$this->wexcxApp->auth;$opendata=$auth->session($code);如果(isset($opendata['openid'])){$openid=$opendata['openid'];$type='weixcx';$model=newDiyUserModel();//查询获取微信小程序用户$user=$model->where('openid',$openid)->where('type',$type)->find();$data['openid']=$openid;$数据['类型']=$类型;$data['昵称']=$userInfo['昵称'];$数据['头像']=$userInfo['avatarUrl'];$data['国家']=$userInfo['国家'];$data['省']=$userInfo['省'];$data['性别']=$userInfo['性别'];如果($user){$userId=$user->toArray()['id'];$data['id']=$userId;$用户->编辑($数据);}else{$model=newDiyUserModel();$模型->添加($数据);$userId=$data['id'];$token="bearer".JWTAuth::builder(['uid'=>$userId]);$opendata['token']=$token;$data=array_merge($data,$opendata);返回$this->successData($data);}else{return$this->errorData($opendata,'enclosure');}}/***检查密钥包,自定义包*/publicfunctiongetSignPackage(){$url=$this->request->param('url');尝试{return$this->successData($this->wexcxApp->jssdk->buildConfig(['checkJsApi','onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','hideMenuItems','showMenuItems','hideAllNonBaseMenuItem','showAllNonBaseMenuItem','translateVoice','startRecord','stopRecord','onRecordEnd','playVoice','pauseVoice','stopVoice','uploadVoice','downloadVoice','chooseImage','previewImage','uploadImage','downloadImage','getNetworkType','openLocation','getLocation','hideOptionMenu','showOptionMenu','closeWindow','scanQRCode','chooseWXPay','openProductSpecificView','addCard','chooseCard','openCard'],false,false,false,['wx-open-launch-weapp'],'https://php.diygw.com/pay/index.html'));}catch(Throwable|Exception$e){return$this->error("获取用户失败,请重试");}}}