Java spring boot 實現支付寶支付功能的示例程式碼
阿新 • • 發佈:2020-06-16
一、準備工作:
1、登陸支付寶開發者中心,申請一個開發者賬號。
地址:https://openhome.alipay.com/
2、進入研發服務:
3、點選連結進入工具下載頁面:
4、點選下載對應版本的RSA公鑰生成器:
5、生成公鑰金鑰(記錄你的應用私鑰):
6、在支付寶配置公鑰(點選儲存):
二、搭建demo
1、引入jia包:
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.9.9</version> </dependency>
2、搭建工程,目錄結構如下:
3、編寫alipay.properties配置檔案
# 您的APPID appId = 2016102200738709 # 商戶私鑰 privateKey = 您的商戶私鑰 # 支付寶公鑰 publicKey = 您的支付寶公鑰 # 伺服器非同步通知頁面路徑,需要公網能訪問到。 notifyUrl = http://公網能訪問的路徑 # 頁面跳轉同步通知頁面路徑 需要公網能訪問到。 returnUrl = http://公網能訪問的路徑 # 簽名方式 signType = RSA2 # 字元編碼格式 charset = utf-8 # 支付寶閘道器 gatewayUrl = https://openapi.alipaydev.com/gateway.do # 支付寶閘道器 logPath = "C:\\"
4、編寫AlipayBean:
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"; /** * 省略get set 方法 */ }
5、編寫Alipay:
/** * 支付寶支付介面 */ @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; } }
6、編寫AlipayProperties:
/** * 應用啟動載入檔案 */ @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); } }
7、編寫PropertiesListener:
/** * 配置檔案監聽器,用來載入自定義配置檔案 */ @Component public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { AlipayProperties.loadProperties(); } }
8、編寫PayService:
/** * 支付服務 */ public interface PayService { /** * 支付寶支付介面 * @param alipayBean * @return * @throws AlipayApiException */ String aliPay(AlipayBean alipayBean) throws AlipayApiException; }
9、編寫PayServiceImpl:
@Service public class PayServiceImpl implements PayService { @Autowired private Alipay alipay; @Override public String aliPay(AlipayBean alipayBean) throws AlipayApiException { return alipay.pay(alipayBean); } }
10、編寫OrderController:
/** * 訂單介面 * * @author Louis * @date Dec 12,2018 */ @RestController() @RequestMapping("order") public class OrderController { @Autowired private PayService payService; @RequestMapping(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); } //支付成功支付寶呼叫方法: @RequestMapping(value = "ok") public void ok(){ System.out.println("付款成功!"); } }
11、訪問頁面,輸入資訊進入支付頁面:
12、點選支付寶支付,頁面跳轉,成功!
總結
到此這篇關於Java spring boot 實現支付寶支付功能的示例程式碼的文章就介紹到這了,更多相關spring boot 支付寶支付內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!