package com.szwl.service.impl; import cn.com.sand.hmpay.HmpayClient; import cn.com.sand.hmpay.contants.TradeContants; import cn.com.sand.hmpay.enums.TradeApiMethodEnums; import cn.com.sand.hmpay.enums.TradeApiSignTypeEnums; import cn.com.sand.hmpay.enums.TradeBizPayWayEnums; import cn.com.sand.hmpay.util.RsaUtil; import cn.com.sand.hmpay.vo.common.ExtendParams; import cn.com.sand.hmpay.vo.request.TradeRequest; import cn.com.sand.hmpay.vo.request.biz.TradePayRequest; import cn.com.sand.hmpay.vo.request.biz.TradePrecreateRequest; import cn.com.sand.hmpay.vo.request.common.DisCountInfo; import cn.com.sand.hmpay.vo.request.common.GoodsDetail; import cn.com.sand.hmpay.vo.response.TradeResponse; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.szwl.mapper.TShandeMchMapper; import com.szwl.model.entity.TShandeMch; import com.szwl.service.TShandeMchService; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.net.InetAddress; import java.net.UnknownHostException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** *

* 杉德支付收款信息 服务实现类 *

* * @author wuhs * @since 2022-04-29 */ @Service public class TShandeMchServiceImpl extends ServiceImpl implements TShandeMchService { String host = "https://hmpay.sandpay.com.cn/gateway"; String url = host + "/api"; HmpayClient hmpayClient = new HmpayClient(url); String appId = "664401000051141"; // 代理商664401000051141 String subAppId = ""; // 商户 // 平台公钥-验签 String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM6wa5i8uABLTIUOGyeQ9OvYW93kSeKS8EOJS0Qewr4lDBmvADgTj8Rb6wir+Hh5aFrh8ksVwhqNWGonMCMrjHsaIEmTOcEBX+e6kr+f6a3TgW3ppqykD2oE86mvunmeMV1uszsKx9coS+iHnx4UflmswNfY+x1ZBXSHQ8c7nRnwIDAQAB"; // 代理商或商户私钥-签名 String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALjX7Od7H+WuDJ1kzu9jCzBkxYD+TXD+n6ZRbd1qBwgwq5jcI6hlVlNW2C9jI6R/I03ByKlEPYSwOQyJeAaNLpt2+gd9BIBU7CaQ1Yd1fxIh3KCgjrO5q6KliDU5oVsCcheO7zcEyAiYKJYBbncpR1n36R30zjfr+IOLkbEd0YXNAgMBAAECgYBFKP1J6yaXIFU1+sb2iL9h/wJq7jt5gLupViReSNKSvOsniY6J7kira/39VQG8BZnm+Q+kTZUa9XiS+UiAYwjBnKUkoRDwmTLpjpkiqDxyBTmb1NKt+/kG7cUI732+1/yLWWprJiY+rS0JFHreB5p4dP8oEaaLodt4HEYSJkOtIQJBAPNthG4SThCjtbJmoyHnkQyrqcNvPl6jWlVA2ZPv75pDFBGGcux3NOvtQmAdBCRYFyCl/hzmxjmFAgZ+yiOmHWUCQQDCY9WwZS0I6mZPBYygWzXa+JkWPtKBU4ZZsi74BsYU2OaXoGfMlw8KdL6CgpsoAgVtf4+gdgqjqsPueXictlRJAkEAkragCWM476hrmyFnEskpT/SqdaK3eXyEtjtGp3YF9/iTwNhCrahPGh6+UYVLeMgq8D97P1oLgKFcjiOemwvETQJAeTE6+bWqhSN2U4/3I8aZ5gIVl2vlW+HSIjvX0qSOfFLaOPA1prULGI6+c6mvyBqyfZkI15agfXeqnhQ3YSS/mQJBAKBAzycKoPctGF2EkvSZXJZErSqreSJ7G2/GH2+7i/cCZ5MDHnNkZKL+9UiDKUXvUzZ9oUOWqzMCqQ0fjAN3g78="; // 商户公钥 String merchantPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC41+znex/lrgydZM7vYwswZMWA/k1w/p+mUW3dagcIMKuY3COoZVZTVtgvYyOkfyNNwcipRD2EsDkMiXgGjS6bdvoHfQSAVOwmkNWHdX8SIdygoI6zuauipYg1OaFbAnIXju83BMgImCiWAW53KUdZ9+kd9M436/iDi5GxHdGFzQIDAQAB"; String format = "JSON"; String charset = "UTF-8"; String signType = TradeApiSignTypeEnums.RSA.name(); String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String version = "1.0"; String operatorId = "operatorId"; String storeId = "100001"; String terminalId = "terminalId"; Double totalAmount = 0.02D; Double refundAmount = null; Double confirmAmount = null; // 商户订单号 String outOrderNo = String.valueOf(System.currentTimeMillis()); String body = "订单描述-测试"; String createIp = "192.168.1.1";//生产环境请使用真实IP TODO String notifyUrl = ""; String nonce = String.valueOf(System.currentTimeMillis()); // String goodsTag = "goodsTag"; String goodsTag = null; Double discountableAmount = null; String productCode = ""; // GoodsDetail[] goodsDetail = // { // GoodsDetail.builder().goodsId("goodsId1").goodsName("goodsName1").quantity(1).price(0.01).body("body1").showUrl("http://www.baidu.com").build(), // GoodsDetail.builder().goodsId("goodsId2").goodsName("goodsName2").quantity(1).price(0.01).body("body2").showUrl("http://www.baidu.com").build() // }; GoodsDetail[] goodsDetail = null; String limitPay = null; //C扫B 统一预下单接口 @Override public String uniPay(String orderNo, BigDecimal amount, String productName, String productDesc, String commonParameter, String returnUrl, String notifyUrl, String frpCode, String isShowPic, String openId, String authCode, String appid, String transactionModel, String tradeMerchantNo, String buyerId, String isAlt, String altType, JSONArray altInfo, String altUrl, BigDecimal marketingAmount) { // long startTime = System.currentTimeMillis(); notifyUrl= "http://pay.sunzee.com.cn:49013/tOrder/shandeRefund.htm"; // notifyUrl= "http://sunzee.free.idcfengye.com/api/order/shandeRefund.htm"; // String payWay = null; // if(frpCode.equals("ALIPAY_NATIVE")){ // payWay = TradeBizPayWayEnums.ALIPAY.name(); // }else { // payWay = TradeBizPayWayEnums.WECHAT.name(); // } InetAddress ia = null; try { ia = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } String localip = ia.getHostAddress(); if(localip.equals("10.0.0.153")){ localip = "112.74.63.148"; }else { localip = "47.112.127.131"; } String payWay = TradeBizPayWayEnums.AUTO.name(); String method = TradeApiMethodEnums.TRADE_PRECREATE.getMethod(); String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); String expireTime = LocalDateTime.now().plusMinutes(10).format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); TradeRequest tradeRequest = TradeRequest .builder() .appId(appId) .subAppId(subAppId) .format(format) .charset(charset) .method(method) .signType(signType) .timestamp(timestamp) .nonce(nonce) .bizContent( TradePrecreateRequest .builder() .body(productName) .createIp(localip) .createTime(createTime) .expireTime(expireTime) .notifyUrl(notifyUrl) .operatorId(operatorId) .outOrderNo(orderNo) .payWay(payWay) .storeId(storeId) .terminalId(terminalId) .totalAmount(amount.doubleValue()) .extendParams(ExtendParams.builder().accessPartyCode("SDK_TEST").build()) .discountInfo(DisCountInfo.builder() .goodsTag(goodsTag) .discountableAmount(discountableAmount) .productCode(productCode) .build()) .goodsDetails(goodsDetail) .limitPay(limitPay) .build() .toJsonString() ) .version(version) .build() .buildSign(privateKey); // String data = JacksonUtil.beanToJson(tradeRequest); // // String respStr = HttpClientUtil.getInstance().postJson(url, data); // System.out.println(tradeRequest); TradeResponse tradeResponse = hmpayClient.execute(tradeRequest); // System.out.println(tradeResponse); // long endTime = System.currentTimeMillis(); // System.out.println("花费时间: " + (endTime - startTime) + "ms"); boolean b = RsaUtil.rsaCheckV2(tradeResponse.toMap(), publicKey, TradeContants.CHARSET); // System.out.println("验签结果:" + b); String data = tradeResponse.getData(); return data; } //B扫C 被扫支付 @Override public String cardPay(String orderNo, BigDecimal amount, String productName, String productDesc, String commonParameter, String returnUrl, String notifyUrl, String frpCode, String isShowPic, String openId, String authCode, String appid, String transactionModel, String tradeMerchantNo, String buyerId, String isAlt, String altType, JSONArray altInfo, String altUrl, BigDecimal marketingAmount) { // long startTime = System.currentTimeMillis(); String method = TradeApiMethodEnums.TRADE_PAY.getMethod(); notifyUrl= "http://app.sunzee.com.cn/api/order/shandeRefund.htm"; InetAddress ia = null; try { ia = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } String localip = ia.getHostAddress(); if(localip.equals("10.0.0.153")){ localip = "112.74.63.148"; }else { localip = "47.112.127.131"; } // notifyUrl= "http://sunzeeshande.5gzvip.91tunnel.com/api/order/shandeRefund.htm"; // String payWay = TradeBizPayWayEnums.WECHAT.name(); String payWay = TradeBizPayWayEnums.AUTO.name(); amount = amount.setScale(2,BigDecimal.ROUND_HALF_UP); TradeRequest tradeRequest = TradeRequest.builder() .appId(appId) .subAppId(subAppId) .format(format) .charset(charset) .method(method) .signType(signType) .timestamp(timestamp) .nonce(nonce) .version(version) .bizContent( TradePayRequest.builder() .body(productName) .createIp(localip) .notifyUrl(notifyUrl) .operatorId(operatorId) .outOrderNo(orderNo) .payWay(payWay) .storeId(storeId) .terminalId(terminalId) .totalAmount(amount.doubleValue()) .authCode(authCode) .extendParams(ExtendParams.builder().accessPartyCode("SDK_TEST").build()) .discountInfo(DisCountInfo.builder() .goodsTag(goodsTag) .discountableAmount(discountableAmount) .productCode(productCode) .build()) .goodsDetails(goodsDetail) .limitPay(limitPay) .build() .toJsonString() ) .build() .buildSign(privateKey); TradeResponse tradeResponse = hmpayClient.execute(tradeRequest); // System.out.println(tradeResponse); // long endTime = System.currentTimeMillis(); // System.out.println("花费时间: " + (endTime - startTime) + "ms"); boolean b = RsaUtil.rsaCheckV2(tradeResponse.toMap(), publicKey, TradeContants.CHARSET); // System.out.println("验签结果:" + b); return null; } }