fixed micropay

This commit is contained in:
jinyu 2016-10-21 22:00:39 +08:00
parent 1a4bf6faa9
commit ba3af7d154
3 changed files with 50 additions and 19 deletions

View File

@ -14,7 +14,6 @@ import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
@ -107,12 +106,16 @@ public class PayApi extends MchApi {
}
String tradeType = payPackage.getTradeType().toUpperCase();
if (TradeType.MICROPAY.name().equals(tradeType)) {
super.declareMerchant(payPackage);
JSONObject _payPackage = (JSONObject) JSON.toJSON(payPackage);
// 不需要设置TradeType参数
_payPackage.remove("trade_type");
payPackage.setSign(weixinSignature.sign(_payPackage));
String para = XmlStream.toXML(payPackage);
MchPayPackage _payPackage = new MchPayPackage(payPackage.getBody(),
payPackage.getDetail(), payPackage.getOutTradeNo(),
DateUtil.formatFee2Yuan(payPackage.getTotalFee()), null,
null, payPackage.getCreateIp(), null, null,
payPackage.getAuthCode(), null, payPackage.getAttach(),
null, null, payPackage.getGoodsTag(),
payPackage.getLimitPay(), payPackage.getSubAppId());
super.declareMerchant(_payPackage);
_payPackage.setSign(weixinSignature.sign(_payPackage));
String para = XmlStream.toXML(_payPackage);
WeixinResponse response = weixinExecutor.post(
getRequestUri("micropay_uri"), para);
MICROPayRequest microPayRequest = response

View File

@ -9,6 +9,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.payment.PayPackage;
import com.foxinmy.weixin4j.type.CurrencyType;
import com.foxinmy.weixin4j.type.TradeType;
/**
@ -32,6 +33,12 @@ public class MchPayPackage extends PayPackage {
@XmlElement(name = "trade_type")
@JSONField(name = "trade_type")
private String tradeType;
/**
* 符合ISO 4217标准的三位字母代码默认人民币CNY 非必须
*/
@XmlElement(name = "fee_type")
@JSONField(name = "fee_type")
private String feeType;
/**
* 用户在商户 appid 下的唯一 标识, trade_type JSAPI ,此参数必传
*/
@ -96,9 +103,9 @@ public class MchPayPackage extends PayPackage {
public MchPayPackage(String body, String outTradeNo, double totalFee,
String notifyUrl, String createIp, TradeType tradeType,
String openId, String authCode, String productId, String attach) {
this(body, null, outTradeNo, totalFee, notifyUrl, createIp, tradeType,
openId, authCode, productId, attach, null, null, null, null,
null);
this(body, null, outTradeNo, totalFee, CurrencyType.CNY, notifyUrl,
createIp, tradeType, openId, authCode, productId, attach, null,
null, null, null, null);
}
/**
@ -118,6 +125,8 @@ public class MchPayPackage extends PayPackage {
* 订单生成的机器IP <font color="red">必填项</font>
* @param tradeType
* 交易类型 <font color="red">必填项</font>
* @param feeType
* 货币类型 非必填项
* @param openId
* 用户ID <font color="red">tradeType=JSAPI时必填</font>
* @param authCode
@ -139,13 +148,15 @@ public class MchPayPackage extends PayPackage {
* openid和sub_openid可以选传其中之一如果选择传sub_openid ,则必须传sub_appid
*/
public MchPayPackage(String body, String detial, String outTradeNo,
double totalFee, String notifyUrl, String createIp,
TradeType tradeType, String openId, String authCode,
String productId, String attach, Date timeStart, Date timeExpire,
String goodsTag, String limitPay, String subOpenId) {
double totalFee, CurrencyType feeType, String notifyUrl,
String createIp, TradeType tradeType, String openId,
String authCode, String productId, String attach, Date timeStart,
Date timeExpire, String goodsTag, String limitPay, String subOpenId) {
super(body, detial, outTradeNo, totalFee, notifyUrl, createIp, attach,
timeStart, timeExpire, goodsTag);
this.tradeType = tradeType.name();
this.tradeType = tradeType != null ? tradeType.name() : null;
this.feeType = feeType == null ? CurrencyType.CNY.name() : feeType
.name();
this.openId = openId;
this.authCode = authCode;
this.productId = productId;
@ -157,6 +168,10 @@ public class MchPayPackage extends PayPackage {
return tradeType;
}
public String getFeeType() {
return feeType;
}
public String getOpenId() {
return openId;
}
@ -187,9 +202,9 @@ public class MchPayPackage extends PayPackage {
@Override
public String toString() {
return "MchPayPackage [tradeType=" + tradeType + ", openId=" + openId
+ ", productId=" + productId + ", authCode=" + authCode
+ ", limitPay=" + limitPay + ", subOpenId=" + subOpenId + ", "
+ super.toString() + "]";
return "MchPayPackage [tradeType=" + tradeType + ",feeType=" + feeType
+ ", openId=" + openId + ", productId=" + productId
+ ", authCode=" + authCode + ", limitPay=" + limitPay
+ ", subOpenId=" + subOpenId + ", " + super.toString() + "]";
}
}

View File

@ -119,4 +119,17 @@ public final class DateUtil {
return _fee.multiply(new BigDecimal("100"))
.setScale(0, BigDecimal.ROUND_HALF_EVEN).intValue();
}
/**
* 单位为分的金额格式化为元
*
* @param fee
* 金额 单位为分
* @return 四舍六入五成双的金额
*/
public static double formatFee2Yuan(int fee) {
BigDecimal _fee = new BigDecimal(Integer.toString(fee));
return _fee.divide(new BigDecimal("100"))
.setScale(0, BigDecimal.ROUND_HALF_EVEN).doubleValue();
}
}