PHPPHP实战

一个php实现的微信支付工具集

2017-03-02  本文已影响83人  Stone_Zhuo

文档结构

使用说明

使用示例

假设现在有一个名为test的项目需要提供微信支付的支持。

{
    "appId" : "******", // 绑定支付的APPID
    "mchId" : "******", // 商户号
    "key" : "******", // 商户支付密钥
    "appSecret" : "******", // 公众帐号secert
    "sslcertPath" : "../cert/test/apiclient_cert.pem", // 证书pem格式
    "sslkeyPath" : "../cert/test/apiclient_key.pem", // 证书密钥pem格式
    "curlProxyHost" : "0.0.0.0", // 代理地址
    "curlProxyPort" : 0, // 代理端口
    "reportLevel" : 1 // 上报等级,0.关闭上报; 1.仅错误出错上报; 2.全量上报
}
require_once(PATHTOWXPAY.DS.'stone-wxpay'.DS.'lib'.DS.'WxPayApi.php');
require_once(PATHTOWXPAY.DS.'stone-wxpay'.DS.'lib'.DS.'CommonFunctions.php');
$testWxpayConfig = json_decode(file_get_contents(PATHTOWXPAY.DS.'stone-wxpay'.DS.'config'.DS.'test.json'), true);
// 统一下单
$attachData = json_encode(['orderId' => 1]);
$nonceStr = get_random_string(32);
$order = WxPayApi::unified_order([
    'body' => '测试订单', // 商品描述
    'attach' => $attachData, // 附加数据
    'out_trade_no' => '0320170301122543576135', // 商户订单号
    'total_fee' => 1, // 订单总金额,单位为分
    'time_start' => date('YmdHis'), // 交易起始时间
    'time_expire' => date('YmdHis', time() + 600), // 交易结束时间
    'notify_url' => 'https://http-host-or-ip/controller/action',
    'trade_type' => 'APP', // 交易类型
    'appid' => $testWxpayConfig['appId'], // 公众账号ID
    'mch_id' => $testWxpayConfig['mchId'], // 商户号
    'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], // 终端IP
    'nonce_str' => $nonceStr, // 随机字符串
], [
    'appId' => $testWxpayConfig['appId'],
    'mchId' => $testWxpayConfig['mchId'],
    'key' => $testWxpayConfig['key'],
    'sslcertPath' => $testWxpayConfig['sslcertPath'],
    'sslkeyPath' => $testWxpayConfig['sslkeyPath'],
    'curlProxyHost' => $testWxpayConfig['curlProxyHost'],
    'curlProxyPort' => $testWxpayConfig['curlProxyPort'],
    'reportLevel' => $testWxpayConfig['reportLevel'],
    'nonceStr' => $nonceStr,
]);
if ($order['result_code'] == 'SUCCESS' && $order['return_code'] == 'SUCCESS') {

    $pay_info = [
        'appid' => $order['appid'],
        'partnerid' => $order['mch_id'],
        'prepayid' => $order['prepay_id'],
        'package' => 'Sign=WXPay',
        'noncestr' => $nonceStr,
        'timestamp' => time(),
    ];

    $pay_info['sign'] = generate_wxpay_sign($pay_info, $testWxpayConfig['key']);

    return ['status' => 200, 'error' => '000', 'message' => '获取app微信支付参数成功', 'data' => $pay_info];
} else {
    return ['status' => 400, 'error' => '000', 'message' => '获取app微信支付参数失败', 'data' => $order];
}

项目托管地址

https://github.com/betterzfz/stone-wxpay

本文首发于公众号:programmer_cc,转载请注明出处。


微信公众号.jpg
上一篇 下一篇

猜你喜欢

热点阅读