java支付宝网站支付
开发者技术变现资源聚集地
https://www.baiydu.com
官方demo上请求是使用的form提交, 第一步请求支付,然后会返回一个组合好的form,并且有自动提交支付的javascript代码,由于为了撇和我们框架和布局,第一步没有用form下面来代码
memberType deviceCount两个参数是计算用户购买商品支付的总金额所需参数, 其他参数都是平台安全验证所需,下面有个提交界面形象的截图
var alipayParameter="type=1&account="+localStorage.getItem("account")+"&token="+localStorage.getItem("token")+"&memberType="+memberType+"&deviceCount="+deviceCount; $.ajax({ type: "POST", async: true, dataType: "json", data: alipayParameter, url: '<%=basePath%>/selfOnlineBuyServlet', success: function (returnDate) { var status=returnDate.status; if (status == 'success') { //将返回的from标签增加到body后提交支付 const div=document.createElement('divform'); div.innerHTML=returnDate.payUrl; document.body.appendChild(div); // document.forms[0].acceptCharset='GBK';//保持与支付宝默认编码格式一致,如果不一致将会出现:调试错误,请回到请求来源地,重新发起请求,错误代码 invalid-signature 错误原因: 验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配 document.forms[0].submit(); } else { stion'); } $("#transParentLawyer").css({'display': 'none'}); } }); }); });
服务端代码:
package autoDrainage; import java.io.IOException; import java.io.PrintWriter; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import org.json.JSONObject; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradePagePayRequest; import Helper.AAAAYulebaoShareSingleton; import Helper.AlipayConfig; import Helper.ElaseHelper; import Helper.MySqlHepler2; @WebServlet("/selfOnlineBuyServlet") public class selfOnlineBuyServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected final String USER_AGENT = "Mozilla/5.0"; public selfOnlineBuyServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html"); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter writer=response.getWriter(); JSONObject shareJsonObject=new JSONObject(); String token=request.getParameter("token"); String account=request.getParameter("account"); //请求 String result=""; //token验证 int tokenCheck= ElaseHelper.checktoken(account, token,1); if(tokenCheck>0) { JSONObject singleObject=null; String memberType=request.getParameter("memberType"); int deviceCount=Integer.valueOf(request.getParameter("deviceCount")).intValue() ; String total_amount=""; if(deviceCount>0)//必须大于0不能为负数 { //下面一个循环计算需要支付的总额度 for(int i=0;i<AAAAYulebaoShareSingleton.getInstance().memeberTypeAndPriceListArray.length();i++) { singleObject=AAAAYulebaoShareSingleton.getInstance().memeberTypeAndPriceListArray.getJSONObject(i); String tempType=singleObject.getString("memeberType"); if(memberType.equals(tempType)) { total_amount=String.valueOf(deviceCount*singleObject.getInt("price")) ; break; } } //调用支付宝 //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(AlipayConfig.return_url); alipayRequest.setNotifyUrl(AlipayConfig.notify_url); //商户订单号,商户网站订单系统中唯一订单号,必填 String out_trade_no =UUID.randomUUID().toString().replace("-", ""); //付款金额,必填 // total_amount; //订单名称,必填 String subject ="云步引流平台点卡购买"; //商品描述,可空 String body = "产品使用分为,日/周/月/年卡四种类型,用户购买后可以使用平台的引流软件实现自动化引流"; //下面是支付宝回调时验证的参数,单个购买所需参数组成一个字典,以商户订单号为key放到单例模式的字典中,回到成功直接移除,不成功的每天晚上12点自动清空一次。 JSONObject buyMemeberObject=new JSONObject(); buyMemeberObject.put("memberType",memberType); buyMemeberObject.put("deviceCount", deviceCount); AAAAYulebaoShareSingleton.getInstance().alipayOnlineBuyDictionary.put(out_trade_no, buyMemeberObject); alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," + "\"total_amount\":\""+ total_amount +"\"," + "\"subject\":\""+ subject +"\"," + "\"body\":\""+ body +"\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); try { result = alipayClient.pageExecute(alipayRequest).getBody(); } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); } shareJsonObject.put("payUrl", result); shareJsonObject.put("status", "success"); shareJsonObject.put("Msg ", "提交成功"); } } else { shareJsonObject.put("status", "fail");//fail shareJsonObject.put("msg", "tokentimeout"); } writer.println(shareJsonObject); writer.flush(); writer.close(); writer=null; shareJsonObject=null; }}
扫码加微信