前言
paypal是目前全球最大的在線支付工具烈涮,就像國(guó)內(nèi)的支付寶一樣伶授,是一個(gè)基于買賣雙方的第三方支付平臺(tái)寿烟。在集成paypal支付接口之前扑毡,首先要有一系列的準(zhǔn)備航夺,開(kāi)發(fā)者賬號(hào)啊身坐、sdk彬檀、測(cè)試環(huán)境等等先要有簇秒,然后再碼代碼。
一擒悬、環(huán)境準(zhǔn)備
- 注冊(cè)paypal賬號(hào)
- 注冊(cè)paypal開(kāi)發(fā)者賬號(hào)
- 創(chuàng)建兩個(gè)測(cè)試用戶
- 創(chuàng)建應(yīng)用模她,生成用于測(cè)試的clientID 和 密鑰
二、代碼集成
- pom引進(jìn)checkout-sdk的jar包
- 碼代碼
- 后言
注冊(cè)paypal賬號(hào)
(1)在瀏覽器輸入“https://www.paypal.com” 跳轉(zhuǎn)到如下界面懂牧,點(diǎn)擊右上角的注冊(cè)
image.png
(2)選擇侈净,”創(chuàng)建商家用戶”,根據(jù)要求填寫(xiě)信息僧凤,一分鐘的事畜侦,注冊(cè)完得去郵箱激活
image.png
注冊(cè)paypal開(kāi)發(fā)者賬號(hào)
(1)在瀏覽器輸入“https://developer.paypal.com”,點(diǎn)擊右上角的“Log into Dashboard”躯保,用上一步創(chuàng)建好的賬號(hào)登錄
image.png
創(chuàng)建兩個(gè)測(cè)試用戶
(1)登錄成功后旋膳,在左邊的導(dǎo)航欄中點(diǎn)擊 Sandbox 下的 Accountsimage.png
(2)進(jìn)入Acccouts界面后,可以看到系統(tǒng)有兩個(gè)已經(jīng)生成好的測(cè)試賬號(hào)途事,但是我們不要用系統(tǒng)給的測(cè)試賬號(hào)验懊,很卡的,自己創(chuàng)建兩個(gè)image.png
(3)點(diǎn)擊右上角的“Create Account”尸变,創(chuàng)建測(cè)試用戶<1> 先創(chuàng)建一個(gè)“ PERSONAL”類型的用戶义图,國(guó)家一定要選“China”,賬戶余額默認(rèn)5000.00 USD
image.png
<2> 接著創(chuàng)建一個(gè)“BUSINESS”類型的用戶召烂,同上
<3>創(chuàng)建好之后可以點(diǎn)擊測(cè)試賬號(hào)下的”View/edit_account“歌溉,可以查看信息,包括用戶的登錄賬號(hào)和密碼及余額骑晶,如果沒(méi)加載出來(lái)痛垛,刷新image.png
<4>用測(cè)試賬號(hào)(上一步中查看)登錄測(cè)試網(wǎng)站查看,注意桶蛔!這跟paypal官網(wǎng)不同匙头!不是同一個(gè)地址,在瀏覽器輸入:https://www.sandbox.paypal.com 在這里登陸測(cè)試賬戶image.png
創(chuàng)建應(yīng)用仔雷,生成用于測(cè)試的clientID和密鑰
(1)點(diǎn)擊左邊導(dǎo)航欄Dashboard下的My Apps & Credentials蹂析,創(chuàng)建一個(gè)App,這是我創(chuàng)建好的“Test”Appimage.png
(3)點(diǎn)擊剛剛創(chuàng)建好的App“Test”,注意看到”ClientID“ 和”Secret“(Secret如果沒(méi)顯示碟婆,點(diǎn)擊下面的show就會(huì)看到电抚,點(diǎn)擊后show變?yōu)閔ide)image.png
pom引進(jìn)checkout-sdk的jar包
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>checkout-sdk</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
碼代碼
PayPalClient用于調(diào)用PayPal api
import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;
public class PayPalClient {
/**
*Set up the PayPal Java SDK environment with PayPal access credentials.
*This sample uses SandboxEnvironment. In production, use LiveEnvironment.
*/
private PayPalEnvironment environment = new PayPalEnvironment.Sandbox(
"<<PAYPAL-CLIENT-ID>>",
"<<PAYPAL-CLIENT-SECRET>>");
/**
*PayPal HTTP client instance with environment that has access
*credentials context. Use to invoke PayPal APIs.
*/
PayPalHttpClient client = new PayPalHttpClient(environment);
/**
*Method to get client object
*
*@return PayPalHttpClient client
*/
public PayPalHttpClient client() {
return this.client;
}
}
創(chuàng)建訂單,將訂單信息展示在paypal頁(yè)面上
import com.paypal.http.HttpResponse;
import com.paypal.http.exceptions.HttpException;
import com.paypal.orders.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
*1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
*This step extends the SDK client. It's not mandatory to extend the client, you can also inject it.
*/
public class CreateOrder extends PayPalClient {
//2. Set up your server to receive a call from the client
/**
*Method to create order
*
*@param debug true = print response data
*@return HttpResponse<Order> response received from API
*@throws IOException Exceptions from API if any
*/
public HttpResponse<Order> createOrder(boolean debug) throws IOException {
OrdersCreateRequest request = new OrdersCreateRequest();
request.prefer("return=representation");
request.requestBody(buildRequestBody());
//3. Call PayPal to set up a transaction
HttpResponse<Order> response = client().execute(request);
if (debug) {
if (response.statusCode() == 201) {
System.out.println("Status Code: " + response.statusCode());
System.out.println("Status: " + response.result().status());
System.out.println("Order ID: " + response.result().id());
System.out.println("Intent: " + response.result().checkoutPaymentIntent());
System.out.println("Links: ");
/**
* 用戶通過(guò)approve: https://www.sandbox.paypal.com/checkoutnow?token=8GR24834939276359
* 認(rèn)證訂單
*/
for (LinkDescription link : response.result().links()) {
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
}
System.out.println("Total Amount: " + response.result().purchaseUnits().get(0).amountWithBreakdown().currencyCode()
+ " " + response.result().purchaseUnits().get(0).amountWithBreakdown().value());
}
}
return response;
}
/**
*Method to generate sample create order body with AUTHORIZE intent
*
*@return OrderRequest with created order request
*/
private OrderRequest buildRequestBody() {
OrderRequest orderRequest = new OrderRequest();
orderRequest.checkoutPaymentIntent("AUTHORIZE");
/**
* 用戶認(rèn)證完成后竖共,paypal會(huì)通過(guò)returnUrl通知get方式我們
* 在我們的returnUrl后拼接參數(shù)蝙叛,token及orderId
* 示例:https://www.example.com/?token=8GR24834939276359&PayerID=V97XV4A6DQ52N
*/
ApplicationContext applicationContext = new ApplicationContext().brandName("EXAMPLE INC").landingPage("BILLING")
.cancelUrl("https://www.example.com").returnUrl("https://www.example.com").userAction("CONTINUE")
.shippingPreference("SET_PROVIDED_ADDRESS");
orderRequest.applicationContext(applicationContext);
/**
* 綁定訂單信息
*/
List<PurchaseUnitRequest> purchaseUnitRequests = new ArrayList<>();
PurchaseUnitRequest purchaseUnitRequest = new PurchaseUnitRequest().referenceId("PUHF")
.description("Sporting Goods").customId("CUST-HighFashions").softDescriptor("HighFashions")
.amountWithBreakdown(new AmountWithBreakdown().currencyCode("USD").value("220.00")
.amountBreakdown(new AmountBreakdown().itemTotal(new Money().currencyCode("USD").value("180.00"))
.shipping(new Money().currencyCode("USD").value("20.00"))
.handling(new Money().currencyCode("USD").value("10.00"))
.taxTotal(new Money().currencyCode("USD").value("20.00"))
.shippingDiscount(new Money().currencyCode("USD").value("10.00"))))
.items(new ArrayList<Item>() {
{
add(new Item().name("T-shirt").description("Green XL").sku("sku01")
.unitAmount(new Money().currencyCode("USD").value("90.00"))
.tax(new Money().currencyCode("USD").value("10.00")).quantity("1")
.category("PHYSICAL_GOODS"));
add(new Item().name("Shoes").description("Running, Size 10.5").sku("sku02")
.unitAmount(new Money().currencyCode("USD").value("45.00"))
.tax(new Money().currencyCode("USD").value("5.00")).quantity("2")
.category("PHYSICAL_GOODS"));
}
})
.shippingDetail(new ShippingDetail().name(new Name().fullName("John Doe"))
.addressPortable(new AddressPortable().addressLine1("123 Townsend St").addressLine2("Floor 6")
.adminArea2("San Francisco").adminArea1("CA").postalCode("94107").countryCode("US")));
purchaseUnitRequests.add(purchaseUnitRequest);
orderRequest.purchaseUnits(purchaseUnitRequests);
return orderRequest;
}
/**
*This driver function invokes the createOrder function to create
*a sample order.
*/
public static void main(String args[]) {
try {
new CreateOrder().createOrder(true);
} catch (HttpException e) {
System.out.println(e.getLocalizedMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
給訂單授權(quán)
import com.paypal.http.HttpResponse;
import com.paypal.http.serializer.Json;
import com.paypal.orders.LinkDescription;
import com.paypal.orders.Order;
import com.paypal.orders.OrderActionRequest;
import com.paypal.orders.OrdersAuthorizeRequest;
import org.json.JSONObject;
import java.io.IOException;
/**
*1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
*This step extends the SDK client. It's not mandatory to extend the client, you can also inject it.
*/
public class AuthorizeOrder extends PayPalClient {
//2. Set up your server to receive a call from the client
/**
*Method to authorize order after creation
*
*@param orderId Valid Approved Order ID from createOrder response
*@param debug true = print response data
*@return HttpResponse<Order> response received from API
*@throws IOException Exceptions from API if any
*/
public HttpResponse<Order> authorizeOrder(String orderId, boolean debug) throws IOException {
OrdersAuthorizeRequest request = new OrdersAuthorizeRequest(orderId);
request.requestBody(buildRequestBody());
// 3. Call PayPal to authorization an order
HttpResponse<Order> response = client().execute(request);
// 4. Save the authorization ID to your database. Implement logic to save the authorization to your database for future reference.
if (debug) {
System.out.println("Authorization Ids:");
response.result().purchaseUnits()
.forEach(purchaseUnit -> purchaseUnit.payments()
.authorizations().stream()
.map(authorization -> authorization.id())
.forEach(System.out::println));
System.out.println("Link Descriptions: ");
for (LinkDescription link : response.result().links()) {
System.out.println("\t" + link.rel() + ": " + link.href());
}
System.out.println("Full response body:");
System.out.println(new JSONObject(new Json().serialize(response.result())).toString(4));
}
return response;
}
/**
*Building empty request body.
*
*@return OrderActionRequest with empty body
*/
private OrderActionRequest buildRequestBody() {
return new OrderActionRequest();
}
/**
*This driver function invokes the authorizeOrder function to
*create a sample order.
*
*@param args
*/
public static void main(String[] args) {
try {
new AuthorizeOrder().authorizeOrder("8GR24834939276359", true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
確認(rèn)收款
import com.paypal.http.HttpResponse;
import com.paypal.http.serializer.Json;
import com.paypal.orders.OrderRequest;
import com.paypal.payments.AuthorizationsCaptureRequest;
import com.paypal.payments.Capture;
import com.paypal.payments.LinkDescription;
import org.json.JSONObject;
import java.io.IOException;
/**
*1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
*This step extends the SDK client. It's not mandatory to extend the client, you can also inject it.
*/
public class CaptureAuthorization extends PayPalClient {
//2. Set up your server to receive a call from the client
/**
*Method to capture order after authorization
*
*@param authId Authorization ID from authorizeOrder response
*@param debug true = print response data
*@return HttpResponse<Capture> response received from API
*@throws IOException Exceptions from API if any
*/
public HttpResponse<Capture> captureAuth(String authId, boolean debug) throws IOException {
AuthorizationsCaptureRequest request = new AuthorizationsCaptureRequest(authId);
request.requestBody(buildRequestBody());
//3. Call PayPal to capture an authorization.
HttpResponse<Capture> response = client().execute(request);
//4. Save the capture ID to your database for future reference.
if (debug) {
System.out.println("Status Code: " + response.statusCode());
System.out.println("Status: " + response.result().status());
System.out.println("Capture ID: " + response.result().id());
System.out.println("Links: ");
for (LinkDescription link : response.result().links()) {
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
}
System.out.println("Full response body:");
System.out.println(new JSONObject(new Json()
.serialize(response.result())).toString(4));
}
return response;
}
/**
*Create an empty body for capture request
*
*@return OrderRequest request with empty body
*/
public OrderRequest buildRequestBody() {
return new OrderRequest();
}
/**
*This function uses the captureOrder function to
*capture an authorization. Replace the authorization ID with
*the valid authorization ID.
*
*@param args
*/
public static void main(String[] args) {
try {
new CaptureAuthorization().captureAuth("5J830046T71392507", true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
后言
<1>整體流程圖
整體流程圖.png
<2>測(cè)試成功后到https://www.sandbox.paypal.com 登錄測(cè)試賬號(hào)看看余額有沒(méi)有變化
image.png