java - spring paypal api上下文问题

标签 java spring paypal

在 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/

相关文章:

java - Gradle构建没有类的jar

java - 如何使用 SimpleJdbcCall 同步调用存储过程

jquery - PayPal 快速结帐示例代码

java - Android Studio Kotlin 自动补全重复建议

java - 如何在java字符串的特定位置指定字符

java - 如何在 Spring Data 中通过示例对查询的两个日期之间的范围添加限制

jquery - 文本字段的递增名称属性

php - Paypal IPN 监听器代码挂起/无响应

java - 同步两个线程未同步工作

spring - 在 Spring 解决 mongodb 引用