Reputation: 1
I'm trying to implement PayPal Standard Checkout in PHP, and when I make a server-side request, this error appears: "Following authentication credentials are required:\n-> Client is not authorized. An OAuth token is needed to make API calls."
and the $client returns an empty array ({}). Can someone please help me?
However, the client_id and client_secret are correct. I have already run composer require paypal-server-sdk.
<?php
require_once "vendor/autoload.php";
use PaypalServerSdkLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
use PaypalServerSdkLib\Environment;
use PaypalServerSdkLib\PaypalServerSDKClientBuilder;
use PaypalServerSdkLib\Models\Builders\MoneyBuilder;
use PaypalServerSdkLib\Models\Builders\OrderRequestBuilder;
use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSdkLib\Models\Builders\AmountWithBreakdownBuilder;
use PaypalServerSdkLib\Models\Builders\ShippingDetailsBuilder;
use PaypalServerSdkLib\Models\Builders\ShippingOptionBuilder;
use PaypalServerSdkLib\Models\ShippingType;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
$PAYPAL_CLIENT_ID = getenv("PAYPAL_CLIENT_ID");
$PAYPAL_CLIENT_SECRET = getenv("PAYPAL_CLIENT_SECRET");
$client = PaypalServerSdkClientBuilder::init()
->clientCredentialsAuthCredentials(
ClientCredentialsAuthCredentialsBuilder::init(
$PAYPAL_CLIENT_ID,
$PAYPAL_CLIENT_SECRET
)
)
->environment(Environment::SANDBOX)
->build();
echo json_encode($client);
This code should return an OAuth token to allow the use of PayPal's createOrder and captureOrder methods.This code is in PayPal's documentation.
Upvotes: 0
Views: 24