本文讲解微信公众号支付功能服务端的接入,微信支付sdk下载地址:https://pay.weixin.qq.com/wiki/doc/api/download/WxpayAPI_php_v3.zip
微信支付接口说明文档:https://pay.weixin.qq.com/wiki/doc/api/index.html,这里以CI框架为例进行说明。
接入微信支付准备工作
要接入微信公众号支付功能,首先要开通以下账号并通过审核。
- 已认证通过的微信公众号(该微信公众号支持公众号支付功能)
- 已审核的通过的微信商户号
- 已通过审核的开发者账号
- 微信公众号开通微信支付应用(目的获取appid)
配置账号信息
在sdkthird_party/wxpay/lib/WxPay.Config
配置账号信息:
**
* 配置账号信息
*/
class WxPayConfig
{
//=======【基本信息设置】=====================================
//
/**
* TODO: 修改这里配置为您自己申请的商户信息
* 微信公众号信息配置
*
* APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
*
* MCHID:商户号(必须配置,开户邮件中可查看)
*
* KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
* 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
*
* APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
* 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
* @var string
*/
const APPID = 'xxxxx';
const MCHID = 'xxxxx';
const KEY = 'xxxxx';
const APPSECRET = 'xxxxx';
//=======【证书路径设置】=====================================
/**
* TODO:设置商户证书路径
* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要,可登录商户平台下载,
* API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书)
* @var path
*/
const SSLCERT_PATH = '../cert/apiclient_cert.pem';
const SSLKEY_PATH = '../cert/apiclient_key.pem';
//=======【curl代理设置】===================================
/**
* TODO:这里设置代理机器,只有需要代理的时候才设置,不需要代理,请设置为0.0.0.0和0
* 本例程通过curl使用HTTP POST方法,此处可修改代理服务器,
* 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置)
* @var unknown_type
*/
const CURL_PROXY_HOST = "0.0.0.0";//"10.152.18.220";
const CURL_PROXY_PORT = 0;//8080;
//=======【上报信息配置】===================================
/**
* TODO:接口调用上报等级,默认紧错误上报(注意:上报超时间为【1s】,上报无论成败【永不抛出异常】,
* 不会影响接口调用流程),开启上报之后,方便微信监控请求调用的质量,建议至少
* 开启错误上报。
* 上报等级,0.关闭上报; 1.仅错误出错上报; 2.全量上报
* @var int
*/
const REPORT_LEVENL = 1;
}
服务端支付接入(CI框架为例)
class Wxpay extends Home_Controller
{
public function __construct(){
parent::__construct();
}
//统一下单,获取支付参数
public function get_params()
{
log_message('debug', json_encode($_POST));
$this->load->model('M_user_deposit');
$type=CHARGE_TYPE_DEPOSIT;
$pay_channel = PAY_CHANNEL_WECHAT; //支付渠道
$user_info = $this->session->userdata(KEY_USER_INFO);
if(empty($user_info)){
$this->api_response_result(11);
}
$deposit = $this->M_user_deposit->get_deposit_info($user_info['id']);
if(!empty($deposit)){
$this->api_response_result(5001);
}
//微信支付sdk引入
require_once APPPATH . 'third_party/wxpay/lib/WxPay.Api.php';
require_once APPPATH . 'third_party/wxpay/WxPay.JsApiPay.php';
$tools = new JsApiPay();
$openId = $user_info['openid'];
$pay_data['title']='体验用车押金';
$pay_data['user_id']=$user_info['id'];
$pay_data['out_trade_no']=$this->build_charge_order_id($pay_channel,$type); //支付订单号
$pay_data['charge_amount']=$this->config->item('deposit_amount') * 100; //
$input = new WxPayUnifiedOrder();
$input->SetBody($pay_data['title']);
$input->SetAttach("{$pay_data['user_id']}.{$type}.{$pay_channel}"); //用户id.充值类型.支付渠道
$input->SetOut_trade_no($pay_data['out_trade_no']);
$input->SetTotal_fee($pay_data['charge_amount']); //$pay_data['charge_amount']
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag($pay_data['title']);
$input->SetNotify_url($this->config->item('app_path') . 'api/wxpay/notify');
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
$jsApiParameters = $tools->GetJsApiParameters($order);
//$data['jsApiParameters'] = $jsApiParameters;
log_message('debug', $jsApiParameters);
$this->api_response_result('0','success',json_decode($jsApiParameters,true));
}
/**
* 微信支付结果通知
* 回调地址: wxpay/notify
*/
public function notify()
{
require_once APPPATH . 'third_party/wxpay/WxPay.Notify.php';
$notify = new PayNotifyCallBack();
$notify->Handle(false);
}
}
支付结果通知处理
//third_party/wxpay/WxPay.Notify.php
require_once "lib/WxPay.Api.php";
require_once 'lib/WxPay.Notify.php';
class PayNotifyCallBack extends WxPayNotify
{
protected $CI;
public function __construct()
{
$this->CI = & get_instance();
}
//查询订单
public function Queryorder($transaction_id)
{
$input = new WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$result = WxPayApi::orderQuery($input);
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_message('debug', "wechat pay notify: " . json_encode($data));
$arr = explode('.', $data['attach']); //用户id.充值类型.支付渠道
$user_id = $arr[0];
$charge_type = $arr[1];
$pay_channel = $arr[2];
$this->CI->load->service('s_charge');
if (!array_key_exists("transaction_id", $data)) {
$msg = "输入参数不正确";
return false;
}
//查询订单,判断订单真实性
if (!$this->Queryorder($data["transaction_id"])) {
$msg = "订单查询失败";
return false;
}
$ret = $this->CI->s_charge->build_charge_order($data['transaction_id'],$data["out_trade_no"], $user_id, $data['total_fee'], $charge_type,$pay_channel);
if ($ret) {
return true;
} else {
log_message('debug', '[NotifyProcess] ret:' . json_encode($ret));
return false;
}
}
}