Created
August 27, 2015 06:13
-
-
Save Logan676/cd5b20477b27dbb0e5db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** JSAPI支付 */ | |
public JSONObject jsPay() { | |
JSONObject obj = new JSONObject(); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
// V3 支付 | |
PayPackage payPackage = new PayPackageV3(weixinAccount, "用户openid", | |
"商品描述", "系统内部订单号", 1d, "IP地址", TradeType.JSAPI); | |
// V2 支付 | |
payPackage = new PayPackageV2("商品描述", weixinAccount.getPartnerId(), | |
"系统内部订单号", 1d, "回调地址", "IP地址"); | |
payPackage.setAttach("ID"); | |
String jspay = null; | |
try { | |
jspay = PayUtil.createPayJsRequestJson(payPackage, weixinAccount); | |
} catch (PayException e) { | |
log.error("create jspay error,{}", weixinAccount, e); | |
} | |
if (StringUtil.isBlank(jspay)) { | |
obj.put("code", "-2"); | |
obj.put("msg", "创建支付链接失败!"); | |
return obj; | |
} | |
obj.put("code", "0"); | |
obj.put("jspay", jspay); | |
/* | |
* 编辑收货地址 SnsToken token = (SnsToken) getSession("AccessToken"); | |
* obj.put("editaddress", PayUtil.createAddressRequestJson( | |
* wx.getAppId(), getFullLoction(), token.getAccess_token())); | |
*/ | |
log.info("js pay....{}", obj); | |
return obj; | |
} | |
/** JSAPI(V2)支付成功(前端)时的回调通知 */ | |
/** | |
* JSAPI(V2)支付成功(前端)时的回调通知<br> | |
* <xml></br> <OpenId><![CDATA[111222]]></OpenId></br> | |
* <AppId><![CDATA[wwwwb4f85f3a797777]]></AppId></br> | |
* <IsSubscribe>1</IsSubscribe></br> | |
* <TimeStamp>1369743511</TimeStamp></br> | |
* <NonceStr><![CDATA[jALldRTHAFd5Tgs5]]></NonceStr></br> | |
* <AppSignature><![CDATA[bafe07f060f22dcda0bfdb4b5ff756f973aecffa]]> | |
* </AppSignature></br> | |
* <SignMethod><![CDATA[sha1]]></SignMethod></br> </xml></br> | |
* 参与签名的字段为: appid、appkey、timestamp、noncestr、openid、issubscribe | |
* | |
* @param 订单信息 | |
* @param inputStream | |
* 用户信息 | |
* | |
* @see com.foxinmy.weixin4j.mp.payment.JsPayNotify | |
* @return success或其他 | |
*/ | |
public String jsNotifyV2(InputStream inputStream) { | |
Map<String, String> objMap = new HashMap<String, String>(); | |
/* | |
* 收集url中携带的参数 /pay/notify/back?attach=8&bank_billno=201410293351060& | |
* bank_type=2032&discount=0&fee_type=1&input_charset=UTF-8& | |
* notify_id=9fKbVf_qg6y- | |
* wSjtSMV0PLXeEn2oGfTM1s9dWSvR2B9U6iFQRTzmjrMWKUxvh9mpBLvnh8aqFbC_OFk1pTvFnFUO00Lln4fh | |
* & out_trade_no=D14102900031&partner=1221928801&product_fee=1& | |
* sign=B9D6E772C271C9B86B8436FC9F5DFC1A& | |
* sign_type=MD5&time_end=20141029183707& | |
* total_fee=1&trade_mode=1&trade_state=0& | |
* transaction_id=1221928801201410296039230054&transport_fee=0 | |
*/ | |
log.info("jspay_notify_orderinfo,{}", objMap); | |
JsPayNotify payNotify = XmlStream.get(inputStream, JsPayNotify.class); | |
log.info("jspay_notify_userinfo,{}", payNotify); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
// 验证财付通签名 | |
String sign = objMap.get("sign"); | |
objMap.remove("sign"); | |
String _sign = PayUtil | |
.paysignMd5(objMap, weixinAccount.getPartnerKey()); | |
log.info("财付通签名----->sign={},vaild_sign={}", sign, _sign); | |
if (!sign.equals(_sign)) { | |
return "fail"; | |
} | |
objMap.clear(); | |
// 验证微信签名 | |
sign = payNotify.getPaySign(); | |
payNotify.setPaySign(null); | |
payNotify.setSignType(null); | |
String vaild_sign = PayUtil.paysignSha(payNotify, | |
weixinAccount.getPaySignKey()); | |
log.info("微信签名----->sign={},vaild_sign={}", sign, vaild_sign); | |
if (!sign.equals(vaild_sign)) { | |
return "fail"; | |
} | |
// 处理业务逻辑 | |
return "success"; | |
} | |
JSAPI(V3)支付成功(前端)时的回调通知 | |
/** | |
* JSAPI(V3)支付成功(前端)时的回调通知 | |
* | |
* | |
* @param inputStream | |
* 订单回调 | |
* @return <xml><br> | |
* <return_code>SUCCESS/FAIL</return_code><br> | |
* <return_msg>如非空,为错误 原因签名失败参数格式校验错误</return_msg><br> | |
* </xml> | |
* @throws DocumentException | |
* @see <a | |
* href="http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7">支付结果通知</a> | |
*/ | |
public String jsNotifyV3(InputStream inputStream) { | |
String orderXml = "从inputStream中读取订单信息"; | |
com.foxinmy.weixin4j.mp.payment.v3.Order order = CouponConverter | |
.fromXML(orderXml, | |
com.foxinmy.weixin4j.mp.payment.v3.Order.class); | |
log.info("jsapi_notify_order_info:", order); | |
String sign = order.getSign(); | |
order.setSign(null); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
String valid_sign = PayUtil.paysignMd5(order, | |
weixinAccount.getPaySignKey()); | |
// 如果订单中存在代金券的情况并不适用 | |
log.info("微信签名----->sign={},vaild_sign={}", sign, valid_sign); | |
if (!sign.equals(valid_sign)) { | |
return XmlStream.to(new XmlResult(Consts.FAIL, "签名错误")); | |
} | |
// 处理业务逻辑 | |
return XmlStream.to(new XmlResult(Consts.SUCCESS, "")); | |
} | |
V2.x版本Native支付时POST数据 | |
/** | |
* V2.x版本Native支付时POST数据<br> | |
* <xml></br> <OpenId><![CDATA[111222]]></OpenId></br> | |
* <AppId><![CDATA[wwwwb4f85f3a797777]]></AppId></br> | |
* <IsSubscribe>1</IsSubscribe></br> | |
* <ProductId>[CDATA[000000]]</ProductId></br> | |
* <TimeStamp>1369743511</TimeStamp></br> | |
* <NonceStr><![CDATA[jALldRTHAFd5Tgs5]]></NonceStr></br> | |
* <AppSignature><![CDATA[bafe07f060f22dcda0bfdb4b5ff756f973aecffa]]> | |
* </AppSignature></br> | |
* <SignMethod><![CDATA[sha1]]></SignMethod></br> </xml></br> | |
* 参与签名的字段为: appid、appkey、timestamp、noncestr、openid、issubscribe、productId | |
* | |
* @param inputStream | |
* | |
* @return 必须返回一个带有Package信息的xml字符串 | |
*/ | |
public String nativeNotifyV2(InputStream inputStream) { | |
// V2.x版本 | |
NativePayNotifyV2 payNotify = XmlStream.get(inputStream, | |
NativePayNotifyV2.class); | |
log.info("native_pay_notify,{}", payNotify); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
String sign = payNotify.getPaySign(); | |
payNotify.setPaySign(null); | |
payNotify.setSignType(null); | |
// 验证微信签名 | |
String vaild_sign = PayUtil.paysignSha(payNotify, | |
weixinAccount.getPaySignKey()); | |
log.info("微信签名----->sign={},vaild_sign={}", sign, vaild_sign); | |
if (!sign.equals(vaild_sign)) { | |
return "fail"; | |
} | |
// 构造订单信息 | |
PayPackageV2 payPackage = new PayPackageV2("商品描述", | |
weixinAccount.getPartnerId(), "系统内部订单号", 1d, "回调地址", "IP地址"); | |
NativePayResponseV2 payResponse = new NativePayResponseV2( | |
weixinAccount, payPackage); | |
return XmlStream.to(payResponse); | |
} | |
V3.x版本native回调 | |
/** | |
* V3.x版本native回调<br> | |
* <xml></br> <openid><![CDATA[111222]]></openid></br> | |
* <appid><![CDATA[wwwwb4f85f3a797777]]></appid></br> | |
* <mch_id><![CDATA[1100022]]></mch_id></br> | |
* <is_subscribe>1</is_subscribe></br> | |
* <product_id>[CDATA[000000]]</product_id></br> | |
* <nonce_str><![CDATA[jALldRTHAFd5Tgs5]]></nonce_str></br> | |
* <sign><![CDATA[bafe07f060f22dcda0bfdb4b5ff756f973aecffa]]> | |
* </sign></br> </xml></br> | |
* | |
* @return | |
* @throws PayException | |
*/ | |
public String nativeNotifyV3(InputStream inputStream) throws PayException { | |
NativePayNotifyV3 payNotify = XmlStream.get(inputStream, | |
NativePayNotifyV3.class); | |
String sign = payNotify.getSign(); | |
payNotify.setSign(null); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
String valid_sign = PayUtil.paysignMd5(payNotify, | |
weixinAccount.getPaySignKey()); | |
log.info("微信签名----->sign={},vaild_sign={}", sign, valid_sign); | |
// 生成Package | |
PayPackageV3 payPackage = new PayPackageV3(weixinAccount, "用户openid", | |
"商品描述", "系统内部订单号", 1d, "IP地址", TradeType.NATIVE); | |
payPackage.setProductId(payNotify.getProductId()); | |
if (!sign.equals(valid_sign)) { | |
// 校验失败 | |
NativePayResponseV3 payReponse = new NativePayResponseV3("签名失败", | |
null); | |
payReponse.setSign(PayUtil.paysignMd5(payReponse, | |
weixinAccount.getPaySignKey())); | |
return XmlStream.to(payReponse); | |
} | |
// 成功返回 | |
NativePayResponseV3 payReponse = new NativePayResponseV3(payPackage, | |
weixinAccount.getPaySignKey()); | |
payReponse.setSign(PayUtil.paysignMd5(payReponse, | |
weixinAccount.getPaySignKey())); | |
return XmlStream.to(payReponse); | |
} | |
告警通知 | |
/** | |
* 告警通知 需要成功返回 success </br> <xml></br> | |
* <AppId><![CDATA[wxf8b4f85f3a794e77]]></AppId></br> | |
* <ErrorType>1001</ErrorType></br> | |
* <Description><![CDATA[错误描述]]></Description></br> | |
* <AlarmContent><![CDATA[错误详情]]></AlarmContent></br> | |
* <TimeStamp>1393860740</TimeStamp></br> | |
* <AppSignature><![CDATA[签名方式跟JsPayRequest中的paySign一样]]></ | |
* AppSignature></br> | |
* <SignMethod><![CDATA[sha1]]></SignMethod></br> | |
* </xml></br> | |
* 参与签名字段:alarmcontent、appid、appkey、description、errortype、timestamp | |
* | |
* @param inputStream | |
* xml数据 | |
* @see com.foxinmy.weixin4j.mp.payment.v2.PayWarn | |
* @return | |
*/ | |
public String warning(InputStream inputStream) { | |
PayWarn payWarn = XmlStream.get(inputStream, PayWarn.class); | |
log.info("pay_warning,{}", payWarn); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
String sign = payWarn.getPaySign(); | |
payWarn.setPaySign(null); | |
payWarn.setSignType(null); | |
// 验证微信签名 | |
String vaild_sign = PayUtil.paysignSha(payWarn, | |
weixinAccount.getPaySignKey()); | |
log.info("微信签名----->sign={},vaild_sign={}", sign, vaild_sign); | |
return "success"; | |
} | |
用户维权 | |
/** | |
* 用户维权 | |
* | |
* @param inputStream | |
* @see com.foxinmy.weixin4j.mp.payment.v2.PayFeedback | |
* @return | |
*/ | |
public String feedback(InputStream inputStream) { | |
PayFeedback feedback = XmlStream.get(inputStream, PayFeedback.class); | |
log.info("pay_feedback_info:{}", feedback); | |
WeixinMpAccount weixinAccount = JSON.parseObject( | |
ConfigUtil.getValue("account"), WeixinMpAccount.class); | |
// 验证微信签名 | |
Map<String, String> obj = new HashMap<String, String>(); | |
obj.put("openid", feedback.getOpenId()); | |
obj.put("appid", feedback.getAppId()); | |
obj.put("timestamp", feedback.getTimeStamp()); | |
obj.put("appkey", weixinAccount.getPaySignKey()); | |
String sign = PayUtil.paysignSha(obj); | |
log.info("微信签名----->sign={},vaild_sign={}", sign, feedback.getPaySign()); | |
return "success"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment