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查询订单信息。
第三步对订单信息进行校验。
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;
}
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版本支付校验就完成了。