protected function options(){ //选项设置 return $options = [ // 前面的appid什么的也得保留哦 'app_id' => 'wx70****b87ab14', 'secret'=>'5136bc10d******4c5c3f4', 'token' => 'your-token', // Token 'aes_key' => '', // EncodingAESKey,安全模式下请一定要填写!!! // ... // payment 'payment' => [ 'merchant_id' => '1******991', 'key' => '28d9d6b641******e31abeb', 'cert_path' => '*********/apiclient_cert.pem', // XXX: 绝对路径!!!! 'key_path' => '********apiclient_key.pem', // XXX: 绝对路径!!!! 'notify_url' => 'http://***************', // ], ]; } //扫码支付生成订单 public function payOrder(){ // 通过EasyWechat来调用 $config = $this->options(); $wxApp = new Application($config); $payment = $wxApp->payment; $attributes = [ 'trade_type'=>Order::NATIVE, 'body'=>"商品描述", 'detail'=>"商品简介", 'out_trade_no'=>'sdfdsfdsgdsadadsafdadsa', 'total_fee'=>'1', 'notify_url'=>'http://pay.codeisland.cn/wx/pay/notify' ]; $o = new Order($attributes); $result = $payment->prepare($o); if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){ $prepayId = $result->prepay_id; $codeUrl = $result->code_url; $qrCode = new QrCode($codeUrl); //生成二维码 $qrCode->setSize(300); $qrCode->setWriterByName('png'); $qrCode->setMargin(10); $qrCode->setEncoding('UTF-8'); $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH); $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]); $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]); // $qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER); //$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png'); //logo 区 $qrCode->setLogoWidth(150); $qrCode->setRoundBlockSize(true); $qrCode->setValidateResult(false); //Directly output the QR code // header('Content-Type: '.$qrCode->getContentType()); // echo $qrCode->writeString(); // die; // // Save it to a file // $qrCode->writeFile(__DIR__.'/qrcode.png'); $imgData= base64_encode($qrCode->writeString()); //转成字符串返回给前端 $data=['status_code'=>200,'msg'=>'发起支付成功','imgData'=>$imgData]; return $this->responseSuccess($data,'request_success'); }else{ return $this->setStatusCode(1001)->responseErrors('生成订单错误!'); } }
public function paysuccess(Request $request) { $app = new Application($this->options()); $payment = $app->payment; $response = $app->payment->handleNotify(function($notify, $successful) { \Log::info('订单号:'.$notify->transaction_id); \Log::info('ooooo:'.json_encode($notify)); $res = DB::table('out_order_wxpay_notify')->insert($notify); return $res; exit; // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单 $order = 查询订单($notify->transaction_id); if (!$order) { // 如果订单不存在 return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了 } // 如果订单存在 // 检查订单是否已经更新过支付状态 if ($order->paid_at) { // 假设订单字段“支付时间”不为空代表已经支付 return true; // 已经支付成功了就不再更新了 } // 用户是否支付成功 if ($successful) { // 不是已经支付状态则修改为已经支付状态 $order->paid_at = time(); // 更新支付时间为当前时间 $order->status = 'paid'; } else { // 用户支付失败 $order->status = 'paid_fail'; } $order->save(); // 保存订单 return true; // 返回处理完成 }); return $response; }