paypal支付v2.0(php)支付代码

第一步:获取access_token:

php 复制代码
<?php

$clientId = '';     // 替换为你的 PayPal Client ID
$clientSecret = ''; // 替换为你的 PayPal Client Secret

// PayPal API 请求的 URL
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";

// 初始化 cURL
$ch = curl_init();

// 设置 cURL 请求的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

// 执行请求
$response = curl_exec($ch);

// 检查请求是否出错
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // 打印响应结果
    echo $response;
}

// 关闭 cURL
curl_close($ch);

第二步:创建订单:

php 复制代码
<?php

$accessToken = ''; // 替换为你的访问令牌
$paypalRequestId = '7b92603e-77ed-4896-8e78-5dea2050476a'; // 替换为你的 PayPal 请求 ID

// API URL
$url = "https://api-m.sandbox.paypal.com/v2/checkout/orders";

// 创建订单的数据
$data = [
    "intent" => "CAPTURE",
    "purchase_units" => [
        [
            "reference_id" => "d9f80740-38f0-11e8-b467-0ed5f89f718b",
            "amount" => [
                "currency_code" => "USD",
                "value" => "1.00"
            ]
        ]
    ],
    "payment_source" => [
        "paypal" => [
            "experience_context" => [
                "return_url" => "https://example.com/returnUrl",
                "cancel_url" => "https://example.com/cancelUrl"
            ]
        ]
    ]
];

// 初始化 cURL
$ch = curl_init();

// 设置 cURL 参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'PayPal-Request-Id: ' . $paypalRequestId,
    'Authorization: Bearer ' . $accessToken,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

// 执行请求并获取响应
$response = curl_exec($ch);

// 检查是否有错误
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // 输出响应
    echo $response;
}

// 关闭 cURL
curl_close($ch);
相关推荐
砖厂小工2 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心2 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心3 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker5 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴5 小时前
Android17 为什么重写 MessageQueue
android
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php