正题
每天早上起来的鸡蛋豆皮火烧(╯▽╰ )好香~~啊!啃完这个火烧我们打开电脑,回想昨天的任务,按照我们程序员的尿性,有事没事先百度,于是有了接下来各种搜索,然而各种五花八门的博客乱象百出,没有一篇能让大家从头到位,从零到一的从没有到完成整个支付宝接口接入功能。
不过还是证明去官方网站看文档和教程比较实在,并且有什么技术问题问技术客服比较简单有效。
一.支付知识背景了解
我们先来看一看现在支付宝接口调用整个支付的流程是怎样的?(盗用支付宝官方图)
支付包已经将所有支付功能集成在SDK里面,也就是我们Java人员所说的jar包。我们只要调用jar包里面的以上六个方法并传入上面六个方法约定好的参数就可以完成相关的功能。
在这里直接给出支付宝关于这方面知识的文档讲解链接:
https://docs.open.alipay.com/270/105899/ 电脑网站支付快速接入
https://docs.open.alipay.com/270/alipay.trade.page.pay 详细参数讲解
二.前提工作
首先先去这个网站https://docs.open.alipay.com/54/103419 下我们所需的Jar包,我
们点进去这个页面
这时候再看我们下载下来的文件有
但我们只需接入支付宝接口只需要上图红箭头标示的两个jar包,这时候就问我们为什么不用maven依赖dependency引用呢,非常遗憾的告诉大家因为maven线上仓库并没有alipay-sdk-java3.3.2.jar这个包,我们需要先把这个包insall进我们的maven仓库,然后才可以通过maven依赖引入这两个jar包
我们打开cmd命令行使用maven insall命令:
这里给出maven命令,如果还不明白可以去参考这篇博客怎么制作jar包
mvn install:install-file -DgroupId=com.alipay -DartifactId=sdk-Java -Dversion=20151021120052 -Dpackaging=jar -Dfile=alipay-sdk-java20151021120052.jar
在上面的maven install命令中我们需将自己的 -Dfile=后面的名称改成自己下载的jar包名称,然后前面是生成的jar包名字,版本号,id什么的,可以不用修改,这里是我们pom.xml引入这个jar包写的名字,随便什么都可以。但是我们要知道这一步进行完以后我们只是将这个jar包放在了我们的maven仓库里面,并没有放在我们的项目当中。
我们先在这里给出maven依赖(pom.xml)等会会在我们的讲解中用到:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alipay</groupId>
<artifactId>sdk-java</artifactId>
<version>20151021120052</version>
</dependency>
二.支付宝官网操作
1.配置沙箱
首先我们打开这个页面
https://openhome.alipay.com/platform/appDaily.htm?tab=account 进入到支付宝沙箱我们创建一个沙箱后,我们需要填写的信息是一个RSA2的公钥。
(这是我设置完成后的状态)
我们再打开 https://docs.open.alipay.com/200/105311 进入支付宝沙箱讲解,这里面有如何创建一个RSA2公钥的方法
进入这个页面 https://docs.open.alipay.com/291/105971
在这个页面我们可以看出
下载后这个样子:
上图中我们先用1来生成一对公钥和私钥,公钥会直接生成在cmd上面我们复制粘贴在刚在的沙箱页面的应用公钥上 ,这时支付宝会对我们生成一个支付宝公钥.OK,到这里我们的沙箱配置完成,先别着急关这个页面。
2生成的RSA2私钥,等会会在配置中用到,打开用NotePad++或者记事本打开都可以。
然后我们再打开
https://docs.open.alipay.com/270/106291 这个链接
下载后是这个样子的:
我们可以看到这种目录结构是一个Java web 项目,我们用eclipse右键import导入一个Exeisting Project 。。。。
然后就会出现这个
我们先进入AlipayConfig.java这个文件,
package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
/* *
*类名:AlipayConfig
*功能:基础配置类
*详细:设置帐户有关信息及返回路径
*修改日期:2017-04-05
*说明:
*以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
*该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
*/
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "2016092400582266";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxBPnCsdzNrUwp\r\n" +
"L3RXw4Y6sYhKRme1yr6cV3x7juEoV+9xUnnyb5W8qARM+pnfst1lulj5QCXR4b/+\r\n" +
"HWU9ILJhLcWfQKsmue8iVY66KHRjUyY2sc0pq0SP+j5Ef/pEb2Z5Sj/0GIkLJHJT\r\n" +
"bQFQUbBWdCnWSTAxCwkWF8iOXJTDqEbVxUZy5wwi2z4I9faJyeLCL6uWzCLeLJRe\r\n" +
"DLcrfPEmE0aTdiZk98GjvIZvZ8qWsiJr2bjNs1wu+9iXnINcjWbHc+r60gq+WmNg\r\n" +
"96EAa0oZWvuxbyMTb+t2AAiRTeghZJmmtmv2g5ivTKAGjwn5oc1Lo+7GLIXoQiMc\r\n" +
"VaZvNvTJAgMBAAECggEASzPz73Ske3R+AJk6cfaM1e6KGhPjB2ZEqCK6rbs7iGoj\r\n" +
"PR9u00H4eRdgDFwCdviD+i26ktlft9SSPiC628dCQOqzJD7S9zbSkuGKKyf4miG/\r\n" +
"AEl1AZjTHd+QCIXeCqjQ/gI/PTIXNnabYznR7w9lPLVI+NJK+OrZR1t/RouqX5wu\r\n" +
"lA+v3sFpE+fVnKttdUGM0MGnG0JKEcmnt2e0xtMQg80bOPz9OT6IQSW8YWkMQqJK\r\n" +
"nSzLOYisqxhX4v2MI1RWiEQgkg4JcVBiraoVk+8V6KzAN8zy6yz52y2sb8evIpg4\r\n" +
"d9Ch8WoBSY98wj1EXzMnUDX7wedrE0P1CMLSpPUOKQKBgQDiXMmsX5Dh3D0reEXX\r\n" +
"qk4jbPPg60camgoorKBKTJkQZ32rQsGfoF/ppmB80Dma8bNP91EM6DMu/zG+dYGq\r\n" +
"ABvtY/27hABWUaOmlhkThfE8UxnbVeGxkDID9/eAEl74t3hGEW9D1kgB3GRjEB44\r\n" +
"xtNbPyESSNgg3cLdG7sYM7y3dwKBgQDIMk9BmYNLYhny0f9zn7MLPe4GGK9oZ9lv\r\n" +
"99LjymMoWFxZz6Cgh4rUHMmIbNXT4khQOc2lkUccsYSZZQLScEf4auKMBEirxOlP\r\n" +
"AbyOtdmuOZGb5KKog7PYTRpuAGmfFf/dYAx1sTv8vgvfv7aeN/EDVOYN8J70fbdX\r\n" +
"GRTZ44FFvwKBgAVP7COxcnL6ei0E+46eHotSST7kBp0nNEnUE0brGUR7SUW1oVmf\r\n" +
"p+CR2P3U3SU3ut+LjEljQaA+uOohU9OrgPxHNnMqG9/qto54Q0pkAE9MG/U7xLoo\r\n" +
"CiRxs0s8IyJvG9F5I0VSSApTI7/3Zt4qPFeN2bbWIsxSp/oMZo2g2TLtAoGAJnlc\r\n" +
"t/u7utoQqJTpJhxz+V4eh3dpBoSmyhjzo6l6ov9QSEZZ1WAXMyqRJLHd6kmboidp\r\n" +
"uT8RTpdjJ2O9RhokCr7n6QbNvI0sVW29AiQ6wHv52t72z7H390LiKG/V3KgKs03J\r\n" +
"NzknkSwo9ghPHVsc64n+aIFs2wnqGUgNtM4/QtsCgYAx3GEqKF2sWtku/c3P4k+H\r\n" +
"ipuZpKn3pffLQrtcl3wDx2/iEapaKMpTJT9giRt19sdRDXLKCxv+ToY/G2kRg8lJ\r\n" +
"INqU+5Cm0x5XSgo3lzrRjeeeYmABf439qP7qU/MW2EstK5xOSPgULtZJ1dloD6lh\r\n" +
"ixsf+5uhriZq30AWT1UTaA==";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtk1ggapsg03NPApUz6oYmzN9kKfjUIAYVgofIm/zqOU2JZI/46GdMz0j2uOh0w7UiH9ewwRtjQVtGInkww/EYcILvqcev/rCSSypj7LTd1+A24npuaP9W6yeg7jKu9LyrlcbsmGn9F4SSiO4T5CPUO5ixvdCaVHImkgPTuqoYbLNXyjHpupNQ0MhlMexjD5sc+LefPzdoernFWokM+VUrzQzQkM68UCzfdfgCeqMkBA9jI4vBc/MDnwrqGF1r6xoo/pJrNaZmRBRaD77Q3ShEVE7uorIXSKVzwVri6Zrsf9VVRQiDK5verPEoE+FFb8Ry2Ex0FZo8S7Xe6YPBNoWMQIDAQAB";
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://工程公网访问地址/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://工程公网访问地址/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";
// 签名方式
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:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @param sWord 要写入日志里的文本内容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我们需要修改的就是将
app_id (沙盒提供的应用ID)
merchant_private_key(PKCS8格式RSA2私钥)
alipay_public_key(沙盒提供的支付宝公钥)
gatewayUrl(请求支付宝网址)"https://openapi.alipaydev.com/gateway.do“ 这个是沙盒请求的,具体使用的可以百度
notify_url
return_url 最后这两个是完成订单后服务器要调整和被通知的路径,因为只是测试我们这里就不再写了
然后运行项目的index.jsp文件在Tomcat上:
我们点击付款后,可以看到跳转到下面页面:
那我们怎么付款呢?我们需要下载一个沙箱钱包:
然后就可以扫码完成支付功能,但是整个项目也是支付宝提供给我们的Demo,但是我们可以将他们的demo拿来用
比如我们新建一个SSM项目,我们现在pom.xml依赖导入我们上面刚提到的两个核心Jar包
我们只要
完成这四步,直接给出代码,都是从Demo中整合,提炼出来的(大家也可以自己整理,增加对这个支付接口的了解):
AlipayConfig.java
package com.qcb.controllers;
import java.io.FileWriter;
import java.io.IOException;
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "2016092400582266";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxBPnCsdzNrUwp\r\n" +
"L3RXw4Y6sYhKRme1yr6cV3x7juEoV+9xUnnyb5W8qARM+pnfst1lulj5QCXR4b/+\r\n" +
"HWU9ILJhLcWfQKsmue8iVY66KHRjUyY2sc0pq0SP+j5Ef/pEb2Z5Sj/0GIkLJHJT\r\n" +
"bQFQUbBWdCnWSTAxCwkWF8iOXJTDqEbVxUZy5wwi2z4I9faJyeLCL6uWzCLeLJRe\r\n" +
"DLcrfPEmE0aTdiZk98GjvIZvZ8qWsiJr2bjNs1wu+9iXnINcjWbHc+r60gq+WmNg\r\n" +
"96EAa0oZWvuxbyMTb+t2AAiRTeghZJmmtmv2g5ivTKAGjwn5oc1Lo+7GLIXoQiMc\r\n" +
"VaZvNvTJAgMBAAECggEASzPz73Ske3R+AJk6cfaM1e6KGhPjB2ZEqCK6rbs7iGoj\r\n" +
"PR9u00H4eRdgDFwCdviD+i26ktlft9SSPiC628dCQOqzJD7S9zbSkuGKKyf4miG/\r\n" +
"AEl1AZjTHd+QCIXeCqjQ/gI/PTIXNnabYznR7w9lPLVI+NJK+OrZR1t/RouqX5wu\r\n" +
"lA+v3sFpE+fVnKttdUGM0MGnG0JKEcmnt2e0xtMQg80bOPz9OT6IQSW8YWkMQqJK\r\n" +
"nSzLOYisqxhX4v2MI1RWiEQgkg4JcVBiraoVk+8V6KzAN8zy6yz52y2sb8evIpg4\r\n" +
"d9Ch8WoBSY98wj1EXzMnUDX7wedrE0P1CMLSpPUOKQKBgQDiXMmsX5Dh3D0reEXX\r\n" +
"qk4jbPPg60camgoorKBKTJkQZ32rQsGfoF/ppmB80Dma8bNP91EM6DMu/zG+dYGq\r\n" +
"ABvtY/27hABWUaOmlhkThfE8UxnbVeGxkDID9/eAEl74t3hGEW9D1kgB3GRjEB44\r\n" +
"xtNbPyESSNgg3cLdG7sYM7y3dwKBgQDIMk9BmYNLYhny0f9zn7MLPe4GGK9oZ9lv\r\n" +
"99LjymMoWFxZz6Cgh4rUHMmIbNXT4khQOc2lkUccsYSZZQLScEf4auKMBEirxOlP\r\n" +
"AbyOtdmuOZGb5KKog7PYTRpuAGmfFf/dYAx1sTv8vgvfv7aeN/EDVOYN8J70fbdX\r\n" +
"GRTZ44FFvwKBgAVP7COxcnL6ei0E+46eHotSST7kBp0nNEnUE0brGUR7SUW1oVmf\r\n" +
"p+CR2P3U3SU3ut+LjEljQaA+uOohU9OrgPxHNnMqG9/qto54Q0pkAE9MG/U7xLoo\r\n" +
"CiRxs0s8IyJvG9F5I0VSSApTI7/3Zt4qPFeN2bbWIsxSp/oMZo2g2TLtAoGAJnlc\r\n" +
"t/u7utoQqJTpJhxz+V4eh3dpBoSmyhjzo6l6ov9QSEZZ1WAXMyqRJLHd6kmboidp\r\n" +
"uT8RTpdjJ2O9RhokCr7n6QbNvI0sVW29AiQ6wHv52t72z7H390LiKG/V3KgKs03J\r\n" +
"NzknkSwo9ghPHVsc64n+aIFs2wnqGUgNtM4/QtsCgYAx3GEqKF2sWtku/c3P4k+H\r\n" +
"ipuZpKn3pffLQrtcl3wDx2/iEapaKMpTJT9giRt19sdRDXLKCxv+ToY/G2kRg8lJ\r\n" +
"INqU+5Cm0x5XSgo3lzrRjeeeYmABf439qP7qU/MW2EstK5xOSPgULtZJ1dloD6lh\r\n" +
"ixsf+5uhriZq30AWT1UTaA==";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtk1ggapsg03NPApUz6oYmzN9kKfjUIAYVgofIm/zqOU2JZI/46GdMz0j2uOh0w7UiH9ewwRtjQVtGInkww/EYcILvqcev/rCSSypj7LTd1+A24npuaP9W6yeg7jKu9LyrlcbsmGn9F4SSiO4T5CPUO5ixvdCaVHImkgPTuqoYbLNXyjHpupNQ0MhlMexjD5sc+LefPzdoernFWokM+VUrzQzQkM68UCzfdfgCeqMkBA9jI4vBc/MDnwrqGF1r6xoo/pJrNaZmRBRaD77Q3ShEVE7uorIXSKVzwVri6Zrsf9VVRQiDK5verPEoE+FFb8Ry2Ex0FZo8S7Xe6YPBNoWMQIDAQAB";
// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://www.baidu.com";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://www.baidu.com";
// 签名方式
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:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @param sWord 要写入日志里的文本内容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
ApliyTrade.java
package com.qcb.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
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.qcb.controllers.AlipayConfig;
public class AlipayTrade {
public String Refund(HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
//商户订单号,商户网站订单系统中唯一订单号
String out_trade_no = new String(request.getParameter("WIDTRout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("WIDTRtrade_no").getBytes("ISO-8859-1"),"UTF-8");
//请二选一设置
//需要退款的金额,该金额不能大于订单金额,必填
String refund_amount = new String(request.getParameter("WIDTRrefund_amount").getBytes("ISO-8859-1"),"UTF-8");
//退款的原因说明
String refund_reason = new String(request.getParameter("WIDTRrefund_reason").getBytes("ISO-8859-1"),"UTF-8");
//标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
String out_request_no = new String(request.getParameter("WIDTRout_request_no").getBytes("ISO-8859-1"),"UTF-8");
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"trade_no\":\""+ trade_no +"\","
+ "\"refund_amount\":\""+ refund_amount +"\","
+ "\"refund_reason\":\""+ refund_reason +"\","
+ "\"out_request_no\":\""+ out_request_no +"\"}");
//请求
String result = alipayClient.execute(alipayRequest).getBody();
//输出
System.out.println(result);
return result;
}
public String Pagepay(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
//获得初始化的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 = new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//付款金额,必填
String total_amount = new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
//订单名称,必填
String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
//商品描述,可空
String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
//输出
System.out.println(result);
return result;
}
public String Close(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
System.out.println(AlipayConfig.app_id);
//设置请求参数
AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
//商户订单号,商户网站订单系统中唯一订单号
String out_trade_no = new String(request.getParameter("WIDTCout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("WIDTCtrade_no").getBytes("ISO-8859-1"),"UTF-8");
//请二选一设置
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," +"\"trade_no\":\""+ trade_no +"\"}");
//请求
String result = alipayClient.execute(alipayRequest).getBody();
//输出
System.out.println(result);
return result;
}
public String FastpayRefund(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
//商户订单号,商户网站订单系统中唯一订单号
String out_trade_no = new String(request.getParameter("WIDRQout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("WIDRQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
//请二选一设置
//请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填
String out_request_no = new String(request.getParameter("WIDRQout_request_no").getBytes("ISO-8859-1"),"UTF-8");
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+"\"trade_no\":\""+ trade_no +"\","
+"\"out_request_no\":\""+ out_request_no +"\"}");
//请求
String result = alipayClient.execute(alipayRequest).getBody();
//输出
System.out.println(result);
return result;
}
public String Query(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
//设置请求参数
AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
//商户订单号,商户网站订单系统中唯一订单号
String out_trade_no = new String(request.getParameter("WIDTQout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String trade_no = new String(request.getParameter("WIDTQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
//请二选一设置
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+"\"trade_no\":\""+ trade_no +"\"}");
//请求
String result = alipayClient.execute(alipayRequest).getBody();
//输出
System.out.println(result);
return result;
}
}
Newfile.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>支付宝电脑网站支付</title>
<style>
* {
margin: 0;
padding: 0;
}
ul, ol {
list-style: none;
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande",
sans-serif;
}
.tab-head {
margin-left: 120px;
margin-bottom: 10px;
}
.tab-content {
clear: left;
display: none;
}
h2 {
border-bottom: solid #02aaf1 2px;
width: 200px;
height: 25px;
margin: 0;
float: left;
text-align: center;
font-size: 16px;
}
.selected {
color: #FFFFFF;
background-color: #02aaf1;
}
.show {
clear: left;
display: block;
}
.hidden {
display: none;
}
.new-btn-login-sp {
padding: 1px;
display: inline-block;
width: 75%;
}
.new-btn-login {
background-color: #02aaf1;
color: #FFFFFF;
font-weight: bold;
border: none;
width: 100%;
height: 30px;
border-radius: 5px;
font-size: 16px;
}
#main {
width: 100%;
margin: 0 auto;
font-size: 14px;
}
.red-star {
color: #f00;
width: 10px;
display: inline-block;
}
.null-star {
color: #fff;
}
.content {
margin-top: 5px;
}
.content dt {
width: 100px;
display: inline-block;
float: left;
margin-left: 20px;
color: #666;
font-size: 13px;
margin-top: 8px;
}
.content dd {
margin-left: 120px;
margin-bottom: 5px;
}
.content dd input {
width: 85%;
height: 28px;
border: 0;
-webkit-border-radius: 0;
-webkit-appearance: none;
}
#foot {
margin-top: 10px;
position: absolute;
bottom: 15px;
width: 100%;
}
.foot-ul {
width: 100%;
}
.foot-ul li {
width: 100%;
text-align: center;
color: #666;
}
.note-help {
color: #999999;
font-size: 12px;
line-height: 130%;
margin-top: 5px;
width: 100%;
display: block;
}
#btn-dd {
margin: 20px;
text-align: center;
}
.foot-ul {
width: 100%;
}
.one_line {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #eeeeee;
width: 100%;
margin-left: 20px;
}
.am-header {
display: -webkit-box;
display: -ms-flexbox;
display: box;
width: 100%;
position: relative;
padding: 7px 0;
-webkit-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
background: #1D222D;
height: 50px;
text-align: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
box-pack: center;
-webkit-box-align: center;
-ms-flex-align: center;
box-align: center;
}
.am-header h1 {
-webkit-box-flex: 1;
-ms-flex: 1;
box-flex: 1;
line-height: 18px;
text-align: center;
font-size: 18px;
font-weight: 300;
color: #fff;
}
</style>
</head>
<body text=#000000 bgColor="#ffffff" leftMargin=0 topMargin=4>
<header class="am-header">
<h1>这是支付宝电脑网站支付体验入口页</h1>
</header>
<div id="main">
<div id="tabhead" class="tab-head">
<h2 id="tab1" class="selected" name="tab">付 款</h2>
<h2 id="tab2" name="tab">交 易 查 询</h2>
<h2 id="tab3" name="tab">退 款</h2>
<h2 id="tab4" name="tab">退 款 查 询</h2>
<h2 id="tab5" name="tab">交 易 关 闭</h2>
</div>
<form name=alipayment action=http://localhost:8080/Myssm/views/hello0 method=post
target="_blank">
<div id="body1" class="show" name="divcontent">
<dl class="content">
<dt>商户订单号 :</dt>
<dd>
<input id="WIDout_trade_no" name="WIDout_trade_no" />
</dd>
<hr class="one_line">
<dt>订单名称 :</dt>
<dd>
<input id="WIDsubject" name="WIDsubject" />
</dd>
<hr class="one_line">
<dt>付款金额 :</dt>
<dd>
<input id="WIDtotal_amount" name="WIDtotal_amount" />
</dd>
<hr class="one_line">
<dt>商品描述:</dt>
<dd>
<input id="WIDbody" name="WIDbody" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
style="text-align: center;">付 款</button>
</span> <span class="note-help">如果您点击“付款”按钮,即表示您同意该次的执行操作。</span>
</dd>
</dl>
</div>
</form>
<form name=tradequery action=http://localhost:8080/Myssm/views/hello1 method=post
target="_blank">
<div id="body2" class="tab-content" name="divcontent">
<dl class="content">
<dt>商户订单号 :</dt>
<dd>
<input id="WIDTQout_trade_no" name="WIDTQout_trade_no" />
</dd>
<hr class="one_line">
<dt>支付宝交易号 :</dt>
<dd>
<input id="WIDTQtrade_no" name="WIDTQtrade_no" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
style="text-align: center;">交 易 查 询</button>
</span> <span class="note-help">商户订单号与支付宝交易号二选一,如果您点击“交易查询”按钮,即表示您同意该次的执行操作。</span>
</dd>
</dl>
</div>
</form>
<form name=traderefund action=alipay.trade.refund.jsp method=post
target="_blank">
<div id="body3" class="tab-content" name="divcontent">
<dl class="content">
<dt>商户订单号 :</dt>
<dd>
<input id="WIDTRout_trade_no" name="WIDTRout_trade_no" />
</dd>
<hr class="one_line">
<dt>支付宝交易号 :</dt>
<dd>
<input id="WIDTRtrade_no" name="WIDTRtrade_no" />
</dd>
<hr class="one_line">
<dt>退款金额 :</dt>
<dd>
<input id="WIDTRrefund_amount" name="WIDTRrefund_amount" />
</dd>
<hr class="one_line">
<dt>退款原因 :</dt>
<dd>
<input id="WIDTRrefund_reason" name="WIDTRrefund_reason" />
</dd>
<hr class="one_line">
<dt>退款请求号 :</dt>
<dd>
<input id="WIDTRout_request_no" name="WIDTRout_request_no" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
style="text-align: center;">退 款</button>
</span> <span class="note-help">商户订单号与支付宝交易号二选一,如果您点击“退款”按钮,即表示您同意该次的执行操作。</span>
</dd>
</dl>
</div>
</form>
<form name=traderefundquery
action=alipay.trade.fastpay.refund.query.jsp method=post
target="_blank">
<div id="body4" class="tab-content" name="divcontent">
<dl class="content">
<dt>商户订单号 :</dt>
<dd>
<input id="WIDRQout_trade_no" name="WIDRQout_trade_no" />
</dd>
<hr class="one_line">
<dt>支付宝交易号 :</dt>
<dd>
<input id="WIDRQtrade_no" name="WIDRQtrade_no" />
</dd>
<hr class="one_line">
<dt>退款请求号 :</dt>
<dd>
<input id="WIDRQout_request_no" name="WIDRQout_request_no" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
style="text-align: center;">退 款 查 询</button>
</span> <span class="note-help">商户订单号与支付宝交易号二选一,如果您点击“退款查询”按钮,即表示您同意该次的执行操作。</span>
</dd>
</dl>
</div>
</form>
<form name=tradeclose action=alipay.trade.close.jsp method=post
target="_blank">
<div id="body5" class="tab-content" name="divcontent">
<dl class="content">
<dt>商户订单号 :</dt>
<dd>
<input id="WIDTCout_trade_no" name="WIDTCout_trade_no" />
</dd>
<hr class="one_line">
<dt>支付宝交易号 :</dt>
<dd>
<input id="WIDTCtrade_no" name="WIDTCtrade_no" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
style="text-align: center;">交 易 关 闭</button>
</span> <span class="note-help">商户订单号与支付宝交易号二选一,如果您点击“交易关闭”按钮,即表示您同意该次的执行操作。</span>
</dd>
</dl>
</div>
</form>
<div id="foot">
<ul class="foot-ul">
<li>支付宝版权所有 2015-2018 ALIPAY.COM</li>
</ul>
</div>
</div>
</body>
<script language="javascript">
var tabs = document.getElementsByName('tab');
var contents = document.getElementsByName('divcontent');
(function changeTab(tab) {
for(var i = 0, len = tabs.length; i < len; i++) {
tabs[i].onmouseover = showTab;
}
})();
function showTab() {
for(var i = 0, len = tabs.length; i < len; i++) {
if(tabs[i] === this) {
tabs[i].className = 'selected';
contents[i].className = 'show';
} else {
tabs[i].className = '';
contents[i].className = 'tab-content';
}
}
}
function GetDateNow() {
var vNow = new Date();
var sNow = "";
sNow += String(vNow.getFullYear());
sNow += String(vNow.getMonth() + 1);
sNow += String(vNow.getDate());
sNow += String(vNow.getHours());
sNow += String(vNow.getMinutes());
sNow += String(vNow.getSeconds());
sNow += String(vNow.getMilliseconds());
document.getElementById("WIDout_trade_no").value = sNow;
document.getElementById("WIDsubject").value = "测试";
document.getElementById("WIDtotal_amount").value = "0.01";
}
GetDateNow();
</script>
</html>
index.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/views/";
%>
<html>
<head>
<link href="<%=basePath%>css/classification/Classification_page.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
<h2>Hello World!</h2>
</body>
</html>