From 867010e1afdd294d4bb139a24b4091e47ce72d09 Mon Sep 17 00:00:00 2001 From: maxiucheng Date: Tue, 5 Sep 2017 14:37:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=94=AF=E4=BB=98=E5=AE=9D?= =?UTF-8?q?=E5=BD=93=E9=9D=A2=E4=BB=98=E4=B9=8B=E6=89=AB=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xxpay/common/constant/PayConstant.java | 1 + .../mgr/controller/PayChannelController.java | 3 +- .../resources/templates/pay_channel/edit.ftl | 1 + .../PayChannel4AlipayController.java | 68 +++++++++++++++++++ .../web/controller/PayOrderController.java | 2 + .../web/service/PayOrderServiceClient.java | 14 ++++ 6 files changed, 88 insertions(+), 1 deletion(-) diff --git a/xxpay-common/src/main/java/org/xxpay/common/constant/PayConstant.java b/xxpay-common/src/main/java/org/xxpay/common/constant/PayConstant.java index be51503..d9187b5 100644 --- a/xxpay-common/src/main/java/org/xxpay/common/constant/PayConstant.java +++ b/xxpay-common/src/main/java/org/xxpay/common/constant/PayConstant.java @@ -21,6 +21,7 @@ public class PayConstant { public final static String PAY_CHANNEL_ALIPAY_MOBILE = "ALIPAY_MOBILE"; // 支付宝移动支付 public final static String PAY_CHANNEL_ALIPAY_PC = "ALIPAY_PC"; // 支付宝PC支付 public final static String PAY_CHANNEL_ALIPAY_WAP = "ALIPAY_WAP"; // 支付宝WAP支付 + public final static String PAY_CHANNEL_ALIPAY_QR = "ALIPAY_QR"; // 支付宝当面付之扫码支付 public final static String TRANS_CHANNEL_WX_APP = "TRANS_WX_APP"; // 微信APP转账 public final static String TRANS_CHANNEL_WX_JSAPI = "TRANS_WX_JSAPI"; // 微信公众号转账 diff --git a/xxpay-mgr/src/main/java/org/xxpay/mgr/controller/PayChannelController.java b/xxpay-mgr/src/main/java/org/xxpay/mgr/controller/PayChannelController.java index a11256a..8cbc8ff 100644 --- a/xxpay-mgr/src/main/java/org/xxpay/mgr/controller/PayChannelController.java +++ b/xxpay-mgr/src/main/java/org/xxpay/mgr/controller/PayChannelController.java @@ -75,7 +75,8 @@ public class PayChannelController { // 对于配置支付宝参数时,前端将+号转为空格bug处理 if(PayConstant.PAY_CHANNEL_ALIPAY_MOBILE.equals(channelId) || PayConstant.PAY_CHANNEL_ALIPAY_PC.equals(channelId) || - PayConstant.PAY_CHANNEL_ALIPAY_WAP.equals(channelId)) { + PayConstant.PAY_CHANNEL_ALIPAY_WAP.equals(channelId) || + PayConstant.PAY_CHANNEL_ALIPAY_QR.equals(channelId)) { JSONObject paramObj = null; try{ paramObj = JSON.parseObject(po.getString("param")); diff --git a/xxpay-mgr/src/main/resources/templates/pay_channel/edit.ftl b/xxpay-mgr/src/main/resources/templates/pay_channel/edit.ftl index 8823a26..bc4b6c2 100755 --- a/xxpay-mgr/src/main/resources/templates/pay_channel/edit.ftl +++ b/xxpay-mgr/src/main/resources/templates/pay_channel/edit.ftl @@ -17,6 +17,7 @@ + diff --git a/xxpay4spring-cloud/xxpay-service/src/main/java/org/xxpay/service/controller/PayChannel4AlipayController.java b/xxpay4spring-cloud/xxpay-service/src/main/java/org/xxpay/service/controller/PayChannel4AlipayController.java index c2d5647..9a07aa3 100644 --- a/xxpay4spring-cloud/xxpay-service/src/main/java/org/xxpay/service/controller/PayChannel4AlipayController.java +++ b/xxpay4spring-cloud/xxpay-service/src/main/java/org/xxpay/service/controller/PayChannel4AlipayController.java @@ -7,9 +7,11 @@ import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.domain.AlipayTradeAppPayModel; import com.alipay.api.domain.AlipayTradePagePayModel; +import com.alipay.api.domain.AlipayTradePrecreateModel; import com.alipay.api.domain.AlipayTradeWapPayModel; import com.alipay.api.request.AlipayTradeAppPayRequest; import com.alipay.api.request.AlipayTradePagePayRequest; +import com.alipay.api.request.AlipayTradePrecreateRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; @@ -229,4 +231,70 @@ public class PayChannel4AlipayController { map.put("payParams", payParams); return XXPayUtil.makeRetData(map, resKey); } + + /** + * 支付宝当面付之扫码支付 + * 文档:https://docs.open.alipay.com/api_1/alipay.trade.precreate + * @param jsonParam + * @return + */ + @RequestMapping(value = "/pay/channel/ali_qr") + public String doAliPayQrReq(@RequestParam String jsonParam) { + String logPrefix = "【支付宝当面付之扫码支付下单】"; + JSONObject paramObj = JSON.parseObject(new String(MyBase64.decode(jsonParam))); + PayOrder payOrder = paramObj.getObject("payOrder", PayOrder.class); + String payOrderId = payOrder.getPayOrderId(); + String mchId = payOrder.getMchId(); + String channelId = payOrder.getChannelId(); + MchInfo mchInfo = mchInfoService.selectMchInfo(mchId); + String resKey = mchInfo == null ? "" : mchInfo.getResKey(); + if("".equals(resKey)) return XXPayUtil.makeRetFail(XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "", PayConstant.RETURN_VALUE_FAIL, PayEnum.ERR_0001)); + PayChannel payChannel = payChannelService.selectPayChannel(channelId, mchId); + alipayConfig.init(payChannel.getParam()); + AlipayClient client = new DefaultAlipayClient(alipayConfig.getUrl(), alipayConfig.getApp_id(), alipayConfig.getRsa_private_key(), AlipayConfig.FORMAT, AlipayConfig.CHARSET, alipayConfig.getAlipay_public_key(), AlipayConfig.SIGNTYPE); + AlipayTradePrecreateRequest alipay_request = new AlipayTradePrecreateRequest(); + // 封装请求支付信息 + AlipayTradePrecreateModel model=new AlipayTradePrecreateModel(); + model.setOutTradeNo(payOrderId); + model.setSubject(payOrder.getSubject()); + model.setTotalAmount(AmountUtil.convertCent2Dollar(payOrder.getAmount().toString())); + model.setBody(payOrder.getBody()); + // 获取objParams参数 + String objParams = payOrder.getExtra(); + if (StringUtils.isNotEmpty(objParams)) { + try { + JSONObject objParamsJson = JSON.parseObject(objParams); + if(StringUtils.isNotBlank(objParamsJson.getString("discountable_amount"))) { + //可打折金额 + model.setDiscountableAmount(objParamsJson.getString("discountable_amount")); + } + if(StringUtils.isNotBlank(objParamsJson.getString("undiscountable_amount"))) { + //不可打折金额 + model.setUndiscountableAmount(objParamsJson.getString("undiscountable_amount")); + } + } catch (Exception e) { + _log.error("{}objParams参数格式错误!", logPrefix); + } + } + alipay_request.setBizModel(model); + // 设置异步通知地址 + alipay_request.setNotifyUrl(alipayConfig.getNotify_url()); + // 设置同步地址 + alipay_request.setReturnUrl(alipayConfig.getReturn_url()); + String payUrl = null; + try { + payUrl = client.execute(alipay_request).getBody(); + } catch (AlipayApiException e) { + e.printStackTrace(); + } + _log.info("{}生成跳转路径:payUrl={}", logPrefix, payUrl); + payOrderService.updateStatus4Ing(payOrderId, null); + _log.info("{}生成请求支付宝数据,req={}", logPrefix, alipay_request.getBizModel()); + _log.info("###### 商户统一下单处理完成 ######"); + Map map = XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_SUCCESS, null); + map.put("payOrderId", payOrderId); + map.put("payUrl", payUrl); + return XXPayUtil.makeRetData(map, resKey); + } + } diff --git a/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/controller/PayOrderController.java b/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/controller/PayOrderController.java index 69bde31..c14a136 100644 --- a/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/controller/PayOrderController.java +++ b/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/controller/PayOrderController.java @@ -95,6 +95,8 @@ public class PayOrderController { return payOrderServiceClient.doAliPayPcReq(getJsonParam("payOrder", payOrder)); case PayConstant.PAY_CHANNEL_ALIPAY_WAP : return payOrderServiceClient.doAliPayWapReq(getJsonParam("payOrder", payOrder)); + case PayConstant.PAY_CHANNEL_ALIPAY_QR : + return payOrderServiceClient.doAliPayQrReq(getJsonParam("payOrder", payOrder)); default: return XXPayUtil.makeRetFail(XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "不支持的支付渠道类型[channelId="+channelId+"]", null, null)); } diff --git a/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/service/PayOrderServiceClient.java b/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/service/PayOrderServiceClient.java index 512ca58..494c458 100644 --- a/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/service/PayOrderServiceClient.java +++ b/xxpay4spring-cloud/xxpay-web/src/main/java/org/xxpay/web/service/PayOrderServiceClient.java @@ -89,4 +89,18 @@ public class PayOrderServiceClient { return "error"; } + /** + * 处理支付宝当面付扫码支付 + * @param jsonParam + * @return + */ + @HystrixCommand(fallbackMethod = "doAliPayQrReqFallback") + public String doAliPayQrReq(String jsonParam) { + return restTemplate.getForEntity("http://XXPAY-SERVICE/pay/channel/ali_qr?jsonParam=" + MyBase64.encode(jsonParam.getBytes()), String.class).getBody(); + } + + public String doAliPayQrReqFallback(String jsonParam) { + return "error"; + } + } \ No newline at end of file