2

I am getting below error when i am trying to create a payment in paypal from salesforce using REST API.

   System.HttpResponse[Status=Internal Server Error, StatusCode=500]

  <ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat">
    <ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.NullPointerException</ns1:faultstring></ns1:XMLFault>

here is my code:

public class PaypalConnect {
    Public PaypalConnect() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.sandbox.paypal.com/v1/oauth2/token');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.setHeader('Accept', 'application/json');
        req.setHeader('Accept-Language', 'en_US');
        Blob headerValue = Blob.valueOf('clientId:secret'); //clientId:secret replaced with my paypal org creds
        system.debug('***Header:' + headerValue);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        system.debug('***:' + EncodingUtil.base64Encode(headerValue));
        req.setHeader('Authorization', authorizationHeader);
        req.setBody('grant_type=client_credentials');
        Http http = new Http();
        HttpResponse response = http.send(req);
        system.debug('***mapBody' + response.getBody());
        Map < String, Object > mapBody = (Map < String, Object > ) JSON.deserializeUntyped(response.getBody());
        String access_token = (String) mapBody.get('access_token');
        Integer expires_in = Integer.valueOF(mapBody.get('expires_in'));
        system.debug('***access_token' + access_token);
        system.debug('***expires_in' + expires_in);
        makePayment(access_token);
    }

    public void makePayment(String token) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.sandbox.paypal.com/v1/payments/payment');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.setHeader('Accept-Language', 'en_US');
        req.setHeader('Accept', 'application/json');
        String bToke = 'Bearer ' + token;
        req.setHeader('Authorization', bToke);
        String s = createPayment.generateJSONContent();
        system.debug('***s:' + s);
        req.setBody(s);
        Http http = new Http();
        HttpResponse response = http.send(req);
        system.debug('***mapBody' + response.getBody());
    }
}

Create Payment class to get JSON data:

public class createPayment {
    public String intent; //sale
    public cls_payer payer;
    public cls_transactions[] transactions;
    class cls_payer {
        public String payment_method; //credit_card
        public cls_funding_instruments[] funding_instruments;
    }
    class cls_funding_instruments {
        public cls_credit_card credit_card;
    }
    class cls_credit_card {
        public String number1; //4417119669820331
        public String type; //visa
        public Integer expire_month; //11
        public Integer expire_year; //2018
        public String cvv2; //874
        public String first_name; //Betsy
        public String last_name; //Buyer
        public cls_billing_address billing_address;
    }
    class cls_billing_address {
        public String line1; //111 First Street
        public String city; //Saratoga
        public String state; //CA
        public String postal_code; //95070
        public String country_code; //US
    }
    class cls_transactions {
        public cls_amount amount;
        public String description; //This is the payment transaction description.
    }
    class cls_amount {
        public String total; //7.47
        public String currency1; //USD
        public cls_details details;
    }
    class cls_details {
        public String subtotal; //7.41
        public String tax; //0.03
        public String shipping; //0.03
    }
    public static createPayment parse(String json) {
        return (createPayment) System.JSON.deserialize(json, createPayment.class);
    }
    public static String generateJSONContent() {
        // Create an object to write to the JSON string.

        cls_billing_address cba = new cls_billing_address();
        cba.line1 = '111 First Street';
        cba.city = 'Saratoga';
        cba.state = 'CA';
        cba.postal_code = '95070';
        cba.country_code = 'US';

        cls_credit_card ccc = new cls_credit_card();

        ccc.number1 = '4293405113454010';
        ccc.type = 'visa';
        ccc.expire_month = 10;
        ccc.expire_year = 2019;
        ccc.cvv2 = '123';
        ccc.first_name = 'Betsy';
        ccc.last_name = 'Buyer';
        ccc.billing_address = cba;

        cls_funding_instruments cfi = new cls_funding_instruments();
        cfi.credit_card = ccc;

        List < cls_funding_instruments > flList = new List < cls_funding_instruments > ();
        flList.add(cfi);

        cls_payer cp = new cls_payer();
        cp.payment_method = 'credit_card';
        cp.funding_instruments = flList;

        cls_details cd = new cls_details();
        cd.subtotal = '7.41';
        cd.tax = '0.03';
        cd.shipping = '0.03';

        cls_amount ca = new cls_amount();
        ca.total = '7.47';
        ca.currency1 = 'USD';
        ca.details = cd;

        cls_transactions clsTrans = new cls_transactions();
        clsTrans.amount = ca;
        clsTrans.description = 'This is the payment transaction description.';

        List < cls_transactions > transactList = new List < cls_transactions > ();
        transactList.add(clsTrans);



        createPayment x = new createPayment();
        x.intent = 'sale';
        x.transactions = transactList;
        x.payer = cp;


        String s = json.serialize(x);
        system.debug('****serialized:' + s);
        s = s.replace('"currency1":', '"currency":');
        s = s.replace('"number1":', '"number":');
        system.debug('****replaced:' + s);
        return s;
    }

}
  • Can anyone look into this and help me out. – sunny Jul 16 '15 at 9:18
  • Silly question, but have you created a test class for your code and what do your debug logs tell you is happening? If you've not created a test class, you're likely wasting people's time looking at this as you'll learn a lot about what may be the source of your problem from unit testing this code first. Ditto in reviewing the debug logs from your unit tests and in trying to run the code. – crmprogdev Jul 17 '15 at 14:22
  • 1
    Did you read my question properly. I am not moving this code to production now or the problem is about unit test coverage. I am trying to create payment in PayPal sandbox. And the response I got from logs was the first few statements in the question. I am not here to waste some ones time. I am learning the process of integration using rest-api. Since I am not able to resolve the issue I have posted here. And please do remember that no question will be silly if some one does not know its root cause. – sunny Jul 17 '15 at 14:33
  • I was referring to my question as being "silly", not yours. We each develop differently. A unit test can be very helpful in diagnosing these issues. – crmprogdev Jul 17 '15 at 14:39
  • 2
    Based on your error log, it looks like you're getting a 500 response from PayPal, and then getting a NPE because you're trying to unserialize the null body. Are you sure that the request you're sending to Paypal is valid? Can you try sending the exact same request to Paypal via Curl or a REST Explorer and see what the result is? – Christian Carter Jul 17 '15 at 14:42
2
+50

Assuming the problem happens in your makePayment method, the first change to try is to change this in that method:

 req.setHeader('Content-Type', 'application/x-www-form-urlencoded');

to this:

 req.setHeader('Content-Type', 'application/json');

as it is JSON you are sending. The receiving service may be generating the NullPointerException because it is trying to interpret your data based on that incorrect content type.

(The OAUTH2 protocol mandates form encoding so it is needed there. But you will see in the PayPal documentation the use of the JSON encoding.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.