1.首先导入jar
netpayclient.jar
2.导入需要的公钥和商户私钥
3.application.properties配置
CHINAPAY_MERCHANT_NO=xxxxxxxxxx #商户号
PUBLIC_KEY_PATH=/chinapay/PgPubk_new.key #公钥路径
MERCHANT_KEY_PATH=/chinapay/MerPrK_808080301002146.key #私钥路径
CHINAPAY_Version=20141120 #版本号
CHINAPAY_OrderHead=000002146 #订单头
CHINAPAY_CuryId=156 #交易币种
CHINAPAY_TransType=0001 #交易类型(付款)
CHINAPAY_BgRetUrl=https://www.hhr360.com/user_recharge/bgRet.htm #回调后台地址
CHINAPAY_PageRetUrl=https://www.hhr360.com/user_recharge/pageRet.htm #回调前台地址
CHINAPAY_GateId= #支付网关号,可选
CHINAPAY_Priv1=hhr360 #商户私有域,可选
CHINAPAY_QueryUrl=http://console.chinapay.com/QueryWeb/processQuery.jsp #查询地址
4.支付action /**
* 银联充值申请成功
*
* @return
* @throws Exception
*/
@Action("quickSucced")
public String quickSucced() {
try {
FuRecharge recharge = fuRechargeService.get(id);
if (recharge != null) {
chinapay.PrivateKey key1 = new chinapay.PrivateKey();
chinapay.SecureLink t;
// 注意要用哪一个商户号签名就要用对应的key文件
key1.buildKey(Property.getProperty("CHINAPAY_MERCHANT_NO"), 0, this.getServletContext().getRealPath(Property.getProperty("MERCHANT_KEY_PATH")));
t = new chinapay.SecureLink(key1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
NumberFormat ndf1 = new DecimalFormat("0000000");
NumberFormat ndf2 = new DecimalFormat("000000000000");
// 支付订单数据准备
String MerId = Property.getProperty("CHINAPAY_MERCHANT_NO");
String OrdId = Property.getProperty("CHINAPAY_OrderHead") + ndf1.format(recharge.getId());// 000002146+0000074
String TransDate = sdf.format(new Date());
String Version = Property.getProperty("CHINAPAY_Version");
String TransAmt = ndf2.format(recharge.getRechargeMoney().multiply(new BigDecimal(100)));
String CuryId = Property.getProperty("CHINAPAY_CuryId");
String TransType = Property.getProperty("CHINAPAY_TransType");
String BgRetUrl = Property.getProperty("CHINAPAY_BgRetUrl");
String PageRetUrl = Property.getProperty("CHINAPAY_PageRetUrl");
String GateId = Property.getProperty("CHINAPAY_GateId");
String Priv1 = Property.getProperty("CHINAPAY_Priv1");
String ChkValue = t.Sign(MerId + OrdId + TransAmt + CuryId + TransDate + TransType + Version + BgRetUrl + PageRetUrl + GateId + Priv1);
JSONObject obj = new JSONObject();
obj.put("MerId", MerId);
obj.put("OrdId", OrdId);
obj.put("TransAmt", TransAmt);
obj.put("CuryId", CuryId);
obj.put("TransDate", TransDate);
obj.put("TransType", TransType);
obj.put("Version", Version);
obj.put("BgRetUrl", BgRetUrl);
obj.put("PageRetUrl", PageRetUrl);
obj.put("GateId", GateId);
obj.put("Priv1", Priv1);
obj.put("ChkValue", ChkValue);
this.getActionContext().put("obj", obj);
}
this.getActionContext().put("poundageMoney", poundageMoney);//上一个页面传过来的参数:手续费
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
/**
* 银联充值申请成功
*
* @return
* @throws Exception
*/
@Action("quickSucced")
public String quickSucced() {
try {
FuRecharge recharge = fuRechargeService.get(id);
if (recharge != null) {
chinapay.PrivateKey key1 = new chinapay.PrivateKey();
chinapay.SecureLink t;
// 注意要用哪一个商户号签名就要用对应的key文件
key1.buildKey(Property.getProperty("CHINAPAY_MERCHANT_NO"), 0, this.getServletContext().getRealPath(Property.getProperty("MERCHANT_KEY_PATH")));
t = new chinapay.SecureLink(key1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
NumberFormat ndf1 = new DecimalFormat("0000000");
NumberFormat ndf2 = new DecimalFormat("000000000000");
// 支付订单数据准备
String MerId = Property.getProperty("CHINAPAY_MERCHANT_NO");
String OrdId = Property.getProperty("CHINAPAY_OrderHead") + ndf1.format(recharge.getId());// 000002146+0000074
String TransDate = sdf.format(new Date());
String Version = Property.getProperty("CHINAPAY_Version");
String TransAmt = ndf2.format(recharge.getRechargeMoney().multiply(new BigDecimal(100)));
String CuryId = Property.getProperty("CHINAPAY_CuryId");
String TransType = Property.getProperty("CHINAPAY_TransType");
String BgRetUrl = Property.getProperty("CHINAPAY_BgRetUrl");
String PageRetUrl = Property.getProperty("CHINAPAY_PageRetUrl");
String GateId = Property.getProperty("CHINAPAY_GateId");
String Priv1 = Property.getProperty("CHINAPAY_Priv1");
String ChkValue = t.Sign(MerId + OrdId + TransAmt + CuryId + TransDate + TransType + Version + BgRetUrl + PageRetUrl + GateId + Priv1);
JSONObject obj = new JSONObject();
obj.put("MerId", MerId);
obj.put("OrdId", OrdId);
obj.put("TransAmt", TransAmt);
obj.put("CuryId", CuryId);
obj.put("TransDate", TransDate);
obj.put("TransType", TransType);
obj.put("Version", Version);
obj.put("BgRetUrl", BgRetUrl);
obj.put("PageRetUrl", PageRetUrl);
obj.put("GateId", GateId);
obj.put("Priv1", Priv1);
obj.put("ChkValue", ChkValue);
this.getActionContext().put("obj", obj);
}
this.getActionContext().put("poundageMoney", poundageMoney);//上一个页面传过来的参数:手续费
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
5.提交表单给银联
<div id="one_1" class="display-b">
<form action="https://payment.chinapay.com/pay/TransGet" target="_blank" id="chinaForm" METHOD=POST>
<input type=hidden name="MerId" id="MerId" value="${obj.MerId }">
<input type=hidden name="OrdId" id="OrdId" value="${obj.OrdId }">
<input type=hidden name="TransAmt" id="TransAmt" value="${obj.TransAmt }">
<input type=hidden name="CuryId" id="CuryId" value="${obj.CuryId }">
<input type=hidden name="TransDate" id="TransDate" value="${obj.TransDate }">
<input type=hidden name="TransType" id="TransType" value="${obj.TransType }">
<input type=hidden name="Version" id="Version" value="${obj.Version }">
<input type=hidden name="BgRetUrl" id="BgRetUrl" value="${obj.BgRetUrl }">
<input type=hidden name="PageRetUrl" id="PageRetUrl" value="${obj.PageRetUrl }">
<input type=hidden name="GateId" id="GateId" value="${obj.GateId }">
<input type=hidden name="Priv1" id="Priv1" value="${obj.Priv1 }">
<input type=hidden name="ChkValue" id="ChkValue" value="${obj.ChkValue }">
<div class=" ny_bt" style="height:30px;">
<span class="pbsx"></span><span class="ny_bt_cont">充值申请成功</span>
</div>
<div class="form czjel" style="height:30px;">
<div class="fp form_fontl">账户余额:</div>
<div class="fp radio" style="line-height:18px;">
<span class="zhye"><fmt:formatNumber value="${empty fuUser.accountBalance?0:fuUser.accountBalance}" pattern="#,###,##0.00"/></span>元
</div>
<div class="clr"></div>
</div>
<div class="form czjel" style="height:30px;">
<div class="fp form_fontl">充值金额:</div>
<div class="fp radio" >
<fmt:formatNumber value="${obj.TransAmt/100}" pattern="#,###,##0.00"/>元
</div>
<div class="clr"></div>
</div>
<div class="form czjel" style="height:30px;">
<div class="fp form_fontl">充值手续费:</div>
<div class="fp radio">
<span class="zhye">${poundageMoney}</span>元
</div>
<div class="clr"></div>
</div>
<div class="form czjel" style="height:30px;" >
<div class="fp form_fontl">实际支付金额:</div>
<div class="fp radio" >
<fmt:formatNumber value="${obj.TransAmt/100}" pattern="#,###,##0.00"/>元
</div>
<div class="clr"></div>
</div>
<div style="margin:30px 0 30px 20px; padding-bottom:30px;">
<div style="margin:30px 0 30px 100px;" style="height:30px;">
<input name="" type="submit" οnclick="toPay();" value="去付款" class="tijiao">
</div>
</div>
</form>
</div>
6.回调action,根据支付返回的状态码做相关业务处理
package com.hongwei.futures.web.action.user_recharge;
import java.math.BigDecimal;
import java.util.Date;
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.hongwei.futures.model.FuRecharge;
import com.hongwei.futures.model.FuUser;
import com.hongwei.futures.service.FuAdminService;
import com.hongwei.futures.service.FuRechargeService;
import com.hongwei.futures.util.Property;
import com.hongwei.futures.web.action.BaseAction;
@ParentPackage("fu_common")
public class ChinaBackAction extends BaseAction {
@Autowired
private FuRechargeService fuRechargeService;
@Autowired
private FuAdminService fuAdminService;
private String merid;
private String transdate;
private String orderno;
private String transtype;
private String amount;
private String currencycode;
private String status;
private String checkvalue;
/**
* 银联支付反馈的后台地址
*
* @return
* @throws Exception
*/
@Action("bgRet")
public String bgRet() {
chinapay.PrivateKey key = new chinapay.PrivateKey();
chinapay.SecureLink t;
boolean flag = key.buildKey("999999999999999", 0, this.getServletContext().getRealPath(Property.getProperty("PUBLIC_KEY_PATH")));
if (flag == false) {
return SUCCESS;
}
t = new chinapay.SecureLink(key);
boolean flag1 = t.verifyTransResponse(merid, orderno, amount, currencycode, transdate, transtype, status, checkvalue);
if (flag1 == false) {
} else if (status.equals("1001")) {//支付成功
String id = orderno.substring(9);
FuRecharge recharge = fuRechargeService.get(Long.parseLong(id));
try {
if (recharge != null && recharge.getRechargeStatus() == 0) {// 如果状态还是未审核
recharge.setPayStatus(1);
recharge.setOrderNum(orderno);
recharge.setPayTime(new Date());
recharge.setRechargeStatus(2);// 通过
recharge.setFuAdmin(fuAdminService.get(1L));
recharge.setCheckTime(new Date());
FuUser user = recharge.getFuUser();
// 充值总额
user.setRechargeMoney((user.getRechargeMoney() == null ? new BigDecimal(0.00) : user.getRechargeMoney()).add(recharge.getRechargeMoney()));
user.setAccountBalance(user.getAccountBalance().add(recharge.getRechargeMoney()));
fuRechargeService.saveRecharge(recharge, user);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (!status.equals("1001")) {//支付失败
String id = orderno.substring(9);
FuRecharge recharge = fuRechargeService.get(Long.parseLong(id));
try {
if (recharge != null && recharge.getRechargeStatus() == 0) {// 如果状态还是未审核
recharge.setPayStatus(0);
recharge.setPayTime(null);
recharge.setRechargeStatus(3);// 拒绝
recharge.setFuAdmin(fuAdminService.get(1L));
recharge.setCheckTime(new Date());
fuRechargeService.save(recharge);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 银联支付反馈的前台地址
*
* @return
* @throws Exception
*/
@Action(value = "pageRet", results = { @Result(type = "redirect", location = "/user_recharge/rechargeList.htm") })
public String pageRet() {
chinapay.PrivateKey key = new chinapay.PrivateKey();
chinapay.SecureLink t;
t = new chinapay.SecureLink(key);
boolean flag = key.buildKey("999999999999999", 0, this.getServletContext().getRealPath(Property.getProperty("PUBLIC_KEY_PATH")));
if (flag == false) {
return SUCCESS;
}
boolean flag1 = t.verifyTransResponse(merid, orderno, amount, currencycode, transdate, transtype, status, checkvalue);
if (flag1 == false) {
} else if (status.equals("1001")) {//支付成功
String id = orderno.substring(9);
FuRecharge recharge = fuRechargeService.get(Long.parseLong(id));
try {
if (recharge != null && recharge.getRechargeStatus() == 0) {// 如果状态还是未审核
recharge.setPayStatus(1);
recharge.setOrderNum(orderno);
recharge.setPayTime(new Date());
recharge.setRechargeStatus(2);// 通过
recharge.setFuAdmin(fuAdminService.get(1L));
recharge.setCheckTime(new Date());
FuUser user = recharge.getFuUser();
// 充值总额
user.setRechargeMoney((user.getRechargeMoney() == null ? new BigDecimal(0.00) : user.getRechargeMoney()).add(recharge.getRechargeMoney()));
user.setAccountBalance(user.getAccountBalance().add(recharge.getRechargeMoney()));
fuRechargeService.saveRecharge(recharge, user);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (!status.equals("1001")) {//支付失败
String id = orderno.substring(9);
FuRecharge recharge = fuRechargeService.get(Long.parseLong(id));
try {
if (recharge != null && recharge.getRechargeStatus() == 0) {// 如果状态还是未审核
recharge.setPayStatus(0);
recharge.setPayTime(null);
recharge.setRechargeStatus(3);// 拒绝
recharge.setFuAdmin(fuAdminService.get(1L));
recharge.setCheckTime(new Date());
fuRechargeService.save(recharge);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return SUCCESS;
}
public FuRechargeService getFuRechargeService() {
return fuRechargeService;
}
public void setFuRechargeService(FuRechargeService fuRechargeService) {
this.fuRechargeService = fuRechargeService;
}
public FuAdminService getFuAdminService() {
return fuAdminService;
}
public void setFuAdminService(FuAdminService fuAdminService) {
this.fuAdminService = fuAdminService;
}
public String getMerid() {
return merid;
}
public void setMerid(String merid) {
this.merid = merid;
}
public String getTransdate() {
return transdate;
}
public void setTransdate(String transdate) {
this.transdate = transdate;
}
public String getOrderno() {
return orderno;
}
public void setOrderno(String orderno) {
this.orderno = orderno;
}
public String getTranstype() {
return transtype;
}
public void setTranstype(String transtype) {
this.transtype = transtype;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getCurrencycode() {
return currencycode;
}
public void setCurrencycode(String currencycode) {
this.currencycode = currencycode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCheckvalue() {
return checkvalue;
}
public void setCheckvalue(String checkvalue) {
this.checkvalue = checkvalue;
}
}
下面是效果图