gpt4 book ai didi

java - spring paypal api上下文问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:34 26 4
gpt4 key购买 nike

在 spring boot 应用程序中集成 paypal 时我遇到了这个问题

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

***************************
APPLICATION FAILED TO START
***************************

Description:

Field apiContext in com.bookstore.service.impl.PaypalService required a bean of type 'com.paypal.base.rest.APIContext' that could not be found.


Action:

Consider defining a bean of type 'com.paypal.base.rest.APIContext' in your configuration.

我的主要样子

@SpringBootApplication
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class BookstoreAngularApplication implements CommandLineRunner {

错误指向我使用了 paypal 的 api 上下文的服务

import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;
@Autowired
private APIContext apiContext;

public Payment createPayment(
Double total,
String currency,
PaypalPaymentMethod method,
PaypalPaymentIntent intent,
String description,
String cancelUrl,
String successUrl) throws PayPalRESTException{
Amount amount = new Amount();
amount.setCurrency(currency);
amount.setTotal(total.toString());

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.toString());

Payment payment = new Payment();
payment.setIntent(intent.toString());
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);
}

我有一个运行 paypal sdk 的项目运行良好,但是当我将它添加到我的应用程序时,我厌倦了 paypal api 上下文的这个错误

enter image description here

enter image description here

最佳答案

我之前从未使用过 com.paypal.base.rest.APIContext,但您的错误消息表明您需要一个类型为 APIContext 的 bean,以便它可以 Autowiring 。

Consider defining a bean of type 'com.paypal.base.rest.APIContext' in your configuration.

尝试将 bean 添加到您的配置类(如果您仍在使用基于 .xml 的配置,则添加到 xml 文件)。读取 com.paypal.base.rest.APIContext documentation我猜你需要这样的东西:

@Configuration
public class AppConfiguration {

@Bean
public APIContext apiContext() {
APIContext apiContext = new APIContext("yourClientID", "yourClientSecret", "yourMode");
return apiContext;
}

试一试,看看是否可行。请记住,AppConfiguration 必须放在 BookstoreAngularApplication 的子包中,以便可以加载它。这是一个 example .

稍后,从配置文件中读取"yourClientID"、"yourClientSecret"、"yourMode",而不是对其进行硬编码。

关于java - spring paypal api上下文问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com