java实现支付宝接口---支付

java实现实现支付宝接口

蚂蚁沙箱环境 (Beta) 是协助开发者进行接口功能开发及主要功能联调的辅助环境。可以模拟实现支付宝的支付功能。
接下的实现技术是运用到springmvc注解方法实现的支付。

准备工作

  1. 去支付宝开放平台,用你的支付登录开通沙箱支付的服务。支付宝开放平台

  2. 点击右上角的立即入驻之后,确认加入。在这里插入图片描述

  3. 点击导航栏的开发者中心,之后就进入到沙箱应用,再点击右边的设置。在这里插入图片描述

  4. 点击研发服务在这里插入图片描述
    在这里插入图片描述

  5. 使用支付宝秘钥生成器生成自己的私钥和公钥,公钥填至框中,私钥自己保管好。使用工具生成器生成好的密钥会自动给你保存到你的电脑。 然后保存设置。在这里插入图片描述
    在这里插入图片描述

  6. 然后点击查看,可以看到你的公钥和支付宝的公钥,说明你已经配置好了。

  7. 沙箱账号就是买家和商家的账号和密码,还有金额,可以进行充值,手机下载沙箱钱包,用买家账号密码登录即可,就可以扫码付款了,付款的金额就到了商家的账户余额里。

java代码实现支付

  1. 前台发请求的代码,需要获取订单号和订单金额传到后台。
    注意:后台返回的数据格式不能输json类型
    因为json类型的返回的数据
//支付的请求
function topay(price,that){
	//获取订单号
	var ono=$(that).attr("id");
	
		$.post("../../apli/pay",{ono:ono,price:price},function(data){
			$("#apli").html(data);
		},"text")
	}
  1. 支付宝接口的配置类 ApplicationConfig.java
    对应的地方修改成你自己的一些信息。
    特别注意一下异步和同步通知页面路径的处理
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";
	 
	    /*
	     * 服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数
	     * 必须外网可以正常访问
		 * 一般就是支付成功时,修改数据库之类的操作
		 */
		public static String notify_url = "http://localhost:8080/project/apli/update";
		
		/*
		 * 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
		 * 通知页面一般用来,支付成功后,需要跳转什么页面之类的处理
		 */
	    public static String return_url = "http://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:\\";
	}
  1. ApliConfigController.java类。 具体操作的实现
@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);
    }
}

支付宝沙箱支付的官方文档

  1. 沙箱支付文档
    更加深入的学习。
  2. 如若有问题,留言,我们一起探讨…
  • 19
    点赞
  • 161
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值