紅包接口調用:javascript
在使用紅包請求功能的時候,注意mch_billno(商戶訂單號)這個參數;php
商戶訂單號(每一個訂單號必須惟一)html
組成: mch_id+yyyymmdd+10位一天內不能重複的數字。java
微信支付:數組
用easywechat來開發微信支付功能,步驟以下:微信
一,須要有一個商品下單頁面,頁面上有你的商品的信息,還要有購買數量,和一個購買按鈕。微信開發
用戶在這個頁面瀏覽商品信息的時候,能夠選擇購買的數量,而後點擊提交進入下一個頁面。app
(這個頁面,跟淘寶的商品購買頁面同樣。)函數
二,建立一個頁面,接收上面一步提交來的數據,而後作以下計算 。post
1,根據商品ID或是提交來的商品信息,生成商品的參數數組。
- $product = [
- 'trade_type' => 'JSAPI',
- 'body' => '一盒火柴',
- 'detail' => '一盒火柴',
- 'out_trade_no' => 'MYERPORDERID12345678',
- 'total_fee' => 8888,
- 'notify_url' => 'http://www.xxx.com/order_notify', // 支付結果通知網址,若是不設置則會使用配置裏的默認地址 (填寫發起支付請求的網址)
- 'openid' => 'you-open-id',
-
- ];
2,接下來,生成商品對象。
- $order = new Order($product);
3,再接下來,要進行調用前計算了。
- $app = new Application(config('wechat'));
- $payment = $app->payment;
- //統一下單(像公衆號支付、掃碼支付、APP支付都使用這個接口)
- $result = $payment->prepare($order);
- $prepayId = null;
- if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
- $prepayId = $result->prepay_id;
- } else {
- var_dump($result);
- die("出錯了。");
- }
- $config = $payment->configForJSSDKPayment($prepayId);
4,就快進入正題,可是,還沒完。還要取得一個數據。
- $app = new Application(config('wechat'));
- $js = $app->js;
5,上面得到的$js 和 $config數據,要在頁面裏顯示出來。
注意,上面幾步都不涉及顯示,只是在生成頁面顯示所須要的數據。
如今才須要把這些顯示在頁面上。
- <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript" charset="utf-8"></script>
-
- <script type="text/javascript" charset="utf-8">
- wx.config({{ $js->config(array('chooseWXPay')) }});
- </script>
- <script>
- $(function(){
-
- $(".btn-do-it").click(function(){
- wx.chooseWXPay({
- timestamp: "{{$config['timestamp']}}",
- nonceStr: '{{$config['nonceStr']}}',
- package: '{{$config['package']}}',
- signType: '{{$config['signType']}}',
- paySign: '{{$config['paySign']}}',
- success: function (res) {
-
- if(res.err_msg == "get_brand_wcpay_request:ok" ) {
- alert('支付成功。');
- window.location.href="{{url("wechat/pay_ok")}}";
- }else{
-
- alert("支付失敗,請返回重試。");
- }
- },
- fail: function (res) {
- alert("支付失敗,請返回重試。");
- }
- });
- });
- });
- </script>
沒錯,只須要修改這些。
再來理一個,第二步裏,這個頁面須要顯示一個確認信息,好比商品的名稱,和支付的總金額。
頁面上應該有一個按鈕,用來觸發支付(.btn-do-it)。
頁面裏的js就是第5步裏要顯示的內容。
總結:
第一步,商品信息頁面,用戶選擇購買數據,點購買,提交到第二步的確認頁面。
第二步的確認頁面,根據商品信息,和微信配置信息,生成必要的支付數據,並顯示購買確認信息,和一個支付按鈕,
用戶點擊支付按鈕,發起支付。
支付完成,提示用戶,或轉到相應頁面。
微信的開發團隊提醒咱們,請以微信的推送信息爲準來處理訂單,否則有可能人財兩空。
product.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>一盒火柴</title>
- </head>
- <body>
-
- <h1>一盒火柴</h1>
-
- <div>
- 賣火柴的小女孩,手裏有一堆火柴。
- </div>
-
- <hr>
-
- <div>
- <form name="form1" action="payment.php" method="post">
- <lable>數量:</lable>
- <input name="qty" value="1" />
- <input name="submit" type="submit" value="購買" />
- </form>
- </div>
-
- </body>
- </html>
payment.php
- <?php
- use EasyWeChat\Foundation\Application;
- use EasyWeChat\Payment\Order;
-
- $product = [
- 'body' => '一盒火柴',
- 'trade_type' => 'JSAPI',
- 'out_trade_no' => 'ERP'.time(),
- 'total_fee' => 1,
- 'notify_url' => 'http://test.xxoo.com/wechat/notify/',
- 'openid' => $_SESSION['openid'],
- 'attach' => '賣火柴的小女孩',
- ];
-
- $order = new Order($product);
-
- $app = new Application(config('wechat'));
- $js = $app->js;
- $payment = $app->payment;
- $result = $payment->prepare($order);
- $prepayId = null;
- if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
- $prepayId = $result->prepay_id;
- } else {
- var_dump($result);
- die("出錯了。");
- }
- $config = $payment->configForJSSDKPayment($prepayId);
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>購買支付</title>
- </head>
- <body>
-
- <h1>一盒火柴</h1>
-
- <div>
- <p>您購買了「一盒火柴」,總價格: 0.01元。</p>
- <p>數量:1盒。</p>
- </div>
-
- <hr>
-
- <div>
- <input name="button" id="btnPay" type="button" value="支付" />
- </div>
-
-
-
-
- <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript" charset="utf-8"></script>
- <script type="text/javascript" charset="utf-8">
- wx.config({{ $js->config(array('chooseWXPay')) }});
- </script>
- <script>
- $(function(){
-
- $(".btn-do-it").click(function(){
- wx.chooseWXPay({
- timestamp: "{{$config['timestamp']}}",
- nonceStr: '{{$config['nonceStr']}}',
- package: '{{$config['package']}}',
- signType: '{{$config['signType']}}',
- paySign: '{{$config['paySign']}}',
- success: function (res) {
-
- if(res.err_msg == "get_brand_wcpay_request:ok" ) {
- alert('支付成功。');
- window.location.href="{{url("wechat/pay_ok")}}";
- }else{
-
- alert("支付失敗,請返回重試。");
- }
- },
- fail: function (res) {
- alert("支付失敗,請返回重試。");
- }
- });
- });
- });
- </script>
- </body>
- </html>
這些是僞代碼,payment.php並無對product.html傳來的商品信息作處理。
其實只要看payment.php文件應該知道怎麼作了。
其它就兩點。a,獲取$config,b, 本身寫wx.chooseWXPay。
其它的看easywechat文檔就好了。