Java通过JsApi方式实现微信支付
发布时间:2020-05-25 05:56:31 所属栏目:Java 来源:互联网
导读:要使用JsApi进行微信支付,首先要从微信获得一个prepay_id,然后通过调用微信的jsapi完成支付,JSAPI的返回结果get_brand_wcpay_request:ok仅在用户成功完成支付时返回。由于前端交互复杂,get_brand_wcpay_request:
|
要使用JsApi进行微信支付,首先要从微信获得一个prepay_id,然后通过调用微信的jsapi完成支付,JS API的返回结果get_brand_wcpay_request:ok仅在用户成功完成支付时返回。由于前端交互复杂,get_brand_wcpay_request:cancel或者get_brand_wcpay_request:fail可以统一处理为用户遇到错误或者主动放弃,不必细化区分。
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest',{
"appId" : "wx2421b1c4370ec43b",//公众号名称,由商户传入
"timeStamp":" 1395712654",//时间戳,自1970年以来的秒数
"nonceStr" : "e61463f8efa94090b1f366cccfbbb444",//随机串
"package" : "u802345jgfjsdfgsdg888","signType" : "MD5",//微信签名方式:
"paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
},function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {} // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。
}
);
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady',onBridgeReady,false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady',onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady',onBridgeReady);
}
}else{
onBridgeReady();
}
以上传入的参数package,即为prepay_id 下面讲的是获得参数来调用jsapi
package com.unstoppedable.protocol;
import com.unstoppedable.common.Configure;
import com.unstoppedable.common.HttpService;
import com.unstoppedable.common.RandomStringGenerator;
import com.unstoppedable.common.Signature;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class UnifiedOrderReqData {
private String appid;
private String mch_id;
private String device_info;
private String nonce_str;
private String sign;
private String body;
private String detail;
private String attach;
private String out_trade_no;
private String fee_type;
private int total_fee;
private String spbill_create_ip;
private String time_start;
private String time_expire;
private String goods_tag;
private String notify_url;
private String trade_type;
private String product_id;
private String limit_pay;
private String openid;
private UnifiedOrderReqData(UnifiedOrderReqDataBuilder builder) {
this.appid = builder.appid;
this.mch_id = builder.mch_id;
this.device_info = builder.device_info;
this.nonce_str = RandomStringGenerator.getRandomStringByLength(32);
this.body = builder.body;
this.detail = builder.detail;
this.attach = builder.attach;
this.out_trade_no = builder.out_trade_no;
this.fee_type = builder.fee_type;
this.total_fee = builder.total_fee;
this.spbill_create_ip = builder.spbill_create_ip;
this.time_start = builder.time_start;
this.time_expire = builder.time_expire;
this.goods_tag = builder.goods_tag;
this.notify_url = builder.notify_url;
this.trade_type = builder.trade_type;
this.product_id = builder.product_id;
this.limit_pay = builder.limit_pay;
this.openid = builder.openid;
this.sign = Signature.getSign(toMap());
}
public void setAppid(String appid) {
this.appid = appid;
}
public void setMch_id(String mch_id) {
this.mch_id = mch_id;
}
public void setDevice_info(String device_info) {
this.device_info = device_info;
}
public void setNonce_str(String nonce_str) {
this.nonce_str = nonce_str;
}
public void setSign(String sign) {
this.sign = sign;
}
public void setBody(String body) {
this.body = body;
}
public void setDetail(String detail) {
this.detail = detail;
}
public void setAttach(String attach) {
this.attach = attach;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public void setFee_type(String fee_type) {
this.fee_type = fee_type;
}
public void setTotal_fee(int total_fee) {
this.total_fee = total_fee;
}
public void setSpbill_create_ip(String spbill_create_ip) {
this.spbill_create_ip = spbill_create_ip;
}
public void setTime_start(String time_start) {
this.time_start = time_start;
}
public void setTime_expire(String time_expire) {
this.time_expire = time_expire;
}
public void setGoods_tag(String goods_tag) {
this.goods_tag = goods_tag;
}
public void setNotify_url(String notify_url) {
this.notify_url = notify_url;
}
public void setTrade_type(String trade_type) {
this.trade_type = trade_type;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public void setLimit_pay(String limit_pay) {
this.limit_pay = limit_pay;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public Map<String,Object> toMap() {
Map<String,Object> map = new HashMap<String,Object>();
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
Object obj;
try {
obj = field.get(this);
if (obj != null) {
map.put(field.getName(),obj);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return map;
}
public static class UnifiedOrderReqDataBuilder {
private String appid;
private String mch_id;
private String device_info;
private String body;
private String detail;
private String attach;
private String out_trade_no;
private String fee_type;
private int total_fee;
private String spbill_create_ip;
private String time_start;
private String time_expire;
private String goods_tag;
private String notify_url;
private String trade_type;
private String product_id;
private String limit_pay;
private String openid;
public UnifiedOrderReqDataBuilder(String appid,String mch_id,String body,String out_trade_no,Integer total_fee,String spbill_create_ip,String notify_url,String trade_type) {
if (appid == null) {
throw new IllegalArgumentException("传入参数appid不能为null");
}
if (mch_id == null) {
throw new IllegalArgumentException("传入参数mch_id不能为null");
}
if (body == null) {
throw new IllegalArgumentException("传入参数body不能为null");
}
if (out_trade_no == null) {
throw new IllegalArgumentException("传入参数out_trade_no不能为null");
}
if (total_fee == null) {
throw new IllegalArgumentException("传入参数total_fee不能为null");
}
if (spbill_create_ip == null) {
throw new IllegalArgumentException("传入参数spbill_create_ip不能为null");
}
if (notify_url == null) {
throw new IllegalArgumentException("传入参数notify_url不能为null");
}
if (trade_type == null) {
throw new IllegalArgumentException("传入参数trade_type不能为null");
}
this.appid = appid;
this.mch_id = mch_id;
this.body = body;
this.out_trade_no = out_trade_no;
this.total_fee = total_fee;
this.spbill_create_ip = spbill_create_ip;
this.notify_url = notify_url;
this.trade_type = trade_type;
}
public UnifiedOrderReqDataBuilder setDevice_info(String device_info) {
this.device_info = device_info;
return this;
}
public UnifiedOrderReqDataBuilder setDetail(String detail) {
this.detail = detail;
return this;
}
public UnifiedOrderReqDataBuilder setAttach(String attach) {
this.attach = attach;
return this;
}
public UnifiedOrderReqDataBuilder setFee_type(String fee_type) {
this.fee_type = fee_type;
return this;
}
public UnifiedOrderReqDataBuilder setTime_start(String time_start) {
this.time_start = time_start;
return this;
}
public UnifiedOrderReqDataBuilder setTime_expire(String time_expire) {
this.time_expire = time_expire;
return this;
}
public UnifiedOrderReqDataBuilder setGoods_tag(String goods_tag) {
this.goods_tag = goods_tag;
return this;
}
public UnifiedOrderReqDataBuilder setProduct_id(String product_id) {
this.product_id = product_id;
return this;
}
public UnifiedOrderReqDataBuilder setLimit_pay(String limit_pay) {
this.limit_pay = limit_pay;
return this;
}
public UnifiedOrderReqDataBuilder setOpenid(String openid) {
this.openid = openid;
return this;
}
public UnifiedOrderReqData build() {
if("JSAPI".equals(this.trade_type) && this.openid == null) {
throw new IllegalArgumentException("当传入trade_type为JSAPI时,openid为必填参数");
}
if("NATIVE".equals(this.trade_type) && this.product_id == null) {
throw new IllegalArgumentException("当传入trade_type为NATIVE时,product_id为必填参数");
}
return new UnifiedOrderReqData(this);
}
}
}
因为有些参数为必填,有些参数为选填。而且sign要等所有参数传入之后才能计算的出,所以这里用了builder模式。关于builder模式。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
