支付寶網站掃碼支付流程以及回撥程式碼
阿新 • • 發佈:2021-11-03
1:https://opendocs.alipay.com/open/028sma網址 電腦網站支付產品
2:需要準備的 :appid 支付寶公鑰(公鑰有兩個,切記是支付寶公鑰不是應用公鑰) 應用私鑰
依賴
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
3:如圖所示
例項
@Value("${zfb.pay.notifyUrl}") public String payNotifyali; @Value("${zfb.pay.appid}") public String APPID; /** * 支付寶公鑰 */ @Value("${zfb.pay.alipayPublicKey}") public String ALIPAYPUBLICKEY; /** * 應用私鑰 */ @Value("${zfb.pay.aLIPAYAPPPRIVATEKEY}") public String ALIPAYAPPPRIVATEKEY; /** * 阿里支付域名 */ static final String SERVER_URL = "https://openapi.alipay.com/gateway.do"; /** * 支付寶支付 * * @return */ public AlipayTradePagePayResponse createQrCode(String orderNo, double money, String proName, String returnUrl) throws AlipayApiException { AlipayClient alipayClient = new DefaultAlipayClient(SERVER_URL, APPID, ALIPAYAPPPRIVATEKEY, "json", "UTF-8", ALIPAYPUBLICKEY, "RSA2"); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); request.setNotifyUrl(payNotifyali); //request.setNotifyUrl("https://edu-dev.zt-spacex.com/eduapi/payNotifyali"); if (StringUtils.isNotBlank(returnUrl)) { request.setReturnUrl(returnUrl); } JSONObject bizContent = new JSONObject(); bizContent.put("out_trade_no", orderNo); bizContent.put("total_amount", money); bizContent.put("subject", proName); bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); request.setBizContent(bizContent.toString()); log.info(bizContent.toString()); AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET"); if (response.isSuccess()) { log.info("呼叫成功"); return response; } else { log.info("呼叫失敗"); return response; } }
上面所示程式碼是生成支付頁面。
建立了支付資訊,設定了回撥地址,那麼伴隨著我們的業務邏輯就需要在回撥中處理.
回撥程式碼例項:
程式碼例項:
@Override @Transactional public String payNotifyzfb(HttpServletRequest request) throws AlipayApiException { Map<String, String> parameterMap = getParameterMap(request); log.info("開始驗籤"); log.info("轉map後引數為" + parameterMap); // 該請求是否來源於阿里 驗籤 boolean signVerified = AlipaySignature .rsaCheckV1(parameterMap, AlipayConfig.alipayPublicKey, AlipayConstants.CHARSET_UTF8, AlipayConstants.SIGN_TYPE_RSA2); log.info(parameterMap.toString()); // 判斷訂單狀態是否成功 if (signVerified) { AlipayNotifyParam param = buildAlipayNotifyParam(parameterMap); String tradeStatus = param.getTradeStatus(); // 支付成功 if (tradeStatus.equals(AlipayTradeStatus.TRADE_SUCCESS.getStatus()) || tradeStatus.equals(AlipayTradeStatus.TRADE_FINISHED.getStatus())) { // 處理支付成功邏輯 try { String successTime = ""; if (param.getGmtPayment() != null) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(param.getGmtPayment()); successTime = dateString; } //儲存支付資訊 } catch (Exception e) { e.printStackTrace(); log.info("支付寶回撥業務處理報錯,params:", e.getMessage()); } } else { log.info("沒有處理支付寶回撥業務,支付寶交易狀態:{},params:{}", tradeStatus); } return "success"; } else { return "failure"; } } private AlipayNotifyParam buildAlipayNotifyParam(Map<String, String> params) { String json = JSON.toJSONString(params); return JSON.parseObject(json, AlipayNotifyParam.class); }
/** * 將request中的資料轉換成map * * @param request * @return */ public static Map<String, String> getParameterMap(HttpServletRequest request) { Map paramsMap = request.getParameterMap(); log.info("獲取的引數為" + request.getParameterMap()); Map<String, String> returnMap = new HashMap(); Iterator entries = paramsMap.entrySet().iterator(); Map.Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (Map.Entry) entries.next(); name = (String) entry.getKey(); Object valueObj = entry.getValue(); if (null == valueObj) { value = ""; } else if (valueObj instanceof String[]) { String[] values = (String[]) valueObj; for (int i = 0; i < values.length; i++) { value = values[i] + ","; } value = value.substring(0, value.length() - 1); } else { value = valueObj.toString(); } returnMap.put(name, value); } return returnMap; }
private AlipayNotifyParam buildAlipayNotifyParam(Map<String, String> params) { String json = JSON.toJSONString(params); return JSON.parseObject(json, AlipayNotifyParam.class); }
這就是一套支付寶支付以及回撥業務處理的流程,不喜勿噴!!!!!