将PayPal与Java集成通常涉及使用PayPal提供的API和SDK。以下是一个基本的步骤指南,帮助你在Java项目中集成PayPal:
首先,你需要在PayPal开发者网站注册一个开发者账号,并创建一个应用以获取客户端ID和密钥。
你可以使用Maven或Gradle将PayPal SDK添加到你的Java项目中。
在你的pom.xml
文件中添加以下依赖:
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.14.0</version>
</dependency>
在你的build.gradle
文件中添加以下依赖:
dependencies {
implementation 'com.paypal.sdk:rest-api-sdk:1.14.0'
}
创建一个配置类或直接在代码中配置PayPal API的客户端ID和密钥。
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException;
public class PayPalConfig {
private static final String CLIENT_ID = "YOUR_CLIENT_ID";
private static final String CLIENT_SECRET = "YOUR_CLIENT_SECRET";
private static final String MODE = "sandbox"; // 或者 "live"
public static APIContext getAPIContext() throws PayPalRESTException {
OAuthTokenCredential tokenCredential = new OAuthTokenCredential(CLIENT_ID, CLIENT_SECRET, MODE);
String accessToken = tokenCredential.getAccessToken();
return new APIContext(accessToken, MODE);
}
}
使用PayPal SDK创建支付请求。
import com.paypal.api.payments.*;
import com.paypal.base.rest.PayPalRESTException;
import java.util.ArrayList;
import java.util.List;
public class PayPalPaymentExample {
public static void main(String[] args) {
try {
APIContext apiContext = PayPalConfig.getAPIContext();
// 创建支付对象
Payment payment = new Payment();
payment.setIntent("sale");
// 创建付款详情
Details details = new Details();
details.setTotal("10.00");
payment.setDetails(details);
// 创建交易
Transaction transaction = new Transaction();
transaction.setDescription("Test Payment");
transaction.setAmount(new Amount().currency("USD").total("10.00"));
List<Transaction> transactions = new ArrayList<>();
transactions.add(transaction);
payment.setTransactions(transactions);
// 创建支付链接
Payer payer = new Payer();
payer.setPaymentMethod("paypal");
payment.setPayer(payer);
// 创建重定向URL
RedirectUrls redirectUrls = new RedirectUrls();
redirectUrls.setReturnUrl("http://localhost:8080/success");
redirectUrls.setCancelUrl("http://localhost:8080/cancel");
payment.setRedirectUrls(redirectUrls);
// 创建支付
Payment createdPayment = payment.create(apiContext);
System.out.println("Payment created: " + createdPayment.getId());
// 获取批准链接
for (Links link : createdPayment.getLinks()) {
if (link.getRel().equals("approval_url")) {
System.out.println("Approval URL: " + link.getHref());
break;
}
}
} catch (PayPalRESTException e) {
e.printStackTrace();
}
}
}
在你的应用程序中处理支付结果,通常是通过重定向URL来处理成功和取消的支付。
@GetMapping("/success")
public String success(HttpServletRequest request) {
String paymentId = request.getParameter("paymentId");
String payerId = request.getParameter("PayerID");
try {
APIContext apiContext = PayPalConfig.getAPIContext();
Payment payment = new Payment();
payment.setId(paymentId);
PaymentExecution paymentExecution = new PaymentExecution();
paymentExecution.setPayerId(payerId);
Payment executedPayment = payment.execute(apiContext, paymentExecution);
System.out.println("Payment executed: " + executedPayment.getState());
} catch (PayPalRESTException e) {
e.printStackTrace();
}
return "success";
}
@GetMapping("/cancel")
public String cancel() {
return "cancel";
}
确保在PayPal开发者沙箱中测试你的集成,以避免在生产环境中出现问题。
领取专属 10元无门槛券
手把手带您无忧上云