WechatPaymentHandler.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package com.mb.payment.handler.wchat;
  2. import cn.beecloud.bean.RedpackInfo;
  3. import cn.beecloud.bean.TransferParameter;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.mb.game.platform.util.CommonUtil;
  6. import com.mb.game.platform.util.NumberUtils;
  7. import com.mb.payment.context.PaymentContext;
  8. import com.mb.payment.exception.PaymentException;
  9. import com.mb.payment.handler.wchat.parameter.WchatTransferParameter;
  10. import com.mb.payment.handler.wchat.parameter.WechatTransferBankParameter;
  11. import com.mb.payment.handler.wchat.parameter.WechatTransferSmallChangeParameter;
  12. import com.mb.payment.utils.WechatRSAUtils;
  13. import org.apache.commons.lang.StringUtils;
  14. import java.math.BigDecimal;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * 微信公众号支付
  19. */
  20. public class WechatPaymentHandler extends AbstractWechatPaymentHandler {
  21. /**
  22. * 红包转账
  23. */
  24. private String transferUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
  25. private String redpackQueryUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo";
  26. //转款到零钱
  27. private final String transferSmallChangeUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
  28. //转款到银行卡
  29. private final String transferSmallBankUrl = "https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank";
  30. public WechatPaymentHandler(String appId, String mchId, String apiKey, String notifyPayUrl, String notifyRefundUrl) {
  31. super(appId, mchId, apiKey, notifyPayUrl, notifyRefundUrl);
  32. }
  33. @Override
  34. public String doRestPay(PaymentContext paymentContext) {
  35. throw new UnsupportedOperationException("不支持的操作");
  36. }
  37. @Override
  38. public String transfer(TransferParameter transferParameter) {
  39. Map<String, Object> parameter = new HashMap<>();
  40. String sendUrl;
  41. if (transferParameter instanceof WchatTransferParameter){
  42. transferParameter(parameter, (WchatTransferParameter) transferParameter);
  43. sendUrl = transferUrl;
  44. }else if (transferParameter instanceof WechatTransferBankParameter){
  45. transferBankParameter(parameter, (WechatTransferBankParameter) transferParameter);
  46. sendUrl = transferSmallBankUrl;
  47. }else if (transferParameter instanceof WechatTransferSmallChangeParameter){
  48. transferSmallChangeParameter(parameter, (WechatTransferSmallChangeParameter) transferParameter);
  49. sendUrl = transferSmallChangeUrl;
  50. }else {
  51. throw new PaymentException("不支持的参数");
  52. }
  53. parameter.put("nonce_str", NumberUtils.getNonceString());
  54. parameter.put("sign", NumberUtils.getSign(parameter, apiKey));
  55. String mapToXml;
  56. try {
  57. mapToXml = CommonUtil.mapToXml(parameter);
  58. } catch (Exception ex) {
  59. throw new PaymentException("支付参数错误", ex);
  60. }
  61. String result = sendSSLRequest(sendUrl, mapToXml);
  62. JSONObject jsonObject = JSONObject.parseObject(result);
  63. if (!jsonObject.get("return_code").toString().equals("SUCCESS") || !jsonObject.get("result_code").toString().equals("SUCCESS")) {
  64. log.info(mapToXml + "-" + jsonObject.toJSONString());
  65. throw new PaymentException("支付错误");
  66. }
  67. return result;
  68. }
  69. /**
  70. * 企业打款到银行卡
  71. * @param parameter
  72. * @param wchatTransferObject
  73. */
  74. private void transferBankParameter(Map<String, Object> parameter, WechatTransferBankParameter wchatTransferObject){
  75. parameter.put("mch_id", this.mchId);
  76. parameter.put("partner_trade_no", wchatTransferObject.getPartner_trade_no());
  77. parameter.put("bank_code", wchatTransferObject.getBank_code());
  78. try{
  79. parameter.put("enc_bank_no", WechatRSAUtils.encrypt(wchatTransferObject.getEnc_bank_no()));
  80. parameter.put("enc_true_name", WechatRSAUtils.encrypt(wchatTransferObject.getEnc_true_name()));
  81. }catch (Exception e){
  82. log.info(parameter.toString()+"----enc_bank_no:"+wchatTransferObject.getEnc_bank_no()+"---enc_true_name:"+wchatTransferObject.getEnc_true_name());
  83. throw new PaymentException("对银行卡或收款方用户名加密失败");
  84. }
  85. BigDecimal amount = wchatTransferObject.getAmount();
  86. if (amount.compareTo(new BigDecimal("0.01")) < 0){
  87. log.info(parameter.toString() + "---" + amount);
  88. throw new PaymentException("支付金额错误, 小于最低支付金额");
  89. }
  90. parameter.put("amount", amount.multiply(new BigDecimal(100)).intValue());
  91. parameter.put("desc", wchatTransferObject.getDesc());
  92. }
  93. /**
  94. * 企业打款到零钱
  95. * @param parameter
  96. * @param wchatTransferObject
  97. */
  98. private void transferSmallChangeParameter(Map<String, Object> parameter, WechatTransferSmallChangeParameter wchatTransferObject){
  99. parameter.put("mchid", this.mchId);
  100. parameter.put("mch_appid", this.appId);
  101. parameter.put("check_name", "FORCE_CHECK");
  102. parameter.put("partner_trade_no", wchatTransferObject.getPartner_trade_no());
  103. parameter.put("openid", wchatTransferObject.getOpenid());
  104. parameter.put("re_user_name", wchatTransferObject.getRe_user_name());
  105. BigDecimal amount = wchatTransferObject.getAmount();
  106. if (amount.compareTo(new BigDecimal("0.3")) < 0){
  107. log.info(parameter.toString() + "---" + amount);
  108. throw new PaymentException("支付金额错误, 小于最低支付金额");
  109. }
  110. parameter.put("amount", amount.multiply(new BigDecimal(100)).intValue());
  111. parameter.put("desc", wchatTransferObject.getDesc());
  112. parameter.put("spbill_create_ip", wchatTransferObject.getSpbill_create_ip());
  113. parameter.put("device_info", wchatTransferObject.getDevice_info());
  114. }
  115. /**
  116. * 红包转账
  117. * @param parameter
  118. * @param wchatTransferObject
  119. */
  120. private void transferParameter(Map<String, Object> parameter, WchatTransferParameter wchatTransferObject){
  121. parameter.put("mch_billno", wchatTransferObject.getTransferNo());
  122. parameter.put("mch_id", this.mchId);
  123. parameter.put("wxappid", this.appId);
  124. parameter.put("send_name", wchatTransferObject.getOrgName());
  125. parameter.put("re_openid", wchatTransferObject.getChannelUserId());
  126. parameter.put("total_amount", wchatTransferObject.getTotalFee());
  127. parameter.put("total_num", 1);
  128. RedpackInfo redpackInfo = wchatTransferObject.getRedpackInfo();
  129. parameter.put("wishing", redpackInfo.getWishing());
  130. parameter.put("client_ip", wchatTransferObject.getClientIp());
  131. parameter.put("act_name", redpackInfo.getActivityName());
  132. parameter.put("remark", wchatTransferObject.getDescription());
  133. parameter.put("scene_id", wchatTransferObject.getSceneId());
  134. }
  135. @Override
  136. public String findTransferOder(String orderNo) {
  137. if (StringUtils.isEmpty(orderNo)) {
  138. throw new PaymentException("不支持的参数");
  139. }
  140. Map<String, Object> parameter = new HashMap<>();
  141. parameter.put("nonce_str", NumberUtils.getNonceString());
  142. parameter.put("mch_billno", orderNo);
  143. parameter.put("mch_id", this.mchId);
  144. parameter.put("appid", this.appId);
  145. parameter.put("bill_type", "MCHT");
  146. parameter.put("sign", NumberUtils.getSign(parameter, apiKey));
  147. String mapToXml = null;
  148. try {
  149. mapToXml = CommonUtil.mapToXml(parameter);
  150. } catch (Exception ex) {
  151. throw new PaymentException("支付参数错误", ex);
  152. }
  153. String result = sendSSLRequest(redpackQueryUrl, mapToXml);
  154. JSONObject jsonObject = JSONObject.parseObject(result);
  155. if (!jsonObject.get("return_code").toString().equals("SUCCESS") || !jsonObject.get("result_code").toString().equals("SUCCESS")) {
  156. log.info(mapToXml + "-" + jsonObject.toJSONString());
  157. throw new PaymentException("支付错误");
  158. }
  159. return result;
  160. }
  161. private String encryptionInfoData(String data){
  162. Map<String, Object> parameter = new HashMap<>();
  163. parameter.put("mch_id", this.mchId);
  164. parameter.put("sign_type", "MD5");
  165. parameter.put("nonce_str", NumberUtils.getNonceString());
  166. parameter.put("sign", NumberUtils.getSign(parameter, apiKey));
  167. String mapToXml;
  168. try {
  169. mapToXml = CommonUtil.mapToXml(parameter);
  170. } catch (Exception ex) {
  171. throw new PaymentException("支付参数错误", ex);
  172. }
  173. String result = sendSSLRequest("https://fraud.mch.weixin.qq.com/risk/getpublickey", mapToXml);
  174. JSONObject jsonObject = JSONObject.parseObject(result);
  175. if (!jsonObject.get("return_code").toString().equals("SUCCESS") || !jsonObject.get("result_code").toString().equals("SUCCESS")) {
  176. log.info(mapToXml + "-" + jsonObject.toJSONString());
  177. throw new PaymentException("获取公匙失败");
  178. }
  179. String pub_key = jsonObject.getString("pub_key");
  180. log.info("\n\n");
  181. log.info(pub_key);
  182. log.info("\n\n");
  183. return pub_key;
  184. }
  185. public static void main(String[] args){
  186. //银行卡转账和到零钱
  187. WechatPaymentHandler wechatPaymentHandler = new WechatPaymentHandler(
  188. "wx45468204ffa9a9a0","1501370501","maibachuxing2018MAIBACHUXING2018",
  189. "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers",null);
  190. // WechatTransferBankParameter wchatTransferObject = new WechatTransferBankParameter();
  191. // wchatTransferObject.setPartner_trade_no("H100084h10000h192185");
  192. // wchatTransferObject.setEnc_bank_no("6230540460117503676");
  193. // wchatTransferObject.setEnc_true_name("刘毅");
  194. // bank_code 明细参见: https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_4
  195. // wchatTransferObject.setBank_code("1005");
  196. // wchatTransferObject.setAmount(new BigDecimal("0.04"));
  197. // wchatTransferObject.setDesc("test");
  198. // WechatTransferSmallChangeParameter wchatTransferObject = new WechatTransferSmallChangeParameter();
  199. // wchatTransferObject.setPartner_trade_no("H100084h10000h194125");
  200. // wchatTransferObject.setOpenid("osWpr5alVrod6NkS7bGkJd23DcrQ");
  201. // wchatTransferObject.setRe_user_name("冉泉");
  202. // wchatTransferObject.setAmount(new BigDecimal("1"));
  203. // wchatTransferObject.setDesc("test");
  204. // wchatTransferObject.setSpbill_create_ip("118.113.206.83");
  205. // wchatTransferObject.setDevice_info("sb_123456");
  206. // //红包是公众号APP_ID
  207. // WechatPaymentHandler wechatPaymentHandler = new WechatPaymentHandler(
  208. // "wx043c9a9826176649","1501370501","maibachuxing2018MAIBACHUXING2018",
  209. // "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers",null);
  210. // WchatTransferParameter wchatTransferObject = new WchatTransferParameter();
  211. // wchatTransferObject.setOrgName("麦巴出行");
  212. // wchatTransferObject.setChannelUserId("oV4Ih00eq70KvN3IIRAYJ43pW1No");
  213. // wchatTransferObject.setDescription("余额提现");
  214. // wchatTransferObject.setClientIp("118.113.206.83");
  215. // RedpackInfo redpackInfo = new RedpackInfo();
  216. // wchatTransferObject.setRedpackInfo(redpackInfo);
  217. // redpackInfo.setActivityName("推广奖励TEST");
  218. // redpackInfo.setWishing("多推广,多奖励TEST");
  219. // wchatTransferObject.setTotalFee(new BigDecimal("1").multiply(BigDecimal.valueOf(100)).intValue());
  220. // wchatTransferObject.setTransferNo("H100084h10000h202225");
  221. // wechatPaymentHandler.transfer(wchatTransferObject);
  222. }
  223. }