模式一:
1、微信商戶號開通 Native支付 ,設置回調鏈接
2、根據產品id,生成二維碼
public function generatePayQrcode() { $productId = $this->getPost('productId'); if (!$productId) return $this->sendFaild('參數不足'); // 生成內容 $Wechat = new EasyWechat(); $content = $Wechat->generatePayQrcode($productId); // public function generatePayQrcode($productId) // { // $app = Factory::payment(Config::PAYCONFIG); // return $app->scheme($productId); // } // 返回二維碼地址 $qrcode = SimpleQrcode::qrcode($content); // public static function qrcode(string $text = "default Text") // { // $filename = uniqid() . '.svg'; // $filePath = Config::IMG_PATH . $filename; // $qrcode = new Generator; // $qrcode->size(500); // $qrcode->encoding('UTF-8'); // $qrcode->generate($text, $filePath); // return $_SERVER['HTTP_HOST'] === 'localhost' ? "http://localhost/BJLH/media-back/static/imgs/$filename" : "http://media-back.solomore.net/static/imgs/$filename"; // } return $this->sendResult($qrcode); }
3、第一步設置的回調,生成預付訂單
public function paynotice() { // Monolog::log(file_get_contents('php://input')); $wechat = new EasyWechat(); $response = $wechat->getPrepayId(); // public function getPrepayId() // { // $app = Factory::payment(Config::PAYCONFIG); // // 掃碼支付通知接收第三個參數 `$alert`,如果觸發該函數,會返回“業務錯誤”到微信服務器,觸發 `$fail` 則返回“通信錯誤” // $response = $app->handleScannedNotify(function ($message, $fail, $alert) use ($app) { // // 如:$alert('商品已售空'); // // 根據不同的appid生成不同的訂單,並返回body、out_trade_no、total_fee // $wechat = new Wechat(); // $config = Config::OTHER_APPID_CONFIG[$message['appid']]; // 配置文件 // $generateOrder = $config['generateOrderFunction']; // 生成訂單函數 // $order = $wechat->$generateOrder($message); // 調用函數 // // 如業務流程正常,則要調用“統一下單”接口,並返回 prepay_id 字符串,代碼如下 // $result = $app->order->unify([ // 'trade_type' => 'NATIVE', // 'product_id' => $message['product_id'], // $message['product_id'] 則為生成二維碼時的產品 ID // 'body' => $order->body, // 'out_trade_no' => $order->out_trade_no, // 'total_fee' => $order->total_fee, // 'openid' => $message['openid'] // ]); // return $result['prepay_id']; // }); // return $response; // } $response->send(); }
4、用戶支付成功與否的回調(config定義的url)
public function paydone() { $response = new EasyWechat(); $response = $response->confirmPayDone(); // public function confirmPayDone() // { // $app = Factory::payment(Config::PAYCONFIG); // $response = $app->handlePaidNotify(function($message, $fail){ // // TODO 可以進一步優化 // // // 使用通知里的 "微信支付訂單號" 或者 "商戶訂單號" 去自己的數據庫找到訂單 // // $order = 查詢訂單($message['out_trade_no']); // // if (!$order || $order->paid_at) { // 如果訂單不存在 或者 訂單已經支付過了 // // return true; // 告訴微信,我已經處理完了,訂單沒找到,別再通知我了 // // } // if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信狀態,不代表支付狀態 // // // 用戶是否支付成功 // // if (array_get($message, 'result_code') === 'SUCCESS') { // // $order->paid_at = time(); // 更新支付時間為當前時間 // // $order->status = 'paid'; // // // 用戶支付失敗 // // } elseif (array_get($message, 'result_code') === 'FAIL') { // // $order->status = 'paid_fail'; // // } // // 判斷是否支付成功 // $status = $message['result_code'] === 'SUCCESS'; // // 根據不同的appid處理不同的回調 // $wechat = new Wechat(); // $config = Config::OTHER_APPID_CONFIG[$message['appid']]; // 配置文件 // $payDoneFunction = $config['payDoneFunction']; // 處理回調的函數 // $wechat->$payDoneFunction($status, $message); // 調用函數 // } else { // return $fail('通信失敗,請稍后再通知我'); // } // // $order->save(); // 保存訂單 // return true; // 返回處理完成 // }); // return $response; // } $response->send(); }
config配置
// 支付設置 const PAYCONFIG = [ // 必要配置 'app_id' => '', 'mch_id' => '', 'key' => '', // API 密鑰 // 如需使用敏感接口(如退款、發送紅包等)需要配置 API 證書路徑(登錄商戶平台下載 API 證書) 'cert_path' => 'path/to/your/cert.pem', // XXX: 絕對路徑!!!! 'key_path' => 'path/to/your/key', // XXX: 絕對路徑!!!! 'notify_url' => 'https://media-back.solomore.net/wechat/paydone', // 你也可以在下單時單獨設置來想覆蓋它 ]; // 支付時,不同appid做不同操作 const OTHER_APPID_CONFIG = [ 'wxfaec7ba0b7fad2cc' => [ 'generateOrderFunction' => 'generateOrder', 'payDoneFunction' => 'mediaPayDone' ] ];