java第三方支付--支付宝

首先将支付宝的jar包导入到maven工程中,具体操作请参考 maven导入集中仓储中没有的jar包到本地仓储。

然后代码如下,与支付宝官方给出的demo基本相同,重点在于

 AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", BasePayConstants.ALIPAY_PARTNER, BasePayConstants.RSA_PRIVATE_KEY, "json", BasePayConstants.ALIPAY_CHARSET, BasePayConstants.RSA_PUBLIC_KEY, "RSA2");

这段代码中的参数。

1、https://openapi.alipay.com/gateway.do 为固定字段,参考支付宝开发者文档即可。

2、BasePayConstants.ALIPAY_PARTNER   为合作商户ID,这个可以在支付宝商户信息中查询支付宝地址

3、BasePayConstants.RSA_PRIVATE_KEY  这是用户私钥,java代码是pkcs8格式的,一定要在支付宝中的应用公钥来校验一下,具体如何生成请参考(支付宝如何生成商户公钥与私钥)。

4、BasePayConstants.RSA_PUBLIC_KEY 只是支付宝公钥,在商户信息中可以查询。

下面代码执行成功后:

alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2017052007295052&biz_content=%7B%22body%22%3A%22%5C%5Cu5728%5C%5Cu7ebf%5C%5Cu652f%5C%5Cu4ed8%5C%5Cu9ad8%5C%5Cu901f%5C%5Cu901a%5C%5Cu884c%5C%5Cu8d39%22%2C%22out_trade_no%22%3A%22com.sddg.appapi.server.escrow.MakeOrderNum%4034f2336f%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%5C%5Cu652f%5C%5Cu4ed8%5C%5Cu8f66%5C%5Cu8d39%22%2C%22timeout_express%22%3A%2290m%22%2C%22total_amount%22%3A%220.1%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Fn52prx.natappfree.cc%2Falipay%2FtradeCallback&sign=gfpmKSNRVehKRsNavUXmqLoqoHUMve7Vx1Ew7xOuCVTVV%2FHEdYq8W3R01uNBouhznFAWWULA5B1XMTHJhuUCqKa1h2cvzdrBlYqog18oW0ewCgeS5Tnznat4HzrB4%2F%2BCc8qiDWW%2FrbvdSn0l1cHVcyvcTmTdf2jwyIYPsJ9BK1A4DW0c1vPKXaEoSHTJ1z4uCUKLdCV9OBK3pPsaF%2FPAKo8ZnoAlAw3Fi59vZLmMokIWbeVNBc1%2BohdNNM61tPdjES7PCW04uoBK6NBaiV1JPy0ynuDxIfRgdKJuB4pF6xuaUmGG5Ri6qMByLLvayy0oLN7WiLfKZrlb6EJsdaDiXw%3D%3D&sign_type=RSA2×tamp=2017-06-06+14%3A41%3A42&version=1.0

public class AliPayApi {
    /*支付宝支付支付动作流程:
    1、请求参数按照key=value&key=value方式拼接的未签名原始字符串
    2、再对原始字符串进行签名
    3、对请求字符串的所有一级value(biz_content作为一个value)进行encode(编码格式按请求串中的charset为准)
    */
    Logger logger = LoggerFactory.getLogger(getClass());
    public String getTradeUrl(BigDecimal payMoney) throws AlipayApiException {
        MakeOrderNum makeOrderNum = new MakeOrderNum();
        //支付宝SDK已经封装掉了公共参数,这里只需要传入业务参数
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", BasePayConstants.ALIPAY_PARTNER, BasePayConstants.RSA_PRIVATE_KEY, "json", BasePayConstants.ALIPAY_CHARSET, BasePayConstants.RSA_PUBLIC_KEY, "RSA2");
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //写入业务参数
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(BasePayConstants.GOODS_INFO);                         //商品信息
        model.setSubject(BasePayConstants.GOODS_NAME);                      //商品名称
        model.setOutTradeNo(String.valueOf(makeOrderNum));                  //商户订单号(自动生成)
        model.setTimeoutExpress(BasePayConstants.ALIPAY_TIMEOUT_EXPRESS);   //交易超时时间
        model.setTotalAmount(String.valueOf(payMoney));                     //支付金额
        model.setProductCode(BasePayConstants.ALIPAY_PRODUCT_CODE);         //销售产品码
        request.setBizModel(model);
        request.setNotifyUrl(BasePayConstants.ALIPAY_NOTIFY_URL + BasePayConstants.ALIPAY_FRONT_CALLBACK_URL);  //回调地址
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());
            return response.getBody();               //就是orderString 可以直接给客户端请求,无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "error";
    }
}

你可能感兴趣的:(java)