Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize JS API payment in Wechat Development

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how Wechat development to achieve JS API payment, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

Process implementation 1. OAuth3.0 authorization

Before JSAPI payment, you need to call the login authorization API to obtain the Openid of the user. So you need to do an authorization, and this authorization does not pop up a confirmation box.

Its essence is that when users visit

Http://www.fangbei.org/wxpay/js_api_call.php

Jump to the

Https://open.weixin.qq.com/connect/oauth3/authorize?appid=wx8888888888888888&redirect_uri=http://www.fangbei.org/wxpay/js_api_call.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

Use this to obtain code parameters, and obtain authorization access_token and openid according to code

For the detailed process of its implementation, please refer to the development of Wechat public platform (71) OAuth3.0 web page authorization.

In WeChat Pay's Demo, the code is

1 / / use the jsapi interface 2$ jsApi = new JsApi_pub (); 3 4 / = step 1: get the user openid= 5 / / obtain the openid 6 if through code (! isset ($_ GET) 7 {8 / / trigger Wechat to return the code 9$ url = $jsApi- > createOauthUrlForCode (WxPayConf_pub::JS_API_CALL_URL); 10 Header ("Location: $url") 11} else12 {13 / / get the code to get openid14 $code = $_ GET ['code']; 15$ jsApi- > setCode ($code); 16$ openid = $jsApi- > getOpenId (); 17}

The end result of this step is to get the openid of the current user.

Ou9dHt0L8qFLI1foP-kj5x1mDWsM2. Unified payment

Unified payment is an interface for JSAPI/NATIVE/APP to generate payment orders and return prepaid order numbers under various payment scenarios. At present, WeChat Pay uses this interface in all scenarios.

The following parameters in Unified payment are obtained from the configuration or automatically generated by the class, and do not need to be filled in by the user.

$this- > parameters ["appid"] = WxPayConf_pub::APPID;// public account ID$this- > parameters ["mch_id"] = WxPayConf_pub::MCHID;// merchant number $this- > parameters ["spbill_create_ip"] = $_ SERVER ['REMOTE_ADDR']; / / Terminal ip $this- > parameters ["nonce_str"] = $this- > createNoncestr (); / / Random string $this- > parameters ["sign"] = $this- > getSign ($this- > parameters); / / signature

In the JSAPI payment, enter the following additional parameters

/ / in the unified payment API, when trade_type is JSAPI, openid is required! $unifiedOrder- > setParameter ("openid", "$openid"); / / Product description $unifiedOrder- > setParameter ("body", "Square Studio"); / / Product description / / Custom order number, for example only $timeStamp = time (); $out_trade_no = WxPayConf_pub::APPID. "$timeStamp"; $unifiedOrder- > setParameter ("out_trade_no", "$out_trade_no"); / / Merchant order No. $unifiedOrder- > setParameter ("total_fee", "1") / / Total amount $unifiedOrder- > setParameter ("notify_url", WxPayConf_pub::NOTIFY_URL); / / Notification address $unifiedOrder- > setParameter ("trade_type", "JSAPI"); / / transaction type

Other parameters are optional.

/ / optional parameters: / / $unifiedOrder- > setParameter ("sub_mch_id", "XXXX"); / / sub-merchant number / / $unifiedOrder- > setParameter ("device_info", "XXXX"); / / device number / / $unifiedOrder- > setParameter ("attach", "XXXX"); / / additional data / / $unifiedOrder- > setParameter ("time_start", "XXXX"); / / transaction start time / / $unifiedOrder- > setParameter ("time_expire", "XXXX") / / transaction end time / / $unifiedOrder- > setParameter ("goods_tag", "XXXX"); / / Commodity tag / / $unifiedOrder- > setParameter ("openid", "XXXX"); / / user ID / / $unifiedOrder- > setParameter ("product_id", "XXXX"); / / Commodity ID

These parameters eventually form such xml data.

1 10012345

Submit these data to the unified payment interface

Https://api.mch.weixin.qq.com/pay/unifiedorder

The data will be returned as follows

It contains the most important prepaid ID parameter, prepay_id, with a value of

Wx201410272009395522657a6903892851003, JS API payment

After the previous preparatory work is done, JS API generates jsapi payment parameters according to prepay_id

The generated code is as follows

/ / = step 3: use jsapi to call up payment = $jsApi- > setPrepayId ($prepay_id); $jsApiParameters = $jsApi- > getParameters ()

The generated json data is as follows

{"appId": "wx8888888888888888", "timeStamp": "1414411784", "nonceStr": "gbwr71b5no6q6ne18c8up1u7l7he2y75", "package": "prepay_id=wx201410272009395522657a690389285100", "signType": "MD5", "paySign": "9C6747193720F851EB876299D59F6C7D"}

Debug the js interface in Wechat browser with the following code

Wechat Security pay / / call Wechat JS api pay function jsApiCall () {WeixinJSBridge.invoke ('getBrandWCPayRequest', function (res) {WeixinJSBridge.log (res.err_msg); / / alert (res.err_code+res.err_desc+res.err_msg) });} function callpay () {if (typeof WeixinJSBridge = = "undefined") {if (document.addEventListener) {document.addEventListener ('WeixinJSBridgeReady', jsApiCall, false);} else if (document.attachEvent) {document.attachEvent (' WeixinJSBridgeReady', jsApiCall) Document.attachEvent ('onWeixinJSBridgeReady', jsApiCall);}} else {jsApiCall ();}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report