function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Venkat HVenkat H 

MALFORMED_REQUEST: “Incoming JSON request does not map to API request” with Paypal integration in salesforce

I am doing paypal integration in my salesforce account, but am getting a validation error when doing payment through Rest API call: Incoming JSON request does not map to API request
I have tried the below link sample json values:https://developer.paypal.com/docs/api/payments/#payment
Share the correct json values for payment, or assist me on how to proceed.
Please find the apex code for payment:
public void Fetchcredentail(){ Http http = new Http(); HttpResponse response; response = new HttpResponse(); response.setBody(convertJsonBody()); HttpRequest req = new HttpRequest(); req.setEndpoint('https://api.sandbox.paypal.com/v1/payments/payment'); req.setMethod('POST'); req.setHeader('Content-Type', 'application/json'); req.setHeader('Authorization', 'Bearer <Access token>'); response = http.send(req); system.debug(response+'-------'+response.getBody()); } public string convertJsonBody(){ string json= '{'+ ' "intent": "sale",'+ ' "payer": {'+ ' "payment_method": "credit_card",'+ ' "funding_instruments": [{'+ ' "credit_card": {'+ ' "number": "4417119669820331",'+ ' "type": "visa",'+ ' "expire_month": 11,'+ ' "expire_year": 2018,'+ ' "cvv2": "874",'+ ' "first_name": "Venkat",'+ ' "last_name": "H",'+ ' "billing_address": {'+ ' "line1": "111 First Street",'+ ' "city": "Chennai",'+ ' "state": "TN",'+ ' "postal_code": "6000088",'+ ' "country_code": "IN"'+ ' }'+ ' }'+ ' }]'+ ' },'+ ' "transactions": [{'+ ' "amount": {'+ ' "total": "1.47",'+ ' "currency": "USD",'+ ' "details": {'+ ' "subtotal": "1.41",'+ ' "tax": "0.03",'+ ' "shipping": "0.03"'+ ' }'+ ' },'+ ' "description": "The payment transaction description."'+ ' }]'+ '}'; return json; }
json
jigarshahjigarshah
Venkat,

This error outlines that the Json string being generated through your code is faulty and you may need to relook at it. Can you print the value of the json variable in the debug log and share that string.

Infact you could print the string and use this Online Json Viewing Utility (http://jsonviewer.stack.hu/) to view the string as objects. Instead of generating the Json content manually by appending strings a better way would be to use a wrapper class, similar to the one mentioned below and then using the JSON.serialize() method to generate the Json string using the wrapper class's instance. This technique would prove less cumbersome for Json generation and will save you the overhead of troubleshooting string errors like the one you are facing.
 
//Wrapper class that holds all the elements of Paypal Payment Json
public class PaypalPayment{

	public String intent {get; set;}
	public Payer payer {get; set;}
	.
	.
	
	//Constructor
	public PaypalPayment(String pIntent, Payer pPayerIstance){}
	
	//Inner Class
	public class Payer{
	
		public String payment_method {get; set;}
		public List<FundingInstruments> funding_instruments {get; set;}
	    .
		.
	
	}
	
	//Inner Class
	public class FundingInstruments{
	
		public String number {get; set;}
		public String type {get; set;}
		.
		.
		.
		.
		public Address billing_address {get; set;}
	
	}
}

//Code to generate the Json string
public String convertJsonBody(){

	PaypalPayment paymentInstance = new PaypalPayment();
	
	//Logic to instantiate and populate the values within wrapper goes here
	.
    .
	return JSON.serialize(paymentInstance, false);
}
Please do not forget to mark this thread as SOLVED if this answer helps resolve your issue.