欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 跨境电商支付之Springboot整合paypal支付

跨境电商支付之Springboot整合paypal支付

2025/3/15 9:23:55 来源:https://blog.csdn.net/a694704123b/article/details/146265258  浏览:    关键词:跨境电商支付之Springboot整合paypal支付

在Spring Boot中集成PayPal支付功能,通常涉及以下几个步骤:

一. 创建PayPal开发者账户

首先,你需要在PayPal开发者平台上创建一个账户,并创建一个应用以获取Client ID和Secret。

这些凭证将用于与PayPal API进行通信。

二. 添加PayPal SDK依赖

在你的Spring Boot项目中,添加PayPal SDK的依赖。你可以使用Maven或Gradle来管理依赖。

Maven依赖

<dependency><groupId>com.paypal.sdk</groupId><artifactId>rest-api-sdk</artifactId><version>1.14.0</version>
</dependency>

Gradle依赖

implementation 'com.paypal.sdk:rest-api-sdk:1.14.0'

三. 配置PayPal API凭证

在application.yml或者application.properties中配置PayPal的Client ID和Secret。

paypal:client:id: your-client-idsecret: your-client-secretmode: sandbox

四. 创建PayPal配置类

创建一个配置类来初始化PayPal的API上下文。

package com.really.config;import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;@Configuration
public class PayPalConfig {@Value("${paypal.client.id}")private String clientId;@Value("${paypal.client.secret}")private String clientSecret;@Value("${paypal.mode}")private String mode;@Beanpublic Map<String, String> paypalSdkConfig() {Map<String, String> configMap = new HashMap<>();configMap.put("mode", mode);return configMap;}@Beanpublic OAuthTokenCredential oAuthTokenCredential() {return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig());}@Beanpublic APIContext apiContext() throws PayPalRESTException {APIContext context = new APIContext(this.oAuthTokenCredential().getAccessToken());context.setConfigurationMap(this.paypalSdkConfig());return context;}}

五. 创建支付业务

创建一个服务类来处理支付逻辑。

package com.really.service.impl;import com.paypal.api.payments.*;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;
import com.really.service.PayPalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;@Service
public class PayPalServiceImpl implements PayPalService {@Autowiredprivate APIContext apiContext;public Payment createPayment(Double total, String currency, String method, String intent, String description, String cancelUrl, String successUrl) throws PayPalRESTException {Amount amount = new Amount();amount.setCurrency(currency);amount.setTotal(String.format("%.2f", total));Transaction transaction = new Transaction();transaction.setDescription(description);transaction.setAmount(amount);List<Transaction> transactions = new ArrayList<>();transactions.add(transaction);Payer payer = new Payer();payer.setPaymentMethod(method);Payment payment = new Payment();payment.setIntent(intent);payment.setPayer(payer);payment.setTransactions(transactions);RedirectUrls redirectUrls = new RedirectUrls();redirectUrls.setCancelUrl(cancelUrl);redirectUrls.setReturnUrl(successUrl);payment.setRedirectUrls(redirectUrls);return payment.create(apiContext);}public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException {Payment payment = new Payment();payment.setId(paymentId);PaymentExecution paymentExecute = new PaymentExecution();paymentExecute.setPayerId(payerId);return payment.execute(apiContext, paymentExecute);}}
package com.really.service;import com.paypal.api.payments.Payment;
import com.paypal.base.rest.PayPalRESTException;public interface PayPalService {Payment createPayment(Double total, String currency, String method, String intent, String description, String cancelUrl, String successUrl) throws PayPalRESTException;Payment executePayment(String paymentId, String payerId) throws PayPalRESTException;}
package com.really.controller;import com.paypal.api.payments.*;
import com.paypal.base.rest.PayPalRESTException;
import com.really.service.PayPalService;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@Controller
@RequestMapping("/paypal")
public class PayPalController {@Autowiredprivate PayPalService payPalService;@PostMapping("/pay")public String createPayment(@RequestParam("total") Double total, @RequestParam("currency") String currency, @RequestParam("method") String method, @RequestParam("intent") String intent, @RequestParam("description") String description) throws PayPalRESTException {String cancelUrl = "http://localhost:8080/paypal/cancel";String successUrl = "http://localhost:8080/paypal/success";Payment payment = payPalService.createPayment(total, currency, method, intent, description, cancelUrl, successUrl);for (Links link: payment.getLinks()) {if (link.getRel().equals("approval_url")) {return "redirect:" + link.getHref();}}return "redirect:/";}@GetMapping("/success")public String paymentSuccess(@RequestParam("paymentId") String paymentId, @RequestParam("payerId") String payerId) throws PayPalRESTException {Payment payment = payPalService.executePayment(paymentId, payerId);if (payment.getState().equals("approved")) {return "paymentSuccess";}return "paymentError";}@GetMapping("/cancel")public String paymentCancel() {return "paymentCancel";}}

六. 支付业务前端页面

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>PayPal Payment</title></head><body><form action="/paypal/pay" method="post"><input type="hidden" name="total" value="10.00"><input type="hidden" name="currency" value="USD"><input type="hidden" name="method" value="paypal"><input type="hidden" name="intent" value="sale"><input type="hidden" name="description" value="Payment Test"><button type="submit">Pay with PayPal</button></form></body>
</html>

七. 运行和测试

启动Spring Boot应用程序,并访问前端页面进行支付测试。你可以使用PayPal的沙盒账户进行测试。

八. 处理支付结果

根据支付结果,你可以重定向到不同的页面(如支付成功页面或支付取消页面)。

九. 部署到生产环境

在部署到生产环境时,确保将paypal.mode更改为live,并使用真实的Client ID和Secret。

总结

通过以上步骤,你可以在Spring Boot项目中集成PayPal支付功能。PayPal SDK提供了丰富的API来处理支付、退款等操作。你可以根据业务需求进一步扩展和定制支付流程。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词