Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding samples for parsing errors #11

Merged
merged 4 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ php:
- 7.1
- 7.2
- 7.3
- hhvm-3.18
before_script:
- composer self-update
- composer install
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ This will retrieve a payouts batch
$request = new PayoutsGetRequest($batchId);
$response = $client->execute($request);
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
```

### Parsing Failure Response
This will execute a Get request to simulate a failure
```PHP
try{
$request = new PayoutsGetRequest(null);
$response = $client->execute($request);
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
} catch(HttpException $e){
echo $e->getMessage()
var_dump(json_decode($e->getMessage()));

}


```
Expand Down
82 changes: 47 additions & 35 deletions samples/CreatePayoutSample.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

namespace Sample;

require __DIR__ . '/../vendor/autoload.php';

use Sample\PayPalClient;
use PaypalPayoutsSDK\Payouts\PayoutsPostRequest;
use PayPalHttp\HttpException;

class CreatePayoutSample
{
public static function buildRequestBody()
{
return json_decode(
'{

public static function buildRequestBody()
{
return json_decode(
'{
"sender_batch_header":
{
"email_subject": "SDK payouts test txn"
Expand All @@ -23,39 +28,46 @@ public static function buildRequestBody()
"amount":
{
"currency": "USD",
"value": "1.00"
"value": "1"
}
}]
}',
true);
}
/**
* This function can be used to create payout.
*/
public static function CreatePayout($debug=false)
{
$request = new PayoutsPostRequest();
$request->body = self::buildRequestBody();
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug)
{
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->batch_header->batch_status}\n";
print "Batch ID: {$response->result->batch_header->payout_batch_id}\n";
print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
}',
true
);
}
/**
* This function can be used to create payout.
*/
public static function CreatePayout($debug = false)
{
try {
$request = new PayoutsPostRequest();
$request->body = self::buildRequestBody();
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->batch_header->batch_status}\n";
print "Batch ID: {$response->result->batch_header->payout_batch_id}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
return $response;
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
}
return $response;
} catch (HttpException $e) {
//Parse failure response
echo $e->getMessage() . "\n";
$error = json_decode($e->getMessage());
echo $error->message . "\n";
echo $error->name . "\n";
echo $error->debug_id . "\n";
}
}
}

if (!count(debug_backtrace()))
{
CreatePayoutSample::CreatePayout(true);
}
if (!count(debug_backtrace())) {
CreatePayoutSample::CreatePayout(true);
}
57 changes: 33 additions & 24 deletions samples/GetPayoutSample.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
<?php

namespace Sample;

require __DIR__ . '/../vendor/autoload.php';

use Sample\PayPalClient;
use PaypalPayoutsSDK\Payouts\PayoutsGetRequest;
use Sample\CreatePayoutSample;
use PayPalHttp\HttpException;

class GetPayoutSample
{

/**
* This function can be used to create payout.
*/
public static function GetPayout($batchId,$debug=false)
public static function GetPayout($batchId, $debug = false)
{
$request = new PayoutsGetRequest($batchId);
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug)
{
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->batch_header->batch_status}\n";
print "Batch ID: {$response->result->batch_header->payout_batch_id}\n";
print "First Item ID: {$response->result->items[0]->payout_item_id}\n";
try {
$request = new PayoutsGetRequest($batchId);
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->batch_header->batch_status}\n";
print "Batch ID: {$response->result->batch_header->payout_batch_id}\n";
print "First Item ID: {$response->result->items[0]->payout_item_id}\n";

print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
return $response;
} catch (HttpException $e) {
//Parse failure response
echo $e->getMessage() . "\n";
$error = json_decode($e->getMessage());
echo $error->message . "\n";
echo $error->name . "\n";
echo $error->debug_id . "\n";
}
return $response;
}
}

if (!count(debug_backtrace()))
{
$response = CreatePayoutSample::CreatePayout(true);
GetPayoutSample::GetPayout($response->result->batch_header->payout_batch_id,true);

}
if (!count(debug_backtrace())) {
$response = CreatePayoutSample::CreatePayout(true);
GetPayoutSample::GetPayout($response->result->batch_header->payout_batch_id, true);
}
55 changes: 32 additions & 23 deletions samples/ItemCancelSample.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
<?php

namespace Sample;

require __DIR__ . '/../vendor/autoload.php';

use Sample\PayPalClient;
use PaypalPayoutsSDK\Payouts\PayoutsItemCancelRequest;
use Sample\CreatePayoutSample;
use Sample\GetPayoutSample;
use PayPalHttp\HttpException;

class ItemCancelSample
{

/**
* This function can be used to create payout.
*/
public static function CancelItem($itemId,$debug=false)
public static function CancelItem($itemId, $debug = false)
{
$request = new PayoutsItemCancelRequest($itemId);
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug)
{
print "Status Code: {$response->statusCode}\n";
print "Item status: {$response->result->transaction_status}\n";
try {
$request = new PayoutsItemCancelRequest($itemId);
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Item status: {$response->result->transaction_status}\n";

print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
return $response;
} catch (HttpException $e) {
//Parse failure response
echo $e->getMessage() . "\n";
$error = json_decode($e->getMessage());
echo $error->message . "\n";
echo $error->name . "\n";
echo $error->debug_id . "\n";
}
return $response;
}
}

if (!count(debug_backtrace()))
{
$payout = CreatePayoutSample::CreatePayout(true);
$response= GetPayoutSample::getPayout($payout->result->batch_header->payout_batch_id,true);
ItemCancelSample::CancelItem($response->result->items[0]->payout_item_id,true);

}
if (!count(debug_backtrace())) {
$payout = CreatePayoutSample::CreatePayout(true);
$response = GetPayoutSample::getPayout($payout->result->batch_header->payout_batch_id, true);
ItemCancelSample::CancelItem($response->result->items[0]->payout_item_id, true);
}
55 changes: 32 additions & 23 deletions samples/ItemGetSample.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
<?php

namespace Sample;

require __DIR__ . '/../vendor/autoload.php';

use Sample\PayPalClient;
use PaypalPayoutsSDK\Payouts\PayoutsItemGetRequest;
use Sample\CreatePayoutSample;
use Sample\GetPayoutSample;
use PayPalHttp\HttpException;

class ItemGetSample
{

/**
* This function can be used to create payout.
*/
public static function GetPayoutItem($itemId,$debug=false)
public static function GetPayoutItem($itemId, $debug = false)
{
$request = new PayoutsItemGetRequest($itemId);
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug)
{
print "Status Code: {$response->statusCode}\n";
print "Item status: {$response->result->transaction_status}\n";
try {
$request = new PayoutsItemGetRequest($itemId);
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Item status: {$response->result->transaction_status}\n";

print "Links:\n";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
}
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
return $response;
} catch (HttpException $e) {
//Parse failure response
echo $e->getMessage() . "\n";
$error = json_decode($e->getMessage());
echo $error->message . "\n";
echo $error->name . "\n";
echo $error->debug_id . "\n";
}
return $response;
}
}

if (!count(debug_backtrace()))
{
$payout = CreatePayoutSample::CreatePayout(true);
$response= GetPayoutSample::getPayout($payout->result->batch_header->payout_batch_id,true);
ItemGetSample::GetPayoutItem($response->result->items[0]->payout_item_id,true);

}
if (!count(debug_backtrace())) {
$payout = CreatePayoutSample::CreatePayout(true);
$response = GetPayoutSample::getPayout($payout->result->batch_header->payout_batch_id, true);
ItemGetSample::GetPayoutItem($response->result->items[0]->payout_item_id, true);
}