Ping++ 支付接口对接
2019独角兽企业重金招聘Python工程师标准>>>
一、请求charge对象
- package com.bra.modules.util.pingplusplus;
- import com.bra.common.utils.SystemPath;
- import com.pingplusplus.Pingpp;
- import org.springframework.stereotype.Service;
- import java.io.File;
- /**
- * Created by Afon on 16/4/26.
- */
- @Service
- public class PingPlusPlusService {
- /**
- * Pingpp 管理平台对应的 API Key
- */
- private final static String apiKey = "";
- /**
- * Pingpp 管理平台对应的应用 ID
- */
- private final static String appId = "";
- /**
- * 你生成的私钥路径
- */
- private final static String privateKeyFilePath = File.separator+SystemPath.getClassPath()+"res"+ File.separator+"rsa_private_key.pem";
- public static String charge(String orderNo,int amount,String subject,String body,String channel,String clientIP){
- // 设置 API Key
- Pingpp.apiKey = apiKey;
- // 设置私钥路径,用于请求签名
- Pingpp.privateKeyPath = privateKeyFilePath;
- PingPlusCharge charge=new PingPlusCharge(appId);
- String chargeString=charge.createCharge(orderNo,amount,subject,body,channel,clientIP);
- return chargeString;
- }
- }
二、生成charge 对象
- package com.bra.modules.util.pingplusplus;
- import com.pingplusplus.exception.PingppException;
- import com.pingplusplus.model.Charge;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by Afon on 16/4/26.
- */
- public class PingPlusCharge {
- private String appId;
- PingPlusCharge(String appId) {
- this.appId = appId;
- }
- public String createCharge(String orderNo, int amount, String subject, String body, String channel, String clientIP) {
- /**
- * 或者直接设置私钥内容
- Pingpp.privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
- "... 私钥内容字符串 ...\n" +
- "-----END RSA PRIVATE KEY-----\n";
- */
- Map<String, Object> chargeMap = new HashMap<String, Object>();
- chargeMap.put("amount", amount);
- chargeMap.put("currency", "cny");
- 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);
- chargeMap.put("client_ip", clientIP); // 客户端 ip 地址(ipv4)
- Map<String, String> app = new HashMap<String, String>();
- app.put("id", appId);
- chargeMap.put("app", app);
- String chargeString = null;
- try {
- //发起交易请求
- Charge charge = Charge.create(chargeMap);
- // 传到客户端请先转成字符串 .toString(), 调该方法,会自动转成正确的 JSON 字符串
- chargeString = charge.toString();
- } catch (PingppException e) {
- e.printStackTrace();
- }
- return chargeString;
- }
- }
三、webhook
- @RequestMapping(value = "webhooks")
- @ResponseBody
- public void webhooks ( HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
- /*System.out.println("ping++ webhooks");*/
- request.setCharacterEncoding("UTF8");
- //获取头部所有信息
- Enumeration headerNames = request.getHeaderNames();
- String signature=null;
- while (headerNames.hasMoreElements()) {
- String key = (String) headerNames.nextElement();
- String value = request.getHeader(key);
- if("x-pingplusplus-signature".equals(key)){
- signature=value;
- }
- }
- /*System.out.println("signature"+signature);*/
- // 获得 http body 内容
- StringBuffer eventJson=new StringBuffer();
- BufferedReader reader= null;
- try {
- reader = request.getReader();
- do{
- eventJson.append(reader.readLine());
- }while(reader.read()!=-1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- reader.close();
- JSONObject event=JSON.parseObject(eventJson.toString());
- boolean verifyRS=false;
- try {
- PublicKey publicKey= WebhooksVerifyService.getPubKey();
- /* System.out.println(publicKey);*/
- verifyRS=WebhooksVerifyService.verifyData(eventJson.toString(),signature,publicKey);
- } catch (Exception e) {
- e.printStackTrace();
- }
- if(verifyRS) {
- /*System.out.println("签名验证成功");*/
- if ("charge.succeeded".equals(event.get("type"))) {
- JSONObject data = JSON.parseObject(event.get("data").toString());
- JSONObject object = JSON.parseObject(data.get("object").toString());
- String orderId = (String) object.get("order_no");
- /*System.out.println("orderId:"+orderId);*/
- String channel = (String) object.get("channel");
- String payType = null;
- int amountFen = (int) object.get("amount");
- Double amountYuan = amountFen * 1.0 / 100;//ping++扣款,精确到分,而数据库精确到元
- Double weiXinInput = null;
- Double aliPayInput = null;
- Double bankCardInput = null;
- if ("wx".equals(channel)) {
- payType = "4";//支付类型(1:储值卡,2:现金,3:银行卡,4:微信,5:支付宝,6:优惠券,7:打白条;8:多方式付款;9:微信个人,10:支付宝(个人))
- weiXinInput = amountYuan;
- } else if ("alipay".equals(channel)) {
- payType = "5";
- aliPayInput = amountYuan;
- } else if ("upacp".equals(channel) || "upacp_wap".equals(channel) || "upacp_pc".equals(channel)) {
- payType = "3";
- bankCardInput = amountYuan;
- }
- Double couponInput;
- ReserveVenueCons order = reserveAppVenueConsService.get(orderId);
- if (order != null) {
- Double orderPrice = order.getShouldPrice();
- couponInput = orderPrice - amountYuan;//订单金额-ping++扣款 等于优惠金额
- Boolean bool = reserveAppVenueConsService.saveSettlement(order, payType, amountYuan,
- 0.0, bankCardInput, weiXinInput, aliPayInput, couponInput);
- if (bool) {
- /* System.out.println("订单结算成功");*/
- response.setStatus(200);
- //return "订单结算成功";
- } else {
- /* System.out.println("订单结算失败");*/
- //return "订单结算失败";
- response.setStatus(500);
- }
- } else {
- /* System.out.println("该订单不存在");*/
- //return "该订单不存在";
- response.setStatus(500);
- }
- }
- }else{
- /*System.out.println("签名验证失败");*/
- //return "签名验证失败";
- response.setStatus(500);
- }
- }
四、WebhooksVerifyService
- package com.bra.modules.util.pingplusplus;
- import com.bra.common.utils.SystemPath;
- import org.apache.commons.codec.binary.Base64;
- import java.io.*;
- import java.security.*;
- import java.security.spec.X509EncodedKeySpec;
- /**
- * Created by sunkai on 15/5/19. webhooks 验证签名示例
- *
- * 该实例演示如何对 Ping++ webhooks 通知进行验证。
- * 验证是为了让开发者确认该通知来自 Ping++ ,防止恶意伪造通知。用户如果有别的验证机制,可以不进行验证签名。
- *
- * 验证签名需要 签名、公钥、验证信息,该实例采用文件存储方式进行演示。
- * 实际项目中,需要用户从异步通知的 HTTP header 中读取签名,从 HTTP body 中读取验证信息。公钥的存储方式也需要用户自行设定。
- *
- * 该实例仅供演示如何验证签名,请务必不要直接 copy 到实际项目中使用。
- *
- */
- public class WebhooksVerifyService {
- private static String pubKeyPath = File.separator+ SystemPath.getClassPath()+"res"+ File.separator+"pingpp_public_key.pem";
- private static String eventPath = File.separator+SystemPath.getClassPath()+"res"+ File.separator+"webhooks_raw_post_data.json";
- private static String signPath = File.separator+SystemPath.getClassPath()+"res"+ File.separator+"signature.txt";
- /**
- * 验证 webhooks 签名,仅供参考
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- runDemos();
- }
- public static void runDemos() throws Exception {
- // 该数据请从 request 中获取原始 POST 请求数据, 以下仅作为示例
- String webhooksRawPostData = getStringFromFile(eventPath);
- System.out.println("------- POST 原始数据 -------");
- System.out.println(webhooksRawPostData);
- // 签名数据请从 request 的 header 中获取, key 为 X-Pingplusplus-Signature (请忽略大小写, 建议自己做格式化)
- String signature = getStringFromFile(signPath);
- System.out.println("------- 签名 -------");
- System.out.println(signature);
- boolean result = verifyData(webhooksRawPostData, signature, getPubKey());
- System.out.println("验签结果:" + (result ? "通过" : "失败"));
- }
- /**
- * 读取文件, 部署 web 程序的时候, 签名和验签内容需要从 request 中获得
- * @param filePath
- * @return
- * @throws Exception
- */
- public static String getStringFromFile(String filePath) throws Exception {
- FileInputStream in = new FileInputStream(filePath);
- InputStreamReader inReader = new InputStreamReader(in, "UTF-8");
- BufferedReader bf = new BufferedReader(inReader);
- StringBuilder sb = new StringBuilder();
- String line;
- do {
- line = bf.readLine();
- if (line != null) {
- if (sb.length() != 0) {
- sb.append("\n");
- }
- sb.append(line);
- }
- } while (line != null);
- return sb.toString();
- }
- /**
- * 获得公钥
- * @return
- * @throws Exception
- */
- public static PublicKey getPubKey() throws Exception {
- String pubKeyString = getStringFromFile(pubKeyPath);
- pubKeyString = pubKeyString.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", "");
- byte[] keyBytes = Base64.decodeBase64(pubKeyString);
- // generate public key
- X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- PublicKey publicKey = keyFactory.generatePublic(spec);
- return publicKey;
- }
- /**
- * 验证签名
- * @param dataString
- * @param signatureString
- * @param publicKey
- * @return
- * @throws NoSuchAlgorithmException
- * @throws InvalidKeyException
- * @throws SignatureException
- */
- public static boolean verifyData(String dataString, String signatureString, PublicKey publicKey)
- throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
- byte[] signatureBytes = Base64.decodeBase64(signatureString);
- Signature signature = Signature.getInstance("SHA256withRSA");
- signature.initVerify(publicKey);
- signature.update(dataString.getBytes("UTF-8"));
- return signature.verify(signatureBytes);
- }
- }
转载于:https://my.oschina.net/rightemperor/blog/886562
Ping++ 支付接口对接相关推荐
- 第三方银联支付接口对接_聊聊三方支付对接那点事儿(附Demo)
每一个做过支付对接的少年上辈子都是折翼的天使.--题记 三方支付对接是一件比较有意思的事儿,今天就拿这个话题来掰扯掰扯.相信每个做过支付对接的小伙伴都有段血与火的经历,那段日子只有痛苦与煎熬,恨不得大 ...
- 特殊格式的 汇潮支付接口对接
在公司业务中,对接汇潮支付,--------该需求是调用汇潮的支付接口,他们作为中台,由他们调用支付宝接口 在异步回调的时候,遇到了"参数通过 post 方式提交, Content-Type ...
- 支付宝支付接口对接的总结
本周工作最大的困难还是支付宝支付接口的对接. 遇到主要的问题是两个:1. 发送订单给支付宝接口,接口验证签名失败. 2.支付宝付款结束后,发送信息给网站接收方进行二次验签,还是签名过不了.验签的方式是 ...
- Paypal REST API Java 版 PC端商城支付接口对接。
引言: 同类文借鉴链接:http://blog.csdn.net/change_on/article/details/73881791(对此博主万分感谢) Paypal账号注册网址:https://w ...
- Java支付宝支付接口对接(app端)
前言:大致说一下流程,其实支付宝官方文档写的很清楚了,还有就是下面我写的一些描述可能转载了其他博客的内容. 用户在app端提交订单--->选择支付方式即支付宝付款(调用了商家端的付款接口,调用之 ...
- 【项目】关于杉德支付接口对接
文章目录 前言 对接杉德的一键快捷支付 杉德的商家中心 代码 问题 参考文献 前言 该支付就是调用他们的支付页面,绑卡无需我们操作,所有支付操作都有他们控制.对接的支付是,一键快捷支付,参考的文档是他 ...
- C#微信公众号支付接口对接
这次是第二次对接微信公众号的支付了,第一次代码写的很乱,没有找现成的SDK,感觉里面乱七八糟的东西太多不够清爽,而我仅仅是做一个小功能,对接又不复杂干脆自己做. 不想重复做轮子的朋友可以参考一下:Se ...
- 第四方汇聚支付接口对接Php
2020年08月24日 下午15:06:07 原文链接:http://note.youdao.com/noteshare?id=315218dd673a75d8d378a50e5c1644a4& ...
- 富友支付接口对接不是必填的值如何处理
1.拿注册接口举例子 正常需要的签名明文: back_notify_url+"|"+bank_nm+"|"+capAcntNo+"|"+ce ...
最新文章
- c 高级函数的简单用法
- 使用 Firefox攻击Web2.0应用(一)
- python封装方法有几种_python之--------封装
- sql server数据库导入导出bcp方法
- Day Two(Beta)
- sbt笔记二 Running
- SPH(光滑粒子流体动力学)流体模拟实现二:SPH算法(2)-粒子受力分析
- Smali语法汇总(一)
- [iOS] 响应式编程开发-ReactiveCocoa(二)
- 软件基本功:工作目标经常变化,要及时跟进
- ai人工智能的本质和未来_人工智能简介:这就是未来
- 最大公约数与最小公倍数求法 C语言版
- Windows10快捷键合集
- STM32F7 硬件IIC驱动
- Macbook电池优化的七种建议
- 数据为王,聚数学院引领大数据新时代
- RGB排列和Pentile排列有什么区别
- 三菱FX3G_24MT PLC、GS2110_WTBD_N触摸屏实现伺服位置控制编程实例
- 《安士全书》原文及白话版
- Java游戏服务器代码热更新
热门文章
- liunx 命令 之 mkdir 与 touch
- fftw3 嵌入式linux安装,Ubuntu18.04下快速的安装UHD与GnuRadio并连接USRP设备
- linux 64 mysql下载官网_Linux下安装MySQL5.7
- Spring Boot 2 Webflux的全局异常处理
- 【亲测有效】运行docker ps 出现Got permission denied问题的解决方案
- 春季:@Component与@Bean
- Android Studio - 如何更改Android SDK路径
- win11关机后主机依旧运行怎么办 Windows11关机后主机依旧运行的解决方法
- win11杜比视界音效怎么打开 window11开启杜比视界音效的步骤方法
- 计算机一级信息技术基础知识,计算机一级考试之信息技术基础.doc