content: package com.example.pay.controller;
  
  import com.alipay.api.AlipayApiException;
  import com.alipay.api.AlipayClient;
  import com.alipay.api.domain.AlipayTradeWapPayModel;
  import com.alipay.api.internal.util.AlipaySignature;
  import com.alipay.api.request.AlipayTradeWapPayRequest;
  import com.example.pay.configuration.AlipayProperties;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.UUID;
  
  /**
   * 支付宝-手机网站支付.
   */
  @Slf4j
  @Controller
  @RequestMapping("/alipay/wap")
  public class AlipayWAPPayController {
  
      @Autowired
      private AlipayProperties alipayProperties;
  
      @Autowired
      private AlipayClient alipayClient;
  
      /**
       * 去支付
       *
       * 支付宝返回一个form表单，并自动提交，跳转到支付宝页面
       *
       * @param response
       * @throws Exception
       */
      @PostMapping("/gotoPayPage")
      public void gotoPayPage(HttpServletResponse response) throws AlipayApiException, IOException {
          // 订单模型
          String productCode="QUICK_WAP_WAY";
          AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
          model.setOutTradeNo(UUID.randomUUID().toString());
          model.setSubject("支付测试");
          model.setTotalAmount("0.01");
          model.setBody("支付测试，共0.01元");
          model.setTimeoutExpress("5m");
          model.setProductCode(productCode);
  
          AlipayTradeWapPayRequest wapPayRequest =new AlipayTradeWapPayRequest();
          wapPayRequest.setReturnUrl("http://yxep7y.natappfree.cc/alipay/wap/returnUrl");
          wapPayRequest.setNotifyUrl(alipayProperties.getNotifyUrl());
          wapPayRequest.setBizModel(model);
  
          // 调用SDK生成表单, 并直接将完整的表单html输出到页面
          String form = alipayClient.pageExecute(wapPayRequest).getBody();
          System.out.println(form);
          response.setContentType("text/html;charset=" + alipayProperties.getCharset());
          response.getWriter().write(form);
          response.getWriter().flush();
          response.getWriter().close();
      }
  
  
      /**
       * 支付宝页面跳转同步通知页面
       * @param request
       * @return
       * @throws UnsupportedEncodingException
       * @throws AlipayApiException
       */
      @RequestMapping("/returnUrl")
      public String returnUrl(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, AlipayApiException {
          response.setContentType("text/html;charset=" + alipayProperties.getCharset());
  
          //获取支付宝GET过来反馈信息
          Map<String,String> params = new HashMap<>();
          Map requestParams = request.getParameterMap();
          for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
              String name = (String) iter.next();
              String[] values = (String[]) requestParams.get(name);
              String valueStr = "";
              for (int i = 0; i < values.length; i++) {
                  valueStr = (i == values.length - 1) ? valueStr + values[i]
                          : valueStr + values[i] + ",";
              }
              //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
              valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
              params.put(name, valueStr);
          }
  
          boolean verifyResult = AlipaySignature.rsaCheckV1(params, alipayProperties.getAlipayPublicKey(), alipayProperties.getCharset(), "RSA2");
          if(verifyResult){
              //验证成功
              //请在这里加上商户的业务逻辑程序代码，如保存支付宝交易号
              //商户订单号
              String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
              //支付宝交易号
              String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
  
              return "wapPaySuccess";
  
          }else{
              return "wapPayFail";
  
          }
      }
  }
  