package com.fdkankan.ucenter.controller; import com.alibaba.fastjson.JSONObject; import com.fdkankan.ucenter.common.BaseController; import com.fdkankan.ucenter.common.Result; import com.fdkankan.ucenter.common.ResultData; import com.fdkankan.ucenter.common.constants.NacosProperty; import com.fdkankan.ucenter.pay.factory.PayFactory; import com.fdkankan.ucenter.pay.paypal.PayPalDefaultConfig; import com.fdkankan.ucenter.pay.paypal.sdk.PaypalService; import com.fdkankan.ucenter.pay.strategy.OrderStrategyFactory; import com.fdkankan.ucenter.vo.request.PlaceOrderParam; import com.paypal.api.payments.Payment; import com.paypal.base.rest.PayPalRESTException; import lombok.extern.log4j.Log4j2; import okhttp3.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Enumeration; @Log4j2 /**支付中心-paypal支付模块*/ @Controller @RequestMapping("/ucenter/order/pay") public class OrderPaypalController extends BaseController { @Autowired private OrderStrategyFactory orderStrategyFactory; @Autowired private PayFactory payFactory; @Autowired private PaypalService paypalService; @Autowired private PayPalDefaultConfig config; //正式环境或测试环境 @Value("${spring.profiles.active}") private String ev; /** * Paypal付款 * orderId 订单id * orderType 订单类型,0购买相机,1点数重置,2扩容,3商业版,4增值服务,5场景下载 * spaceId 0表示国内订单,1表示国外订单 */ @ResponseBody @RequestMapping(value = "/paypal", method = RequestMethod.POST) public Result paypal(@RequestBody PlaceOrderParam order) throws Exception { JSONObject jsonObject = payFactory.scanPay("paypal", order); return Result.success(jsonObject); } @RequestMapping(value = "/paypalH5", method = RequestMethod.POST) public String paypalH5(PlaceOrderParam order) throws Exception { JSONObject jsonObject = payFactory.h5Pay("paypal", order, null); if (jsonObject != null && jsonObject.get("redirect") != null){ return "redirect:" + jsonObject.getString("redirect"); } return "redirect:" + NacosProperty.getMainUrl() + config.getH5FailUrl(); } /** * 支付成功 * @param paymentId * @param payerId * @return */ @RequestMapping(value = "/paypal/callback", method = RequestMethod.GET) public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId) { try { log.warn("=====paypal支付回调=====\npaymentId:"+paymentId+"\npayerId:"+payerId); Payment payment = paypalService.executePayment(paymentId, payerId); if (payment.getState().equals("approved")) { log.info("paypal支付成功回调"); String custom = payment.getTransactions().get(0).getCustom(); String txnId = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId(); String payerEmail = payment.getTransactions().get(0).getPayee().getEmail(); log.warn("=====custom:"+custom+"\ntxnId:"+txnId+"\npayerEmail:"+payerEmail); String orderSn = custom.split("_")[0]; if(custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")){ orderSn += "_" + custom.split("_")[2]; } orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, custom.split("_")[1], 2); return "redirect:" + NacosProperty.getMainUrl() + config.getSuccessUrl(); } } catch (PayPalRESTException e) { log.error(e.getMessage()); } catch (Exception e) { log.error("paypal支付回调异常,errorMsg:{}", e.getMessage()); } return "redirect:" + NacosProperty.getMainUrl() + config.getFailUrl(); } /** * 支付成功 * @param paymentId * @param payerId * @return */ @RequestMapping(value = "/paypal/callbackH5", method = RequestMethod.GET) public String successPayH5(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId) { try { log.warn("=====paypal支付回调=====\npaymentId:"+paymentId+"\npayerId:"+payerId); Payment payment = paypalService.executePayment(paymentId, payerId); if (payment.getState().equals("approved")) { log.info("paypal支付成功回调"); String custom = payment.getTransactions().get(0).getCustom(); String txnId = payment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId(); String payerEmail = payment.getTransactions().get(0).getPayee().getEmail(); log.warn("=====custom:"+custom+"\ntxnId:"+txnId+"\npayerEmail:"+payerEmail); String orderSn = custom.split("_")[0]; if(custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")){ orderSn += "_" + custom.split("_")[2]; } orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, custom.split("_")[1], 2); return "redirect:" + NacosProperty.getMainUrl() + config.getH5SuccessUrl(); } } catch (PayPalRESTException e) { log.error(e.getMessage()); } catch (Exception e) { log.error("paypal支付回调异常,errorMsg:{}", e.getMessage()); } return "redirect:" + NacosProperty.getMainUrl() + config.getH5FailUrl(); } /** * 取消支付 * @return */ @RequestMapping(value = "/paypal/cancel", method = RequestMethod.GET) public String cancelPay(){ return "redirect:" + NacosProperty.getMainUrl() + config.getFailUrl(); } /** * 取消支付 * @return */ @RequestMapping(value = "/paypal/cancelH5", method = RequestMethod.GET) public String cancelPayH5(){ return "redirect:" + NacosProperty.getMainUrl() + config.getH5FailUrl(); } /** * Paypal的INP异步通知服务器, 需要修改Paypal官网即时付款通知IPN通告URL */ @ResponseBody @RequestMapping(value = "/paypal/notify", method = RequestMethod.POST) public void callback() { Enumeration en = request.getParameterNames(); String str = "cmd=_notify-validate"; while (en.hasMoreElements()) { String paramName = (String) en.nextElement(); String paramValue = request.getParameter(paramName); try { str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue, "utf-8"); } catch (UnsupportedEncodingException e) { StringWriter trace=new StringWriter(); e.printStackTrace(new PrintWriter(trace)); log.error(trace.toString()); } //此处的编码一定要和自己的网站编码一致,不然会出现乱码,paypal回复的通知为‘INVALID’ } final String itemName = request.getParameter("item_name");//商品名 final String itemNumber = request.getParameter("item_number");//购买数量 final String paymentDate = request.getParameter("payment_date");//交易时间 final String receiverEmail = request.getParameter("receiver_email");//收款人email final String payerEmail = request.getParameter("payer_email");//付款人email final String paymentAmount = request.getParameter("mc_gross");//交易钱数 final String paymentCurrency = request.getParameter("mc_currency");//货币种类 final String paymentStatus = request.getParameter("payment_status");//交易状态 final String txnId = request.getParameter("txn_id");//交易id String custom = request.getParameter("custom");//发送payment请求时候自定义的业务服务器订单号 log.info("itemName:"+itemName); log.info("itemNumber:"+itemNumber); log.info("paymentDate:"+paymentDate); log.info("receiverEmail:"+receiverEmail); log.info("payerEmail:"+payerEmail); log.info("paymentAmount:"+paymentAmount); log.info("paymentCurrency:"+paymentCurrency); log.info("paymentStatus:"+paymentStatus); log.info("txnId:"+txnId); log.info("custom:"+custom); String orderSn = custom.split("_")[0]; try { if ("Completed".equals(paymentStatus)) { //根据自己业务进行处理(修改订单状态,支付时间等等操作) try { log.info("paypal支付成功回调"); //表示续费,有消费记录id if (custom.split("_").length > 2 && custom.split("_")[2].matches("^-?[0-9]+")) { orderStrategyFactory.doHandler(custom.split("_")[0] + "_" + custom.split("_")[2], txnId, payerEmail, custom.split("_")[1], 2); } else { orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, custom.split("_")[1], 2); } } catch (Exception e) { log.error("paypal支付回调异常,errorMsg:{}", e.getMessage()); } } }catch (Exception e) { log.error("paypal支付回调异常,errorMsg:{}", e.getMessage()); } } @RequestMapping(value = "/paypalresult", method = RequestMethod.GET) @org.springframework.web.bind.annotation.ResponseBody public Result paypalresult(HttpServletRequest request) throws Exception { // if(!ev.equals("test-eur")){ // return Result.failure("正式环境不允许调用!"); // } String orderSn = request.getParameter("orderSn"); String txnId = request.getParameter("txnId"); String payerEmail = request.getParameter("payer_email");//付款人email String type = request.getParameter("type");//付款人email //download ,increment orderStrategyFactory.doHandler(orderSn, txnId, payerEmail, type, 2); return Result.success(); } }