支付寶藉口

java服務端–支付寶APP支付接口

java服務端–支付寶APP支付接口

1、首先設置支付寶賬戶有關信息及返回路徑

/**說明:
 *以下代碼只是爲了方便商戶測試而提供的樣例代碼,商戶可以根據自己網站的需要,按照技術文檔編寫,並非一定要使用該代碼。
 *該代碼僅供學習和研究支付寶接口使用,只是提供一個參考。
 */
public class AlipayConfig {

//↓↓↓↓↓↓↓↓↓↓請在這裏配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    //合作身份者ID,簽約賬號,以2088開頭由16位純數字組成的字符串,查看地址:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
    public static String partner = "2**************1";

    //商戶的私鑰,需要PKCS8格式,RSA公私鑰生成:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1
public static String private_key = "*************=";

    //支付寶的公鑰,查看地址:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
public static String alipay_public_key  = "*********";

    // 簽名方式
    public static String sign_type = "RSA";

    // 調試用,創建TXT日誌文件夾路徑,見AlipayCore.java類中的logResult(String sWord)打印方法。
    public static String log_path ="C://";

    // 字符編碼格式 目前支持 gbk 或 utf-8
    public static String input_charset = "utf-8";

    // 接收通知的接口名
    public static String service = "http://60.***.***.00/callbacks.do";
    //public static String service = "mobile.securitypay.pay";

    //APPID
    public static String app_id="2016**********12";

//↑↑↑↑↑↑↑↑↑↑請在這裏配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}

2、支付寶APP支付–申請支付請求參數

[申請支付請求參數說明] (https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.O57CIo&treeId=193&articleId=105465&docType=1)

@ResponseBody
@RequestMapping(value = "/alipay.do",  produces = "text/html;charset=UTF-8",method={RequestMethod.GET})
public static String alipay(String body, String subject, String out_trade_no, String total_amount) throws Exception {

         //公共參數
         Map<String, String> map = new HashMap<String, String>();
         map.put("app_id", AlipayConfig.app_id);
         map.put("method", "alipay.trade.app.pay");
         map.put("format", "json");
         map.put("charset", "utf-8");
         map.put("sign_type", "RSA");
         map.put("timestamp", UtilDate.getDateFormatter());
         map.put("version", "1.0");
         map.put("notify_url", AlipayConfig.service);

         Map<String, String> m = new HashMap<String, String>();

         m.put("body", body);
         m.put("subject", subject);
         m.put("out_trade_no", out_trade_no);
         m.put("timeout_express", "30m");
         m.put("total_amount", total_amount);
         m.put("seller_id", AlipayConfig.partner);
         m.put("product_code", "QUICK_MSECURITY_PAY");

         JSONObject bizcontentJson= JSONObject.fromObject(m);

         map.put("biz_content", bizcontentJson.toString());
        //對未簽名原始字符串進行簽名       
        String rsaSign = AlipaySignature.rsaSign(map, AlipayConfig.private_key, "utf-8");

         Map<String, String> map4 = new HashMap<String, String>();

         map4.put("app_id", AlipayConfig.app_id);
         map4.put("method", "alipay.trade.app.pay");
         map4.put("format", "json");
         map4.put("charset", "utf-8");
         map4.put("sign_type", "RSA");
         map4.put("timestamp", URLEncoder.encode(UtilDate.getDateFormatter(),"UTF-8"));
         map4.put("version", "1.0");
         map4.put("notify_url",  URLEncoder.encode(AlipayConfig.service,"UTF-8"));
         //最後對請求字符串的所有一級value(biz_content作爲一個value)進行encode,編碼格式按請求串中的charset爲準,沒傳charset按UTF-8處理
         map4.put("biz_content", URLEncoder.encode(bizcontentJson.toString(), "UTF-8"));

        Map par = AlipayCore.paraFilter(map4); //除去數組中的空值和簽名參數
        String json4 = AlipayCore.createLinkString(map4);   //拼接後的字符串

        json4=json4 + "&sign=" + URLEncoder.encode(rsaSign, "UTF-8");

        System.out.println(json4.toString());

        AliPayMsg apm = new AliPayMsg();
        apm.setCode("1");
        apm.setMsg("支付成功");
        apm.setData(json4.toString());  

        JSONObject json = JSONObject.fromObject(apm);


        System.out.println(json.toString());

        return json.toString();     

     }

支付寶請求參數組裝

3、支付寶支付結果異步通知業務處理

[支付結果異步通知] (https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.afod0z&treeId=193&articleId=105301&docType=1)

@ResponseBody
@RequestMapping(value = "/callbacks.do",  produces = "text/html;charset=UTF-8",method={RequestMethod.POST})
public String callbacks( HttpServletRequest request ) throws Exception {
        //接收支付寶返回的請求參數

        Map requestParams = request.getParameterMap();

        JSONObject json = JSONObject.fromObject(requestParams);

        String trade_status = json.get("trade_status").toString().substring(2,json.get("trade_status").toString().length()-2);
        String out_trade_no = json.get("out_trade_no").toString().substring(2,json.get("out_trade_no").toString().length()-2);
        String notify_id = json.get("notify_id").toString().substring(2,json.get("notify_id").toString().length()-2);

        System.out.println("====================================================");
        System.out.println(json.toString());
        System.out.println("支付寶回調地址!");
        System.out.println("商戶的訂單編號:" + out_trade_no);
        System.out.println("支付的狀態:" + trade_status);    

        if(trade_status.equals("TRADE_SUCCESS")) {

                /**
                 *支付成功之後的業務處理
                 */

                return "SUCCESS";
            }
        }else {

            /**
             *支付失敗後的業務處理
             */

            return "SUCCESS";

        }
        return "SUCCESS";
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章