| 1 |
<?php |
| 2 |
|
| 3 |
require __DIR__ . '/../vendor/autoload.php'; |
| 4 |
|
| 5 |
use PayPalCheckoutSdk\Orders\OrdersCreateRequest; |
| 6 |
use Sample\PayPalClient; |
| 7 |
use PayPalHttp\HttpException; |
| 8 |
|
| 9 |
class ErrorSample |
| 10 |
{ |
| 11 |
public static function prettyPrint($jsonData, $pre="") |
| 12 |
{ |
| 13 |
$pretty = ""; |
| 14 |
foreach ($jsonData as $key => $val) |
| 15 |
{ |
| 16 |
$pretty .= $pre . ucfirst($key) .": "; |
| 17 |
if (strcmp(gettype($val), "array") == 0){ |
| 18 |
$pretty .= "\n"; |
| 19 |
$sno = 1; |
| 20 |
foreach ($val as $value) |
| 21 |
{ |
| 22 |
$pretty .= $pre . "\t" . $sno++ . ":\n"; |
| 23 |
$pretty .= self::prettyPrint($value, $pre . "\t\t"); |
| 24 |
} |
| 25 |
} |
| 26 |
else { |
| 27 |
$pretty .= $val . "\n"; |
| 28 |
} |
| 29 |
} |
| 30 |
return $pretty; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Body has no required parameters (intent, purchase_units) |
| 35 |
*/ |
| 36 |
public static function createError1() |
| 37 |
{ |
| 38 |
$request = new OrdersCreateRequest(); |
| 39 |
$request->body = "{}"; |
| 40 |
print "Request Body: {}\n\n"; |
| 41 |
|
| 42 |
print "Response:\n"; |
| 43 |
try{ |
| 44 |
$client = PayPalClient::client(); |
| 45 |
$response = $client->execute($request); |
| 46 |
} |
| 47 |
catch(HttpException $exception){ |
| 48 |
$message = json_decode($exception->getMessage(), true); |
| 49 |
print "Status Code: {$exception->statusCode}\n"; |
| 50 |
print(self::prettyPrint($message)); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Body has invalid parameter value for intent |
| 56 |
*/ |
| 57 |
public static function createError2() |
| 58 |
{ |
| 59 |
$request = new OrdersCreateRequest(); |
| 60 |
$request->body = array ( |
| 61 |
'intent' => 'INVALID', |
| 62 |
'purchase_units' => |
| 63 |
array ( |
| 64 |
0 => |
| 65 |
array ( |
| 66 |
'amount' => |
| 67 |
array ( |
| 68 |
'currency_code' => 'USD', |
| 69 |
'value' => '100.00', |
| 70 |
), |
| 71 |
), |
| 72 |
), |
| 73 |
); |
| 74 |
print "Request Body:\n" . json_encode($request->body, JSON_PRETTY_PRINT) . "\n\n"; |
| 75 |
|
| 76 |
try{ |
| 77 |
$client = PayPalClient::client(); |
| 78 |
$response = $client->execute($request); |
| 79 |
} |
| 80 |
catch(HttpException $exception){ |
| 81 |
print "Response:\n"; |
| 82 |
$message = json_decode($exception->getMessage(), true); |
| 83 |
print "Status Code: {$exception->statusCode}\n"; |
| 84 |
print(self::prettyPrint($message)); |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
print "Calling createError1 (Body has no required parameters (intent, purchase_units))\n"; |
| 91 |
ErrorSample::createError1(); |
| 92 |
|
| 93 |
print "\n\nCalling createError2 (Body has invalid parameter value for intent)\n"; |
| 94 |
ErrorSample::createError2(); |
| 95 |
|