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.MissingCredentialE
I solved this problem. i am posting the answer
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;
}
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);
}
}