PayPal-PHP-SDK
PayPal-PHP-SDK copied to clipboard
Negative Testing with PHP-SDK
General information
- SDK/Library version: 1.13.0
- Environment: Sandbox
-
PayPal-Debug-ID
values: n/a - Language, language version, and OS: PHP 5.6.x, OS X, Linux
Issue description
I don't know how to perform negative testing with Payments API. As a note, I am using Reference Transactions. Here is some sample code to start.
$this->paypalContext = new ApiContext(
new OAuthTokenCredential($client_id,$client_secret)
);
$this->paypalContext->setConfig([
'mode' => ('sandbox'),
]);
$payment_api = new PayPalPayment(json_decode('{"intent":"sale","payer":{"payment_method":"paypal","funding_instruments":[{"billing":{"billing_agreement_id":"B-BILLINGAGREEMENT"}}]},"transactions":[{"amount":{"total":9.99,"currency":"USD"},"item_list":{"items":[{"name":"My item","sku":"ECH9272RHD","quantity":1,"price":"9.99","currency":"USD","url":"http:\/\/website.com\/item\/17827\/item-description"}]},"reference_id":"order-181367","description":"Order #181367"}]}');
$this->paypalContext->addRequestHeader("PayPal-Mock-Response",'{"mock_application_codes":"INSTRUMENT_DECLINED"}');
$result = $payment_api->create($this->paypalContext);
Running this throws the exception:
exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 404 when accessing https://api.sandbox.paypal.com/v1/oauth2/token.' in /www/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php:202
When I remove the $this->paypalContext->addRequestHeader()
line, it runs without a hitch. The main problem is, when creating a payment with a reference transaction, as soon as it is created it appears to be executed, so I do not see any way to inject the negative test header using the PHP SDK.
As the SDK documentation on PayPalConnectionException
does not really provide any useful information, I must trigger a negative test to see and parse the error properly in my app.
Can you help out with this?
I have the same issue but I did find a workaround, the problem comes from the fact that the create
method of the Payment
class sets the headers
to null
when it calls executeCall
. Here is the related code from the Payment
class,
public function create($apiContext = null, $restCall = null)
{
$payLoad = $this->toJSON();
$json = self::executeCall(
"/v1/payments/payment",
"POST",
$payLoad,
null,
$apiContext,
$restCall
);
$this->fromJson($json);
return $this;
}
My workaround was to create a mock class which extends Payment
and overrides the create
, and use that in my test code instead of Payment
.
<?php
use PayPal\Api\Payment;
class MockPayment extends Payment {
public function create($apiContext = null, $restCall = null)
{
$payLoad = $this->toJSON();
$json = self::executeCall(
"/v1/payments/payment",
"POST",
$payLoad,
['PayPal-Mock-Response' => '{"mock_application_codes":"MALFORMED_REQUEST"}'],
$apiContext,
$restCall
);
$this->fromJson($json);
return $this;
}
}
Doing so resulted to receiving a MALFORMED_REQUEST
error with an HTTP status code of 400
as expected, instead of 404
like I did previously.