Java spring boot 實現支付寶支付功能的示例代碼
一、準備工作:
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配置文件
# 您的APPIDappId = 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:
/** * 支付寶支付接口 */@Componentpublic 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:
/** * 應用啟動加載文件 */@Componentpublic 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:
/** * 配置文件監聽器,用來加載自定義配置文件 */@Componentpublic 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:
@Servicepublic 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 支付寶支付內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: