duanbimo7212 2019-03-03 13:54
浏览 178

PayPal PHP致命错误:未找到类'Sample \ CreateOrder'以及如何正确配置PayPal结帐sdk环境

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);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?