1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| <?php
namespace Sample;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
require_once(__DIR__ . '/vendor/autoload.php');
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
class PayPalClient {
/**
* Returns PayPal HTTP client instance with environment that has access
* credentials context. Use this instance to invoke PayPal APIs, provided the
* credentials have access.
*/
public static function client() {
return new PayPalHttpClient(self::environment());
}
/**
* Set up and return PayPal PHP SDK environment with PayPal access credentials.
* This sample uses SandboxEnvironment. In production, use LiveEnvironment.
*/
public static function environment() {
$clientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$clientSecret = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
return new SandboxEnvironment($clientId, $clientSecret);
}
}
class CreateOrder {
// 2. Set up your server to receive a call from the client
/**
* This is the sample function to create an order. It uses the
* JSON body returned by buildRequestBody() to create an order.
*/
public static function createOrder($debug = false) {
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = self::buildRequestBody();
// 3. Call PayPal to set up a transaction
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To print the whole response body, uncomment the following line
// echo json_encode($response->result, JSON_PRETTY_PRINT);
}
// 4. Return a successful response to the client.
return $response;
}
/**
* Setting up the JSON request body for creating the order with minimum request body. The intent in the
* request body should be "AUTHORIZE" for authorize intent flow.
*
*/
private static function buildRequestBody() {
return array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'return_url' => 'https://domain.tld/paypal/?action=success',
'cancel_url' => 'https://domain.tld/paypal/?action=cancel'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'EUR',
'value' => '220.00'
)
)
)
);
}
}
/**
* This is the driver function that invokes the createOrder function to create
* a sample order.
*/
if (!empty($_POST['action']) && $_POST['action'] === 'submit') {
$order = CreateOrder::createOrder(false);
header("Location: " . $order->result->links[1]->href);
// echo PHP_EOL . '<pre>$vardump: ' . print_r($order, true) . '</pre>' . PHP_EOL;
exit;
}
?>
<h1>Paypal test server side only</h1>
<form method="post" action="">
<input type="hidden" name="action" value="submit" />
<button type="submit" class="btn btn-primary">Payer 220 via Paypal</button>
</form> |
Partager