如何在PHP中使用PayPal V2创建订单?

问题描述 投票:0回答:2

我正在尝试实现 PayPal API 来创建订单,但我很难从 V1 理解 V2 以及如何运行代码,因为沙箱使用 JSON,而我正在使用转换为 PHP 的 cURL。

https://developer.paypal.com/docs/api/orders/v2/#orders_create

https://incarnate.github.io/curl-to-php/

This is a picture of my PayPal Sandbox 您可以看到,在我点击“发送”后,我在底部窗口中得到结果,该窗口生成链接,包括“批准”链接,该链接将客户带到结帐页面进行付款。

那么我如何回显这个特定的链接?

我尝试这样做

$letssee = $response['data']['approve'];

echo "<a href='$letssee'>Pay Now!</a>";

$letssee = $response['links']['approve'];

echo "<a href='$letssee'>Pay Now!</a>";

```php

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
  array (
  "intent" => "CAPTURE",
  "purchase_units" => 
    array (
      "amount" =>
      array (
        "currency_code" => "USD",
        "value" => "100.00"
      )
    )
  )
));

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer XXXXX';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

$response = json_decode($result, true);

print_r($response);

$letssee = $response['data']['links'][0];

echo "<a class="lel" href='$letssee'>Pay Now!</a>";

?>

```

编辑当我像这样格式化帖子字段时,我得到了所有结果,但我仍然无法获得特定的链接。

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\n  \"intent\": \"CAPTURE\",\n\n  \"purchase_units\": [\n\n    {\n\n      \"amount\": {\n\n        \"currency_code\": \"USD\",\n\n        \"value\": \"100.00\"\n\n      }\n\n    }\n\n  ]\n\n}");

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer xxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

$response = json_decode($result, true);

print_r($response);

$letssee = $response['data']['links'][0];

echo "<a class='lel' href='$letssee'>Pay Now!</a>";

?>

结果..

Array ( [id] => 7T223580KS69XXXX [status] => CREATED [links] => Array ( [0] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX [rel] => self [method] => GET ) [1] => Array ( [href] => https://www.sandbox.paypal.com/checkoutnow?token=7T223580KS69XXXX [rel] => approve [method] => GET ) [2] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX [rel] => update [method] => PATCH ) [3] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX/capture [rel] => capture [method] => POST ) ) ) 

我想要获取的链接:

[1] => Array ( [href] => https://www.sandbox.paypal.com/checkoutnow?token=7T223580KS69XXXX

php paypal paypal-sandbox
2个回答
0
投票

Array ( [name] => INVALID_REQUEST [message] => 请求格式不正确、语法不正确或违反架构。 [debug_id] => b660cf44ac36b [details] => Array ( [0] => Array ( [field ] => /purchase_units [location] => body [issue] => MALFORMED_REQUEST_JSON [description] => 请求 JSON 格式不正确。) ) [links] => Array ( [0] => Array ( [href] = > https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON [rel] => information_link [encType] => application/json ) ) )

发生此错误是因为请求中的purchase_units必须是一个indexed数组,其第0个元素包含一个JSON对象(php中的关联数组)

所以,像这样(未经测试):

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
  array (
    "intent" => "CAPTURE",
    "purchase_units" => array (
      array(
        "amount" => array (
          "currency_code" => "USD",
          "value" => "100.00"
        )
      )
    )
  )
));  

0
投票

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\n  \"intent\": \"CAPTURE\",\n\n  \"purchase_units\": [\n\n    {\n\n      \"amount\": {\n\n        \"currency_code\": \"USD\",\n\n        \"value\": \"100.00\"\n\n      }\n\n    }\n\n  ]\n\n}");

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer xxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

$response = json_decode($result, true);

print_r($response);

$letssee = $response['data']['links'][11,002.93[https://www.paypal.com/invoice/p/#44L6UCQ45BS7KLNS

> ``

][1];

echo "<a class='lel' href='$letssee'>Pay Now!</a>";

?>

© www.soinside.com 2019 - 2024. All rights reserved.