spring paypal api上下文问题

qvsjd97n  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(330)

当集成贝宝在 Spring 启动应用程序我得到这个问题

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);
        }

我有项目运行贝宝sdk正常工作,但当我把它添加到我的应用程序我恶心这个贝宝api上下文错误

fzwojiic

fzwojiic1#

这是因为我放置配置文件的包的名称必须类似于基本包。*我在一开始就放置了一个快速名称,我现在修复了它,使其成为包名称,就像其他扫描的包一样,工作正常

rmbxnbpk

rmbxnbpk2#

我从来没用过 com.paypal.base.rest.APIContext 但是您的错误消息提示您需要类型为的bean APIContext 所以它可以自动连线。
考虑在配置中定义类型为“com.paypal.base.rest.apicontext”的bean。
尝试将bean添加到配置类(如果仍然使用基于.xml的配置,则添加到xml文件)。阅读 com.paypal.base.rest.APIContext 文档我猜您需要以下内容:

@Configuration
public class AppConfiguration {

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

试试这个,看看能不能用。请记住 AppConfiguration 必须放在 BookstoreAngularApplication 这样就可以加载了。下面是一个例子。
稍后,阅读 "yourClientID", "yourClientSecret", "yourMode" 从配置文件而不是硬编码。

相关问题