支付宝借口

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";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值