对于ping++支付接口对接,其实就是创建一个 charge对象向用户收款。 charge 是一个支付凭据对象,所有和支付相关的要素信息都存储在这个对象中,你的服务端可以通过发起支付请求来创建一个新的 charge对象,也可以随时查询一个或者多个 charge对象的状态。每个 charge对象都拥有一个标识id
,该id
在 Ping系统内唯一。
首先第一步:我们需要去ping++平台进行注册一个商户得到一个应用 ID Ping++ 系统中标识你的应用标识,其次获得 管理平台对应的 API Key,最后获得一个你的私钥和公钥
我这里使用的 管理平台对应的 API Key 是测试的Key,并且使用的是私钥,注册好了之后咋们开始:
首先我们需要导入的jar包
以及引入到jsp界面的脚本文件
jar包和js的下载地址:http://download.csdn.net/detail/qq_27026603/9803005
下面是代码部分:
第一部分
public class PingPlusCharge {
private String appId;
public PingPlusCharge(String appId) {
this.appId = appId;
}
public Charge createCharge(String orderNo, int amount, String subject,
String body, String channel, String clientIP) {
Map chargeMap = new HashMap();
chargeMap.put("amount", amount);// 金额
chargeMap.put("currency", "cny");// 3 位 ISO 货币代码
chargeMap.put("subject", subject);// 商品标题
chargeMap.put("body", body);// 商品描述信息
chargeMap.put("order_no", orderNo); // 商户订单号,
chargeMap.put("channel", channel); // 支付使用的第三方支付渠道
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 15);// 15分钟失效
long timestamp = cal.getTimeInMillis() / 1000L;
chargeMap.put("time_expire", timestamp); // 订单失效时间,用 Unix
// 时间戳表示。时间范围在订单创建后的 1 分钟到
// 15 天,默认为 1 天,创建时间以 Ping++
// 服务器时间为准。 微信对该参数的有效值限制为 2
// 小时内;银联对该参数的有效值限制为 1 小时内。
chargeMap.put("client_ip", clientIP); // 客户端 ip 地址(ipv4)
Map extra = new HashMap();
// extra.put("open_id", "USER_OPENID");
// 取得session信息 再次查询
extra.put(
"success_url",
"http://192.168.2.102:8080/pomConfig/userCommonURL_tiaozhuangChongZhi.html?orderNoAndUserId=" + orderNo);
chargeMap.put("extra", extra);// 特定渠道发起交易时需要的额外参数,以及部分渠道支付成功返回的额外参数
Map app = new HashMap();
app.put("id", appId);// 支付使用的 app 对象的 id
chargeMap.put("app", app);
Charge charge = null;
try {
// 发起交易请求
charge = Charge.create(chargeMap);
// 传到客户端请先转成字符串 .toString(), 调该方法,会自动转成正确的 JSON 字符串
System.out.println(charge);
} catch (PingppException e) {
e.printStackTrace();
}
return charge;
}
}
public class GetCharge{
private static final long serialVersionUID = 1L;
/**
* Pingpp 管理平台对应的 API Key
*/
private final static String apiKey = "##################################";
/**
* Pingpp 管理平台对应的应用 ID
*/
private final static String appId = "##################################";
/**
* 你生成的私钥路径
*/
private final static String privateKeyFilePath = new CapitalCommonAction()
.getClass().getClassLoader().getResource("").getPath()
+ "org/company/group/finance/web/resource/rsa_private_key.pem";
public static Charge getCharge(String orderNo, String amount, String channel)
throws Exception {
// 设置 API Key
Pingpp.apiKey = apiKey;
// 设置私钥路径,用于请求签名
Pingpp.privateKeyPath = privateKeyFilePath;
PingPlusCharge chargetools = new PingPlusCharge(appId);
String clientIP = "127.0.0.1";
String subject = "支付宝支付";
String body = "好吃点";
//int intAmount = Integer.parseInt(amount);
System.out.println(amount);
Charge charge = chargetools.createCharge(orderNo,
Integer.parseInt(amount), subject, body, channel, clientIP);
System.out.println(charge+"--------------------------");
return charge;
}
}
第三部分
@SuppressWarnings("all")
@Controller(value = "capitalCommonAjaxAction")
@Scope(value = "request")
public class CapitalCommonAjaxAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Autowired
private CapitalService capitalService;
private String capital;// 充值金额
private BaseModel baseModel = new BaseModel();
private Charge charge;
private Integer userId;
private String info;
public String ajaxchongzhiMoney() {
try {
if (userId != null) {
Capital mycapital = capitalService.findCapitalByUserId(userId);
if (mycapital != null) {
mycapital.setCapital(mycapital.getCapital()
+ Float.parseFloat(capital));
capitalService.modifyCapital(mycapital);
String orderId = userId + "A" + (new Date()).getTime();
charge = GetCharge.getCharge(orderId, capital,
"alipay_pc_direct");
System.out.println(charge);
this.info = "2";
}
}
} catch (Exception e) {
e.printStackTrace();
}
// request.put("baseModelError", baseModel);
return SUCCESS;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Charge getCharge() {
return charge;
}
public void setCharge(Charge charge) {
this.charge = charge;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
}
jsp的ajax界面
$.ajax({
url : "capitalCommonAjaxURL_ajaxchongzhiMoney.html",
data : param,
async : false,
cache : false,
dataType : "json",
type : "post",
success : function(accp) {
console.log(accp.info);
var charge = accp.charge;
if (accp.info == "2") {
pingpp.createPayment(charge, function(result, err) {
if (result == "success") {
alert("1");
// 只有微信公众账号 wx_pub 支付成功的结果会在这里返回,其他的支付结果都会跳转到 extra 中对应的 URL。
} else if (result == "fail") {
alert("2");
// charge 不正确或者微信公众账号支付失败时会在此处返回
} else if (result == "cancel") {
alert("3");
// 微信公众账号支付取消支付
}
});
}
}
});
info,charge.*
另外参数配置详细的信息:https://www.pingxx.com/api?language=Java#charges-%E6%94%AF%E4%BB%98
管理平台对应的应用 ID
管理平台对应的 API Key
私钥和公钥
最后演示的结果