-
Notifications
You must be signed in to change notification settings - Fork 316
payment_jsapi
thenbsp edited this page Mar 3, 2016
·
4 revisions
use Thenbsp\Wechat\Wechat\Jsapi;
use Thenbsp\Wechat\Payment\Unifiedorder;
/**
* 生成 Jsapi 配置文件
*/
$jsapi = new Jsapi($accessToken);
$jsapi->setCache($cache);
$jsapi->addApi('chooseWXPay');
/**
* 统一下单
*/
$unifiedorder = new Unifiedorder('appid', 'mch_id', 'key');
$unifiedorder->set('body', '微信支付测试商品');
$unifiedorder->set('total_fee', 1);
$unifiedorder->set('openid', 'OPENID');
$unifiedorder->set('out_trade_no', date('YmdHis').mt_rand(10000, 99999));
$unifiedorder->set('notify_url', 'http://example.com/your-notify.php');
/**
* 生成 ChooseWXPay 配置文件
*/
$config = new PayChoose($unifiedorder);
Javascript
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
/**
* 注入 Jsapi 配置
*/
wx.config(<?php echo $jsapi; ?>);
/**
* 点击支付按款
*/
var WXPayment = function() {
var config = <?php echo $config; ?>;
config.success = function() {
alert('支付成功!');
}
config.cancel = function() {
alert('用户取消成功!');
}
wx.chooseWXPay(config);
}
</script>
HTML
<button type="button" onclick="WXPayment()">
支付 ¥<?php echo ($unifiedorder['total_fee']/100); ?> 元</button>
use Thenbsp\Wechat\Payment\Unifiedorder;
use Thenbsp\Wechat\Payment\Jsapi\PayRequest;
/**
* 统一下单
*/
$unifiedorder = new Unifiedorder('appid', 'mch_id', 'key');
$unifiedorder->set('body', '微信支付测试商品');
$unifiedorder->set('total_fee', 1);
$unifiedorder->set('openid', 'OPENID');
$unifiedorder->set('out_trade_no', date('YmdHis').mt_rand(10000, 99999));
$unifiedorder->set('notify_url', 'http://example.com/your-notify.php');
/**
* 生成 PayRequest 配置文件
*/
$config = new PayRequest($unifiedorder);
Javascript
<script>
var WXPayment = function() {
if( typeof WeixinJSBridge === 'undefined' ) {
alert('请在微信在打开页面!');
return false;
}
WeixinJSBridge.invoke('getBrandWCPayRequest', <?php echo $config; ?>, 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;
}
});
}
</script>
HTML
<button type="button" onclick="WXPayment()">
支付 ¥<?php echo ($unifiedorder['total_fee']/100); ?> 元</button>