模式一:
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' ] ];