Okay, this is going to be a two part question because I dont know which part is causing the problem.
Question 1. I think the problem that I have might stem from the way I setted up the paypal checkout sdk environment. Here is a link to the tutorial I'm following https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/ I am not positive where to place the HTTP Request Headers either on another page or inside the environment configuration page or if I even need them at all? Or if I have to configure the headers at all or place them as is?
Here is my HTTP Request Headers
$request = new OrdersCreateRequest();
$request->headers["prefer"] = "return=representation";
Here is my environment configuration
namespace Sample;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
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 ProductionEnvironment.
*/
public static function environment()
{
$clientId = getenv("CLIENT_ID") ?: "Aa2IfcoEvHnfJRnVQLSFrSs3SmTTkv5N1weMEL66ysqYIeHfAqXpDVkjOv3vLhkhbP4eKB6MpRlQIcJw";
$clientSecret = getenv("CLIENT_SECRET") ?: "EF6l6PDQJEZbdKTeg35pbBSft6WRdALQC3Xrl5vvG0VNgBUehQyTCQ09QdIauxoccvJOf5Aoy-OGsH5G";
return new SandboxEnvironment($clientId, $clientSecret);
}
}
Question 2. I have been trying to update a transaction that a user will place using the PayPal Checkout. Here is a link to the tutorial I'm following https://developer.paypal.com/docs/checkout/integration-features/update-order-details/
I get the following error:
( ! ) Fatal error: Class 'Sample\CreateOrder' not found on line 78
The error is found on line 78 which is the piece of code found below.
$createdOrder = CreateOrder::createOrder(true)->result;
Here is the full code.
namespace Sample;
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up the Server SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersPatchRequest;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
class PatchOrder
{
// 2. Set up your server to receive a call from the client
public static function patchOrder($orderId)
{
// 3. Call PayPal to patch the transaction details
$client = PayPalClient::client();
$request = new OrdersPatchRequest($orderId);
$request->body = PatchOrder::buildRequestBody();
$client->execute($request);
$response = $client->execute(new OrdersGetRequest($orderId));
print "Status Code: {$response->statusCode}
";
print "Status: {$response->result->status}
";
print "Order ID: {$response->result->id}
";
print "Intent: {$response->result->intent}
";
print "Links:
";
foreach($response->result->links as $link)
{
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}
";
}
print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}
";
// To toggle printing the whole response body comment/uncomment below line
echo json_encode($response->result, JSON_PRETTY_PRINT), "
";
}
/*
*
* Build sample PATCH request
*/
private static function buildRequestBody()
{
return array (
0 =>
array (
'op' => 'replace',
'path' => '/intent',
'value' => 'CAPTURE',
),
1 =>
array (
'op' => 'replace',
'path' => '/purchase_units/@reference_id==\'PUHF\'/amount',
'value' =>
array (
'currency_code' => 'USD',
'value' => '200.00',
'breakdown' =>
array (
'item_total' =>
array (
'currency_code' => 'USD',
'value' => '180.00',
),
'tax_total' =>
array (
'currency_code' => 'USD',
'value' => '20.00',
),
),
),
),
);
}
}
if (!count(debug_backtrace()))
{
print "Before PATCH:
";
$createdOrder = CreateOrder::createOrder(true)->result;
print "
After PATCH (Changed Intent and Amount):
";
PatchOrder::patchOrder($createdOrder->id);
}