1、uni-app
lijiZhifu(id) {
console.log('支付-----');
let data = {
id
}
this.yrApi.request('order/payOrder', 'POST', data).then(res => {
if (res.data.code == 0) {
let that = this
// console.log('order----', res.data.data);
wx.requestPayment({
'timeStamp': res.data.data.timestamp,
'nonceStr': res.data.data.nonceStr,
'package': res.data.data.package,
'signType': res.data.data.signType,
'paySign': res.data.data.paySign,
'success': function(res) {
console.log('res----', 支付成功);
},
'fail': function(res) {}
})
}
});
},
2、通过实例化小程序获取wx.requestPayment里面的数据
//去支付
public function payOrder()
{
$data = input();
$order = Db::name('order')->find($data['id']);
//实例化小程序
$user = Db::name('wechat_user')->where('id', '=', $order['uid'])->find();
$mini_program_config = Config::get('wechat.mini_program');
$wechat_pay_config = Config::get('wechat.wechat_pay');
$app = new Application($wechat_pay_config);
$xinxi = [
'mchid' => (string)$app->getMerchant()->getMerchantId(),
'out_trade_no' => $order['dingdanNumber'],
'appid' => $mini_program_config['app_id'],
'description' => '保洁清洁',
'notify_url' => 'https://' . $_SERVER['HTTP_HOST'] . '/api/wechat_pay_notify/notify', //必须是https
'amount' => [
// 'total' => $order['price']*100,
'total' => 1,
'currency' => 'CNY',
],
'payer' => [
'openid' => $user['openid'],
],
];
try {
$response = $app->getClient()->post('/v3/pay/transactions/jsapi', ['json' => $xinxi]);
// print_r($response->toArray());
if ($response->toArray()) {
$config = $app->getUtils()->buildSdkConfig($response->toArray()['prepay_id'], $mini_program_config['app_id']); // 返回数组
return apiData(0, '支付成功', $config);
}
} catch (ServerException $err) {
// dump($err->getResponse()->getContent(false));
return apiData(1, '支付失败', $err->getResponse()->getContent(false));
} catch (ClientException $err) {
// dump($err->getResponse()->getContent(false));
return apiData(2, '支付失败', $err->getResponse()->getContent(false));
}
}
3、通过notify_url的地址拿到支付成功的回调,执行订单支付成功的流程
//接收腾讯发来的支付通知
public function notify()
{
$wechat_pay_config = Config::get('wechat.wechat_pay');
$app = new Application($wechat_pay_config);
$server = $app->getServer();
// 处理支付事件
$server->handlePaid(function ($message) {
//判断是否支付完成
$res = $message->toArray();
file_put_contents('pay.txt', json_encode($res, JSON_UNESCAPED_UNICODE));
if ($res['trade_state'] == 'SUCCESS') {
$order = Db::name('order')->where('dingdanNumber', '=', $res['out_trade_no'])->find();
file_put_contents('order.txt', json_encode($order, JSON_UNESCAPED_UNICODE));
if (!$order['payTime']) {
//支付完成
Db::name('order')
->where('id', $order['id'])
->update(['state' => 2, 'payTime' => time(), 'updateTime' => time()]);
file_put_contents('zhifuwancheng.txt', json_encode($order, JSON_UNESCAPED_UNICODE));
}
}
return json_encode(['code' => 'SUCCESS', 'message' => '成功'], JSON_UNESCAPED_UNICODE);
});
return $server->serve();
}