1. 程式人生 > >基於ping++第三方集成各類支付

基於ping++第三方集成各類支付

gif intval val mount attribute sub header null del

首先需要開通ping++賬戶下所需要的支付渠道 技術分享

以上為掃碼支付的流程,首先需要獲取支付對象charge

技術分享
 1 Charge charge = null;
 2 String appId="app_G840088yHyL0q9mH";
 3 Map<String, String> app = new HashMap<String, String>();
 4 Map<String, Object> chargeMap = new HashMap<String, Object>();
 5 app.put("id",appId);
 6 chargeMap.put("amount", amount.multiply(new
BigDecimal(100)).intValue());//支付金額,單位為分 7 chargeMap.put("app",app); 8 chargeMap.put("currency", "cny"); //人民幣 9 chargeMap.put("subject", subject);//商品描述 10 chargeMap.put("order_no", sn);//訂單號碼 11 chargeMap.put("body", "商品"); 12 chargeMap.put("time_expire",(calendar.getTime().getTime())/1000);//支付過期時間 用時間戳標示 秒 13
Map<String, String> extra = new HashMap<String, String>(); 14 chargeMap.put("extra", extra); 15 chargeMap.put("client_ip", "127.0.0.1"); 16 chargeMap.put("channel","wx_pub"); 17 18 charge = Charge.create(chargeMap);
View Code

返回的charge對象字段裏面有生成二維碼的網址,用它生成二維碼,支付完成之後ping++會發送消息到你的接口中去

接口如下

技術分享
 1 /**
 2      * Ping++ 支付 Webhooks
 3      */
 4     @RequestMapping(value="/plugin_notify",method = RequestMethod.POST)
 5     @ResponseBody
 6     @Transactional
 7     public void pluginNotify(HttpServletRequest request, HttpServletResponse response) {
 8         Pingpp.apiKey = "sk_live_OKi1iTPa9SK0SerX5050i9qL";
 9         // 獲得 http body 內容
10         BufferedReader reader = null;
11         try {
12             request.setCharacterEncoding("UTF8");
13             //獲取頭部所有信息
14             Enumeration headerNames = request.getHeaderNames();
15             while (headerNames.hasMoreElements()) {
16                 String key = (String) headerNames.nextElement();
17                 String value = request.getHeader(key);
18                 System.out.println(key+" "+value);
19             }
20             //獲得http body內容
21             reader = request.getReader();
22             StringBuffer buffer = new StringBuffer();
23             String string;
24             while ((string = reader.readLine()) != null) {
25                 buffer.append(string);
26             }
27             // 解析異步通知數據
28             Event event = Webhooks.eventParse(buffer.toString());
29             //獲取訂單號
30             String eventType = event.getType();
31             if (null == eventType || !("charge.succeeded".equals(eventType))) {
32                 response.setStatus(500);
33             }
34             Charge chargeMap = (Charge) event.getData().getObject();
35             String sn = (String) chargeMap.getOrderNo();
36             OrderEntity orderEntity = orderService.selectBySn(sn);
37             if("charge.succeeded".equals(event.getType())){
38 //                支付成功
39                 orderEntity.setfPaystate(PayStateEnum.successfulPayment.getId());
40                 orderEntity.setfOrderstate(OrderStatusEnum.inEffect.getId());
41                 String pickCode=null;
42 
43                 Integer id =  1;
44                 while (id!=0){
45                     pickCode=RandomUtils.randomStringValue(8);
46                     id=orderService.getPickCode(pickCode);
47                 }
48                 orderEntity.setfPickcode(pickCode);
49 
50                 orderEntity.setfPickstate(PickStateEnum.noPickup.getId());
51                 orderEntity.setfPaysn(((Charge) event.getData().getObject()).getId());
52                 //設置過期時間
53                 long current=System.currentTimeMillis();//當前時間毫秒數
54                 long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零點零分零秒的毫秒數
55                 long twelve=zero+((24*60*60*1000)-1000);//今天23點59分59秒的毫秒數
56                 orderEntity.setfExpiredtime(new Timestamp(twelve));
57                 orderService.update(orderEntity);
58                 response.setStatus(200);
59             } else {
60                 //支付失敗
61                 orderEntity.setfPaystate(PayStateEnum.paymentFailure.getId());
62                 orderEntity.setfOrderstate(OrderStatusEnum.expired.getId());
63                 orderEntity.setfPickstate(Pickstate.Canceled.ordinal());
64                 orderEntity.setfPaysn("");
65                 orderService.update(orderEntity);
66                 response.setStatus(500);
67             }
68         } catch (IOException e) {
69             e.printStackTrace();
70             response.setStatus(500);
71         } finally {
72             if (null != reader) {
73                 try {
74                     reader.close();
75                 } catch (IOException e) {
76                     e.printStackTrace();
77                 }
78             }
79         }
80     }
View Code

完成之後客戶端h5接入代碼如下

 1 success: function (charge) {
 2                                                 pingpp.createPayment(charge, function (result, err) {
 3                                                     if (result == "success") {
 4                                                         // 只有微信公眾賬號 wx_pub 支付成功的結果會在這裏返回,其他的支付結果都會跳轉到 extra 中對應的 URL。
 5                                                         window.location.href = "/order/orders/" + charge.orderNo;
 6                                                     } else if (result == "fail") {
 7                                                         // charge 不正確或者微信公眾賬號支付失敗時會在此處返回
 8                                                     } else if (result == "cancel") {
 9                                                         // 微信公眾賬號支付取消支付
10                                                     }
11                                                 });

以上為一個支付流程

------------------------------------------------------------------------分 隔 符------------------------------------------------------------------------------------------------

ping++賬號需要的配置

ping++需要的支付渠道需要打開,每個支付渠道會需要傳不同的參數放到extra中,詳見api文檔 https://www.pingxx.com/api#支付渠道-extra-參數說明

ping++設置裏面webhooks中需要webhooks回調設置通知事件 api文檔 https://www.pingxx.com/api#event-事件類型

微信公眾號支付 wx_pub中微信設置如下

首先需要在開發-接口權限-網頁授權-功能設置-網頁授權域名 設置域名如圖所示技術分享

還需要設置微信支付-開發配置-支付授權目錄 微信公眾號支付接口 例如

  • http://hebenyt.com/goods/detail/

微信支付需要openId

獲取openID的方法如下:需要先獲得code 再獲得appid(微信賬號有),redirect_uri(回調地址及點擊關註之後你需要跳向的地址)

例如以下url: <a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbe93b5f7e303365e&redirect_uri=http%3A%2F%2Fhebenyt.com%2Fhome%2Flogin&response_type=code&scope=snsapi_base&state=pingpp#wechat_redirect">點擊關註平臺</a>

你需要引導用戶去這個url ,回調地址需要https加密

然後獲取code

通過code獲取openid

技術分享
 1 @RequestMapping(value="/login",method = RequestMethod.GET)
 2     public String login(Model model,HttpServletRequest request , HttpServletResponse response){
 3         HttpSession session = request.getSession();
 4         String codes = request.getParameter("code");
 5         String url="https://api.weixin.qq.com/sns/oauth2/access_token" +
 6                 "?appid=" +wxAppId+
 7                 "&secret=" +wxAppSecret+
 8                 "&code=" +codes+
 9                 "&grant_type=authorization_code";
10         Map<String, String> openId = getUserInfoAccessToken(codes);
11         int x = userService.selectByOpenId(openId.get("openid"));
12         if(x==0){
13             UserEntity userEntity = new UserEntity();
14             userEntity.setfOpenid(openId.get("openid"));
15             userService.insert(userEntity);
16             logger.debug("這裏是x=0的homecontroller中的userId的值哈哈sssssssssss"+userEntity.getfId());
17             session.setAttribute("openId",openId.get("openid"));
18             session.setAttribute("userId", userEntity.getfId());
19         }else{
20             UserEntity userEntity = userService.getByOpenId(openId.get("openid"));
21             session.setAttribute("openId",openId.get("openid"));
22             session.setAttribute("userId", userEntity.getfId());
23             logger.debug("這裏是x=1的homecontroller中的userId的值哈哈sssssssssss"+userEntity.getfId());
24         }
25         return "home";
26     }
View Code 技術分享
 1 /**
 2      * 獲取請求用戶信息的openid,access_token
 3      *
 4      * @param code
 5      * @return
 6      */
 7     public static Map<String, String> getUserInfoAccessToken(String code) {
 8         JsonObject object = null;
 9         Map<String, String> data = new HashMap();
10         try {
11             String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
12                     wxAppId, wxAppSecret, code);
13             DefaultHttpClient httpClient = new DefaultHttpClient();
14             HttpGet httpGet = new HttpGet(url);
15             HttpResponse httpResponse = httpClient.execute(httpGet);
16             HttpEntity httpEntity = httpResponse.getEntity();
17             String tokens = EntityUtils.toString(httpEntity, "utf-8");
18             Gson token_gson = new Gson();
19             object = token_gson.fromJson(tokens, JsonObject.class);
20             data.put("openid", object.get("openid").toString().replaceAll("\"", ""));
21             data.put("access_token", object.get("access_token").toString().replaceAll("\"", ""));
22         } catch (Exception ex) {
23             logger.error("fail to request wechat access token. [error={}]", ex);
24         }
25         return data;
26     }
View Code

以上~~

基於ping++第三方集成各類支付