PayPal与PHP的集成不提供批准链接

zqdjd7g9  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(103)

我正试图整合贝宝支付网关。我得到授权令牌。创建订单调用返回订单ID。但捕获不返回批准链接。你能帮我检查一下下面的代码,让我知道哪里出错了吗?

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"
    }]
}
hc2pp10m

hc2pp10m1#

您正在寻找的批准链接就在响应中,标记为rel:payer-action。
然而,这样的链接是针对旧的集成模式,重定向远离您的网站。您不应该重定向远离您的网站。
相反,让您的网站在后台加载,并使用JS SDK显示一个上下文审批窗口。
你可以在这里找到一个demo:https://developer.paypal.com/demo/checkout/#/pattern/server和standard integration guide中的示例(使用node.js作为其示例后端)。这个后端当然可以用任何语言实现,包括PHP。创建和捕获路由应该只返回/输出JSON数据(没有HTML或文本,即我从来没有对任何非JSON的内容使用print或其他调试语句)
integration builder也有各种样品。

相关问题