In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to develop Mini Program and call WeChat Pay and Wechat callback address", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Mini Program how to develop and call WeChat Pay and Wechat callback address"!
First of all, watch the documents provided by Wechat
Https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3&index=1
Be clear about the parameters that must be passed to call WeChat Pay
Because Wechat provides a way for Mini Program to evoke WeChat Pay, the back end only needs to pass the corresponding parameters to the front end.
First, configure the fixed parameters of the application in the program.
Wx.open.app_id= user's appidwx.open.app_secret= this is the key of the weixin.pay.partner= merchant number wexxin.pay.partenerkey= merchant number for login
Write a tool class to read a fixed value
@ Component//@PropertySource ("classpath:application.properties") public class ConstantPropertiesUtil implements InitializingBean {/ / reads the configuration file and assigns @ Value ("${wx.open.app_id}") private String appId; @ Value ("${wx.open.app_secret}") private String appSecret; @ Value ("{weixin.pay.partner}") private String partner; @ Value ("{wexxin.pay.partenerkey}") private String partenerkey Public static String WX_OPEN_APP_ID; public static String WX_OPEN_APP_SECRET; public static String PARTNER; public static String PARTNERKET; @ Override public void afterPropertiesSet () throws Exception {WX_OPEN_APP_ID = appId; WX_OPEN_APP_SECRET = appSecret; PARTNER = partner; PARTNERKET = partenerkey;}}
When the user clicks to buy, an order will be generated. The code is omitted here.
When you click to log in, call the back end to pass the required value to the front end.
Corresponding Wechat document https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
As you can see, apart from some fixed values, there are some things that need to be dealt with by ourselves.
Signature: according to the document, we can find that there are certain requirements for signature.
To put it simply, other fields with fixed values are sorted and spliced, and encrypted according to the key of the merchant.
Payment interface @ Autowired private WXService wxService; @ GetMapping ("pay") public R creatNative (Integer orderid) {try {Map map = wxService.payment (orderid); return R.ok (). Data (map);} catch (UnsupportedEncodingException e) {return R.error (). Message ("payment failure");}}
Write service logic and pass values according to the document
@ Servicepublic class WXServiceImpl implements WXService {@ Autowired private OrderService orderService; @ Override public Map payment (Integer orderid) throws UnsupportedEncodingException {/ / encapsulate the Wechat address parameter Map paramMap = new HashMap (); paramMap.put ("appid", ConstantPropertiesUtil.WX_OPEN_APP_ID); / / official account id paramMap.put ("mch_id", ConstantPropertiesUtil.PARTNER) / / merchant number paramMap.put ("nonce_str", WXPayUtil.generateNonceStr ()); / / Random string, call tool class paramMap.put ("out_trade_no", orderid); / / order pipeline number Order order = orderService.getById (orderid); paramMap.put ("total_fee", order.getPayment ()) / / amount paramMap.put ("spbill_create_ip", "127.0.0.1"); / / Terminal ip paramMap.put ("notify_url", "http://XXXXX/weixin/callBack");// callback address paramMap.put (" body ", order.getProductname ()); / / Product name paramMap.put (" timeStamp ", WXUtil.getCurrentTimestamp () +") / / get the current timestamp (in seconds) String sign = WXUtil.genSignature (ConstantPropertiesUtil.PARTNERKET,paramMap); / / sing paramMap.put ("sign", sign); / / signature return paramMap }} signature tool class, and timestamp method public class WXUtil {public static String genSignature (String secretKey, Map params) throws UnsupportedEncodingException {if (secretKey = = null | | params = = null | | params.size () = = 0) {return ";} / / 1. The parameter names are sorted according to the ascending order of the ASCII code table: String [] keys = params.keySet (). ToArray (new String [0]); Arrays.sort (keys); / / 2. Splice parameter names with parameter values StringBuffer paramBuffer = new StringBuffer (); for (String key: keys) {paramBuffer.append ("&" + key) .append (params.get (key) = = null? ":" = "+ params.get (key));} / / 3. Concatenate the secretKey to the last paramBuffer=paramBuffer.append ("& key=" + secretKey); String pa = paramBuffer.substring (1); / / 4. MD5 is a 128-bit digest algorithm, expressed in hexadecimal, a hexadecimal character can represent four bits, so the length of the signed string is fixed at 32 hexadecimal characters. Return DigestUtils.md5Hex (pa.getBytes ("UTF-8")) .toUpperCase ();} / * * get the current timestamp in seconds * @ return * / public static long getCurrentTimestamp () {return System.currentTimeMillis () / 1000 } / * get the current timestamp (in milliseconds) * @ return * / public static long getCurrentTimestampMs () {return System.currentTimeMillis ();}}
At this point, the payment can be completed. After WeChat Pay, Wechat will call back the address to send us a message, from which we can determine the payment status and obtain the parameters returned by WeChat Pay.
Callback interface / / callback interface @ RequestMapping ("callBack") public String callBack (HttpServletRequest request, HttpServletResponse response) throws Exception {System.out.println ("API has been called"); ServletInputStream inputStream = request.getInputStream (); String notifyXml = StreamUtils.inputStream2String (inputStream, "utf-8"); System.out.println (notifyXml) / / the result returned by parsing Map notifyMap = WXPayUtil.xmlToMap (notifyXml) / / determine whether the payment is successful or not if ("SUCCESS" .equals (notifyMap.get ("result_code")) {/ / write your own implementation logic / / pay successfully: send Wechat a response I have received notification / / create a response object Map returnMap = new HashMap () ReturnMap.put ("return_code", "SUCCESS"); returnMap.put ("return_msg", "OK"); String returnXml = WXPayUtil.mapToXml (returnMap); response.setContentType ("text/xml"); System.out.println ("payment successful"); return returnXml }} / / create response object: after receiving the result of failed verification, Wechat will repeatedly call the current callback function Map returnMap = new HashMap (); returnMap.put ("return_code", "FAIL"); returnMap.put ("return_msg", ""); String returnXml = WXPayUtil.mapToXml (returnMap) Response.setContentType ("text/xml"); System.out.println ("check failed"); return returnXml;}
Receive input stream conversion tool class
Public class StreamUtils {private static int _ buffer_size = 1024; / * InputStream stream converted to String string * @ param inStream InputStream stream * @ param encoding encoding format * @ return String string * / public static String inputStream2String (InputStream inStream, String encoding) {String result = null; ByteArrayOutputStream outStream = null Try {if (inStream! = null) {outStream = new ByteArrayOutputStream (); byte [] tempBytes = new byte [_ buffer_size]; int count =-1; while ((count = inStream.read (tempBytes, 0, _ buffer_size)! =-1) {outStream.write (tempBytes, 0, count) } tempBytes = null; outStream.flush (); result = new String (outStream.toByteArray (), encoding); outStream.close ();}} catch (Exception e) {result = null } finally {try {if (inStream! = null) {inStream.close (); inStream = null;} if (outStream! = null) {outStream.close (); outStream = null }} catch (IOException e) {e.printStackTrace ();}} return result;}} so far, I believe you have a better understanding of "how Mini Program develops and calls WeChat Pay and Wechat callback addresses". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.