//验证订单,paypalId是前端支付成功传给后端的,orderNo是支付时传给PayPal的订单号
public CommonResult verifyPaypal(String paypalId,String orderNo){
String url = "https://api.sandbox.paypal.com/v1/oauth2/token";
String clientId = "paypal clientId";
String clientSecret = "paypal clientSecret";
//获取accessToken
Map<String,Object> fromMap = new HashMap<>(1);
fromMap.put("grant_type","client_credentials");
String response = HttpRequest.post(url)
.form(fromMap)
.basicAuth(clientId, clientSecret)
.execute().body();
String accessToken = JSONObject.parseObject(response)
.getString("access_token");
String url = https://api.paypal.com/v1/payments/payment/+paypalId;
String res = HttpRequest.get(url)
.header("Content-Type", "application/json")
.header("Authorization", accessToken)
.execute().body();
JSONObject response = JSONObject.parseObject(res);
String state = response.getString("state");
if ("approved".equalsIgnoreCase(state)){
JSONArray transactions = response.getJSONArray("transactions");
JSONObject transaction = (JSONObject)transactions.get(0);
String invoice_number = transaction.getString("invoice_number");
if(!Objects.equals(orderNo,invoice_number)){
log.info("PayPpl:充值失败,商户订单号与paypal返回的商户订单号不同");
return CommonResult.failed(ResultCode.PAYMENT_FAILURE);
}
//....业务逻辑
return CommonResult.success();
}
return CommonResult.failed(ResultCode.PAYMENT_FAILURE);
}
paypalv1支付验证
最新推荐文章于 2023-10-09 17:15:55 发布
该代码片段展示了如何通过PayPal API验证前端支付成功的订单。首先,它使用客户端ID和密钥获取accessToken,然后利用这个token获取订单详情。如果订单状态为'approved',则比较商户订单号与PayPal返回的订单号是否一致,确保充值成功并执行相应的业务逻辑。否则,返回充值失败的信息。
摘要由CSDN通过智能技术生成