1

while integrating paypal in spring boot app im getting this issue

    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.

my main looks like

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

the error points to the service where i have used api context of paypal

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

i have project running the paypal sdk working fine but when i add it to my app im sicking this error of paypal api context

enter image description here

enter image description here

tero17
  • 1,570
  • 1
  • 11
  • 20

2 Answers2

1

I never used com.paypal.base.rest.APIContext before but your error message suggests that you need a bean of type APIContext so it can be autowired.

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

Try adding the bean to your configuration class (or xml file if you still using .xml based configuration). Reading com.paypal.base.rest.APIContext documentation I would guess that you need something like:

@Configuration
public class AppConfiguration {

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

Try this out and see if that works. Keep in mind that AppConfiguration must be placed in a sub-package of BookstoreAngularApplication so it can be loaded. Here is an example.

Later, read "yourClientID", "yourClientSecret", "yourMode" from a configuration file instead of hard coding it.

Rafa
  • 1,997
  • 3
  • 21
  • 33
  • i ve configured them like that and added keys in Appproperties and called them check my update upstairs pleas – tero17 Sep 05 '17 at 00:28
  • But what is the error message now? Does it still the same? – Rafa Sep 05 '17 at 02:01
  • they are in from the begining @Rafa – tero17 Sep 05 '17 at 02:18
  • 1
    I would guess that spring-boot is not reading `PaypalConfig`. Make sure spring-boot loads `PaypalConfig`, add a `System.out.println("TetingConfig")` and check it is being loaded. Also, try following these steps https://stackoverflow.com/questions/42907553/field-required-a-bean-of-type-that-could-not-be-found-error-spring-restful-ap – Rafa Sep 05 '17 at 02:53
  • oh god thank u it was cause the name of the package where i putted the config file he must be like base.* i have putted a fast name in the begining i fix it now and he s scanned thank you @Rafa – tero17 Sep 05 '17 at 11:52
  • 1
    Glad to hear. Please mark as "answered" if that worked for you. Cheers – Rafa Sep 05 '17 at 14:15
0

it was cause the name of the package where i putted the config file must be like base-packages.* i have putted a fast name in the begining I fix it now and make it the package name like others it s scanned and works fine

tero17
  • 1,570
  • 1
  • 11
  • 20