简体   繁体   English

PayPal 结账与智能支付按钮的集成

[英]PayPal Checkout Integration with Smart Payment Buttons

I'm currently working with the PHP framework Codeigniter 4.0.4 and trying to add the PayPal Checkout Intergration with Smart Payment Buttons.我目前正在使用 PHP 框架 Codeigniter 4.0.4 并尝试添加 PayPal Checkout Intergration with Smart Payment Buttons。

I used the PayPal API as an example, but I always get an error message when I try to create an order.我以 PayPal API 为例,但在尝试创建订单时总是收到错误消息。 When I click on the PayPal button to pay, the window opens for 1-2 seconds and then closes again immediately.当我点击 PayPal 按钮进行支付时,窗口会打开 1-2 秒,然后立即再次关闭。

Console error:控制台错误:

Uncaught SyntaxError: Unexpected token < in JSON at position 0

Error: Unexpected token < in JSON at position 0
    at $t.error (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:59754)
    at Object.<anonymous> (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67521)
    at JSON.parse (<anonymous>)
    at o (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67380)
    at cr (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67533)
    at Cr.u.on (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:72204)
    at Cr (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:72341)
    at https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:78460
    at Function.n.try (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:14069)
    at https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:78257

Serverside:服务器端:

$clientId = getenv('paypal.CLIENT_ID');
$clientSecret = getenv('paypal.CLIENT_SECRET');

$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);

$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
    "intent" => "CAPTURE",
    "purchase_units" => [[
        'reference_id' => '123',
        "amount" => [
            "value" => 10,
            "currency_code" => "USD"
        ]
    ]],
    "application_context" => [
        "cancel_url" => base_url() . "/checkout",
        "return_url" => base_url() . "/checkout"
    ]
];

try {
    $response = $client->execute($request);
    return json_encode($response);
} catch (HttpException $ex) {
    echo $ex->statusCode;
    print_r($ex->getMessage());
}

Clientside:客户端:

<script type="text/javascript">
    paypal.Buttons({
        env: 'sandbox',

        style: {
            layout: 'vertical',
            size: 'responsive',
            shape: 'pill',
            color: 'blue',
            label: 'pay'
        },

        createOrder: function() {
            return fetch('/checkout/paypal', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                }
            }).then(function(response) {
                return response.json();
            }).then(function(resJson) {
                return resJson.result.id;
            });
        }
    }).render('#paypal-button-container');
</script>

Try尝试

<script type="text/javascript">
    paypal.Buttons({
        env: 'sandbox',

        style: {
            layout: 'vertical',
            size: 'responsive',
            shape: 'pill',
            color: 'blue',
            label: 'pay'
        },

        createOrder: function() {
            return fetch('/checkout/paypal', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                }
            }).then(function(response) {
                console.log(response);
          
            });
        }
    }).render('#paypal-button-container');
</script>

what will return on Console控制台会返回什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM