注:此項(xiàng)是微信公眾號(hào)開(kāi)發(fā),請(qǐng)?jiān)谕驴粗埃葘?shí)現(xiàn)網(wǎng)頁(yè)微信授權(quán)登陸功能,具體參看我簡(jiǎn)書的另一篇文章://m.survivalescaperooms.com/article/117004.htm
1.打開(kāi)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', ''), // ... ],以上參數(shù),請(qǐng)依照自己的情況配置,請(qǐng)勿直接拷貝代碼!
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); } } }
新聞熱點(diǎn)
疑難解答
圖片精選