新聞中心
小編給大家分享一下怎么用vue.js和laravel實(shí)現(xiàn)微信支付,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站建設(shè)、濱湖網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、濱湖網(wǎng)絡(luò)營(yíng)銷、濱湖企業(yè)策劃、濱湖品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供濱湖建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
注:此項(xiàng)是微信公眾號(hào)開發(fā),請(qǐng)?jiān)谕驴粗埃葘?shí)現(xiàn)網(wǎng)頁(yè)微信授權(quán)登陸功能,具體參看我簡(jiǎn)書的另一篇文章:https://www.jb51.net/article/117004.htm
1.打開app/config/wechat.php,配置微信支付參數(shù):
/*
* 微信支付
*/
'payment' => [
'merchant_id' => env('WECHAT_PAYMENT_MERCHANT_ID', 'your-mch-id'),//商家號(hào)ID,請(qǐng)將其放在.env文件中
'key' => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),//商家支付key,請(qǐng)將其放在.env文件中
'cert_path' => env('WECHAT_PAYMENT_CERT_PATH', storage_path('app/public/apiclient_cert.pem')), //微信支付證書apiclient_cert.pem的絕對(duì)路徑,我放在storage/app/public/下
'key_path' => env('WECHAT_PAYMENT_KEY_PATH', storage_path('app/public/apiclient_key.pem')), //微信支付證書apiclient_key.pem的絕對(duì)路徑,我放在storage/app/public/下徑
// 'device_info' => env('WECHAT_PAYMENT_DEVICE_INFO', ''),
// 'sub_app_id' => env('WECHAT_PAYMENT_SUB_APP_ID', ''),
// 'sub_merchant_id' => env('WECHAT_PAYMENT_SUB_MERCHANT_ID', ''),
// ...
],2.配置微信支付和回調(diào)路由
//以下路由我放在api.php路由里,如果你放在web.php路由,請(qǐng)自行調(diào)整!
Route::middleware('api')->post('wxpay','BillsController@wxpay');
Route::middleware('api')->post('wx_notify','BillsController@wxnotify');3.在相應(yīng)的控制器里創(chuàng)建wxpay的方法
/**
* 這是我自己項(xiàng)目的內(nèi)部代碼示例,具體根據(jù)自己的業(yè)務(wù)邏輯調(diào)整,切不可直接拷貝!
*/
public function wxpay(Request $request)
{
//本實(shí)例傳遞的參數(shù)為user_id 和 broadcast_id,具體
if($request->has('user_id') && $request->has('broadcast_id')){
$out_trade_no = md5(Carbon::now().str_random(8));
$user_id = $request->get('user_id');
$broadcast_id = $request->get('broadcast_id');
$num = $request->get('num');
$flag = $request->get('flag');
$openid = $this->user->getOpenid($user_id);
$broadcast = $this->broadcast->getById($broadcast_id);
$speaker_id = $broadcast->speaker_id;
$body = $broadcast->title;
$detail = '';
$paid_at = null;
$status = 'pre_paid';
$amount = ($broadcast->price)*$num;
$attributes = [
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
'body' => $body,
'detail' => $detail,
'out_trade_no' => $out_trade_no,
'total_fee' => $amount, // 單位:分
'notify_url' => $_ENV['APP_URL'].'/api/wx_notify', // 支付結(jié)果通知網(wǎng)址,如果不設(shè)置則會(huì)使用配置里的默認(rèn)地址
'openid' => $openid, // trade_type=JSAPI,此參數(shù)必傳,用戶在商戶appid下的唯一標(biāo)識(shí),
// ...
];
$order = new Order($attributes);
$result = $this->wechat->payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
//創(chuàng)建預(yù)訂單
$param = [
'out_trade_no'=>$out_trade_no,
'user_id'=>$user_id,
'broadcast_id'=>$broadcast_id,
'speaker_id'=>$speaker_id,
'body'=>$body,
'detail'=>$detail,
'paid_at'=>$paid_at,
'amount'=>$amount,
'flag'=>$flag,
'status'=>$status,
'num'=>$num
];
$this->bill->store($param);
//返回
$prepayId = $result->prepay_id;
$config = $this->wechat->payment->configForPayment($prepayId,false);
return response()->json($config);
}
}
}4.在相應(yīng)的控制器里添加回調(diào)wxnotify方法
/**
* 這是我自己項(xiàng)目的內(nèi)部代碼示例,具體根據(jù)自己的業(yè)務(wù)邏輯調(diào)整,切不可直接拷貝!
*/
public function wxnotify()
{
$response = $this->wechat->payment->handleNotify(function($notify, $successful){
$order = $this->bill->getBillByOrderno($notify->out_trade_no);//查詢訂單($notify->out_trade_no);
if (!$order) { // 如果訂單不存在
return 'Order not exist.'; // 告訴微信,我已經(jīng)處理完了,訂單沒(méi)找到,別再通知我了
}
// 如果訂單存在
// 檢查訂單是否已經(jīng)更新過(guò)支付狀態(tài)
if ($order->paid_at) { // 假設(shè)訂單字段“支付時(shí)間”不為空代表已經(jīng)支付
return true; // 已經(jīng)支付成功了就不再更新了
}
// 用戶是否支付成功
if ($successful) {
// 不是已經(jīng)支付狀態(tài)則修改為已經(jīng)支付狀態(tài)
$order->paid_at = Carbon::now(); // 更新支付時(shí)間為當(dāng)前時(shí)間
$order->status = 'paid';
} else { // 用戶支付失敗
$order->status = 'paid_fail';
}
$order->save(); // 保存訂單
return true; // 返回處理完成
});
return $response;
}5.vue.js中methods的方法代碼參考
wechatpay(){
var param = {
'user_id':this.getStorage(),
'broadcast_id':this.id,
'num':1,
'flag':'buy',
}
this.$http.post(this.GLOBAL.apiUrl+'/wxpay',param).then((response) => {
WeixinJSBridge.invoke(
'getBrandWCPayRequest', response.data,
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
# 回調(diào)成功后跳轉(zhuǎn)
# router.push({name: 'Room',params:{id:this.id}});
}
}
);
})
},6.微信公眾平臺(tái)配置
1) 在“公眾賬號(hào)設(shè)置”—“JS接口安全域名”設(shè)置中填寫前端域名
2) 在“微信支付”—“開發(fā)配置”頁(yè)面中,公眾賬號(hào)支付下填寫“支付授權(quán)目錄”,注意的是,此授權(quán)url為前端支付按鈕所在頁(yè)面的url
7.接下來(lái)你就可以測(cè)試了
看完了這篇文章,相信你對(duì)“怎么用vue.js和laravel實(shí)現(xiàn)微信支付”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)站名稱:怎么用vue.js和laravel實(shí)現(xiàn)微信支付
文章URL:http://m.biofuelwatch.net/article/gogpoo.html


咨詢
建站咨詢
