下面示例使用的是支付宝的网页支付,最终的效果如图:
1.前置条件
对接支付宝你需要了解的知识点
1.加密算法
对称加密和非对称加密,RSA2加密算法,签名验证
2.支付宝平台openid unionId的概念
https://opendocs.alipay.com/pre-open/06z4jd?sourceCode=SC00004290&projectCode=1964001821&sceneVersion=2
3.内网开发内网穿透(本地调试)
下载地址https://natapp.cn/
使用教程:https://natapp.cn/article/natapp_newbie
异常情况;
这种情况属于已经同了,但是本地端口号对应的服务没启动或者在natapp服务端配置的端口号和本地的对应不上
4.支付的整体流程
支付宝开放平台登记信息-》填写
应用公钥信息 用于给支付宝 支付宝拿着去做验证
获取支付宝公钥信息,用于对支付的回调接口做签名签证
获取支付宝网关信息 用于发起支付调用支付宝进行支付
5.沙箱环境的使用
https://opendocs.alipay.com/common/02kkv7
6.SDK的下载
https://opendocs.alipay.com/open/02np94?pathHash=647fc236
引入依赖即可
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.34.0.ALL</version></dependency>
7.示例代码的下载
1.
2.
3.
8.api文档
https://opendocs.alipay.com/open/29ae8cb6_alipay.trade.wap.pay?scene=21&pathHash=1ef587fd
9.常见的异常错误码
https://opensupport.alipay.com/support/FAQ/d6f3097d-14a9-4edc-a488-4e5a1d6aaf8a
2.细节流程
1.创建自己的产品
在控制台选择你需要开发的产品
这里选择网页移动应用,点击创建网页/移动应用
配置完之后进入应用详情
2.配置基础信息
1.配选择接口加签名方式,
证书或者签名这里选择签名。
按照操作配置签名
2.配置公钥和私钥
如何生成公钥和私钥?
https://opendocs.alipay.com/support/01rauw
3.配置回调地址
附支付接口代码
demo
package com.css.tom.service;/*** @author weiwensi* @version 1.0-SNAPSHOT* @since 2024/7/6 21:23*/import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.domain.InvoiceKeyInfo;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.domain.InvoiceInfo;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.SubMerchant;import com.alipay.api.FileItem;
import org.springframework.stereotype.Service;import java.util.Base64;
import java.util.ArrayList;
import java.util.List;
@Service
public class AlipayTradePagePay {public String pay() throws AlipayApiException {// 初始化SDKAlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());// 构造请求参数以调用接口AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();AlipayTradePagePayModel model = new AlipayTradePagePayModel();// 设置商户门店编号model.setStoreId("NJ_001");// 设置订单绝对超时时间model.setTimeExpire("2024-07-07 19:05:01");// 设置业务扩展参数ExtendParams extendParams = new ExtendParams();//个人中心 mapi网关产品密钥 沙箱环境-》沙箱账号 2088721033555793/* extendParams.setSysServiceProviderId("2088612340891761");extendParams.setHbFqSellerPercent("100");extendParams.setHbFqNum("3");extendParams.setIndustryRefluxInfo("{\"scene_code\":\"metro_tradeorder\",\"channel\":\"xxxx\",\"scene_data\":{\"asset_name\":\"ALIPAY\"}}");extendParams.setSpecifiedSellerName("tom的跨境小铺");extendParams.setRoyaltyFreeze("true");extendParams.setCardType("S0JP0000");model.setExtendParams(extendParams);*/// 设置订单标题model.setSubject("Iphone6 16G");// 设置请求来源地址//model.setRequestFromUrl("https://");// 设置产品码model.setProductCode("FAST_INSTANT_TRADE_PAY");// 设置PC扫码支付的方式model.setQrPayMode("1");// 设置商户自定义二维码宽度model.setQrcodeWidth(100L);// 设置请求后页面的集成方式model.setIntegrationType("PCWEB");// 设置订单包含的商品列表信息List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();GoodsDetail goodsDetail0 = new GoodsDetail();goodsDetail0.setGoodsName("ipad");goodsDetail0.setAlipayGoodsId("20010001");goodsDetail0.setQuantity(1L);goodsDetail0.setPrice("2000");goodsDetail0.setGoodsId("apple-01");goodsDetail0.setGoodsCategory("34543238");goodsDetail0.setCategoriesTree("124868003|126232002|126252004");goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");goodsDetail.add(goodsDetail0);model.setGoodsDetail(goodsDetail);// 设置商户的原始订单号model.setMerchantOrderNo("20161008001");// 设置二级商户信息SubMerchant subMerchant = new SubMerchant();/*subMerchant.setMerchantId("2088000603999128");subMerchant.setMerchantType("alipay");model.setSubMerchant(subMerchant);*/// 设置开票信息/* InvoiceInfo invoiceInfo = new InvoiceInfo();InvoiceKeyInfo keyInfo = new InvoiceKeyInfo();keyInfo.setTaxNum("1464888883494");keyInfo.setIsSupportInvoice(true);keyInfo.setInvoiceMerchantName("ABC|003");invoiceInfo.setKeyInfo(keyInfo);invoiceInfo.setDetails("[{\"code\":\"100294400\",\"name\":\"服饰\",\"num\":\"2\",\"sumPrice\":\"200.00\",\"taxRate\":\"6%\"}]");model.setInvoiceInfo(invoiceInfo);*/// 设置商户订单号model.setOutTradeNo("20150320010101001");// 设置外部指定买家/*ExtUserInfo extUserInfo = new ExtUserInfo();extUserInfo.setCertType("IDENTITY_CARD");extUserInfo.setCertNo("2088722033555808");extUserInfo.setName("李明");extUserInfo.setMobile("16587658765");extUserInfo.setMinAge("18");extUserInfo.setNeedCheckInfo("F");extUserInfo.setIdentityHash("27bfcd1dee4f22c8fe8a2374af9b660419d1361b1c207e9b41a754a113f38fcc");model.setExtUserInfo(extUserInfo);*/// 设置订单总金额model.setTotalAmount("88.88");// 设置商户传入业务信息// model.setBusinessParams("{\"mc_create_trade_ip\":\"127.0.0.1\"}");// 设置优惠参数//model.setPromoParams("{\"storeIdType\":\"1\"}");request.setBizModel(model);// 第三方代调用模式下请设置app_auth_token// request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");// 如果需要返回GET请求,请使用// AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");return response.getBody();}private static AlipayConfig getAlipayConfig() {String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCZbzZ/n0DMKfr1y+0YvL2Q38R7HEV4r3AmzVisteCzqpW9Q86dPLlVsUOTSRqT9TMkHsLWd9dZ0YstDJf69PuKsAan6/G0SorfXEXRUAKJtJXq9BKtcNr50XQlirUSj77ziwc6XZfFyRphgcmu0HzRfXJQQnrGGCiSlW1yYxRF6EL9mypi/dHErARyu/qNu1K/kdf+hyrSvAhbvANL4M3RkOjssvFjpXFY+ggQgFdqVp1VTbs6G5/mSWqsSJAiH1DsDQhVmfzQaIoTms8j1gTklBjrZKMq+DBbhm8B5R2PunSwtYFWLgkCkgGUU5me4SAXQRDESV92jsDOxqb6G3WvAgMBAAECggEAblGxPKezflg80hgS3HNwVUcezWmxZam2mJ+Sv3NyIZxoV5H2b8WmeD+hF7g7PODTKYhYjL3UAAeS6wwgBle4uKoBzyDtQ7VJDnoY1PuTJ5HJLQralwwK1g5MVWXa9YKXSCLRSpzasGH87tjTuza6WCG7XIvAHMhn+6Um8VTd0bil0gOYQyJDuzombAxWGbSCv4PfJBMO7xT2HtFxejpCI9wPkJ5hnZ2PzPplmciivQMIWpxZCssDjsAdpjjWH/ETaXr141uD6V+QVQmlWjfCDVCwFGNxJIBkkDmUOE+czyp+q+ssH877F/OYon1s/RuHVli8WRllXjqz167Ov1WveQKBgQDJH+vREzjrOZkQEHbzcQBKuSqnmW+fjdUvfdgCW79JMTn9UN2OQCcgP4B23VqMIKz6oJZKCiPgLQoFeOx62jj4NrR9fZEK7SbHg/uxqjevTvzX1qJQjqFwYGeNvuaks7YGDhaQRHl31G0uZsGTPgcS0r2rn5XVkUuFYDrvYXeKDQKBgQDDTD2Zsou8tTYu1aMNhdHc7+nLw1WEI/lW4HfZ0s+5lbefTRGb23mX6dVYnExyHum64kiEREUZewWZSec8z6/IY0wgWRWBDHIpoRaTSVuu2F9b9PPXCTnbmn3e0q1Blk07nQU90Zjz3indBvmuOPNHiQrgAe1eVlSDeXDDOiZ7qwKBgQDIYcxiRwOLoJw2XCEXVLuUtz2fNAj5b6c6JOhlGSHQ+4GqN7o18wbIY1xLkSiYuil9nyd9ZvLtdBIq95qsn+TosCBNcyBiQXtEhuPq1Auc11akeMzI9AI2Y2Zo3A0RhZdSGxEx9XcMA5IwC+LLH56Oi3a7mkj/Oj6DCShOvc4KtQKBgDNsF6wofvKpJCMgzVX6Lk/mO8ETJaktb7GYWPZZs6hB659eQ4yuNU3ym+JBBT/nM9OpUT7CWw9ifQ7njqNMbBRvUNQToL3h8Q1VBQSzBBj9j94+mRW9sCZKf2n0PvpOAPf5WC/2yDcuAAfallNxW10ykFJ7L2zvfkE/8Jj27rsRAoGAcbr5OLgcxOg5GzK2dNRJWzzif3TU35dX6H+aI0jG8d9ftxNhmEnKGA4ahKmYuCXsk3xpJ+c/iqVIitOnRxqlPCPAE9UyOOFNCYIXx+t2QGuKrX446PF7b0ROQKwPJ4M0ZgJIAkwcwRLbM0Mpgn098dZTEQ+IHAeYxcPuLt75xHU=";String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5cKTEguXYKSskkbSXMNxC8rS7Qn4OibXrFIJwxh3/lw+c2/dtBDhl/6Mh1XM1m598UBRgmTvz4cpuYhYqrYgWEritfPEyZe6GU1q6+BKPrDZDX4oRgfFJtXRJfMI6C3otcEpSxkNk1R5UhAADRj8wIoAgVUHo8kZORhRdPLozK8X4/VpMlffdwz+HNvuz8ZfKRFcieNkhFJm3UkctF10ZFPjekkCdFazbqb7mxpA4XL0e1BLLANV57IktOFpwHpUA0YZ3vnSncC+zRJNm4dsl091fszlDFbwLAaXYYLjZQW9MU6ziXnfqXtRseyu1S6JP6RsP7IrXjLRBLAhmCiW+QIDAQAB";AlipayConfig alipayConfig = new AlipayConfig();alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");alipayConfig.setAppId("9021000136613031");alipayConfig.setPrivateKey(privateKey);alipayConfig.setFormat("json");alipayConfig.setAlipayPublicKey(alipayPublicKey);alipayConfig.setCharset("UTF-8");alipayConfig.setSignType("RSA2");return alipayConfig;}
}