h5网页调用支付宝支付

1,调用依赖

支付宝开放平台官方api说的比较清楚,地址为下,还有联调
https://opendocs.alipay.com/open/29ae8cb6_alipay.trade.wap.pay?pathHash=0a6313c7&ref=api&scene=21
pom文件调用

      <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.10.0.ALL</version>
        </dependency>

2,调用代码

  package com.java.sdk.demo;
  
  import com.alipay.api.AlipayApiException;
  import com.alipay.api.AlipayClient;
  import com.alipay.api.DefaultAlipayClient;
  import com.alipay.api.CertAlipayRequest;
  import com.alipay.api.AlipayConfig;
  import com.alipay.api.response.AlipayTradeWapPayResponse;
  import com.alipay.api.domain.AlipayTradeWapPayModel;
  import com.alipay.api.request.AlipayTradeWapPayRequest;
  import com.alipay.api.FileItem;
  import java.util.Base64;
  import java.util.ArrayList;
  import java.util.List;
  
  public class AlipayTradeWapPay {
  
      public Object alipay throws AlipayApiException {
          String privateKey = "<-- 当前帐号未登录,登录后会自动填充当前沙箱帐号的应用私钥 -->";
          String alipayPublicKey = "<-- 当前帐号未登录,登录后会自动填充当前沙箱帐号的支付宝公钥 -->";
          AlipayConfig alipayConfig = new AlipayConfig();
          alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
          alipayConfig.setAppId("<-- 当前帐号未登录,登录后会自动填充当前沙箱帐号的AppId -->");
          alipayConfig.setPrivateKey(privateKey);
          alipayConfig.setFormat("json");
          alipayConfig.setAlipayPublicKey(alipayPublicKey);
          alipayConfig.setCharset("UTF-8");
          alipayConfig.setSignType("RSA2");
          AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
          AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
          AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
          model.setOutTradeNo("70501111111S001111119");
          model.setTotalAmount("9.00");
          model.setSubject("大乐透");
          model.setProductCode("QUICK_WAP_WAY");
          model.setSellerId("2088102147948060");
          request.setBizModel(model);
          AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
          // 如果需要返回GET请求,请使用
          // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
          String pageRedirectionData = response.getBody();
          System.out.println(pageRedirectionData);
          if (response.isSuccess()) {
              System.out.println("调用成功");
              return response
          } else {
              System.out.println("调用失败");
              // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
              // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
              // System.out.println(diagnosisUrl);
              
          }
          return null;
      }
  }

response为一个form标签,提交给前端,前端做处理
返回的form标签代码

<form name="punchout_form" method="post" action="https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=UTF8&method=alipay.trade.wap.pay&sign=fj4PFFSGlRbwlAnnVq8RNg%2Fcbu86TXGa2f782xp2DEYZZeuZewyHke8vjXXjn5Cx4GSjcTMh6UiRROgx1oVB2SdtjcewIO4PVSGCapVP302lUzT0u3sDBpSdqqz73wM3JYDZgWLh0blLgccO2uSCcW2jbcABY8i02ff%2BOtuhMU7pGiZ989hs18rwF67B1vZveReRisUdlQezmMCHWqrADmKgIReVzw1t8rGqzZQ2vGYUrF4kRtpFpqk861A4dg01XJPjg4TCeMRELlguA6z03dN0Jm3BmH78t7mw5qQ8CD%2Fg97wD%2F3BsPm8BcVmMBNvGYR8fzrT8AN16rXtM9EeIsA%3D%3D&app_id=9021000133624051&sign_type=RSA2×tamp=2023-12-25+15%3A27%3A04&alipay_sdk=alipay-sdk-java-4.35.171.ALL&format=json"> <input type="hidden" name="biz_content" value="{"out_trade_no":"70501111111S001111119","total_amount":"9.00","subject":"大乐透","product_code":"QUICK_WAP_WAY","seller_id":"2088102147948060"}"> <input type="submit" value="立即支付" style="display:none" > </form> <script>document.forms[0].submit();</script>

3,前端处理

 $.ajax({
                       type: 'get',
                       url: 'localhost:8080/h5alipay',//后端的url
                       success: function (res) {
                            const div = document.createElement('formdiv');
                            div.innerHTML = res.body;
                            document.body.appendChild(div);
                            document.forms[0].setAttribute('target', '_self');
                            document.forms[0].submit();
                            div.remove();

                       }
                })

你可能感兴趣的:(java)