paypal流程有如下幾步
1創建應用程序
2啓用與palPal的連接
3應用評論
4構建按鈕
5獲取授權碼
6獲取訪問令牌
7爲access_token交換refresh_token
8獲取客戶信息
9測試集成
10上線
這裏主要講java驗證 palpal支付,也就是5-9的步驟。
java驗證paypal支付主要分爲以下幾步;
第一步,獲取權限認證的token.
第二步,獲取前端支付成功後返回的paymentId。並且拿去paypal查詢訂單信息。
第三步對訂單信息進行校驗。
1獲取權限認證的token.
private static String getAccessToken() {
try {
URL url = new URL(TOKEN_URL);
String authorization = clientId + ":" + secret;
authorization = Base64.encodeBase64String(authorization.getBytes());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");// 提交模式
// 設置請求頭header
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Accept-Language", "en_US");
conn.setRequestProperty("Authorization", "Basic " + authorization);
// conn.setConnectTimeout(10000);//連接超時 單位毫秒
// conn.setReadTimeout(2000);//讀取超時 單位毫秒
conn.setDoOutput(true);// 是否輸入參數
String params = "grant_type=client_credentials";
conn.getOutputStream().write(params.getBytes());// 輸入參數
InputStreamReader inStream = new InputStreamReader(conn.getInputStream());
BufferedReader reader = new BufferedReader(inStream);
StringBuilder result = new StringBuilder();
String lineTxt = null;
while ((lineTxt = reader.readLine()) != null) {
result.append(lineTxt);
}
reader.close();
System.out.println("getAccessToken:" + result);
PayPalToken palToken = JSONObject.parseObject(result.toString(), PayPalToken.class);
return palToken.getAccess_token();
} catch (Exception err) {
err.printStackTrace();
}
return null;
}
2獲取前端支付成功後返回的paymentId。並且拿去paypal查詢訂單信息。
public String getPaymentDetails(String paymentId) {
try {
URL url = new URL(PAYMENT_DETAIL_TEST + paymentId);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");// 提交模式
// 設置請求頭header
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer " + getAccessToken());
// conn.setConnectTimeout(10000);//連接超時 單位毫秒
// conn.setReadTimeout(2000);//讀取超時 單位毫秒
InputStreamReader inStream = new InputStreamReader(conn.getInputStream());
BufferedReader reader = new BufferedReader(inStream);
StringBuilder result = new StringBuilder();
String lineTxt = null;
while ((lineTxt = reader.readLine()) != null) {
result.append(lineTxt);
}
reader.close();
return result.toString();
} catch (Exception err) {
err.printStackTrace();
}
return null;
}
返回後的消息如下
{
"id": "PAYID-LV262LY1GF17006GU287063F",
"intent": "sale",
"state": "approved",
"cart": "6G500886VN698753Y",
"payer": {
"payment_method": "paypal",
"status": "VERIFIED",
"payer_info": {
"email": "[email protected]",
"first_name": "zero",
"last_name": "zero",
"payer_id": "RHDCTBPXH3LKG",
"shipping_address": {
"recipient_name": "zero zero"
},
"phone": "4083363521",
"country_code": "US"
}
},
"transactions": [{
"amount": {
"total": "0.02",
"currency": "USD",
"details": {
"subtotal": "0.02",
"shipping": "0.00",
"insurance": "0.00",
"handling_fee": "0.00",
"shipping_discount": "0.00"
}
},
"payee": {
"merchant_id": "YJFGW6CVTV9XS",
"email": "[email protected]"
},
"description": "100粉鑽",
"item_list": {
"shipping_address": {
"recipient_name": "zero zero"
}
},
"related_resources": [{
"sale": {
"id": "7N444941X3531852Y",
"state": "completed",
"amount": {
"total": "0.02",
"currency": "USD",
"details": {
"subtotal": "0.02",
"shipping": "0.00",
"insurance": "0.00",
"handling_fee": "0.00",
"shipping_discount": "0.00"
}
},
"payment_mode": "INSTANT_TRANSFER",
"protection_eligibility": "ELIGIBLE",
"protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
"transaction_fee": {
"value": "0.02",
"currency": "USD"
},
"parent_payment": "PAYID-LV262LY1GF17006GU287063F",
"create_time": "2019-09-09T06:12:20Z",
"update_time": "2019-09-09T06:12:20Z",
"links": [{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/7N444941X3531852Y",
"rel": "self",
"method": "GET"
}, {
"href": "https://api.sandbox.paypal.com/v1/payments/sale/7N444941X3531852Y/refund",
"rel": "refund",
"method": "POST"
}, {
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LV262LY1GF17006GU287063F",
"rel": "parent_payment",
"method": "GET"
}]
}
}]
}],
"create_time": "2019-09-09T06:11:59Z",
"update_time": "2019-09-09T06:12:20Z",
"links": [{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-LV262LY1GF17006GU287063F",
"rel": "self",
"method": "GET"
}]
}
這裏最簡單的就是對state進行校驗。state顯示approved爲成功。然後再跟進自己的業務對數據進行校驗,一個簡單的java版本支付校驗就完成了。