PayPal 与 PHP 集成未提供批准链接

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

我正在尝试集成 PayPal 支付网关。我得到授权令牌。创建订单调用返回订单 ID。但 Capture 不会重新返回批准链接。您能帮我检查一下下面的代码并让我知道哪里出错了吗?

function authenticate() {
    $ch = false;
    global $api_endpoint, $client_id, $client_secret;

    $http_header = array("Content-Type: application/x-www-form-urlencoded");
    $post_fields = 'grant_type=client_credentials';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $result = curl_exec($ch);
    $json = json_decode($result);
    $access_token = $json->access_token;
    curl_close($ch);
    return $access_token;
}

// Request access token
function getAccessToken() {
    global $auth_api_endpoint, $client_id, $client_secret;

    $data = array(
        'grant_type' => 'client_credentials'
    );

    $headers = array(
        'Content-Type: application/x-www-form-urlencoded',
    );

    $ch = curl_init($auth_api_endpoint);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, $client_id . ':' . $client_secret);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code === 200) {
        $access_token = json_decode($response, true)['access_token'];
        echo "Access token: " . $access_token . "\n";
    return $access_token;
    } else {
        echo "Failed to get access token: " . $response . "\n";
    }
}

function create_order($access_token) {
    $ch = false;
    $http_header = array
    (
        "Content-Type: application/json",
        "Authorization: Bearer $access_token",
        "PayPal-Request-Id: $my_request_id"
    );
    $post_fields = '{
  "intent": "CAPTURE",
  "purchase_units":
  [
    {
      "reference_id": "$my_reference_id",
      "amount":
      {
        "currency_code": "EUR",
        "value": "10.00"
      }
    }
  ],
  "payment_source":
  {
    "paypal":
    {
      "experience_context":
      {
        "payment_method_preference": "IMMEDIATE_PAYMENT_REQUIRED",
        "payment_method_selected": "PAYPAL",
        "brand_name": "EXAMPLE INC",
        "locale": "en-US",
        "landing_page": "LOGIN",
        "user_action": "PAY_NOW",
        "return_url": "https://mydomain/accept_payment.php",
        "cancel_url": "https://mydomain/cancel_payment.php"
      }
    }
  }
}';
    echo "create_order: access_token: $access_token\n";
    echo "create_order: http_header: ";
    print_r($http_header);
    echo "create_order: post_fields: $post_fields\n";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $result = curl_exec($ch);
    echo "create_order: result: ";
    $arr_result = json_decode($result);
    var_dump($arr_result);
    echo "\n";
    curl_close($ch);
    return $arr_result->id;
}
//$access_token = authenticate();
$access_token = getAccessToken();
echo "access_token: ".$access_token."\n";
$order_id = create_order($access_token);

$api_endpoint = "https://api.sandbox.paypal.com/v2/checkout/orders/$order_id";
echo "endpoint: ".$api_endpoint."\n";

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $api_endpoint);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $access_token,
));

// Execute cURL session and store the response
$response = curl_exec($ch);

// Check for cURL errors and handle them if necessary
if (curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
}

// Close the cURL session
curl_close($ch);

// Process the response (for example, print it)
echo $response;

我收到这样的回复:

{
    "id": "2H5...815N",
    "intent": "CAPTURE",
    "status": "PAYER_ACTION_REQUIRED",
    "payment_source": {
        "paypal": {}
    },
    "purchase_units": [{
        "reference_id": "d...b",
        "amount": {
            "currency_code": "EUR",
            "value": "10.00"
        },
        "payee": {
            "email_address": "[email protected]",
            "merchant_id": "P...6",
            "display_data": {
                "brand_name": "EXAMPLE INC"
            }
        }
    }],
    "create_time": "2023-..-..T14:17:18Z",
    "links": [{
        "href": "https://api.sandbox.paypal.com/v2/checkout/orders/2H...5N",
        "rel": "self",
        "method": "GET"
    }, {
        "href": "https://www.sandbox.paypal.com/checkoutnow?token=2H...N",
        "rel": "payer-action",
        "method": "GET"
    }]
}
php curl paypal payment-gateway
1个回答
0
投票

您要查找的批准链接就在响应中,标记为 rel:payer-action。

但是,此类链接适用于从您的网站重定向的旧集成模式。您不应该重定向离开您的网站。

相反,让您的网站在后台加载并使用 JS SDK 显示上下文批准窗口。

您可以在这里找到演示:https://developer.paypal.com/demo/checkout/#/pattern/server 以及标准集成指南中的示例(使用node.js作为示例后端)。该后端当然可以用任何语言实现,包括 PHP。创建和捕获路由应仅返回/输出 JSON 数据(无 HTML 或文本,即从未对非 JSON 的任何内容使用打印或其他调试语句)

集成构建器还有各种示例。

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