Razorpay 做爲印度本土的一家支付公司,相似中國的支付寶 微信,本篇記錄一下對接印度第三方支付公司前端
準備工做:java
註冊公司web
申請Razorpay帳號數據庫
申請正式環境json
Razorpay工做臺:api
獲取key_id key_secret 獲取路徑:工做臺-->settings-->API Keys微信
配置webhooks(支付回調) 配置路徑:工做臺-->settings-->webhooksapp
圖片示例:dom
java代碼示例 如下爲建立訂單及客戶支付完成後後續處理:spa
注意點:傳入rzp的金額精度爲分,若是支付爲1盧比,前臺傳來,後臺須要乘以100
--------------------------開始分割線-------------------------------------
pom.xml文件加入依賴
<!--RazorPay start--> <dependency> <groupId>com.razorpay</groupId> <artifactId>razorpay</artifactId> <version>指定版本</version> </dependency> <!--RazorPay end-->
java程序:
//建立razorpay客戶端 RazorpayClient createRazorpayClient() { String keyId = "razorpay儀表盤的key_id"; String keySecret = "razorpay儀表盤的key_secret"; if (StringUtils.isBlank(keyId) || StringUtils.isBlank(keySecret)) { throw new BaseException("Please specify API and Secret Key in configuration file"); } RazorpayClient razorpayClient = null; try { razorpayClient = new RazorpayClient(keyId, keySecret); } catch (RazorpayException e) { throw new BaseException("razorpayClient建立異常", e); } if (razorpayClient == null) { throw new BaseException("razor pay error"); } return razorpayClient; }
//建立本地訂單和razorpay訂單,關聯訂單 public PayDO createOrder(BigDecimal amount) { //省略部分業務邏輯,PayDO爲本地支付類 //傳入第三方的金額,精度爲分因此要 乘以 100 BigDecimal razorPayAmount = amount.multiply(new BigDecimal("100")); //生成本地隨機訂單號 String orderNo = StringRandom.getNumberAndLetterRandom(10); RazorpayClient razorpayClient = createRazorpayClient(); Order order = null; try { //建立第三方訂單 JSONObject orderRequest = new JSONObject(); orderRequest.put("amount", razorPayAmount); orderRequest.put("currency", "INR"); orderRequest.put("receipt", orderNo); orderRequest.put("payment_capture", false); order = razorpayClient.Orders.create(orderRequest); } catch (RazorpayException e) { logger.error(e.getMessage()); } if (order == null) { return BaseResponse.fail("razor order create error"); }
//訂單關聯,入庫 PayDO payDO = new PayDO(); payDO.setOrderNo(orderNo); payDO.setThirdNo(order.get("id")); payDO.setAmount(amount); payDO.setStatus("支付中"); //插入數據庫 int saveSuccess = save(payDO); if (saveSuccess <= 0) { //建立失敗 return null; } logger.info("用戶,建立支付訂單成功,內部訂單號:{},第三方訂單:{}", orderNo, order.get("id")); return payDO; }
到達這一步,返回給前端若是成功,前端調起支付,等待客戶支付
支付成功回調代碼:
@GetMapping("/back/pay") public BaseResponse<PayDO> callBackHandelOrderStatus(@RequestParam Map<String,Object> requestJson){ return payService.handelOrderStatus(requestJson); }
public BaseResponse<PayDO> handelOrderStatus(Map<String, Object> requestJson) { if (requestJson == null) { return BaseResponse.fail("error params"); } String event = requestJson.get("event") == null ? null : requestJson.get("event").toString(); //不爲空則爲回調 if (StringUtils.isNotBlank(event)) { logger.info("支付回調:" + JsonUtils.toJSONString(requestJson)); JSONObject jsonObject = new JSONObject(JsonUtils.toJSONString(requestJson)); try { //獲取rzp那邊的id String orderId = jsonObject.getJSONObject("payload").getJSONObject("payment").getJSONObject("entity").getString("order_id"); if (StringUtils.isNotBlank(orderId)) { //經過rzp的id查詢本地是否存在 PayDO payDO = getByOutNo(orderId); if (payDO != null) { if ("支付成功".equals(payDO.getStatus())) { //成功 return BaseResponse.success(); } //業務代碼省略,文字說明 //再次校驗rzp支付狀態,若是爲成功且本地爲支付中,那麼更新本地狀態 return BaseResponse.success(); } } } catch (Exception e) { logger.error("支付回調處理異常", e); return BaseResponse.fail("error params"); } } }
此文只是簡單的對接引導,若有業務需求,須要對接的能夠聯繫我,rzp支付,rzpx提現均已通過實際場景檢驗,可定製開發
rzp 指 razorpay
rzpx 指 razorpayX
rzp文檔連接:https://razorpay.com/docs/api/
博客書寫不易,轉載請註明出處!