1. 程式人生 > 其它 >支付寶沙箱模擬支付

支付寶沙箱模擬支付

支付寶沙箱模擬支付

步驟

登入支付寶

登入支付寶賬號,選擇我是開發者,點選進入管理中心,選擇下方沙箱進入

沙箱管理介面如圖所示

 

 

 選擇自定義祕鑰

下載支付寶開放平臺開發助手

 

 

生成應用私鑰,公鑰,新增進去。

下載支付寶電腦網站支付 Demo

下載Demo,解壓,配置環境

 

 

AlipayConfig

package com.yjkj.framework.rbac.web;

import java.io.FileWriter;
import java.io.IOException;

/* *
 *類名:AlipayConfig
 *功能:基礎配置類
 *詳細:設定帳戶有關資訊及返回路徑
 *修改日期:2017-04-05
 *說明:
 *以下程式碼只是為了方便商戶測試而提供的樣例程式碼,商戶可以根據自己網站的需要,按照技術文件編寫,並非一定要使用該程式碼。
 *該程式碼僅供學習和研究支付寶介面使用,只是提供一個參考。
 
*/ public class AlipayConfig { //↓↓↓↓↓↓↓↓↓↓請在這裡配置您的基本資訊↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // 應用ID,您的APPID,收款賬號既是您的APPID對應支付寶賬號 public static String app_id = ""; // 商戶私鑰,您的PKCS8格式RSA2私鑰 public static String merchant_private_key = ""; // 支付寶公鑰,檢視地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。
public static String alipay_public_key = ""; // 伺服器非同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義引數,必須外網可以正常訪問 public static String notify_url = "http://localhost:8080/bargain/notify_url"; // 頁面跳轉同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義引數,必須外網可以正常訪問 public static String return_url = "http://localhost:8080/bargain/return_url
"; // 簽名方式 public static String sign_type = "RSA2"; // 字元編碼格式 public static String charset = "utf-8"; // 支付寶閘道器 public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; // 支付寶閘道器 public static String log_path = "C:\\"; //↑↑↑↑↑↑↑↑↑↑請在這裡配置您的基本資訊↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ /** * 寫日誌,方便測試(看網站需求,也可以改成把記錄存入資料庫) * * @param sWord * 要寫入日誌裡的文字內容 */ public static void logResult(String sWord) { FileWriter writer = null; try { writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis() + ".txt"); writer.write(sWord); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

引入依賴包

<dependency>
    <groupId>com.pentahohub.nexus</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>20150820220052</version>
</dependency>

支付呼叫介面

/**
     * 快捷支付呼叫支付寶支付介面
     */
    @RequestMapping("alipaySum")
        // subject   商品名稱
        // payables 交易金額
        // body       商品描述
    public Object alipayIumpSum(Model model, String payables, String subject, String body, HttpServletResponse response)
            throws Exception {
        // 獲得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigInfo.gatewayUrl, AlipayConfigInfo.app_id,
                AlipayConfigInfo.merchant_private_key, "json", AlipayConfigInfo.charset,
                AlipayConfigInfo.alipay_public_key, AlipayConfigInfo.sign_type);
        // 設定請求引數
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfigInfo.return_url);
        alipayRequest.setNotifyUrl(AlipayConfigInfo.notify_url2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        // 商戶訂單號,商戶網站訂單系統中唯一訂單號,必填
        String out_trade_no = sdf.format(new Date());
        String total_amount = payables.replace(",", "");
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
                + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        // 請求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // System.out.println(result);
        AlipayConfigInfo.logResult(result);// 記錄支付日誌
        response.setContentType("text/html; charset=utf-8");// 設定字串編碼  預設的gdk傳入中文會報錯
        PrintWriter out = response.getWriter();
        out.print(result);
        return null;
    }
    

非同步回撥

/**
     * 支付完成回撥驗證操作
     */
    @RequestMapping("notify_url")
    public void Notify(HttpServletResponse response, HttpServletRequest request) throws Exception {
        System.out.println("----------------------------notify_url------------------------");
        // 商戶訂單號
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "GBK");
        // 付款金額
        String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "GBK");
        // 支付寶交易號
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "GBK");
        // 交易說明
        String cus = new String(request.getParameter("body").getBytes("ISO-8859-1"), "GBK");
        // 交易狀態
        String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "GBK");
        if (trade_status.equals("TRADE_SUCCESS")) {//支付成功商家操作
            //根據你的操作自行編寫
            /*Map<Object, Object> map = new HashMap<Object, Object>();
            map.put("cuId", Integer.valueOf(cus));
            RepaymentPlan repaymentPlan = new RepaymentPlan();
            Integer id = Integer.valueOf(out_trade_no);
            double payablesCheck = Double.valueOf(total_amount);
            RepaymentPlan repayCheck = serviceMain.selectByPrimaryKey(id);
            double total = repayCheck.getPayables();
            if (Double.valueOf(total_amount) < repayCheck.getPayables()) {
                map.put("ubalance", total - Double.valueOf(total_amount));
                serviceMain.updateCusMoney(map);
            }
            repaymentPlan.setId(id);
            repaymentPlan.setActualPayment(total);
            repaymentPlan.setRepaymentStatus(1);
            int i = serviceMain.updateByPrimaryKeySelective(repaymentPlan);
            System.out.println("---------------------還款影響行數----------------------------" + i);*/
        }
    }

返回介面

/**
     * 同步通知的頁面的Controller
     * 我這邊就簡單的返回了一個頁面
     * @param request,response
     * @throws InterruptedException
     */
    @RequestMapping("return_url")
    public String Return_url() throws InterruptedException {
        return "jsp/foreground/Welcome";
    }

 呼叫成功

這個賬戶和密碼可以在你自己的沙箱環境中的沙箱賬號獲取賬號名和密碼