mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-17 05:48:12 +08:00
添加扫码支付生成二维码的接口及其单元测试,#113
This commit is contained in:
@@ -7,6 +7,7 @@ import me.chanjar.weixin.mp.bean.pay.request.WxPaySendRedpackRequest;
|
||||
import me.chanjar.weixin.mp.bean.pay.request.WxPayUnifiedOrderRequest;
|
||||
import me.chanjar.weixin.mp.bean.pay.result.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -235,4 +236,32 @@ public interface WxMpPayService {
|
||||
*/
|
||||
WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 扫码支付模式一生成二维码的方法
|
||||
* 二维码中的内容为链接,形式为:
|
||||
* weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX
|
||||
* 其中XXXXX为商户需要填写的内容,商户将该链接生成二维码,如需要打印发布二维码,需要采用此格式。商户可调用第三方库生成二维码图片。
|
||||
* 文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4
|
||||
* </pre>
|
||||
* @param productId 产品Id
|
||||
* @param sideLength 要生成的二维码的边长,如果为空,则取默认值400
|
||||
* @param logoFile 商户logo图片的文件对象,可以为空
|
||||
* @return 生成的二维码的字节数组
|
||||
*/
|
||||
byte[] createScanPayQrcodeMode1(String productId, File logoFile, Integer sideLength);
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 扫码支付模式二生成二维码的方法
|
||||
* 对应链接格式:weixin://wxpay/bizpayurl?sr=XXXXX。请商户调用第三方库将code_url生成二维码图片。
|
||||
* 该模式链接较短,生成的二维码打印到结账小票上的识别率较高。
|
||||
* 文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5
|
||||
* </pre>
|
||||
* @param codeUrl 微信返回的交易会话的二维码链接
|
||||
* @param logoFile 商户logo图片的文件对象,可以为空
|
||||
* @param sideLength 要生成的二维码的边长,如果为空,则取默认值400
|
||||
* @return 生成的二维码的字节数组
|
||||
*/
|
||||
byte[] createScanPayQrcodeMode2(String codeUrl, File logoFile, Integer sideLength);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.github.binarywang.utils.qrcode.QrcodeUtils;
|
||||
import com.google.common.collect.Maps;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.BeanUtils;
|
||||
@@ -25,6 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
@@ -316,6 +319,41 @@ public class WxMpPayServiceImpl implements WxMpPayService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] createScanPayQrcodeMode1(String productId, File logoFile, Integer sideLength) {
|
||||
//weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX
|
||||
StringBuilder codeUrl = new StringBuilder("weixin://wxpay/bizpayurl?");
|
||||
Map<String, String> params = Maps.newHashMap();
|
||||
params.put("appid", this.getConfig().getAppId());
|
||||
params.put("mch_id", this.getConfig().getPartnerId());
|
||||
params.put("product_id", productId);
|
||||
params.put("time_stamp", String.valueOf(System.currentTimeMillis()));
|
||||
params.put("nonce_str", String.valueOf(System.currentTimeMillis()));
|
||||
|
||||
String sign = this.createSign(params);
|
||||
params.put("sign", sign);
|
||||
|
||||
for (String key : params.keySet()) {
|
||||
codeUrl.append(key + "=" + params.get(key) + "&");
|
||||
}
|
||||
|
||||
String content = codeUrl.toString().substring(0, codeUrl.length() - 1);
|
||||
if (sideLength == null || sideLength < 1) {
|
||||
return QrcodeUtils.createQrcode(content, logoFile);
|
||||
}
|
||||
|
||||
return QrcodeUtils.createQrcode(content, sideLength, logoFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] createScanPayQrcodeMode2(String codeUrl, File logoFile, Integer sideLength) {
|
||||
if (sideLength == null || sideLength < 1) {
|
||||
return QrcodeUtils.createQrcode(codeUrl, logoFile);
|
||||
}
|
||||
|
||||
return QrcodeUtils.createQrcode(codeUrl, sideLength, logoFile);
|
||||
}
|
||||
|
||||
private String executeRequest(String url, String requestStr) throws WxErrorException {
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
if (this.wxMpService.getHttpProxy() != null) {
|
||||
|
||||
Reference in New Issue
Block a user