🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 1.注册微信支付商户号 具体到官网查看https://pay.weixin.qq.com ### 2.下载证书 下载之前要安装操作证书 ![### 3.设置API密钥](https://box.kancloud.cn/0cf2a2d659f23a8067be21754b67a49c_554x230.png) ### 4.登录绑定的微信公众号 获取appid和appsecreat ### 5.接入扫码支付 扫码支付分为模式1和模式2,其中模式1,一般是用于线下支付,先扫码后生成订单,模式2:是生成订单后再扫码支付。 其中模式一需要在公众号的微信支付中配置回调,模式二不需要 这里我们只说模式二 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5 ### 6.php源码开发 下载php源码 打开WxPayConfig配置支付基本信息 ### 7.调起支付界面 ~~~ <?php ini_set('date.timezone','Asia/Shanghai'); //error_reporting(E_ERROR); require_once "../lib/WxPay.Api.php"; require_once "WxPay.NativePay.php"; require_once 'log.php'; //模式二 /** * 流程: * 1、调用统一下单,取得code_url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、支付完成之后,微信服务器会通知支付成功 * 4、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ $input = new WxPayUnifiedOrder(); $input->SetBody("test"); $input->SetAttach("test"); $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis")); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");//回调地址 $input->SetTrade_type("NATIVE"); $input->SetProduct_id("123456789"); $result = $notify->GetPayUrl($input); $url2 = $result["code_url"]; ?> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>微信支付样例-退款</title> </head> <body> <div style="margin-left: 10px;color:#556B2F;font-size:30px;font-weight: bolder;">扫描支付模式二</div><br/> <img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data=<?php echo urlencode($url2);?>" style="width:150px;height:150px;"/> </body> </html> ~~~ ### 8.接收回调通知 接收的地址与Notify_url对应 ~~~ <?php ini_set('date.timezone','Asia/Shanghai'); error_reporting(E_ERROR); require_once "../lib/WxPay.Api.php"; require_once '../lib/WxPay.Notify.php'; require_once 'log.php'; //初始化日志 $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log'); $log = Log::Init($logHandler, 15); class PayNotifyCallBack extends WxPayNotify { //查询订单 public function Queryorder($transaction_id) { $input = new WxPayOrderQuery(); $input->SetTransaction_id($transaction_id); $result = WxPayApi::orderQuery($input); log_debug($transaction_id."查询订单结果:",$result); if(array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") { return true; } return false; } //重写回调处理函数 public function NotifyProcess($data, &$msg) { log_debug("回调数据结果:",json_encode($data)); if(!array_key_exists("transaction_id", $data)){ $msg = "输入参数不正确"; return false; } //查询订单,判断订单真实性 if(!$this->Queryorder($data["transaction_id"])){ $msg = "订单查询失败"; return false; } return true; } } Log::DEBUG("begin notify"); $notify = new PayNotifyCallBack(); $notify->Handle(false); ~~~ 9.自动刷新订单状态 由于微信支付成功,无法自动跳转,需要在支付页面使用ajax轮询刷新订单状态,当订单支付成功,实现自动跳转