spring-bootpaypal-sandboxpaypal-adaptive-payments

Paypal Adaptive Payment in Java with spring boot


Hello I am implementing adaptive payment in pay-pal with spring boot. But i am getting an error when i hit api in postman. Error is : com.paypal.exception.MissingCredentialException: No API accounts have been configured in application properties. Please help what i am doing wrong.

Here is Service Class

package com.AdaptivePayment.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.paypal.exception.ClientActionRequiredException;
import com.paypal.exception.HttpErrorException;
import com.paypal.exception.InvalidCredentialException;
import com.paypal.exception.InvalidResponseDataException;
import com.paypal.exception.MissingCredentialException;
import com.paypal.exception.SSLConfigurationException;
import com.paypal.sdk.exceptions.OAuthException;
import com.paypal.svcs.services.AdaptivePaymentsService;
import com.paypal.svcs.types.ap.PayRequest;
import com.paypal.svcs.types.ap.PayResponse;
import com.paypal.svcs.types.ap.Receiver;
import com.paypal.svcs.types.ap.ReceiverList;
import com.paypal.svcs.types.common.RequestEnvelope;
@Service
public class AdaptivePaymentService {

    String UserName= " caller_1312486258_biz_api1.gmail.com";
    String Password= "1312486294";
    String Signature= " AbtI7HV1xB428VygBUcIhARzxch4AL65.T18CTeylixNNxDZUu0iO87e";

    public Map<String, Object> createPayment(Double total, String email) throws SSLConfigurationException, InvalidCredentialException, UnsupportedEncodingException, HttpErrorException, InvalidResponseDataException, ClientActionRequiredException, MissingCredentialException, OAuthException, IOException, InterruptedException{
        Map<String, Object> response = new HashMap<String, Object>();

        RequestEnvelope env = new RequestEnvelope();
        env.setErrorLanguage("en_US");

        List<Receiver> receiver = new ArrayList<Receiver>();
        Receiver rec = new Receiver();
        rec.setAmount(total);
        rec.setEmail(email);
        receiver.add(rec);
        ReceiverList receiverlst = new ReceiverList(receiver);

        PayRequest payRequest = new PayRequest();
        payRequest.setReceiverList(receiverlst);
        payRequest.setRequestEnvelope(env);

        Map<String, String> customConfigurationMap = new HashMap<String, String>();
        customConfigurationMap.put("mode", "sandbox"); // Load the map with all mandatory parameters

        AdaptivePaymentsService adaptivePaymentsService = new AdaptivePaymentsService(customConfigurationMap);
        PayResponse payResponse = adaptivePaymentsService.pay(payRequest,UserName);
        response.put("status", "success");
        response.put("payment", payResponse);
        return response;

}
}

Here is Controller

package com.AdaptivePayment.Controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.AdaptivePayment.Service.AdaptivePaymentService;


@Controller
public class AdaptivePaymentController {

    @Autowired
    private AdaptivePaymentService adaptiveService;

    @RequestMapping(value = "adaptivePayment", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> payout(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, Object> response = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            Map<String, Object> adaptiveResponse = adaptiveService.createPayment(16.00, "admin-facilitator@e-rrands.in");
            data.put("Adaptive", adaptiveResponse.get("status"));
            response.put("data", data);
            response.put("status", "OK");
            response.put("code", "200");
            response.put("message", "Payout Created successfully.");
            return new ResponseEntity<Map<String, Object>>(response, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("status", "ERROR");
            response.put("code", "500");
            response.put("message", "Something went wrong");
            return new ResponseEntity<Map<String, Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

Solution

  • I solved this problem. i am posting the answer

    Here is Service class

    package com.AdaptivePayment.Service;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.stereotype.Service;
    import com.AdaptivePayment.Config.AdaptivePaymentConfig;
    import com.paypal.svcs.services.AdaptivePaymentsService;
    import com.paypal.svcs.types.ap.ExecutePaymentRequest;
    import com.paypal.svcs.types.ap.ExecutePaymentResponse;
    import com.paypal.svcs.types.ap.PayRequest;
    import com.paypal.svcs.types.ap.PayResponse;
    import com.paypal.svcs.types.ap.Receiver;
    import com.paypal.svcs.types.ap.ReceiverList;
    import com.paypal.svcs.types.common.AckCode;
    import com.paypal.svcs.types.common.RequestEnvelope;
    
    @Service
    public class AdaptivePaymentService {
    
        public Map<String, Object> createPayment() {
            Map<String, Object> response = new HashMap<String, Object>();
    
            RequestEnvelope env = new RequestEnvelope();
            env.setErrorLanguage("en_US");
    
            Receiver rec1 = new Receiver();
            rec1.setAmount(2.00);
            rec1.setEmail("admin-buyer@gmail.com");
    
            Receiver rec2 = new Receiver();
            rec2.setAmount(4.00);
            rec2.setEmail("admin-facilitator@gmail.com");
    
            List<Receiver> receiver = new ArrayList<Receiver>();
            receiver.add(rec1);
            receiver.add(rec2);
            ReceiverList receiverlst = new ReceiverList(receiver);
    
            PayRequest payRequest = new PayRequest();
            payRequest.setReceiverList(receiverlst);
            payRequest.setRequestEnvelope(env);
            payRequest.setActionType("PAY");
            payRequest.setCurrencyCode("USD");
    
            payRequest.setCancelUrl("http://localhost:9090/cancel");
            payRequest.setReturnUrl("http://localhost:9090/success");
    
            Map<String, String> configurationMap = AdaptivePaymentConfig.getAcctAndConfig();
    
            AdaptivePaymentsService service = new AdaptivePaymentsService(configurationMap);
            try {
                String redirectUrl = null;
                PayResponse resp = service.pay(payRequest);
                if(!resp.getResponseEnvelope().getAck().toString().trim().toUpperCase().equals(AckCode.FAILURE.toString())&& !resp.getResponseEnvelope().getAck().toString().trim().toUpperCase().equals(AckCode.FAILUREWITHWARNING.toString()));
                {
                    redirectUrl = "https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_ap-payment&paykey=" + resp.getPayKey();
                }
                response.put("status", "success");
                response.put("redirect_url", redirectUrl);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return response;
        }
    

    Here is Controller Class

     package com.AdaptivePayment.Controller;
    
    import java.util.HashMap;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import com.AdaptivePayment.Service.AdaptivePaymentService;
    
    @Controller
    public class AdaptivePaymentController {
    
        @Autowired
        private AdaptivePaymentService adaptiveService;
    
        @RequestMapping(value = "adaptivePayment", method = RequestMethod.POST)
        public ResponseEntity<Map<String, Object>> adaptivePayment() {
            Map<String, Object> response = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            try {
                Map<String, Object> adaptiveResponse = adaptiveService.createPayment();
                data.put("Adaptive", adaptiveResponse.get("redirect_url"));
                response.put("data", data);
                response.put("status", "OK");
                response.put("code", "200");
                response.put("message", "Click on this Link to pay.");
                return new ResponseEntity<Map<String, Object>>(response, HttpStatus.OK);
            } catch (Exception e) {
                e.printStackTrace();
                response.put("status", "ERROR");
                response.put("code", "500");
                response.put("message", "Something went wrong");
                return new ResponseEntity<Map<String, Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }