蚂蚁沙箱环境 (Beta) 是协助开发者进行接口功能开发及主要功能联调的辅助环境。可以模拟实现支付宝的支付功能。 去支付宝开放平台,用你的支付登录开通沙箱支付的服务。支付宝开放平台 点击右上角的立即入驻之后,确认加入。 点击导航栏的开发者中心,之后就进入到沙箱应用,再点击右边的设置。 点击研发服务 使用支付宝秘钥生成器生成自己的私钥和公钥,公钥填至框中,私钥自己保管好。使用工具生成器生成好的密钥会自动给你保存到你的电脑。 然后保存设置。 然后点击查看,可以看到你的公钥和支付宝的公钥,说明你已经配置好了。 沙箱账号就是买家和商家的账号和密码,还有金额,可以进行充值,手机下载沙箱钱包,用买家账号密码登录即可,就可以扫码付款了,付款的金额就到了商家的账户余额里。
java实现实现支付宝接口
接下的实现技术是运用到springmvc注解方法实现的支付。准备工作
java代码实现支付
注意:后台返回的数据格式不能输json类型
因为json类型的返回的数据//支付的请求 function topay(price,that){ //获取订单号 var ono=$(that).attr("id"); $.post("../../apli/pay",{ono:ono,price:price},function(data){ $("#apli").html(data); },"text") }
对应的地方修改成你自己的一些信息。
特别注意一下异步和同步通知页面路径的处理public class ApplicationConfig { // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号 public static String app_id = "2016102400748299"; // 商户私钥,您的PKCS8格式RSA2私钥 public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDTcUPbyeVtd/a7mgtC/cs1QhvXDo8BJM6"; // 支付宝公钥 public static String alipay_public_key = "SuFoWXQxhVJhPW0Gp7WKtBiPSkVovQoOwY/bFKfyDJSY92oS1mNWjoIzr7vS52fc374rm9fVbZPFau5xu5q6hNmOCekM/W9SEKPyxk4lAB+hQYcgiSjGANBo+v3y55OHQIzrylQIDAQAB"; /* * 服务器异步通知页面路径 需https://格式的完整路径,不能加?id=123这类自定义参数 * 必须外网可以正常访问 * 一般就是支付成功时,修改数据库之类的操作 */ public static String notify_url = "https://localhost:8080/project/apli/update"; /* * 页面跳转同步通知页面路径 需https://格式的完整路径,不能加?id=123这类自定义参数 * 通知页面一般用来,支付成功后,需要跳转什么页面之类的处理 */ public static String return_url = "https://localhost:8080/project/apli/return"; // 签名方式,注意这里,如果步骤设置的是RSA则用RSA public static String sign_type = "RSA2"; // 字符编码格式 public static String charset = "utf-8"; // 支付宝网关 public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; // 支付宝网关 public static String log_path = "C:\"; }
@RestController @RequestMapping("/apli") public class ApliConfigController { @RequestMapping("/pay") //踩坑记录->这里不可直接return result; 必须要response.getWriter().print(result);或者以map键值对方法返回 public String toPay(String ono,double price,HttpServletRequest request,HttpServletResponse resp,HttpSession session) throws AlipayApiException, IOException, ServletException { //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(ApplicationConfig.gatewayUrl, ApplicationConfig.app_id, ApplicationConfig.merchant_private_key, "json", ApplicationConfig.charset, ApplicationConfig.alipay_public_key, ApplicationConfig.sign_type); //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(ApplicationConfig.return_url); alipayRequest.setNotifyUrl(ApplicationConfig.notify_url); alipayRequest.setBizContent("{"out_trade_no":""+ono +""," + ""total_amount":""+ price +""," + ""subject":""+ " Y呀网站支付 " +""," + ""product_code":"FAST_INSTANT_TRADE_PAY"}"); String result = alipayClient.pageExecute(alipayRequest).getBody(); resp.setContentType("text/html; charset=utf-8"); resp.getWriter().print(result); return null; } @RequestMapping("/return") public void toIndex(HttpServletResponse resp,HttpSession session) throws IOException{ //取session MemberInfo member=(MemberInfo) session.getAttribute("loginUser"); //支付成功跳转页面 PrintWriter out = resp.getWriter(); out.print("<script>location.href='../front/page/order.html'</script>"); //支付成功后跳转页面你的session会清空,如果还需要可以在存一次session session.setAttribute("loginUser", member); } }
支付宝沙箱支付的官方文档
更加深入的学习。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算