6 微信支付
Frye edited this page 2015-09-02 11:19:59 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

使用微信支付前,需要先到微信公众平台配置好授权目录测试账号等信息。 微信支付开发文档可参考:https://pay.weixin.qq.com/wiki/doc/api/index.html

部分API参考了thenbsp大神的Wechat设计

##公众号支付 在微信浏览器里面打开H5网页中执行JS调起支付。 参考:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1

###示例 ####PHP

<?php

use Overtrue\Wechat\Payment;
use Overtrue\Wechat\Payment\Order;
use Overtrue\Wechat\Payment\Business;
use Overtrue\Wechat\Payment\UnifiedOrder;

/**
 * 第 1 步:定义商户
 */
$business = new Business(
    APP_ID,
    APP_KEY,
    MCH_ID,
    MCH_KEY
);

/**
 * 第 2 步:定义订单
 */
$order = new Order();
$order->body = 'test body';
$order->out_trade_no = md5(uniqid().microtime());
$order->total_fee = '1';    // 单位为 “分”, 字符串类型
$order->openid = OPEN_ID;
$order->notify_url = 'http://xxx.com/wechat/payment/notify';

/**
 * 第 3 步:统一下单
 */
$unifiedOrder = new UnifiedOrder($business, $order);

/**
 * 第 4 步:生成支付配置文件
 */
$payment = new Payment($unifiedOrder);

####Javascript

var WXPayment = function() {
    if( typeof WeixinJSBridge === 'undefined' ) {
        alert('请在微信在打开页面!');
        return false;
    }
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', <?php echo $payment->getConfig(); ?>, function(res) {
        switch(res.err_msg) {
            case 'get_brand_wcpay_request:cancel':
                alert('用户取消支付!');
                break;
            case 'get_brand_wcpay_request:fail':
                alert('支付失败!('+res.err_desc+'');
                break;
            case 'get_brand_wcpay_request:ok':
                alert('支付成功!');
                break;
            default:
                alert(JSON.stringify(res));
                break;
        }
    });
}

####HTML

<button type="button" onclick="WXPayment()">支付 ¥<?php echo ($order->total_fee / 100); ?></button>

###支付通知

<?php

use Overtrue\Wechat\Payment\Notify;

$notify = new Notify(
    APP_ID,
    APP_KEY,
    MCH_ID,
    MCH_KEY
);

$transaction = $notify->verify();

if (!$transaction) {
    $notify->reply('FAIL', 'verify transaction error');
}

// var_dump($transaction);

echo $notify->reply();