支付宝手机网页支付接口调用

1.前期工作

1.1 登陆支付宝官网

网址:https://www.alipay.com/

1.2 下载手机网站支付的demo的步骤

1.2.1 先点击API,选择手机网站支付模块点击

支付宝手机网页支付接口调用_第1张图片

1.2.2 点击SDK&Demo,选择java版本点击下载

支付宝手机网页支付接口调用_第2张图片

1.2.3 点击下载后的文件压缩包

支付宝手机网页支付接口调用_第3张图片

1.2.4 获取的文件接口名

支付宝手机网页支付接口调用_第4张图片

1.3 沙箱准备工作

1.3.1 寻找沙箱的第一步

支付宝手机网页支付接口调用_第5张图片

1.3.2 选择沙箱环境点击

支付宝手机网页支付接口调用_第6张图片

1.3.3 沙箱应用,启动系统默认密钥

支付宝手机网页支付接口调用_第7张图片

1.3.4 沙箱账号:显示买家信息和买家信息,这是支付宝提供的测试的数据

支付宝手机网页支付接口调用_第8张图片

2 导入测试项目

支付宝手机网页支付接口调用_第9张图片

2.1 AlipayConfig文件主要是配置,和沙箱中的商家信息对应,具体配置参考下文


public class AlipayConfig {
	// 商户appid
	public static String APPID = "";
	// 私钥 pkcs8格式的
	public static String RSA_PRIVATE_KEY = "";
	// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
	// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
	public static String return_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/return_url.jsp";
	// 请求支付宝网关地址 https://openapi.alipaydev.com/gateway.do(测试环境的网址 alipaydev多个dev,一定别搞错了) 而不是https://openapi.alipay.com/gateway.do(正式环境的网址)
	public static String URL = "https://openapi.alipaydev.com/gateway.do";
	// 编码
	public static String CHARSET = "UTF-8";
	// 返回格式
	public static String FORMAT = "json";
	// 支付宝公钥(切记是支付宝公钥,而不是支付宝应用公钥)
	public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjrEVFMOSiNJXaRNKicQuQdsREraftDA9Tua3WNZwcpeXeh8Wrt+V9JilLqSa7N7sVqwpvv8zWChgXhX/A96hEg97Oxe6GKUmzaZRNh0cZZ88vpkn5tlgL4mH/dhSr3Ip00kvM4rHq9PwuT4k7z1DpZAf1eghK8Q5BgxL88d0X07m9X96Ijd0yMkXArzD7jg+noqfbztEKoH3kPMRJC2w4ByVdweWUT2PwrlATpZZtYLmtDvUKG/sOkNAIKEMg3Rut1oKWpjyYanzDgS7Cg3awr1KPTl9rHCazk15aNYowmYtVabKwbGVToCAGK+qQ1gT3ELhkGnf3+h53fukNqRH+wIDAQAB";
	// 日志记录目录
	public static String log_path = "/log";
	// RSA2
	public static String SIGNTYPE = "RSA2";
}




2.2 访问支付宝电脑网站支付体验入口页 index.jsp

访问链接:http://127.0.0.1:8080/alipay.trade.wap.pay-java-utf-8/index.jsp

支付宝手机网页支付接口调用_第10张图片

2.3 点击订单查询

支付宝手机网页支付接口调用_第11张图片

2.4后台支付数据接口代码

package com.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alipay.AlipayConfig;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

import com.pojo.Code;
import com.service.CodeService;
import com.alipay.api.*;
import java.util.*;

import com.alipay.*;





@Controller
public class AliPayController {
	
	
	@Autowired
	private CodeService codeService;
   
	@RequestMapping("alipay_index.html")
	public String page() {
		
		
		return "alipay_index";
		
	}
	
	
	@RequestMapping("alipay_notify_url.html")
	public String notify_url() {
		
		
		return "alipay_notify_url";
		
	}
	
	
	
	@RequestMapping("alipay_return_url.html")
	public String alipay_return_url(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		//获取支付宝GET过来反馈信息
		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
			valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}
		
		//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
		//商户订单号

		String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");

		//支付宝交易号

		String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");

		//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
		//计算得出通知验证结果
		//boolean AlipaySignature.rsaCheckV1(Map params, String publicKey, String charset, String sign_type)
		boolean verify_result = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2");
		
		if(verify_result){//验证成功
			//
			//请在这里加上商户的业务逻辑程序代码
			//该页面可做页面美工编辑
			//out.clear();
			//out.println("验证成功
");
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— // request.setAttribute("msg", "验证成功1"); //调用生成二维码订单的函数 JSONObject jsonObject=headImg(out_trade_no,request,response); //System.out.println("object======"+jsonObject.toString()); request.setAttribute("object", jsonObject); // }else{ //该页面可做页面美工编辑 //out.clear(); //out.println("验证失败"); JSONObject jsonObject=headImg(out_trade_no,request,response); //System.out.println("object======"+jsonObject.toString()); request.setAttribute("object", jsonObject); } return "alipay_return_url"; } //添加二维码,生成二维码 public JSONObject headImg(String codeTitle,HttpServletRequest request, HttpServletResponse response)throws Exception{ //图片存放的基本路径 //String path = "/pwxaccount/statics/uploadfiles"; String path = request.getSession().getServletContext().getRealPath("statics/uploadfiles"); System.out.println("path:==============="+path); //支付生成二维码里的内容 String content = codeTitle; //获得图片最终存储的路径 String fileUrl=ErweimaController.code(path, content); //System.out.println("fileUrl=============="+fileUrl); //录入生成的二维码信息 进入数据库 Code code=new Code(); code.setCodeTitle(codeTitle); code.setCodeImg(fileUrl); code.setCodeTime(new Date()); int i = codeService.insert(code); HashMap<String,Object> map=new HashMap<String,Object>(); JSONObject jsonObject=new JSONObject(); //System.out.println("二维码生成=============="+i); if(i>0) { jsonObject.put("code",6); jsonObject.put("msg","订单二维码生成成功"); jsonObject.put("fileUrl",fileUrl); } else { jsonObject.put("code",5); jsonObject.put("msg","订单二维码生成失败"); jsonObject.put("fileUrl",""); } //前端展示时 res.code==1 return jsonObject; } /* 手机网站支付接口 */ @RequestMapping("alipay_pay.html") public String alipay(HttpServletRequest request,HttpServletResponse response) { //时间撮 商户订单号 String out_trade_no =System.currentTimeMillis()+""; request.setAttribute("out_trade_no", out_trade_no); return "alipay_pay"; } /* 手机网站支付接口 */ @RequestMapping("pay.html") public void order(HttpServletRequest request,HttpServletResponse response,Integer amount,String out_trade_no) throws IOException { // 商户订单号,商户网站订单系统中唯一订单号,必填 //String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); // dai System.currentTimeMillis() //时间撮 //String out_trade_no =System.currentTimeMillis()+""; System.out.println("out_trade_no============="+out_trade_no); // 订单名称,必填 //String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8"); String subject ="test"; // 付款金额,必填 // String total_amount=new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8"); String total_amount= amount.toString(); // 商品描述,可空 //String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8"); String body =""; // 超时时间 可空 String timeout_express="2m"; // 销售产品码 必填 String product_code="QUICK_WAP_WAY"; /**********************/ // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //调用RSA签名方式 AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest(); // 封装请求支付信息 AlipayTradeWapPayModel model=new AlipayTradeWapPayModel(); model.setOutTradeNo(out_trade_no); model.setSubject(subject); model.setTotalAmount(total_amount); model.setBody(body); model.setTimeoutExpress(timeout_express); model.setProductCode(product_code); alipay_request.setBizModel(model); // 设置异步通知地址 alipay_request.setNotifyUrl(AlipayConfig.notify_url); // 设置同步地址 alipay_request.setReturnUrl(AlipayConfig.return_url); // form表单生产 String form = ""; try { // 调用SDK生成表单 form = client.pageExecute(alipay_request).getBody(); response.setContentType("text/html;charset=" + AlipayConfig.CHARSET); response.getWriter().write(form);//直接将完整的表单html输出到页面 response.getWriter().flush(); response.getWriter().close(); } catch(AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* 统一收单交易关闭接口 */ @RequestMapping("alipay_close.html") public String alipayclose() { return "alipay_close"; } /* 统一收单交易关闭接口 */ @RequestMapping("close.html") @ResponseBody public String alipayTradeClose(HttpServletRequest request,HttpServletResponse response) throws IOException, AlipayApiException { //商户订单号,和支付宝交易号二选一 String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号,和商户订单号二选一 String trade_no = new String(request.getParameter("WIDtrade_no").getBytes("ISO-8859-1"),"UTF-8"); // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //调用RSA签名方式 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayTradeCloseRequest alipay_request = new AlipayTradeCloseRequest(); AlipayTradeCloseModel model = new AlipayTradeCloseModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); alipay_request.setBizModel(model); AlipayTradeCloseResponse alipay_response = alipayClient.execute(alipay_request); System.out.println("close========"+alipay_response.getBody()); if (alipay_response.isSuccess()) { System.out.println("close========调用成功"); } else { System.out.println("close========调用失败"); } return alipay_response.getBody(); } /* 统一收单交易退款查询 */ @RequestMapping("alipay_refundQuery.html") public String alipayrefundQuery() { return "alipay_refundQuery"; } /* 统一收单交易退款查询 */ @RequestMapping("refundQuery.html") @ResponseBody public String alipayTradeFastpayRefundQuery(HttpServletRequest request,HttpServletResponse response) throws IOException, AlipayApiException { //商户订单号和支付宝交易号不能同时为空。 trade_no、 out_trade_no如果同时存在优先取trade_no //商户订单号,和支付宝交易号二选一 String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号,和商户订单号二选一 String trade_no = new String(request.getParameter("WIDtrade_no").getBytes("ISO-8859-1"),"UTF-8"); //请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号 String out_request_no = new String(request.getParameter("WIDout_request_no").getBytes("ISO-8859-1"),"UTF-8"); // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //调用RSA签名方式 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayTradeFastpayRefundQueryRequest alipay_request = new AlipayTradeFastpayRefundQueryRequest(); AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); model.setOutRequestNo(out_request_no); alipay_request.setBizModel(model); AlipayTradeFastpayRefundQueryResponse alipay_response = alipayClient.execute(alipay_request); System.out.println("refundQuery========"+alipay_response.getBody()); if (alipay_response.isSuccess()) { System.out.println("refundQuery========调用成功"); } else { System.out.println("refundQuery========调用失败"); } return alipay_response.getBody(); } /* 统一收单线下交易查询 */ @RequestMapping("alipay_query.html") public String alipayquery() { return "alipay_query"; } /* 统一收单线下交易查询 */ @RequestMapping("query.html") @ResponseBody public String alipayTradeQuery(HttpServletRequest request,HttpServletResponse response,Integer amount) throws IOException, AlipayApiException { //商户订单号,商户网站订单系统中唯一订单号,必填 String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("WIDtrade_no").getBytes("ISO-8859-1"),"UTF-8"); // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //调用RSA签名方式 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayTradeQueryRequest alipay_request = new AlipayTradeQueryRequest(); AlipayTradeQueryModel model = new AlipayTradeQueryModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); alipay_request.setBizModel(model); AlipayTradeQueryResponse alipay_response = alipayClient.execute(alipay_request); System.out.println("query========"+alipay_response.getBody()); if (alipay_response.isSuccess()) { System.out.println("query========调用成功"); } else { System.out.println("query========调用失败"); } return alipay_response.getBody(); } /* 统一收单交易退款接口 */ @RequestMapping("alipay_refund.html") public String alipayrefund() { return "alipay_refund"; } /* 统一收单交易退款接口 */ @SuppressWarnings("unused") @RequestMapping("refund.html") @ResponseBody public String alipayTradeRefund(HttpServletRequest request,HttpServletResponse response,Integer amount) throws IOException, AlipayApiException { //商户订单号和支付宝交易号不能同时为空。 trade_no、 out_trade_no如果同时存在优先取trade_no //商户订单号,和支付宝交易号二选一 String out_trade_no = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号,和商户订单号二选一 String trade_no = new String(request.getParameter("WIDtrade_no").getBytes("ISO-8859-1"),"UTF-8"); //退款金额,不能大于订单总金额 String refund_amount=new String(request.getParameter("WIDrefund_amount").getBytes("ISO-8859-1"),"UTF-8"); //退款的原因说明 String refund_reason=new String(request.getParameter("WIDrefund_reason").getBytes("ISO-8859-1"),"UTF-8"); //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。 String out_request_no=new String(request.getParameter("WIDout_request_no").getBytes("ISO-8859-1"),"UTF-8"); // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //调用RSA签名方式 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayTradeRefundRequest alipay_request = new AlipayTradeRefundRequest(); AlipayTradeRefundModel model = new AlipayTradeRefundModel(); model.setOutTradeNo(out_trade_no); model.setTradeNo(trade_no); model.setRefundAmount(refund_amount); model.setRefundReason(refund_reason); model.setOutRequestNo(out_request_no); alipay_request.setBizModel(model); AlipayTradeRefundResponse alipay_response = alipayClient.execute(alipay_request); System.out.println("refund========"+alipay_response.getBody()); //全额退款 才能变成报废订单 if (alipay_response.isSuccess()) { HashMap<Object, Object> map=new HashMap<Object, Object>(); map.put("codeTitle", out_trade_no); Code code =codeService.selectByPrimaryCode(map) ; //使之转化成报废订单 code.setCodeStatus(2); int i= codeService.updateByPrimaryKey(code); if(i>0) { System.out.println("refund========订单报废成功"); } else { System.out.println("refund========订单报废失败"); } } else { System.out.println("refund========退款调用失败"); } return alipay_response.getBody(); } /* 查询对账单下载地址 */ @RequestMapping("alipay_downloadurl.html") public String alipaydownloadurl() { return "alipay_downloadurl"; } /* 查询对账单下载地址 */ @RequestMapping("downloadurl.html") @ResponseBody public String AlipayDataDataserviceBillDownloadurlQuery(HttpServletRequest request,HttpServletResponse response,Integer amount) throws IOException, AlipayApiException { // 账单类型,商户通过接口或商户经开放平台授权后其所属服务商通过接口可以获取以下账单类型:trade、signcustomer; // trade指商户基于支付宝交易收单的业务账单;signcustomer是指基于商户支付宝余额收入及支出等资金变动的帐务账单; String bill_type = new String(request.getParameter("WIDbill_type").getBytes("ISO-8859-1"),"UTF-8"); // 账单时间:日账单格式为yyyy-MM-dd,月账单格式为yyyy-MM。 String bill_date = new String(request.getParameter("WIDbill_date").getBytes("ISO-8859-1"),"UTF-8"); // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //调用RSA签名方式 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayDataDataserviceBillDownloadurlQueryRequest alipay_request = new AlipayDataDataserviceBillDownloadurlQueryRequest(); AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel(); model.setBillType(bill_type); model.setBillDate(bill_date); alipay_request.setBizModel(model); AlipayDataDataserviceBillDownloadurlQueryResponse alipay_response = alipayClient.execute(alipay_request); System.out.println("downloadurl========"+alipay_response.getBody()); if (alipay_response.isSuccess()) { System.out.println("downloadurl========调用成功"); } else { System.out.println("downloadurl========调用失败"); } return alipay_response.getBody(); } }

2.5 登陆:买家账户用的是沙箱提供的买家信息登陆

在这里插入图片描述

2.6 付款:买家账户用的是沙箱提供的买家信息付款

支付宝手机网页支付接口调用_第12张图片

2.7 支付成功

支付宝手机网页支付接口调用_第13张图片

3 提示信息

把支付由jsp数据内容页面接口,改写成ssm架构的后台数据支付接口,接口包括订单支付接口,订单查询,订单退款,和退款查询,希望以上内容能对你有些许帮助,相聚有缘,愿你我更上一层楼

你可能感兴趣的:(支付,java,jsp,服务器,前端,java)