Kitty 開源權限管理系統javascript
項目地址:https://gitee.com/liuge1988/kittyhtml
演示地址:http://139.196.87.48:9002/kittyjava
用戶名:admin 密碼:admingit
到螞蟻金服註冊開發者帳號,註冊地址:https://open.alipay.com,用你的 支付寶 帳號掃碼登陸,完善我的信息,選擇服務類型。web
根據狀況選擇接入方式,咱們這裏選擇自研開發者,若是已經註冊過的省略。spring
選擇 開發者中心 --> 研發服務 --> 沙箱,進入沙箱管理界面。express
進入沙箱,第一次須要填寫信息,記下appId,公鑰設置處須要把下面步驟生成的公鑰設置到這裏。apache
登陸官方地址: https://docs.open.alipay.com/291/105971,進入祕鑰生成頁面。json
打開下載的工具,運行程序,選擇 JAVA 2048 方式,點擊生成祕鑰。api
把此處生成的公鑰複製設置到沙箱環境,就是上面的設置公鑰配置,而後把公私祕鑰保存起來,以備後用。
登陸 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 for Spring 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 { // 一、得到初始化的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); // 二、設置請求參數 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); // 頁面跳轉同步通知頁面路徑 alipayRequest.setReturnUrl(returnUrl); // 服務器異步通知頁面路徑 alipayRequest.setNotifyUrl(notifyUrl); // 封裝參數 alipayRequest.setBizContent(JSON.toJSONString(alipayBean)); // 三、請求支付寶進行付款,並獲取支付結果 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 private Alipay alipay; @Override public String aliPay(AlipayBean alipayBean) throws AlipayApiException { return alipay.pay(alipayBean); } }
啓動類沒什麼,有啓動器註解就能夠了。
EpayApplication.java
package com.louis.epay; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class EpayApplication { public static void main(String[] args) { SpringApplication.run(EpayApplication.class, 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/ 版權全部,歡迎轉載,轉載請註明原文做者及出處。