Creating an Invoice with Paypal-Php-SDK fails with "Can not deserialize instance of java..." error

468 views Asked by At

I'm trying to create an invoice with Paypal-Php-Sdk, everything's okay, but when I try to set Billing Info something goes wrong and I get this error:

{"name":"VALIDATION_ERROR","message":"Invalid request - see details.","debug_id":"2d34973f0cfef","details":[{"field":"billing_info","issue":"Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at line:1, column:2"}]}exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/invoicing/invoices.' in /var/www/admin/data/www/****/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:176
Stack trace:
#0 /var/www/admin/data/www/****/vendor/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php(74): PayPal\Core\PayPalHttpConnection->execute('{"billing_info"...')
#1 /var/www/admin/data/www/****/vendor/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php(103): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/invoicing/i...', 'POST', '{"billing_info"...', NULL)
#2 /var/www/admin/data/www/****/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/Invoice.php(736): PayPal\Common\PayPalResourceModel::executeCall('/v1/invoicing/i...', 'POST', '{"billing_info"...', NULL, Object(PayPal\Rest\ApiContext), NULL)
#3 var/www/admin/data/www/****/application/controllers/store.php(487): PayPal\Api\Invoice->create(Object(PayPal\Rest\ApiContext))
#4 [internal function]: Store->payment()
#5 /var/www/admin/data/www/****/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array)
#6 /var/www/admin/data/www/****/index.php(202): require_once('/var/www/admin/...')
#7 {main}

Here's the code I use:

$client_id = "****";
$secret = "****";

$apiContext = new ApiContext(new OAuthTokenCredential(
$client_id, $secret));

$merchantInfo = new PayPal\Api\MerchantInfo();
$merchantInfo->setEmail("****");
$merchantInfo->setWebsite("****");

$billingInfo = new PayPal\Api\BillingInfo();
$billingInfo->setEmail("****");

$invoice = new PayPal\Api\Invoice();
$invoice->setMerchantInfo($merchantInfo);
$invoice->setBillingInfo($billingInfo);

try
{
    $invoice->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex->getData();
    die($ex);
} catch (Exception $ex) {
    die($ex);
}
1

There are 1 answers

0
Alex Sh. On BEST ANSWER

Here's what I've received from Paypal support:

Change these two lines to this:

$invoice->setMerchantInfo(array($merchantInfo));

$invoice->setBillingInfo(array($billingInfo));

Both billing info and merchant info takes in array of objects, not an object directly.

It's strange but it started to work when I changed this only for Billing Info, so the right code for me was:

$invoice->setMerchantInfo($merchantInfo);
$invoice->setBillingInfo(array($billingInfo));