java后台对接支付宝接口,然后生成订单
package com.alipay.util.httpClient; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.domain.AlipayTradeAppPayModel; import com.alipay.api.domain.AlipayTradeWapPayModel; import com.alipay.api.request.AlipayTradeAppPayRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.response.AlipayTradeAppPayResponse; import com.alipay.api.response.AlipayTradeWapPayResponse; import com.alipay.config.AlipayConfig; import com.alipay.util.AlipaySubmit; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.*; /** * Created by Administrator on 2016/4/22. */ public class AlipayTools { private static Logger logger = LoggerFactory.getLogger(AlipayTools.class); public static String alipay_wap(String yun_order_no,String subject,String total_fee,String show_url,String body){ //把请求参数打包成数组 Map<String, String> sParaTemp = new HashMap<String, String>(); sParaTemp.put("service", AlipayConfig.service); sParaTemp.put("partner", AlipayConfig.partner); sParaTemp.put("seller_id", AlipayConfig.seller_id); sParaTemp.put("_input_charset", AlipayConfig.input_charset); sParaTemp.put("payment_type", AlipayConfig.payment_type); sParaTemp.put("notify_url", AlipayConfig.notify_url); sParaTemp.put("return_url", AlipayConfig.return_url); sParaTemp.put("out_trade_no", yun_order_no); sParaTemp.put("subject", subject); sParaTemp.put("total_fee", total_fee); sParaTemp.put("show_url", show_url); sParaTemp.put("body", body); //其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.2Z6TSk&treeId=60&articleId=103693&docType=1 //如sParaTemp.put("参数名","参数值"); //建立请求 return AlipaySubmit.buildRequest(sParaTemp, "get", "确认"); } /** * app端,会向我们发送请求,获取一个加密过后的支付宝订单字符串 * @param orderId 我们系统的订单号 * @param subject 标题 * @param total_fee 支付金额 * @param body 订单说明 * @param notifyUrl 通知地址 * @return Map: status:0 成功 1:失败 orderString 订单字符串 */ public static Map alipayCreateOrderAppNew(String orderId,String subject,String total_fee,String body,String notifyUrl) { Map map = new LinkedHashMap(); //产生的订单字符串 String orderString = ""; //实例化客户端(参数:网关地址、商户appid、商户私钥、格式、编码、支付宝公钥、加密类型) AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig. aliPayUrl, AlipayConfig.appId, AlipayConfig.developerSpoon, "json", AlipayConfig.input_charset, AlipayConfig.aliPaySpoonNew, "RSA2"); //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数。 // 以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等 model.setTotalAmount(total_fee);//订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] //model.setProductCode("QUICK_MSECURITY_PAY");//销售产品码,商家和支付宝签约的产品码 model.setOutTradeNo(orderId);//商户网站唯一订单号,请保证OutTradeNo值每次保证唯一 model.setTimeoutExpress("30m");//该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。 request.setBizModel(model); request.setNotifyUrl(notifyUrl);//商户外网可以访问的异步地址 AlipayTradeAppPayResponse response = null; try { response = alipayClient.sdkExecute(request); //调用的是sdkExecute接口 System.out.println(JSONObject.fromObject(response).toString()); } catch (AlipayApiException e) { String massage = "alipay.trade.app.pay接口:订单签名错误"; System.out.println(massage); } if (response.isSuccess()) { orderString = response.getBody(); System.out.println(orderString); map.put("status", 0);//生成订单成功 status:0 成功 1:失败 map.put("orderString", orderString);//订单串 logger.info("支付宝App支付:支付订单生成订单成功out_trade_no----"+orderId); } else { logger.info("支付宝App支付:支付订单生成失败out_trade_no----"+orderId); map.put("status",1);//生成订单成功 status:0 成功 1:失败 map.put("orderString", "");//订单串 } return map; } /** * H5支付生成订单 * @param orderId 我们系统的订单号 * @param subject 标题 * @param total_fee 支付金额 * @param body 订单说明 * @param notifyUrl 通知地址 * @return Map: status:0 成功 1:失败 orderString 订单From表单 */ public static Map alipayCreateOrderH5(String orderId,String subject,String total_fee,String body,String notifyUrl) { Map map = new LinkedHashMap(); //产生的订单From表单 String orderString = ""; //实例化客户端(参数:网关地址、商户appid、商户私钥、格式、编码、支付宝公钥、加密类型) AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig. aliPayUrl, AlipayConfig.appId, AlipayConfig.developerSpoon, "json", AlipayConfig.input_charset, AlipayConfig.aliPaySpoonNew, "RSA2"); //当前调用接口名称:alipay.trade.wap.pay AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.setReturnUrl(AlipayConfig.return_url); request.setNotifyUrl(notifyUrl);//商户外网可以访问的异步地址 //使用封装的Model进行传值 AlipayTradeWapPayModel model = new AlipayTradeWapPayModel(); model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等 model.setTotalAmount(total_fee);//订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] model.setOutTradeNo(orderId);//商户网站唯一订单号,请保证OutTradeNo值每次保证唯一 model.setTimeoutExpress("30m");//该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。 //model.setQuitUrl(quitUrl);//用户付款中途退出返回商户网站的地址 request.setBizModel(model); AlipayTradeWapPayResponse response = null; try { //调用SDK生成表单 response = alipayClient.pageExecute(request); System.out.println("创建结果:"+JSONObject.fromObject(response).toString()); } catch (AlipayApiException e) { System.out.println("alipay.trade.wap.pay接口:订单签名错误"); } //判断是否调用成功 if (response.isSuccess()) { orderString = response.getBody(); System.out.println("==="+orderString); map.put("status", 0);//生成订单成功 status:0 成功 1:失败 map.put("orderString", orderString);//订单From表单 logger.info("支付宝H5支付:支付订单生成订单成功out_trade_no----"+orderId); } else { logger.info("支付宝H5支付:支付订单生成失败out_trade_no----"+orderId); map.put("status",1);//生成订单成功 status:0 成功 1:失败 map.put("orderString", "");// } return map; } }
package com.alipay.config; /* * *类名:AlipayConfig *功能:基础配置类 *详细:设置帐户有关信息及返回路径 *版本:3.4 *修改日期:2016-03-08 *说明: *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 */ import com.shopping.app.config.URL; public class AlipayConfig { //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // 合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm public static String partner = "2088021690******"; // 收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号 public static String seller_id = partner; // MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm public static String key = "3w4g99buk1zytpbozavxk2bfw9vfpg0i"; // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 public static String notify_url = URL.url + "/appAlipayReturn.htm"; // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 public static String return_url = URL.url + "/d_/h5_shopping/m_shoppingPayMentResult.html"; public static String return_url2 = URL.url + "/d_/dk_native/group_collage/authen_waiting.html"; public static String show_url=URL.url + "/d_/h5_shopping/m_shoppingPayMentResult.html"; // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可d以正常访问 public static String return_url_cart5 = URL.url + "/d_/h5_shopping/m_shoppingMyOrder.html"; public static String show_url_cart5=URL.url + "/d_/h5_shopping/m_shoppingMyOrder.html"; // 签名方式 public static String sign_type = "MD5"; // 调试用,创建TXT日志文件夹路径,见AlipayCore.java类中的logResult(String sWord)打印方法。 public static String log_path = "d:\\"; // 字符编码格式 目前支持utf-8 public static String input_charset = "utf-8"; // 支付类型 ,无需修改 public static String payment_type = "1"; // 调用的接口名,无需修改 public static String service = "alipay.wap.create.direct.pay.by.user"; public static String aliPayUrl="https://openapi.alipay.com/gateway.do"; //app支付宝id public static String appId="20171013****3857"; //支付宝私钥 public static String developerSpoon="MIIEvQIBA****************ASCBKcwggSjAgEAAoIBAQCRlROPLQ2spEhfeEDGeovFIRzINwTSStKk+fdHg/4sP5xF/B4m3LuBeOidjZrfWdVtPKExfKf+6fdRuIoP6PxLeq5+z16BKOx3xeEI2tH81couOufEaZWniT6E3UpbA5H6lHLlAklm/KeCJlPhJvWYVBK/o6ZrEY6yCkSkEGIJtxU5i6nPyNJfGbllJe9Y0D3S/lCCaDKZk+R7MGDHfqYrm3A6hICIXfwq9K+8EoB+nscpjtyScc0iX29BZgJovx303v0JyGFN6yQWFJbE6TcOY4+GfYOBmFYaNE3l7ffNyRrhszIVXwnuitvc/Vf2JDTxpf8+JclB+/hAcbAS38jhAgMBAAECggEBAIubWRFhFA7tz9V1nTB4jhZxZYd+c2oyhIPejcqQAituqK7YUhXUcR980h1ckRsEq83AaZfqQbQLq7c7b3JIa8D6GeHTfH4Bv5owMVfrd5mtbuAcbuuEkRYDX+fLyiUzbEbPGssQAVH4ka4WkTOFx9jXLs+F4O4l/SAxBpgKeelzqaGUQQulbnZQR33xZhz4cYTkaZBQ26eEZqBlLBGw6bbJC22TU19AYJUeA6e1MDG7E4n7AzDmI/ebulukRwDlIdmJ1+6qas0nUqUFuvl+ugpIp/3NZablRMTP+pexG0r5VjvwM2R5OPDDmdQxv6YFWmQQwYWIoE3LhlkTc22+qAECgYEA3r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oVPTi8XCXKtI6bZ7LQr4hCsPioXDVU1cdtnc3G7CT5WLeCQCGJD/HtsrbZmHse+6tR+V6tan5k83ytsqrs9VgC2rYIM/Gghghb9zGxmLSxqmcTZOgO43XWZuTeqsFRHYXfueHKnozxxMfD8mY3knSi0Ey0ZiZMlEdpaP9bI9S8paN+nvN1CsV8bzwIDAQAB"; //新的公钥APP用的公钥 public static String aliPaySpoonNew="MIIBIjANBgkqh************AQ8AMIIBCgKCAQEAkZUTjy0NrKRIX3hAxnqLxSEcyDcE0krSpPn3R4P+LD+cRfweJty7gXjonY2a31nVbTyhMXyn/un3UbiKD+j8S3qufs9egSjsd8XhCNrR/NXKLjrnxGmVp4k+hN1KWwOR+pRy5QJJZvyngiZT4Sb1mFQSv6OmaxGOsgpEpBBiCbcVOYupz8jSXxm5ZSXvWNA90v5QgmgymZPkezBgx36mK5twOoSAiF38KvSvvBKAfp7HKY7cknHNIl9vQWYCaL8d9N79CchhTeskFhSWxOk3DmOPhn2DgZhWGjRN5e33zcka4bMyFV8J7orb3P1X9iQ08aX/PiXJQfv4QHGwEt/I4QIDAQAB"; // ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ public static String notify_url_light_service_callback = URL.url+"/json/appAlipayLightService_goods.htm"; public static String notify_url_videoCourseTopic_callBack = URL.url+"/json/aliPayVideoCourseTopic_callBack.htm"; public static String notify_url_lightservice_video_callback = URL.url+"/json/aliPaylightService_video_callBack.htm"; public static String aliyun_notify_url_ApplyMavinOrder = URL.url + "/json/pay_aliyun_ApplyMavinOrder_callBack.htm"; public static String aliyun_notify_mavinEnter_callBack = URL.url + "/json/aliyun_mavinEnter_callBack.htm"; public static String aliyun_notify_lightService0_goods_callBack = URL.url + "/callback/aliyun_notify_lightService0_goods_callBack.htm"; public static String aliyun_notify_url_AuditOrder_callBack = URL.url + "/notify/pay_ali_auditOrder_callBack.htm"; }
测试生成的H5返回的From表单是否有效,直接写一个HTML。将测试的串放在里面。例如:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form name="punchout_form" method="post" action="https://openapi.alipay.com/gateway.do?sign=Q5F5g3u1WzCWMjKcmlzzcDmTzOONj726qM3OF8RJP5axv2nOC5lfnslpLesalgUt%2FScgSUknzQYxQzshixIS5TcHIvofFHQSd9j5a8CiolwXcKREwupcplapxxXtvUm94J2%2FgvuFjk%2F989Pt2Qf6h6c6tx3LilC4%2B8nWS2WnlGvR0jWty0KzChX3VEscN6zxTEpZz98jQhc6p29lupsYQEif97OVgljhX8PcNgA%2B5fm%2F7K5dsLvF7KSpuZi4%2FGFYZ2FrQNx8oRFErl7IRsPK5%2FL1cdaIwckgmNbcdffElVddJ6RYsk8UtnND2XPVRtvDLTucTOVyjVpXMD1rdMV6xw%3D%3D×tamp=2019-08-08+12%3A14%3A03&sign_type=RSA2¬ify_url=http%3A%2F%2Faptest.yingyanghome.com%2FappTeamAlipayReturn.htm&charset=utf-8&app_id=2017101309283857&method=alipay.trade.wap.pay&return_url=http%3A%2F%2Faptest.yingyanghome.com%2Fd_%2Fh5_shopping%2Fm_shoppingPayMentResult.html&version=1.0&alipay_sdk=alipay-sdk-java-dynamicVersionNo&format=json"> <input type="hidden" name="biz_content" value="{"body":"H5进行东康签约小组","out_trade_no":"3820120190808121403","subject":"支付东康","timeout_express":"30m","total_amount":"0.02"}"> <input type="submit" value="立即支付" style="display:none" > </form> <script>document.forms[0].submit();</script> </body> </html>
看一下能否跳转