1.让公司申请支付宝商户账号,准备好下面四个资料
合作者账号合作者身份(PID)
安全校验码(Key)
商户公钥
2.导入jar
3.application.properties配置
ALIPAY_PARTNER=xxx
ALIPAY_SELLER_EMAIL=xxx
ALIPAY_KEY=xxx
ALIPAY_NOTIFY_URL=https://www.hhr360.com/user_recharge/notify_url.htm
ALIPAY_PUBLIC_KEY=xxx
ALIPAY_PARTNER=xxx
ALIPAY_SELLER_EMAIL=xxx
ALIPAY_KEY=xxx
ALIPAY_NOTIFY_URL=https://www.hhr360.com/user_recharge/notify_url.htm
ALIPAY_PUBLIC_KEY=xxx
4.在项目里加入支付宝java即时到账demo的文件,如下
5.修改com.alipay.config.AlipayConfig.java
package com.alipay.config;
import com.hongwei.futures.util.Property;
/* *
*类名:AlipayConfig
*功能:基础配置类
*详细:设置帐户有关信息及返回路径
*版本:3.3
*日期:2012-08-10
*说明:
*以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
*该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
*提示:如何获取安全校验码和合作身份者ID
*1.用您的签约支付宝账号登录支付宝网站(www.alipay.com)
*2.点击“商家服务”(https://b.alipay.com/order/myOrder.htm)
*3.点击“查询合作者身份(PID)”、“查询安全校验码(Key)”
*安全校验码查看时,输入支付密码后,页面呈灰色的现象,怎么办?
*解决方法:
*1、检查浏览器配置,不让浏览器做弹框屏蔽设置
*2、更换浏览器或电脑,重新登录查询。
*/
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 合作身份者ID,以2088开头由16位纯数字组成的字符串
public static String partner = Property.getProperty("ALIPAY_PARTNER");
// 收款支付宝账号
public static String seller_email = Property.getProperty("ALIPAY_SELLER_EMAIL");
// 商户的安全校验码
public static String key = Property.getProperty("ALIPAY_KEY");
//商户公钥
public static String ali_public_key = Property.getProperty("ALIPAY_PUBLIC_KEY");
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
// 调试用,创建TXT日志文件夹路径
public static String log_path = "D:\\";
// 字符编码格式 目前支持 gbk 或 utf-8
public static String input_charset = "utf-8";
// 签名方式 不需修改
public static String sign_type = "MD5";
}
6.服务端逻辑(前台支付请求调用"${ctx}/user_recharge/saveAliPay.htm?feeMoney="+feeMoney)
package com.hongwei.futures.web.action.user_recharge;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import com.alipay.config.AlipayConfig;
import com.alipay.util.AlipayNotify;
import com.alipay.util.AlipaySubmit;
import com.hongwei.futures.model.FuUser;
import com.hongwei.futures.service.FuRechargeService;
import com.hongwei.futures.service.FuSmsLogService;
import com.hongwei.futures.util.Property;
import com.hongwei.futures.web.action.BaseAction;
@ParentPackage("fu_common")
public class AliPayAction extends BaseAction {
@Autowired
private FuRechargeService fuRechargeService;
@Autowired
private FuSmsLogService fuSmsLogService;
private Long id;
private FuUser fuUser;
private String feeMoney;
/**
* 支付宝支付请求
*
* @return
* @throws Exception
*/
@Action("saveAliPay")
public String saveAliPay() {
try {
// 支付类型
String payment_type = "1";
// 必填,不能修改
// 服务器异步通知页面路径
String notify_url = Property.getProperty("ALIPAY_NOTIFY_URL");
// 需http://格式的完整路径,不能加?id=123这类自定义参数
// 页面跳转同步通知页面路径
String return_url = Property.getProperty("ALIPAY_RETURN_URL");
// 需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/
// 商户订单号
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
String tradeno = sdf.format(new Date()) + String.valueOf((int) (Math.random() * 90 + 10));
String out_trade_no = new String(tradeno.getBytes("ISO-8859-1"), "UTF-8");
// 商户网站订单系统中唯一订单号,必填
// 订单名称
String subject = "超级合伙人平台充值";
// 必填
// 付款金额
String total_fee = new String(feeMoney.getBytes("ISO-8859-1"), "UTF-8");
// 必填
// 订单描述
String body = "超级合伙人平台充值";
// 商品展示地址
String show_url = new String(("http://www.hhr360.com").getBytes("ISO-8859-1"), "UTF-8");
// 需以http://开头的完整路径,例如:http://www.商户网址.com/myorder.html
// 防钓鱼时间戳
String anti_phishing_key = AlipaySubmit.query_timestamp();
// 若要使用请调用类文件submit中的query_timestamp函数
// 客户端的IP地址
String exter_invoke_ip = "";
// 非局域网的外网IP地址,如:221.0.0.1
//
// 把请求参数打包成数组
Map<String, String> sParaTemp = new HashMap<String, String>();
sParaTemp.put("service", "create_direct_pay_by_user");
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("seller_email", AlipayConfig.seller_email);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
sParaTemp.put("payment_type", payment_type);
sParaTemp.put("notify_url", notify_url);
sParaTemp.put("return_url", return_url);
sParaTemp.put("out_trade_no", out_trade_no);
sParaTemp.put("subject", subject);
sParaTemp.put("total_fee", total_fee);
sParaTemp.put("body", body);
sParaTemp.put("show_url", show_url);
sParaTemp.put("anti_phishing_key", anti_phishing_key);
sParaTemp.put("exter_invoke_ip", exter_invoke_ip);
// 建立请求
String sHtmlText = AlipaySubmit.buildRequest(sParaTemp, "post", "确认");
write(sHtmlText);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 异步通知付款状态
*
* @return
*/
@Action(value = "notify_url", results = { @Result(type = "redirect", location = "/user_recharge/successPay.htm") })
public String notify_url() {
try {
// 获取支付宝POST过来反馈信息
Map<String, String> params = new HashMap<String, String>();
Map requestParams = this.getHttpServletRequest().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"),
// "gbk");
params.put(name, valueStr);
}
// 获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
// 商户订单号
String out_trade_no = new String(this.getHttpServletRequest().getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
// 支付宝交易号
String trade_no = new String(this.getHttpServletRequest().getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
// 交易状态
String trade_status = new String(this.getHttpServletRequest().getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
// 交易金额
String total_fee = new String(this.getHttpServletRequest().getParameter("total_fee").getBytes("ISO-8859-1"), "UTF-8");
// 获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
if (AlipayNotify.verify(params)) {// 验证成功
if (trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) {
//支付成功后的业务处理
Map<String, Object> map = new HashMap<String, Object>();
map.put("orderNum", out_trade_no);
Integer count = fuRechargeService.getCount(map);
if (count == 0 && null != fuUser) {
fuRechargeService.saveAliPay(fuUser, out_trade_no, total_fee);
}
} else {
System.out.println("支付失败");
}
} else {// 验证失败
System.out.println("签名验证失败");
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
/**
* 支付成功
*
* @return
*/
@Action("successPay")
public String successPay() {
return SUCCESS;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public FuUser getFuUser() {
return fuUser;
}
public void setFuUser(FuUser fuUser) {
this.fuUser = fuUser;
}
public String getFeeMoney() {
return feeMoney;
}
public void setFeeMoney(String feeMoney) {
this.feeMoney = feeMoney;
}
}
下面是效果图