个人小程序如何申请微信支付功能? 给你们看一下效果
一、准备材料
① 个体户营业执照
1??可以去当地 工商局办理,免费(一般提供一个地址,提供3张身份证复印件) 2??可以去淘宝叫人代理办理,收费(60-200元)
②新的QQ邮箱
1??一个手机号目前能注册5个,民生手机号不能注册QQ
③一个服务器,一个域名
材料准备号。 现在开始认证小程序
二、认证小程序
①自行认证(300元)
②三方认证(淘宝19.9元)
需要材料是 认证小程序需要提供: 1、法人的姓名 2、法人微信号 3、法人手机号 4、营业执照全称 5、统一信用代码号
三、支付商户号申请
三方申请(淘宝50元)
申请支付需要提供资料: 1、营业执照照片 2、法人身份证正反面 3、门头照片 店内照片各一张 (如果已经有小程序不需要提供) 4、公司需要提供对公户(个体需要提供法人名下银行卡银行卡办理的地区是那个) 5、手机号、邮箱
至此所有准备工作都好了,可以写代码了
四、写代码
分析一下思路
1、 JSAPI下单(生成一个预支付id)
请求参数 官方文档:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
{
"mchid": "1900006XXX",
"out_trade_no": "1217752501201407033233368318",
"appid": "wxdace645e0bc2cXXX",
"description": "Image形象店-深圳腾大-QQ公仔",
"notify_url": "https://weixin.qq.com/",
"amount": {
"total": 1,
"currency": "CNY"
},
"payer": {
"openid": "o4GgauInH_RCEdvrrNGrntXDuXXX"
}
}
返回:
{
"prepay_id": "wx26112221580621e9b071c00d9e093b0000"
}
2、返回 wx.requestPayment 需要参数
官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/payment/wx.requestPayment.html
wx.requestPayment({
timeStamp: '',
nonceStr: '',
package: '',
signType: 'MD5',
paySign: '',
success (res) { },
fail (res) { }
})
3、引入的依赖
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.3.0</version>
</dependency>
4、Service 代码
public ResultInfo pay(String code, String des, double total,String appId,String secret ) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
String openId = weChatService.analysisOpenId(code, appId,secret);
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("mchid", WxConfig.mchId)
.put("appid", appId)
.put("description", des)
.put("notify_url", WxConfig.notify_url)
.put("out_trade_no", System.currentTimeMillis() + "");
rootNode.putObject("amount")
.put("total", (long) (total * 100));
rootNode.putObject("payer")
.put("openid", openId);
objectMapper.writeValue(bos, rootNode);
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(WxConfig.privateKey);
ScheduledUpdateCertificatesVerifier verifier = new ScheduledUpdateCertificatesVerifier(
new WechatPay2Credentials(WxConfig.mchId, new PrivateKeySigner(WxConfig.mchSerialNumber, merchantPrivateKey)),
WxConfig.apiV3Key.getBytes(StandardCharsets.UTF_8));
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(WxConfig.mchId, WxConfig.mchSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
HttpClient httpClient = builder.build();
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
String bodyAsString = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
if (jsonObject.containsKey("code")) {
ThrowException.illegal(true, jsonObject.getString("message"));
}
String prepay_id = jsonObject.getString("prepay_id");
String timeStamp = String.valueOf(System.currentTimeMillis());
String nonceStr = createRandomStringByLength(32);
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(appId + "\n");
stringBuffer.append(timeStamp + "\n");
stringBuffer.append(nonceStr + "\n");
stringBuffer.append("prepay_id=" + prepay_id + "\n");
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(merchantPrivateKey);
signature.update(stringBuffer.toString().getBytes("UTF-8"));
byte[] signBytes = signature.sign();
String paySign = Base64.encodeBytes(signBytes);
JSONObject params = new JSONObject();
params.put("appId", appId);
params.put("timeStamp", timeStamp);
params.put("nonceStr", nonceStr);
params.put("prepay_id", prepay_id);
params.put("signType", "RSA");
params.put("paySign", paySign);
ResultInfo resultInfo = new ResultInfo();
resultInfo.setResult(params);
return resultInfo;
}
private String createRandomStringByLength(int length) {
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(WxConfig.base.length());
sb.append(WxConfig.base.charAt(number));
}
return sb.toString();
}
// code解析出openId
public String analysisOpenId(String code,String appid,String secret){
String url = "https://api.weixin.qq.com/sns/jscode2session" +
"?appid=" + appid +
"&secret=" + secret +
"&js_code=" + code +
"&grant_type=authorization_code";
RestTemplate restTemplate = new RestTemplate();
String str = restTemplate.getForObject(url, String.class);
WeChatCode weChatCode=JSON.parseObject(str, WeChatCode.class);
if (weChatCode==null){
ThrowException.illegal(true,"code获取openId失败");
return null;
}else{
return weChatCode.getOpenid();
}
}
5、小程序 封装的支付
const pay = function (des, total) {
return new Promise((resolve, reject) => {
wx.showLoading({
title: '加载中...',
})
wx.login({
success(res) {
wx.request({
url: 'https:
data: {
code: res.code,
des: des,
total: total,
},
header: {
"content-type": "application/x-www-form-urlencoded"
},
method: 'GET',
success: res => {
if (res.data.code == 200) {
wx.hideLoading();
const params = res.data.result;
wx.requestPayment({
timeStamp: params.timeStamp + '',
nonceStr: params.nonceStr,
package: 'prepay_id=' + params.prepay_id,
signType: params.signType,
paySign: params.paySign,
success(res) {
resolve("success")
},
fail(res) {
reject("fail")
}
})
} else {
wx.showModal({
title: res.data.msg,
showCancel: false
})
reject("返回失败");
}
}
})
}
})
})
}
module.exports = pay;
|