开发当面付首先需要成为服务商,然后创建app、配置你、密钥,相应流程参考官网
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.GMcxsV&treeId=194&articleId=105170&docType=1
我们从第三步开始,首先我们需要下载相应架包配置到自己项目中,下载地址
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.Rb9NCK&treeId=194&articleId=105201&docType=1
下面是配置架包之后具体实现代码
public String alipay() throws ServletException, IOException, AlipayApiException{
//这里需要注意的是如果你使用沙箱网关https://openapi.alipaydev.com/gateway.do
AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2");
//获得初始化的AlipayClient
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
//我用的是时间戳作为订单号
String out_trade_no=Long.toString(new Date().getTime());
//token代表着用户把钱转给谁了;在开发者中心——我的应用——使用者管理里面查看
String token="201702BB89793586af654e05ab57f5b0160e5X36";
//number为前台传过来的值
String total_amount=number;//订单金额
String subject="龙润电子";
request.setBizContent("{" +
" \"out_trade_no\":\""+out_trade_no+"\"," +
" \"total_amount\":\""+total_amount+"\"," +
" \"subject\":\""+subject+"\"," +
" \"store_id\":\"NJ_001\"," +
" \"timeout_express\":\"90m\"}");//设置业务参数
//这里面值得注意的是官网上只传了一个request,具体怎么传可以问客服。打开本博客第二行网址点击右侧动漫头像可以联系到客服
AlipayTradePrecreateResponse response = alipayClient.execute(request,null,token);
//把返回结果放进form以json格式传到前台,在前台调起支付接口,前台代码参考我的另一篇博客http://blog.csdn.net/qq_34218481/article/details/62225009
jsonObject.put("form", response.getBody());
return "success";
}