带有Spring Boot的Java的PayPal自适应付款



你好,我正在使用Spring Poot在Pay-Pal中实现自适应付款。但是当我在Postman中击中API时,我会遇到错误。错误是:com.paypal.exception.missingcredentialexception:在应用程序属性中没有配置API帐户。请帮助我做错了什么。

这是服务类

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

这是控制器

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

我解决了这个问题。我要发布答案

这是服务类

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

相关内容

  • 没有找到相关文章

最新更新