一. H5支付配置
1.在微信商戶平臺(tái)中進(jìn)行登錄并申請相關(guān)功能和配置
1.1微信商戶平臺(tái)https://pay.weixin.qq.com/index.php/core/home/loginreturn_url=%2F
登錄并配置,在商戶平臺(tái)上 - 產(chǎn)品中心 - 開通相關(guān)的產(chǎn)品,比如我這里使用的是 H5支付文章來源地址http://www.zghlxwxcb.cn/news/detail-531071.html
1.2 然后配置相關(guān)的參數(shù)
//APPID
//mchid(商戶號(hào))
//API key(V3的密鑰)
//privateKey(商戶私鑰)
//mchSerialNo (證書序列號(hào))
//以上參數(shù)的配置說明可以參考微信文檔 https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_6_1.shtml
二.開發(fā)
2.1導(dǎo)入相關(guān)依賴
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.2</version>
</dependency>
2.2 代碼編寫
//此方法為微信的H5支付
//cashNum 微信支付的金額單位是 分,如果使用的元,在這里需要轉(zhuǎn)換。
//orderNo 商戶的訂單號(hào),在同一個(gè)商戶號(hào)下必須是唯一的。
//redirect_url 支付成功后的跳轉(zhuǎn)地址。
public ReturnJson weChatPay_H5(int cashNum, String orderNo,String redirect_url) throws Exception {
//1.私鑰為String字符串的方式來加載
//PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(WX_KEY.getBytes("utf-8")));
//2.通過加載文件的方式來讀取私鑰
String path = this.getClass().getClassLoader().getResources("./apiclient_key.pem").nextElement().getPath();
//加載商戶私鑰(私鑰存儲(chǔ)在文件)
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(path));
//加載平臺(tái)證書(mchId:商戶號(hào),mchSerialNo:商戶證書序列號(hào),apiV3Key:V3密鑰)
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(new WechatPay2Credentials(WX_MCH_ID, new PrivateKeySigner(WX_Serial_Number, merchantPrivateKey)),V3_key.getBytes("utf-8"));
//初始化httpClient
CloseableHttpClient builder = WechatPayHttpClientBuilder.create().withMerchant(WX_MCH_ID, WX_Serial_Number, merchantPrivateKey).withValidator(new WechatPay2Validator(verifier)).build();
//請求的微信支付地址
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/h5");
//發(fā)送給微信的部分json參數(shù)
//amount 為訂單的金額信息
//total 為訂單總金額(單位:分)
//currency 貨幣類型
//scene_info 支付的場景相關(guān)信息
//payer_client_ip 用戶的終端ip,支持IPV4和IPV6
//h5_info h5支付的場景信息
//type 支付場景類型
//mch_id 商戶號(hào)
//description 商品描述
//notify_url 回調(diào)通知地址(此地址是用戶支付成功后,微信方要發(fā)送支付成功的通知)
//out_trade_no 商戶訂單號(hào)
//goods_tag 商品標(biāo)簽
//appid appid
String reqData = "{"
+ "\"amount\": {"
+ "\"total\": "+cashNum+","
+ "\"currency\": \"CNY\""
+ "},"
+ "\"scene_info\": {"
+ "\"payer_client_ip\":\"14.23.150.200\","
+ "\"h5_info\": {"
+ "\"type\": \"Wap\"" + "}},"
+ "\"mchid\": \""+WX_MCH_ID+"\","
+ "\"description\": \"優(yōu)選商城\","
+ "\"notify_url\": \""+AliPay_H5_NotifyUrl+"\","
+ "\"out_trade_no\": \""+orderNo+"\","
+ "\"goods_tag\": \"WXG\","
+ "\"appid\": \""+WX_APP_ID+"\"" + "}";
StringEntity entity = new StringEntity(reqData,"utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept","application/json");
CloseableHttpResponse response = builder.execute(httpPost);
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
String url = URLEncoder.encode(redirect_url, "GBK");
JSONObject js = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
String aa = js.get("h5_url").toString();
js.put("h5_url",aa+"&redirect_url="+url);
String payUrl = js.toJSONString();
return ReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(),payUrl);
} else if (statusCode == 204) {
return ReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(), EntityUtils.toString(response.getEntity()));
} else {
return ReturnUtils.returnVal(CommonConstants.appCode.DATAERROR.get(), EntityUtils.toString(response.getEntity()));
}
} finally {
response.close();
}
}
2.3 編寫H5支付回調(diào)接口
//用戶支付成功后,通知的回調(diào)地址
//進(jìn)行兩步操作 1.接收微信發(fā)送的參數(shù)后,驗(yàn)簽并解析。
//2.根據(jù)微信給到的參數(shù),出發(fā)后續(xù)的操作,比如修改訂單狀態(tài)、發(fā)送站內(nèi)信等等
//此接口微信可能會(huì)出現(xiàn)多次調(diào)用,請注意處理
@ResponseBody
@RequestMapping(value = "/wechatRefundNotify",produces = {"application/json;charset=utf-8"})
public ResultEntity wechatRefundNotify(@RequestBody WeChatPayEntity weChatPayEntity) {
AesUtil aesUtils = new AesUtil(this.V3_key.getBytes());
try {
String string = aesUtils.decryptToString(weChatPayEntity.getResource().getAssociated_data().getBytes(), weChatPayEntity.getResource().getNonce().getBytes(), weChatPayEntity.getResource().getCiphertext());
JSONObject jsonObject = JSONObject.parseObject(string);
//獲取訂單號(hào)
String out_trade_no = jsonObject.getString("out_trade_no");
//獲取支付的訂單狀態(tài), SUCCESS(支付成功)
String trade_state = jsonObject.getString("trade_state");
//獲取支付狀態(tài) SUCCESS
if(refund_status.equals("SUCCESS")){
//進(jìn)行支付成功后的相關(guān)操作.....
//然后返回給微信指定的參數(shù),否則微信可能會(huì)再次通知
return new ResultEntity("SUCCESS", "成功");
}else {
//這里是支付狀態(tài)錯(cuò)誤.....
//進(jìn)行相關(guān)的操作
return new ResultEntity("FAIL", "失敗");
}
} catch (Exception e) {
//錯(cuò)誤處理
}
return new ResultEntity("FAIL", "失敗");
}
2.4 微信回調(diào)接收的相關(guān)參數(shù)實(shí)體類
//WeChatPayEntity 類
public class WeChatPayEntity {
//通知?jiǎng)?chuàng)建時(shí)間
private String create_time;
//回調(diào)摘要
private String summary;
//通知數(shù)據(jù)類型
private String resiyrce_type;
//通知的資源數(shù)據(jù)(支付訂單的相關(guān)信息)
private WeChatRequestEntities resource = new WeChatRequestEntities();
}
//WeChatRequestEntities 類
public class WeChatRequestEntities {
//微信返回交易狀態(tài)
//SUCCESS:支付成功
//REFUND:轉(zhuǎn)入退款
//NOTPAY:未支付
//CLOSED:已關(guān)閉
//REVOKED:已撤銷(付款碼支付)
//USERPAYING:用戶支付中(付款碼支付)
//PAYERROR:支付失敗(其他原因,如銀行返回失敗)
private String trade_state;
//商戶訂單號(hào)(我們對接H5支付接口時(shí)候傳入的 訂單號(hào))
private String out_trade_no;
//附加數(shù)據(jù)
private String associated_data;
//隨機(jī)串
private String nonce;
//數(shù)據(jù)密文
private String ciphertext;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-531071.html
到了這里,關(guān)于微信H5支付及通知回調(diào)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!