首页 > 解决方案 > PayPal PHP 致命错误:找不到类“Sample\CreateOrder”以及如何正确配置 PayPal 结帐 sdk 环境

问题描述

好的,这将是一个两部分的问题,因为我不知道哪个部分导致了问题。

问题 1. 我认为我的问题可能源于我设置 paypal checkout sdk 环境的方式。这是我正在关注的教程的链接https://developer.paypal.com/docs/checkout/reference/server-integration/setup-sdk/我不确定将 HTTP 请求标头放在另一个页面上的位置或者在环境配置页面中,或者我什至需要它们?或者,如果我必须完全配置标题或按原样放置它们?

这是我的 HTTP 请求标头

$request = new OrdersCreateRequest();
$request->headers["prefer"] = "return=representation";

这是我的环境配置

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);
    }
} 

问题 2. 我一直在尝试更新用户将使用 PayPal Checkout 进行的交易。这是我正在关注的教程的链接https://developer.paypal.com/docs/checkout/integration-features/update-order-details/

我收到以下错误:

( ! ) Fatal error: Class 'Sample\CreateOrder' not found on line 78

该错误位于第 78 行,即下面的代码段。

$createdOrder = CreateOrder::createOrder(true)->result;

这是完整的代码。

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}\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";
        }
        print "Gross Amount: {$response->result->purchase_units[0]->amount->currency_code} {$response->result->purchase_units[0]->amount->value}\n";
        // To toggle printing the whole response body comment/uncomment below line
        echo json_encode($response->result, JSON_PRETTY_PRINT), "\n";
    }

    /*
    *
    * 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:\n";
    $createdOrder = CreateOrder::createOrder(true)->result;
    print "\nAfter PATCH (Changed Intent and Amount):\n";
    PatchOrder::patchOrder($createdOrder->id);
}

标签: phppaypalsdkhttp-headers

解决方案


推荐阅读