OrderPaypalController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.fdkankan.ucenter.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.ucenter.common.BaseController;
  4. import com.fdkankan.ucenter.common.Result;
  5. import com.fdkankan.ucenter.common.ResultData;
  6. import com.fdkankan.ucenter.common.constants.NacosProperty;
  7. import com.fdkankan.ucenter.pay.factory.PayFactory;
  8. import com.fdkankan.ucenter.pay.paypal.PayPalDefaultConfig;
  9. import com.fdkankan.ucenter.pay.paypal.sdk.PaypalService;
  10. import com.fdkankan.ucenter.pay.strategy.OrderStrategyFactory;
  11. import com.fdkankan.ucenter.vo.request.PlaceOrderParam;
  12. import com.paypal.api.payments.Payment;
  13. import com.paypal.base.rest.PayPalRESTException;
  14. import lombok.extern.log4j.Log4j2;
  15. import okhttp3.*;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestMethod;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.ResponseBody;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.io.IOException;
  26. import java.io.PrintWriter;
  27. import java.io.StringWriter;
  28. import java.io.UnsupportedEncodingException;
  29. import java.net.URLEncoder;
  30. import java.util.Enumeration;
  31. @Log4j2
  32. /**支付中心-paypal支付模块*/
  33. @Controller
  34. @RequestMapping("/ucenter/order/pay")
  35. public class OrderPaypalController extends BaseController {
  36. @Autowired
  37. private OrderStrategyFactory orderStrategyFactory;
  38. @Autowired
  39. private PayFactory payFactory;
  40. @Autowired
  41. private PaypalService paypalService;
  42. @Autowired
  43. private PayPalDefaultConfig config;
  44. //正式环境或测试环境
  45. @Value("${spring.profiles.active}")
  46. private String ev;
  47. /**
  48. * Paypal付款
  49. * orderId 订单id
  50. * orderType 订单类型,0购买相机,1点数重置,2扩容,3商业版,4增值服务,5场景下载
  51. * spaceId 0表示国内订单,1表示国外订单
  52. */
  53. @ResponseBody
  54. @RequestMapping(value = "/paypal", method = RequestMethod.POST)
  55. public Result paypal(@RequestBody PlaceOrderParam order) throws Exception {
  56. JSONObject jsonObject = payFactory.scanPay("paypal", order);
  57. return Result.success(jsonObject);
  58. }
  59. @RequestMapping(value = "/paypalH5", method = RequestMethod.POST)
  60. public String paypalH5(PlaceOrderParam order) throws Exception {
  61. JSONObject jsonObject = payFactory.h5Pay("paypal", order, null);
  62. if (jsonObject != null && jsonObject.get("redirect") != null){
  63. return "redirect:" + jsonObject.getString("redirect");
  64. }
  65. return "redirect:" + NacosProperty.getMainUrl() + config.getH5FailUrl();
  66. }
  67. /**
  68. * 支付成功
  69. * @param paymentId
  70. * @param payerId
  71. * @return
  72. */
  73. @RequestMapping(value = "/paypal/callback", method = RequestMethod.GET)
  74. public String successPay(@RequestParam("paymentId") String paymentId,
  75. @RequestParam("PayerID") String payerId) {
  76. try {
  77. log.warn("=====paypal支付回调=====\npaymentId:"+paymentId+"\npayerId:"+payerId);
  78. Payment payment = paypalService.executePayment(paymentId, payerId);
  79. if (payment.getState().equals("approved")) {
  80. log.info("paypal支付成功回调");
  81. String custom = payment.getTransactions().get(0).getCustom();
  82. String txnId = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
  83. String payerEmail = payment.getTransactions().get(0).getPayee().getEmail();
  84. log.warn("=====custom:"+custom+"\ntxnId:"+txnId+"\npayerEmail:"+payerEmail);
  85. String orderSn = custom.split("_")[0];
  86. if(custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")){
  87. orderSn += "_" + custom.split("_")[2];
  88. }
  89. orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, custom.split("_")[1], 2);
  90. return "redirect:" + NacosProperty.getMainUrl() + config.getSuccessUrl();
  91. }
  92. } catch (PayPalRESTException e) {
  93. log.error(e.getMessage());
  94. } catch (Exception e) {
  95. log.error("paypal支付回调异常,errorMsg:{}", e.getMessage());
  96. }
  97. return "redirect:" + NacosProperty.getMainUrl() + config.getFailUrl();
  98. }
  99. /**
  100. * 支付成功
  101. * @param paymentId
  102. * @param payerId
  103. * @return
  104. */
  105. @RequestMapping(value = "/paypal/callbackH5", method = RequestMethod.GET)
  106. public String successPayH5(@RequestParam("paymentId") String paymentId,
  107. @RequestParam("PayerID") String payerId) {
  108. try {
  109. log.warn("=====paypal支付回调=====\npaymentId:"+paymentId+"\npayerId:"+payerId);
  110. Payment payment = paypalService.executePayment(paymentId, payerId);
  111. if (payment.getState().equals("approved")) {
  112. log.info("paypal支付成功回调");
  113. String custom = payment.getTransactions().get(0).getCustom();
  114. String txnId = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
  115. String payerEmail = payment.getTransactions().get(0).getPayee().getEmail();
  116. log.warn("=====custom:"+custom+"\ntxnId:"+txnId+"\npayerEmail:"+payerEmail);
  117. String orderSn = custom.split("_")[0];
  118. if(custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")){
  119. orderSn += "_" + custom.split("_")[2];
  120. }
  121. orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, custom.split("_")[1], 2);
  122. return "redirect:" + NacosProperty.getMainUrl() + config.getH5SuccessUrl();
  123. }
  124. } catch (PayPalRESTException e) {
  125. log.error(e.getMessage());
  126. } catch (Exception e) {
  127. log.error("paypal支付回调异常,errorMsg:{}", e.getMessage());
  128. }
  129. return "redirect:" + NacosProperty.getMainUrl() + config.getH5FailUrl();
  130. }
  131. /**
  132. * 取消支付
  133. * @return
  134. */
  135. @RequestMapping(value = "/paypal/cancel", method = RequestMethod.GET)
  136. public String cancelPay(){
  137. return "redirect:" + NacosProperty.getMainUrl() + config.getFailUrl();
  138. }
  139. /**
  140. * 取消支付
  141. * @return
  142. */
  143. @RequestMapping(value = "/paypal/cancelH5", method = RequestMethod.GET)
  144. public String cancelPayH5(){
  145. return "redirect:" + NacosProperty.getMainUrl() + config.getH5FailUrl();
  146. }
  147. /**
  148. * Paypal的INP异步通知服务器, 需要修改Paypal官网即时付款通知IPN通告URL
  149. */
  150. @ResponseBody
  151. @RequestMapping(value = "/paypal/notify", method = RequestMethod.POST)
  152. public void callback() {
  153. Enumeration en = request.getParameterNames();
  154. String str = "cmd=_notify-validate";
  155. while (en.hasMoreElements()) {
  156. String paramName = (String) en.nextElement();
  157. String paramValue = request.getParameter(paramName);
  158. try {
  159. str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue, "utf-8");
  160. } catch (UnsupportedEncodingException e) {
  161. StringWriter trace=new StringWriter();
  162. e.printStackTrace(new PrintWriter(trace));
  163. log.error(trace.toString());
  164. }
  165. //此处的编码一定要和自己的网站编码一致,不然会出现乱码,paypal回复的通知为‘INVALID’
  166. }
  167. final String itemName = request.getParameter("item_name");//商品名
  168. final String itemNumber = request.getParameter("item_number");//购买数量
  169. final String paymentDate = request.getParameter("payment_date");//交易时间
  170. final String receiverEmail = request.getParameter("receiver_email");//收款人email
  171. final String payerEmail = request.getParameter("payer_email");//付款人email
  172. final String paymentAmount = request.getParameter("mc_gross");//交易钱数
  173. final String paymentCurrency = request.getParameter("mc_currency");//货币种类
  174. final String paymentStatus = request.getParameter("payment_status");//交易状态
  175. final String txnId = request.getParameter("txn_id");//交易id
  176. String custom = request.getParameter("custom");//发送payment请求时候自定义的业务服务器订单号
  177. log.info("itemName:"+itemName);
  178. log.info("itemNumber:"+itemNumber);
  179. log.info("paymentDate:"+paymentDate);
  180. log.info("receiverEmail:"+receiverEmail);
  181. log.info("payerEmail:"+payerEmail);
  182. log.info("paymentAmount:"+paymentAmount);
  183. log.info("paymentCurrency:"+paymentCurrency);
  184. log.info("paymentStatus:"+paymentStatus);
  185. log.info("txnId:"+txnId);
  186. log.info("custom:"+custom);
  187. String orderSn = custom.split("_")[0];
  188. try {
  189. if ("Completed".equals(paymentStatus)) {
  190. //根据自己业务进行处理(修改订单状态,支付时间等等操作)
  191. try {
  192. log.info("paypal支付成功回调");
  193. //表示续费,有消费记录id
  194. if (custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")) {
  195. orderStrategyFactory.doHandler(custom.split("_")[0] + "_" + custom.split("_")[2], txnId, payerEmail, custom.split("_")[1], 2);
  196. } else {
  197. orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, custom.split("_")[1], 2);
  198. }
  199. } catch (Exception e) {
  200. log.error("paypal支付回调异常,errorMsg:{}", e.getMessage());
  201. }
  202. }
  203. }catch (Exception e) {
  204. log.error("paypal支付回调异常,errorMsg:{}", e.getMessage());
  205. }
  206. }
  207. @RequestMapping(value = "/paypalresult", method = RequestMethod.GET)
  208. @org.springframework.web.bind.annotation.ResponseBody
  209. public Result paypalresult(HttpServletRequest request) throws Exception {
  210. // if(!ev.equals("test-eur")){
  211. // return Result.failure("正式环境不允许调用!");
  212. // }
  213. String orderSn = request.getParameter("orderSn");
  214. String txnId = request.getParameter("txnId");
  215. String payerEmail = request.getParameter("payer_email");//付款人email
  216. String type = request.getParameter("type");//付款人email
  217. //download ,increment
  218. orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, type, 2);
  219. return Result.success();
  220. }
  221. }