微信小程序登录
1、配置小程序参数,建议不放在后台配置信息里,毕竟不是开放显示的东西,配置一次可能终身都不会再配了
common/config/params.php
配置示例如下:
- // 微信小程序配置 具体可参考EasyWechat
- 'wechatMiniProgramConfig' => [
- 'app_id' => 'wxecc4e33da02e3821',
- 'secret' => '7ced37f8f3eec389d5b9123bdcxxxx',
- 'mch_id' => '1504318xxx',
- 'key' => 'lqhbmNK32cXIqfxxx',
- 'cert_path' => Yii::getAlias('@root').'/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
- 'key_path' => Yii::getAlias('@root').'/cert/apiclient_key.pem', // XXX: 绝对路径!!!!
- //'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它
- ],
2、接口地址
common/modules/wechat/api/controllers/UserAuthController.php
ajax API地址:api.xxx.com/wechat/user-auth/wechat-mp/
微信支付
1、场景一:
(1)PC网站
(2)用户根据满意的商品加入购物车,加入完成后,系统生成订单,生成微信支付二维码,用户扫码支付
(3)回调中根据商户订单号,完成订单支付状态
- $totalFee = 1;// 支付金额单位:分
- // $totalFee *= 100;
- $out_trade_no = date("YmdHis") . StringHelper::random(8, true);
- $body = \Yii::$app->request->post("title",'云监控摄像头');
- $detail = \Yii::$app->request->post("detail",'');
- $log = new PayLog();
- $log->out_trade_no = $out_trade_no;
- $log->body = $body;
- $log->detail = $detail;
- $log->total_fee = $totalFee;
- $log->pay_fee = $totalFee;
- if (!$log->save()) {
- throw new HttpException(400,current($log->firstErrors));
- }
- $app = \Yii::$app->wechat->payment;
- /**
- * 注意:如果需要调用扫码支付 请设置 trade_type 为 NATIVE
- *
- * 结果示例:weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX
- */
- $result = $app->order->unify([
- 'body' => $body, //产品名称,
- 'detail' => $detail,//产品简介
- 'out_trade_no' => $out_trade_no,//商户订单号
- 'total_fee' => $totalFee * 100,//金额单位为:分
- // 'spbill_create_ip' => '123.12.12.123', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
- // 'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付结果通知网址,如果不设置则会使用上面config配置里的默认地址
- 'trade_type' => 'NATIVE',// JSAPI,NATIVE,APP...
- ]);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $config = $app->jssdk->sdkConfig($result['prepay_id']);
- $codeUrl = $result["code_url"];
- $ewm = DebrisHelper::getQRcode($codeUrl,"扫码支付");
- return [
- 'ewm' => $ewm,
- 'config' => $config
- ];
- } else {
- throw new HttpException(400,'微信支付异常, 请稍后再试');
- }
生成支付二维码,扫码后回调中会收到的字段是:
{
"appid": "wx312b4f15xxx",
"bank_type": "OTHERS",
"cash_fee": "1",
"fee_type": "CNY",
"is_subscribe": "Y",
"mch_id": "1600700149",
"nonce_str": "5f01692e3ee66",
"openid": "oNqO6s8PTlHlJNxxx",
"out_trade_no": "20200705134xxx",
"result_code": "SUCCESS",
"return_code": "SUCCESS",
"sign": "8339410C80A666BCA80F6B76B51C8978",
"time_end": "20200705134807",
"total_fee": "1",
"trade_type": "NATIVE",
"transaction_id": "420000061620xxx"
}
2、场景二:
(1)PC网站
(2)根据指定产品生成该产品的二维码
(3)扫码下单后支付,回调中可以获取产品的id
- // 产品ID
- $productId = \Yii::$app->request->get("product_id", 1);
- $app = \Yii::$app->wechat->payment;
- $content = $app->scheme($productId);
- $ewm = DebrisHelper::getQRcode($content, "扫码支付");
- return [
- 'ewm' => $ewm,
- ];
生成产品二维码,扫码后回调中会收到的字段是:
{
"appid": "wx312b4xxxx",
"openid": "oNqO6s8PTlxxx",
"mch_id": "1600700149",
"is_subscribe": "Y",
"nonce_str": "lyfaSsxHgp83qUQp",
"product_id": "1",
"sign": "196704844B365Bxxx"
}