Spring Boot中接入2Checkout支付服务

前言

在现代电子商务应用中,支付服务是一个核心组件。2Checkout是一个流行的在线支付服务提供商,支持全球多种支付方式。本文将指导如何在Spring Boot应用中接入2Checkout支付服务,包括创建支付令牌和处理支付。

1. 添加2Checkout的Maven依赖

在你的pom.xml文件中添加2Checkout的Java SDK依赖。

xml 复制代码
<dependency>
    <groupId>com.checkout</groupId>
    <artifactId>checkout-sdk</artifactId>
    <version>5.0.0</version>
</dependency>

2. 配置2Checkout

application.propertiesapplication.yml中配置你的2Checkout API密钥。

yaml 复制代码
2checkout:
  secret-key: "your_2checkout_secret_key"

3. 创建支付服务

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

java 复制代码
import com.checkout.CheckoutApi;
import com.checkout.CheckoutConfiguration;
import com.checkout.CheckoutSdk;
import com.checkout.common.Currency;
import com.checkout.payments.Payments;
import com.checkout.payments.RequestPaymentToken;
import com.checkout.payments.RequestSource;
import com.checkout.payments.Tokens;
import org.springframework.stereotype.Service;

@Service
public class PaymentService {

    private final String secretKey = "your_2checkout_secret_key";
    private Payments payments;
    private Tokens tokens;

    @PostConstruct
    public void init() {
        CheckoutConfiguration config = CheckoutConfiguration.builder()
                .secretKey(secretKey)
                .build();

        CheckoutSdk sdk = new CheckoutSdk(config);
        this.payments = sdk.payments();
        this.tokens = sdk.tokens();
    }

    public String createToken(double amount) throws Exception {
        RequestPaymentToken requestToken = RequestPaymentToken.builder()
                .amount(amount)
                .currency(Currency.USD)
                .build();

        return tokens.requestAsync(requestToken).get();
    }

    public String chargeToken(String token, double amount) throws Exception {
        return payments.requestAsync(new RequestSource().source(RequestSource.Source.TOKEN).token(token).amount(amount).currency(Currency.USD)).get().getId();
    }
}

4. 创建控制器

创建一个控制器PaymentController来处理支付请求。

java 复制代码
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/token")
    public ResponseEntity<String> createPaymentToken(@RequestParam double amount) {
        try {
            String token = paymentService.createToken(amount);
            return ResponseEntity.ok(token);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error creating payment token");
        }
    }

    @PostMapping("/charge")
    public ResponseEntity<String> chargePayment(@RequestParam String token, @RequestParam double amount) {
        try {
            String chargeId = paymentService.chargeToken(token, amount);
            return ResponseEntity.ok(chargeId);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error charging payment");
        }
    }
}

总结

通过以上步骤,你可以在Spring Boot应用中成功接入2Checkout支付服务。这将使你的应用能够处理在线支付,提升用户体验。记得在实际部署之前进行充分的测试,以确保支付流程的稳定性和安全性。

相关推荐
知其然亦知其所以然4 分钟前
RAG 结果太水?用 RRF + Reranker 重排,效果翻倍提升!
java·后端·llm
用户38775434335635 分钟前
Midjourney Imagine API 申请及使用
人工智能·后端
SimonKing6 分钟前
吊打面试官系列:Spring为什么不推荐使用字段依赖注入?
java·后端·架构
这里有鱼汤6 分钟前
熟练掌握MACD这8种形态,让你少走三年弯路(附Python量化代码)| 建议收藏
后端·python
import_random7 分钟前
[机器学习]GBDT(介绍2)
后端
魔镜魔镜_谁是世界上最漂亮的小仙女13 分钟前
java-集合
java·后端·程序员
前端世界19 分钟前
ASP.NET ListBox控件多选实战:3步打造高效兴趣收集系统
后端·asp.net
海奥华222 分钟前
go中的接口返回设计思想
开发语言·后端·golang
weixin_4383354034 分钟前
Spring Boot实现接口时间戳鉴权
java·spring boot·后端
寻月隐君39 分钟前
探索Web3新速度:Sonic高性能Layer-1上的BlindAuction智能合约实践
后端·web3·github