Abstract : A recent project involved the payment service, which uses a micro-channel payment and Alipay, in the process of doing also encountered some problems, so now summarize comb, to share with people in need, but also look back to their own future leave ideas.
A: micro-channel pay access preparation work :
First, micro-channel pay only supports enterprise users, individual users can not access micro-channel pay, so in order to access micro-channel payment, you first need to have a public micro-channel number, the enterprises can apply. With micro-channel public number, you can apply for micro-channel pay-related content, so until you are ready to start writing code needs to put the following parameters apply good: the public account ID, micro-channel pay merchant number, API key, AppSecret is corresponding APPID Interface password, callback address (the callback outside the network must be guaranteed access to this address) and send a request computer's IP
Two: micro-channel payment process Description:
With these parameters mentioned above, then we can access micro-channel pay, here I look at the official documentation micro-channel payment (https://pay.weixin.qq.com/wiki/doc/api/index. html), visit this address may see a variety of payment methods to choose from, here we choose the way to pay the scan code (https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter= 6_1)
Here we choose mode two, the following two timing chart look pattern, as shown below:
Model II and a comparison mode, the process is simpler, it does not depend on paid set callback URL. Call the merchant back office systems to micro-channel pay unified under a single interface, a micro letter backstage return link system parameters code_url, merchant back office systems will code_url value generated two-dimensional code images, users use micro-channel client scan code to initiate the payment. Note: code_url valid for two hours after the expiration scan code can not initiate a payment.
Business Process Description:
(1) The system generates a background line merchant user to buy the goods.
(2) the user to confirm the payment call micro-channel pay [ unified under a single API ] generate a pre-payment transaction;
(3)微信支付系统收到请求后生成预支付交易单,并返回交易会话的二维码链接code_url。
(4)商户后台系统根据返回的code_url生成二维码。
(5)用户打开微信“扫一扫”扫描二维码,微信客户端将扫码内容发送到微信支付系统。
(6)微信支付系统收到客户端请求,验证链接有效性后发起用户支付,要求用户授权。
(7)用户在微信客户端输入密码,确认支付后,微信客户端提交授权。
(8)微信支付系统根据用户授权完成支付交易。
(9)微信支付系统完成支付交易后给微信客户端返回交易结果,并将交易结果通过短信、微信消息提示用户。微信客户端展示支付交易结果页面。
(10)微信支付系统通过发送异步消息通知商户后台系统支付结果。商户后台系统需回复接收情况,通知微信后台系统不再发送该单的支付通知。
(11)未收到支付通知的情况,商户后台系统调用【查询订单API】。
(12)商户确认订单已支付后给用户发货。
三:微信支付所需要的maven依赖:
<!--生成二维码jar--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency>
四:微信支付调用统一下单接口的核心代码
3.1:微信支付工具类:
HttpUtil.java
package com.micai.springboot.util.pay.wx; public class PayConfigUtil { //初始化 // public final static String APP_ID = "11111111111"; //公众账号appid(改为自己实际的) // public final static String APP_SECRET = ""; // public final static String MCH_ID = "111111"; //商户号(改为自己实际的) // public final static String API_KEY = "11111111111"; //(改为自己实际的)key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 //统一下单 public final static String UFDODER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; // public final static String NOTIFY_URL = "http://xxxxxxx"; //微信支付回调接口,就是微信那边收到(改为自己实际的) // //企业向个人账号付款的URL // public final static String SEND_EED_PACK_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; // // public final static String CREATE_IP = "113.69.246.11";//发起支付ip(改为自己实际的) }
package com.micai.springboot.util.pay.wx; import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream; /** * 二维码生产工具类 */ public class QRUtil { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private QRUtil() {} public static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } return image; } public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } } }
访问支付url返回微信二维码,如下图:
使用微信的扫一扫,扫码支付,如下图:
微信支付回调,如下图:
这里对于回调只是简单输出了日志,你可以根据自己的实际情况选择做相应的处理,一般都是对订单的支付状态做更新。
六:总结:
After the above process so, I believe we all understand the process of micro-channel pay, where we make a summary of the above processes, in order to access micro-channel pay, the job must be business users, individual users do not support, so writing code at the beginning before going to the relevant person responsible for applying good micro-channel pay-related configuration parameters, with these in order to carry out the following work, the most important thing is micro-channel pay pullback here, callback, you must configure can external network access in a production environment the URL, domain name must be filed at the same time ever, two domain names can be here for the convenience of our test, so we used the internal network penetration tools natapp, the tool has both free channels also have toll lanes, toll lanes is also very cheap, If the only test, free passage enough, one more thing to note is that the callback micro-channel payment, default micro-channel callback several times, so there will be repeated callback question, here we leave you with a thought, how to prevent micro-channel multiple callbacks, so as not to affect the business, that are interested in small partner can leave pay . These are the micro-channel pay (pay scan code mode two) of all content, and want to have complete source code of a small partner, you can add exchange group, group number: 715 224 124.