Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit dbd8d5a

Browse files
author
Rajaram Padmanathan
committed
Refactored samples, updated docs
Updated samples to indicate how headers can be set as key value pair
1 parent 2c04d9c commit dbd8d5a

File tree

13 files changed

+261
-37
lines changed

13 files changed

+261
-37
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use CheckoutPhpsdk\Core\SandboxEnvironment;
3131
$clientId = "AVNCVvV9oQ7qee5O8OW4LSngEeU1dI7lJAGCk91E_bjrXF2LXB2TK2ICXQuGtpcYSqs4mz1BMNQWuso1";
3232
$clientSecret = "EDQzd81k-1z2thZw6typSPOTEjxC_QbJh6IithFQuXdRFc7BjVht5rQapPiTaFt5RC-HCa1ir6mi-H5l";
3333

34-
$environment = new SandBoxEnvironment($clientUd, $clientSecret);
34+
$environment = new SandBoxEnvironment($clientId, $clientSecret);
3535
$client = new PayPalHttpClient($environment);
3636
```
3737

@@ -107,7 +107,7 @@ Status: CREATED
107107
// $response->result->id gives the orderId of the order created above
108108
$request = new OrdersCaptureRequest($response->result->id);
109109
$request->prefer('return=representation');
110-
$request.requestBody({});
110+
$request->body ={};
111111

112112
try {
113113
// Call API with your client and get a response for your call

samples/AuthorizeIntentExamples/AuthorizeOrder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@
44

55
require __DIR__ . '/../../vendor/autoload.php';
66
use CheckoutPhpsdk\Orders\OrdersAuthorizeRequest;
7-
use Sample\SampleSkeleton;
7+
use Sample\PayPalClient;
88

99

1010
class AuthorizeOrder
1111
{
12+
/**
13+
* Setting up request body for Authorize. This can be populated with fields as per need. Refer API docs for more details.
14+
*
15+
*/
1216
public static function buildRequestBody()
1317
{
1418
return "{}";
1519
}
20+
21+
/**
22+
* This function can be used to perform authorization on the approved order.
23+
* Valid Approved order id should be passed as an argument.
24+
*/
1625
public static function authorizeOrder($orderId, $debug=false)
1726
{
1827
$request = new OrdersAuthorizeRequest($orderId);
1928
$request->body = self::buildRequestBody();
2029

21-
$client = SampleSkeleton::client();
30+
$client = PayPalClient::client();
2231
$response = $client->execute($request);
2332
if ($debug)
2433
{
@@ -43,6 +52,9 @@ public static function authorizeOrder($orderId, $debug=false)
4352
}
4453
}
4554

55+
/**
56+
* This is an driver function which invokes authorize order.
57+
*/
4658
if (!count(debug_backtrace()))
4759
{
4860
AuthorizeOrder::authorizeOrder('1U242387CB956380X', true);

samples/AuthorizeIntentExamples/CaptureOrder.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
44

55
require __DIR__ . '/../../vendor/autoload.php';
66
use CheckoutPhpsdk\Payments\AuthorizationsCaptureRequest;
7-
use Sample\SampleSkeleton;
7+
use Sample\PayPalClient;
88

99
class CaptureOrder
1010
{
11+
/**
12+
* Below method can be used to build the capture request body.
13+
* This request can be updated with required fields as per need.
14+
* Please refer API specs for more info.
15+
*/
1116
public static function buildRequestBody()
1217
{
1318
return "{}";
1419
}
1520

21+
/**
22+
* Below function can be used to capture order.
23+
* Valid Authorization id should be passed as an argument.
24+
*/
1625
public static function captureOrder($authorizationId, $debug=false)
1726
{
1827
$request = new AuthorizationsCaptureRequest($authorizationId);
1928
$request->body = self::buildRequestBody();
20-
$client = SampleSkeleton::client();
29+
$client = PayPalClient::client();
2130
$response = $client->execute($request);
2231

2332
if ($debug)
@@ -37,6 +46,9 @@ public static function captureOrder($authorizationId, $debug=false)
3746
}
3847
}
3948

49+
/**
50+
* Driver function for invoking the capture flow.
51+
*/
4052
if (!count(debug_backtrace()))
4153
{
4254
CaptureOrder::captureOrder('18A38324BV5456924', true);

samples/AuthorizeIntentExamples/CreateOrder.php

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
require __DIR__ . '/../../vendor/autoload.php';
66

77
use CheckoutPhpsdk\Orders\OrdersCreateRequest;
8-
use Sample\SampleSkeleton;
8+
use Sample\PayPalClient;
99

1010
class CreateOrder
1111
{
12+
/**
13+
* Setting up the JSON request body for creating the Order with complete request body. The Intent in the
14+
* request body should be set as "AUTHORIZE" for authorize intent flow.
15+
*
16+
*/
1217
private static function buildRequestBody()
1318
{
1419
return array(
@@ -106,6 +111,10 @@ private static function buildRequestBody()
106111
'shipping' =>
107112
array(
108113
'method' => 'United States Postal Service',
114+
'name' =>
115+
array(
116+
'full_name' => 'John Doe',
117+
),
109118
'address' =>
110119
array(
111120
'address_line_1' => '123 Townsend St',
@@ -121,16 +130,78 @@ private static function buildRequestBody()
121130
);
122131
}
123132

133+
/**
134+
* Setting up the JSON request body for creating the Order with minimum request body. The Intent in the
135+
* request body should be set as "AUTHORIZE" for authorize intent flow.
136+
*
137+
*/
138+
private static function buildMinimumRequestBody()
139+
{
140+
return array(
141+
'intent' => 'AUTHORIZE',
142+
'purchase_units' =>
143+
array(
144+
0 =>
145+
array(
146+
'amount' =>
147+
array(
148+
'currency_code' => 'USD',
149+
'value' => '220.00'
150+
)
151+
)
152+
)
153+
);
154+
}
155+
156+
/**
157+
* This is the sample function which can be used to create an order. It uses the
158+
* JSON body returned by buildRequestBody() to create an new Order.
159+
*/
124160
public static function createOrder($debug=false)
125161
{
126162
$request = new OrdersCreateRequest();
127-
$request->prefer('return=representation');
163+
$request->headers["prefer"] = "return=representation";
128164
$request->body = CreateOrder::buildRequestBody();
129165

130-
$client = SampleSkeleton::client();
166+
$client = PayPalClient::client();
167+
$response = $client->execute($request);
168+
if ($debug)
169+
{
170+
print "Status Code: {$response->statusCode}\n";
171+
print "Status: {$response->result->status}\n";
172+
print "Order ID: {$response->result->id}\n";
173+
print "Intent: {$response->result->intent}\n";
174+
print "Links:\n";
175+
foreach($response->result->links as $link)
176+
{
177+
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
178+
}
179+
180+
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
181+
182+
// To print the whole response body uncomment below line
183+
// echo json_encode($response->result, JSON_PRETTY_PRINT);
184+
}
185+
186+
187+
return $response;
188+
}
189+
190+
/**
191+
* This is the sample function which can be used to create an order. It uses the
192+
* JSON body returned by buildMinimumRequestBody() to create an new Order.
193+
*/
194+
public static function createOrderWithMinimumBody($debug=false)
195+
{
196+
$request = new OrdersCreateRequest();
197+
$request->headers["prefer"] = "return=representation";
198+
$request->body = CreateOrder::buildMinimumRequestBody();
199+
200+
$client = PayPalClient::client();
131201
$response = $client->execute($request);
132202
if ($debug)
133203
{
204+
print "Order With Minimum Body\n";
134205
print "Status Code: {$response->statusCode}\n";
135206
print "Status: {$response->result->status}\n";
136207
print "Order ID: {$response->result->id}\n";
@@ -157,4 +228,5 @@ public static function createOrder($debug=false)
157228
if (!count(debug_backtrace()))
158229
{
159230
CreateOrder::createOrder(true);
231+
CreateOrder::createOrderWithMinimumBody(true);
160232
}

samples/AuthorizeIntentExamples/RunAll.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Sample\AuthorizeIntentExamples\CreateOrder;
66
use Sample\AuthorizeIntentExamples\AuthorizeOrder;
77
use Sample\AuthorizeIntentExamples\CaptureOrder;
8+
use Sample\RefundOrder;
89

910
$order = CreateOrder::createOrder();
1011

@@ -50,7 +51,26 @@
5051
print "Captured Successfully\n";
5152
print "Status Code: {$response->statusCode}\n";
5253
print "Status: {$response->result->status}\n";
53-
print "Order ID: {$response->result->id}\n";
54+
$captureId = $response->result->id;
55+
print "Capture ID: {$captureId}\n";
56+
print "Links:\n";
57+
for ($i = 0; $i < count($response->result->links); ++$i){
58+
$link = $response->result->links[$i];
59+
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
60+
}
61+
}
62+
else {
63+
exit(1);
64+
}
65+
66+
print "\nRefunding Order...\n";
67+
$response = RefundOrder::refundOrder($captureId);
68+
if ($response->statusCode == 201)
69+
{
70+
print "Refunded Successfully\n";
71+
print "Status Code: {$response->statusCode}\n";
72+
print "Status: {$response->result->status}\n";
73+
print "Refund ID: {$response->result->id}\n";
5474
print "Links:\n";
5575
for ($i = 0; $i < count($response->result->links); ++$i){
5676
$link = $response->result->links[$i];

samples/CaptureIntentExamples/CaptureOrder.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
namespace Sample\CaptureIntentExamples;
55

66
require __DIR__ . '/../../vendor/autoload.php';
7+
use Sample\PayPalClient;
78
use CheckoutPhpsdk\Orders\OrdersCaptureRequest;
8-
use Sample\SampleSkeleton;
99

1010
class CaptureOrder
1111
{
12+
13+
/**
14+
* This function can be used to capture an order payment by passing the approved
15+
* order id as argument.
16+
*
17+
* @param orderId
18+
* @param debug
19+
* @returns
20+
*/
1221
public static function captureOrder($orderId, $debug=false)
1322
{
1423
$request = new OrdersCaptureRequest($orderId);
1524

16-
$client = SampleSkeleton::client();
25+
$client = PayPalClient::client();
1726
$response = $client->execute($request);
1827
if ($debug)
1928
{
@@ -25,6 +34,14 @@ public static function captureOrder($orderId, $debug=false)
2534
{
2635
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
2736
}
37+
print "Capture Ids:\n";
38+
foreach($response->result->purchase_units as $purchase_unit)
39+
{
40+
foreach($purchase_unit->payments->captures as $capture)
41+
{
42+
print "\t{$capture->id}";
43+
}
44+
}
2845
// To print the whole response body uncomment below line
2946
// echo json_encode($response->result, JSON_PRETTY_PRINT);
3047
}
@@ -33,7 +50,11 @@ public static function captureOrder($orderId, $debug=false)
3350
}
3451
}
3552

53+
/**
54+
* This is the driver function which invokes the captureOrder function with
55+
* <b>Approved</b> Order Id to capture the order payment.
56+
*/
3657
if (!count(debug_backtrace()))
3758
{
38-
CaptureOrder::captureOrder('71551735D5901444A', true);
59+
CaptureOrder::captureOrder('0F105083N67049335', true);
3960
}

samples/CaptureIntentExamples/CreateOrder.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

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

7+
8+
use Sample\PayPalClient;
79
use CheckoutPhpsdk\Orders\OrdersCreateRequest;
8-
use Sample\SampleSkeleton;
910

1011
class CreateOrder
1112
{
13+
14+
/**
15+
* Setting up the JSON request body for creating the Order. The Intent in the
16+
* request body should be set as "CAPTURE" for capture intent flow.
17+
*
18+
*/
1219
private static function buildRequestBody()
1320
{
1421
return array(
@@ -106,6 +113,10 @@ private static function buildRequestBody()
106113
'shipping' =>
107114
array(
108115
'method' => 'United States Postal Service',
116+
'name' =>
117+
array(
118+
'full_name' => 'John Doe',
119+
),
109120
'address' =>
110121
array(
111122
'address_line_1' => '123 Townsend St',
@@ -121,13 +132,17 @@ private static function buildRequestBody()
121132
);
122133
}
123134

135+
/**
136+
* This is the sample function which can be sued to create an order. It uses the
137+
* JSON body returned by buildRequestBody() to create an new Order.
138+
*/
124139
public static function createOrder($debug=false)
125140
{
126141
$request = new OrdersCreateRequest();
127-
$request->prefer('return=representation');
142+
$request->headers["prefer"] = "return=representation";
128143
$request->body = self::buildRequestBody();
129144

130-
$client = SampleSkeleton::client();
145+
$client = PayPalClient::client();
131146
$response = $client->execute($request);
132147
if ($debug)
133148
{
@@ -151,7 +166,10 @@ public static function createOrder($debug=false)
151166
}
152167

153168

154-
169+
/**
170+
* This is the driver function which invokes the createOrder function to create
171+
* an sample order.
172+
*/
155173
if (!count(debug_backtrace()))
156174
{
157175
CreateOrder::createOrder(true);

0 commit comments

Comments
 (0)