可视化uniapp整合thinkphp6实现微信小程序支付
可视化uniapp整合thinkphp6实现微信小程序支付,调用tp6接口返回微信相关下单参数。
·
可视化uniapp整合thinkphp6实现微信小程序支付,调用tp6接口返回微信相关下单参数。
开源代码:diygw-ui-php: diygwUI+Thinkphp6实现PHP后台框架
//微信小程序相关方法
var Pay = {
async pay(param){
let page = getApp().globalData.currentPage
let session = page.$session;
if(!session.getToken() || !session.getUser().openid){
//如果参数自带openid跳过验证
if(!param.openid){
page.showToast('请先登录')
return;
}
}
if(!param.total){
page.showToast('请配置价格参数total')
return;
}
let data = await page.$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)
return;
}
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字段均为小写。但最新版的支付后台生成签名使用的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,// 支付签名
success: res => {
if(typeof params.success=='function'){
params.success(res)
}else{
console.log('配置支付回调成功方法')
}
},
fail: res => {
if(typeof params.fail=='function'){
params.fail(res)
}else{
console.log('配置支付回调失败方法')
}
}
})
}
}
export default Pay
<?php
namespace app\api\controller;
use app\BaseController;
use app\common\model\DiyOrderModel;
use app\common\model\DiyUserModel;
use EasyWeChat\Factory;
use think\App;
use think\facade\Log;
/*
* 支付
*/
class WepayController extends BaseController
{
//判断是否全部不需要登录
public $notNeedLoginAll = false;
public $isModel = false;
//判断不需要登录的方法
public $notNeedLogin = ['notify'];
public $wepayApp;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
parent::__construct($app);
$paymentConfig = config('wechat.payment');
$this->wepayApp = 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
*/
public function order(){
$userModel = DiyUserModel::where(['id'=>$this->request->userId])->find();
if(!$userModel){
return $this->error('请先登录'.$this->request->userId);
}
$user = $userModel->toArray();
if(empty($user['openid'])){
return $this->error('请先登录');
}
// 生成订单信息
$data = $this->request->param();
$data['orderNo'] = getOrderNo();
$data['status'] = 0;
$data['payStatus'] = 0;
$data['openid'] = $user['openid'];
$data['userId'] = $this->request->userId;
$model = new DiyOrderModel();
$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, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $data['openid'],
];
$result = $this->wepayApp->order->unify($payData);
if ($result['return_code'] === 'SUCCESS') {
$jssdk = $this->wepayApp->jssdk;
$config = $jssdk->bridgeConfig($result['prepay_id'],false); // 返回数组
return $this->successData($config);
}else{
return $this->errorData($result);
}
}
/**
* 支付回调
* @return mixed
*/
public function notify(){
$response = $this->wepayApp->handlePaidNotify(function ($message,$error){
$order = DiyOrderModel::where(['order_no'=>$message['out_trade_no']])->find();
if (!$order || $order['status'] == '1') return true;
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
$order['status'] = '1';
} elseif ($message['result_code'] === 'FAIL') {
$order['status'] = '2';
}
} else {
return $error('通信失败,请稍后再通知我');
}
if ($order->save()){
return true;
}
return false;
});
$send = $response->send();
return $send;
}
}
更多推荐
已为社区贡献4条内容
所有评论(0)