/** * 构造方法 */ public function __construct(Bag $bag, $key) { if (!$bag->has('trade_type')) { $bag->set('trade_type', 'JSAPI'); } if (!$bag->has('nonce_str')) { $bag->set('nonce_str', Util::randomString()); } if (!$bag->has('spbill_create_ip')) { $bag->set('spbill_create_ip', Util::getClientIp()); } // 检测必填字段 foreach ($this->required as $paramName) { if (!$bag->has($paramName)) { throw new PaymentException(sprintf('"%s" is required', $paramName)); } } // trade_type 为 JSAPI 时,必需包含 Openid if ($bag->get('trade_type') === 'JSAPI') { if (!$bag->has($paramName)) { throw new PaymentException('"Openid" is required'); } } $this->bag = $bag; $this->key = $key; }
public static function getJssdk(Wechat $wechat, $apis, $debug = false, $asArray = false) { try { $ticket = $wechat->getTicket('jsapi'); } catch (TicketException $e) { exit($e->getMessage()); } $bag = new Bag(); $bag->set('jsapi_ticket', $ticket); $bag->set('timestamp', time()); $bag->set('noncestr', Util::randomString()); $bag->set('url', Util::currentUrl()); $signGenerator = new SignGenerator($bag); $signGenerator->setUpper(false); $signGenerator->setHashType('sha1'); $config = array('appId' => $wechat->getAppid(), 'nonceStr' => $bag->get('noncestr'), 'timestamp' => $bag->get('timestamp'), 'signature' => $signGenerator->getResult(), 'jsApiList' => $apis); if ($debug) { $config['debug'] = true; } return $asArray ? $config : JSON::encode($config); }
?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>Wechat SDK</title> </head> <body ontouchstart=""> <h1>微信支付测试 <a href="javascript:;" onclick="window.location.reload()">刷新</a></h1> <h4>WeixinJSBridge invoke 方式:</h4> <button type="button" onclick="WXPayment()" style="font-size:16px;height:38px;">支付 ¥<?php echo $bag->get('total_fee') / 100; ?> 元</button> <script> var WXPayment = function() { if( typeof WeixinJSBridge === 'undefined' ) { alert('请在微信在打开页面!'); return false; } WeixinJSBridge.invoke('getBrandWCPayRequest', <?php echo $configJSON; ?> , function(res) { switch(res.err_msg) { case 'get_brand_wcpay_request:cancel':