支付寶支付接入
轉:https://www.cnblogs.com/xifengxiaoma/p/10107635.html
開源專案連結
Kitty 開源許可權管理系統
專案地址:https://gitee.com/liuge1988/kitty
演示地址:http://139.196.87.48:9002/kitty
使用者名稱:admin 密碼:admin
註冊沙箱賬號
到螞蟻金服註冊開發者賬號,註冊地址:https://open.alipay.com,用你的支付寶賬號掃碼登入,完善個人資訊,選擇服務型別。
根據情況選擇接入方式,我們這裡選擇自研開發者,如果已經註冊過的省略。
選擇 開發者中心 --> 研發服務 --> 沙箱,進入沙箱管理介面。
進入沙箱,第一次需要填寫資訊,記下appId,公鑰設定處需要把下面步驟生成的公鑰設定到這裡。
生成公私祕鑰
登入官方地址:https://docs.open.alipay.com/291/105971,進入祕鑰生成頁面。
開啟下載的工具,執行程式,選擇 JAVA 2048 方式,點選生成祕鑰。
把此處生成的公鑰複製設定到沙箱環境,就是上面的設定公鑰配置,然後把公私祕鑰儲存起來,以備後用。
建立專案
登入 https://start.spring.io/,輸入專案資訊,生成 Spring Boot 專案並下載到本地。
使用開發工具匯入專案,我們這裡使用的是 Eclipse ,匯入和清理後項目結構如下圖。
新增依賴
新增專案依賴,主要是引入alipay-sdk-java,提供支付寶支付支援。
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.louis</groupId> <artifactId>epay-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>epay-service</name> <description>Demo project forSpring Boot</description><properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- alipay --> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.1.0</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.48</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
配置檔案
在 resources 目錄下新增一個alipay.properties 檔案,用於寫入支付寶配置資訊。
alipay.properties
# 應用ID,您的APPID,收款賬號既是您的APPID對應支付寶賬號 appId: 2016092300579321 # 商戶私鑰,您的PKCS8格式RSA2私鑰 privateKey: MIIEvgIBADANBgkcbkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCkRbVEFHxPkl754hwz4w9iraA62H3/GjZ6RwHa+8LBy2mNOg89IaodAUd99ojYEDgnVxg8U9RhXF4COsm6Q+APy7YpCpu5GJv6X9HJYLXifyxc5gfr48CdR6JQDx2R5/aqMro0Dd69R3HOuZj5rctpVHs7gIvsSn5PV53OCOgLEWzbtcY3DKLkByevmoXVdnBDHRZKqv1H6N8i83YzNRVx6fr3IHZGiIxVbuEHUn5iT1FBArQVuHLki5JaM6nwWQIC2a0T2Yokp1kjqhFoNp08sgDZnlEtQP9SjoeQMrWO0mFI7Jb3ooMLqJwNsdzy0Bwonh0zfrltPwzYcs2d9vTtAgMBAAECggEAW+Kp+8TBq/ihq4Bwag2N45Je18uU3oaZBliFjynXzHKtPdPGBlm4P7UhejHWZZTc9BRheA0j9Lmn0/NyNwAUaoF2CQroA+biCHBkdhHkaybbd7MrpySj7tXM0p9B2QU5EgaB5PAFMTavUAS8rpwp9WbN74B9w9P/Rv4pRL/ym41iYVxFJ3r59GDOZ/GMYzcNrFi3Io5I7LSPBR/DP2XR/Z0V2ahZ5cVBxea0rIDum+veioknLmwglzmxI9/V8C+PkgryjsfokTlH2FWx+XBbhzfcelCq50QLdHSSLAR4u2kuWv1aiswNYBaEFJ7Ir0TUCa4rYzVUkfHb1W4wTHSYwQKBgQDo7btJatR/QfI0c+TmVVcoHdS9QimcJq5dDhccePH7ocHmK5NDabtB1UgzUBHTg/ePNWhrFzuFSV3GN3XZAMkxRNA5YxBnd9R27cI8zzo9ph+BRgInc79usKE4uM1kt3s/8+Tdlx8mMh31ssxy3PrznBec9W663Uz/tpxM/WanlQKBgQC0ixaHjdP8eW7Hrdj8KnBeHqxsrQMUfH37/LTZcnbvuIDGaFCntYTR3C0HjoOIi7mbbIV584A3nxoEhfSt+fTo6GxHHCkF5TIE2ewsCWOxrMGKhoPWibuJZOe+qBXTICaOCR80sKa5zHu3zkjymQcKBxHiLtVgbwW/CLYSt4Th+QKBgQDaps84i+AN1YnXUzN8RIAcWGRrhMAqpOTo1WE8iQweeinaNu12SrpNgjjUckVJmIe7Fxd978EfzU8J0uX9Xo9+gGo2dJfhiMsZGPdMvfqeBGNuppk/D5iT/5pX9KJZ+SLpVblxiXrkEAevrLfe2zF3nP9Nh9b58uNk57axTu4eQQKBgE83yEbqQF3DmowbB959cJibtORqdbODfHQYyfGve+hreHWWR+2OhBzhExEBw00ioepEj7yWz2eYc/4QGPBNgNzBPuFkxctEadIfHLWl2QyY1MNHiomUHamHkPfjINBmhwRDlGG2MTHNO2vHI9Luulv4BizMh+usS0UrOVi1FaVhAoGBAJGhjac7FODXn2cDQY4Rqf0cw8wUy4+XISRlHfRBt51mogZ/9xyfY4A/S5bkKwpzMAXtaF4r6URYj4tP5wFE48ANepHVDnXs4Bj/x+/NsgbLyiK0Mo2fL2WchYxWym8pzsifvWvrxWVI5pGtJRVl0xKN9KsJjwfRsFKS0YSzM0av # 支付寶公鑰,檢視地址:https://openhome.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。 publicKey: MIIBIjANBnbqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApEW1RBR8T5Je+eIcM+MPYq2gOth9/xo2ekcB2vvCwctpjToPPSGqHQFHffaI2BA4J1cYPFPUYVxeAjrJukPgD8u2KQqbuRib+l/RyWC14n8sXOYH6+PAnUeiUA8dkef2qjK6NA3evUdxzrmY+a3LaVR7O4CL7Ep+T1edzgjoCxFs27XGNwyi5Acnr5qF1XZwQx0WSqr9R+jfIvN2MzUVcen69yB2RoiMVW7hB1J+Yk9RQQK0Fbhy5IuSWjOp8FkCAtmtE9mKJKdZI6oRaDadPLIA2Z5RLUD/Uo6HkDK1jtJhSOyW96KDC6icDbHc8tAcKJ4dM365bT8M2HLNnfb07QIDAQAB # 伺服器非同步通知頁面路徑需http://格式的完整路徑,不能加?id=123這類自定義引數 notifyUrl: http://外網ip:埠/error.html # 頁面跳轉同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義引數 returnUrl: http://外網ip:埠/sccess.html # 簽名方式 signType: RSA2 # 字元編碼格式 charset: utf-8 # 支付寶閘道器 gatewayUrl: https://openapi.alipaydev.com/gateway.do # 支付寶閘道器 logPath: "C:\\"
增加一個PropertiesListener 監聽器用於在應用啟動時載入配置檔案屬性。
PropertiesListener.java
package com.louis.epay.config;import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;import com.louis.epay.alipay.AlipayProperties;
/**
配置檔案監聽器,用來載入自定義配置檔案
@author Louis
@date Dec 12, 2018
*/
@Component
public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
AlipayProperties.loadProperties();
}
}
上面的監聽器載入屬性,是通過具體的屬性載入器載入的,比如支付寶支付屬性載入類如下。
AlipayProperties.java
package com.louis.epay.alipay;import java.util.HashMap;
import java.util.Map;
import java.util.Properties;import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;/**
應用啟動載入檔案
@author Louis
@date Dec 12, 2018
*/
@Component
public class AlipayProperties {public static final String APP_ID = "appId";
public static final String PRIVARY_KEY = "privateKey";
public static final String PUBLIC_KEY = "publicKey";
public static final String NOTIFY_URL = "notifyUrl";
public static final String RETURN_URL = "returnUrl";
public static final String SIGN_TYPE = "signType";
public static final String CHARSET = "charset";
public static final String GATEWAY_URL = "gatewayUrl";
public static final String LOG_PATH = "logPath";/**
- 儲存載入配置引數
*/
private static Map<String, String> propertiesMap = new HashMap<String, String>();/**
- 載入屬性
*/
public static void loadProperties() {
// 獲得PathMatchingResourcePatternResolver物件
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
// 載入resource檔案(也可以載入resources)
Resource resources = resolver.getResource("classpath:alipay.properties");
PropertiesFactoryBean config = new PropertiesFactoryBean();
config.setLocation(resources);
config.afterPropertiesSet();
Properties prop = config.getObject();
// 迴圈遍歷所有得鍵值對並且存入集合
for (String key : prop.stringPropertyNames()) {
propertiesMap.put(key, (String) prop.get(key));
}
} catch (Exception e) {
new Exception("配置檔案載入失敗");
}
}/**
- 獲取配置引數值
- @param key
- @return
*/
public static String getKey(String key) {
return propertiesMap.get(key);
}public static String getAppId() {
return propertiesMap.get(APP_ID);
}public static String getPrivateKey() {
return propertiesMap.get(PRIVARY_KEY);
}public static String getPublicKey() {
return propertiesMap.get(PUBLIC_KEY);
}public static String getNotifyUrl() {
return propertiesMap.get(NOTIFY_URL);
}public static String getReturnUrl() {
return propertiesMap.get(RETURN_URL);
}public static String getSignType() {
return propertiesMap.get(SIGN_TYPE);
}public static String getCharset() {
return propertiesMap.get(CHARSET);
}public static String getGatewayUrl() {
return propertiesMap.get(GATEWAY_URL);
}public static String getLogPath() {
return propertiesMap.get(LOG_PATH);
}}
支付寶支付介面封裝
Alipay 是對支付寶支付功能的封裝,它接收一個AlipayBean 為引數,最終通過呼叫AlipayClient 的pageExecute 方法返回支付頁面。
Alipay.java
package com.louis.epay.alipay;import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;/**
支付寶支付介面
@author Louis
@date Dec 12, 2018
*/
@Component
public class Alipay {/**
- 支付介面
- @param alipayBean
- @return
- @throws AlipayApiException
*/
public String pay(AlipayBean alipayBean) throws AlipayApiException {
// 1、獲得初始化的AlipayClient
String serverUrl = AlipayProperties.getGatewayUrl();
String appId = AlipayProperties.getAppId();
String privateKey = AlipayProperties.getPrivateKey();
String format = "json";
String charset = AlipayProperties.getCharset();
String alipayPublicKey = AlipayProperties.getPublicKey();
String signType = AlipayProperties.getSignType();
String returnUrl = AlipayProperties.getReturnUrl();
String notifyUrl = AlipayProperties.getNotifyUrl();
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
// 2、設定請求引數
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
// 頁面跳轉同步通知頁面路徑
alipayRequest.setReturnUrl(returnUrl);
// 伺服器非同步通知頁面路徑
alipayRequest.setNotifyUrl(notifyUrl);
// 封裝引數
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
// 3、請求支付寶進行付款,並獲取支付結果
String result = alipayClient.pageExecute(alipayRequest).getBody();
// 返回付款資訊
return result;
}
}
介面呼叫引數封裝物件如下。
AlipayBean.java
package com.louis.epay.alipay;/**
支付實體物件
根據支付寶介面協議,其中的屬性名,必須使用下劃線,不能修改
@author Louis
@date Dec 12, 2018
*/
public class AlipayBean {/**
- 商戶訂單號,必填
*/
private String out_trade_no;
/**
- 訂單名稱,必填
*/
private String subject;
/**- 付款金額,必填
- 根據支付寶介面協議,必須使用下劃線
*/
private String total_amount;
/**- 商品描述,可空
*/
private String body;
/**- 超時時間引數
*/
private String timeout_express= "10m";
/**- 產品編號
*/
private String product_code= "FAST_INSTANT_TRADE_PAY";public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTotal_amount() {
return total_amount;
}
public void setTotal_amount(String total_amount) {
this.total_amount = total_amount;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getTimeout_express() {
return timeout_express;
}
public void setTimeout_express(String timeout_express) {
this.timeout_express = timeout_express;
}
public String getProduct_code() {
return product_code;
}
public void setProduct_code(String product_code) {
this.product_code = product_code;
}}
訂單業務封裝
訂單業務提供支付寶支付介面alipay,內部通過呼叫PayService 完成訂單支付。
OrderController.java
package com.louis.epay.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.alipay.api.AlipayApiException;
import com.louis.epay.alipay.AlipayBean;
import com.louis.epay.service.PayService;/**
訂單介面
@author Louis
@date Dec 12, 2018
*/
@RestController()
@RequestMapping("order")
public class OrderController {@Autowired
private PayService payService;/**
- 阿里支付
- @param tradeNo
- @param subject
- @param amount
- @param body
- @return
- @throws AlipayApiException
*/
@PostMapping(value = "alipay")
public String alipay(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
AlipayBean alipayBean = new AlipayBean();
alipayBean.setOut_trade_no(outTradeNo);
alipayBean.setSubject(subject);
alipayBean.setTotal_amount(totalAmount);
alipayBean.setBody(body);
return payService.aliPay(alipayBean);
}
}
PayService 封裝了 Alipay, 統一對外提供的支付服務介面。
PayService.java
package com.louis.epay.service;import com.alipay.api.AlipayApiException;
import com.louis.epay.alipay.AlipayBean;/**
支付服務
@author Louis
@date Dec 12, 2018
*/
public interface PayService {/**
- 支付寶支付介面
- @param alipayBean
- @return
- @throws AlipayApiException
*/
String aliPay(AlipayBean alipayBean) throws AlipayApiException;}
支付服務的實現類,通過對各種支付程式碼的呼叫,統一對外提供支付服務。
PayServiceImpl.java
package com.louis.epay.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.alipay.api.AlipayApiException;
import com.louis.epay.alipay.Alipay;
import com.louis.epay.alipay.AlipayBean;
import com.louis.epay.service.PayService;@Service
public class PayServiceImpl implements PayService {@Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Alipay alipay; @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> String aliPay(AlipayBean alipayBean) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> AlipayApiException { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> alipay.pay(alipayBean); }
}
啟動類沒什麼,有啟動器註解就可以了。
EpayApplication.java
package com.louis.epay;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class EpayApplication {</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { SpringApplication.run(EpayApplication.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args); }
}
測試頁面
在 static 目錄下建立一個index.html 頁面,用於輸入訂單資訊,並進行支付。
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <H1>支付測試</H1> <hr> <div class="form-container"> <form id="form" action="order/alipay" method="post"> *商戶訂單 : <input type="text" name="outTradeNo" value="dzcp100010001"><br> *訂單名稱 : <input type="text" name="subject" value="紅魔手機"><br> *付款金額 : <input type="text" name="totalAmount" value="0.1" ><br> *商品描述 : <input type="text" name="body" value="紅魔手機 努比亞出品遊戲手機"><br> <input type="button" value="支付寶支付" onclick="submitForm('order/alipay')"> <input type="button" value=" 微信支付 " onclick="submitForm('order/wexpay')"> </form> </div> </body><script language="javascript">
function submitForm(action) {
document.getElementById("form").action = action
document.getElementById("form").submit()
}
</script><style>
.form-container {
padding-top:10px;
}
input {
margin:10px;}
</style>
</html>
測試效果
根據需要修改啟動埠,我這裡調整為 9000。
application.yml
server:
port: 9000
訪問地址:http://localhost:9000/,顯示訂單支付頁面。
點選支付寶支付,呼叫 order/alipay 介面,如果呼叫成功,則返回支付寶支付頁面。
到此,支付寶支付的實現案例就完成了。
原始碼下載
碼雲:https://gitee.com/liuge1988/epay.git
作者:朝雨憶輕塵
出處:https://www.cnblogs.com/xifengxiaoma/
版權所有,歡迎轉載,轉載請註明原文作者及出處。