You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Omnipay(PHP)实现无需PayPal账户的信用卡支付

OmniPay集成PayPal无账户信用卡支付方案

一、PayPal API后台设置

  • 登录PayPal商家后台,进入账户设置 > 网站支付 > 网站偏好设置,开启「允许访客结账(Guest Checkout)」,这是启用无账户支付的核心开关
  • 创建并启用PayPal REST应用,获取对应的Client ID和Secret(测试环境用Sandbox密钥,生产环境切换为Live密钥)
  • 确保你的PayPal账户已开通「PayPal Checkout(智能支付按钮)」权限,该权限默认对商家开放,若未开通可联系PayPal客服

二、OmniPay编码实现步骤

首先确保已安装OmniPay PayPal扩展:

composer require omnipay/paypal

核心代码示例

// 初始化PayPal REST网关
$gateway = Omnipay::create('PayPal_Rest');
$gateway->setClientId('你的Client ID');
$gateway->setSecret('你的Secret');
$gateway->setTestMode(true); // 生产环境改为false

// 构造支付参数
$paymentParams = [
    'amount' => '10.00',
    'currency' => 'USD',
    'returnUrl' => 'https://你的域名/payment-success',
    'cancelUrl' => 'https://你的域名/payment-cancel',
    'description' => '测试商品支付',
    // 启用访客结账的关键配置
    'paymentOptions' => [
        'allowed_payment_method' => 'INSTANT_FUNDING_SOURCE',
    ],
    // 注意:直接传递信用卡信息需符合PCI合规,更推荐用PayPal托管支付页面
    // 'card' => [
    //     'number' => '4111111111111111',
    //     'expiryMonth' => '12',
    //     'expiryYear' => '2026',
    //     'cvv' => '123',
    //     'firstName' => '张三',
    //     'lastName' => '李四'
    // ]
];

// 发起支付请求
$response = $gateway->purchase($paymentParams)->send();

// 处理响应
if ($response->isRedirect()) {
    // 跳转到PayPal支付页面,此时会显示「使用信用卡或借记卡」的访客选项
    $response->redirect();
} else {
    // 输出错误信息
    die('支付请求失败:' . $response->getMessage());
}

关键说明

  • 优先使用PayPal托管支付页面:直接收集信用卡信息需要严格符合PCI DSS标准,使用PayPal提供的跳转页面可规避合规风险
  • 测试环境验证:用Sandbox测试卡号(如4111111111111111)测试支付流程,确认访客选项正常显示

三、纯PHP(无OmniPay)实现方案

若不想依赖OmniPay,可直接调用PayPal REST API实现:

步骤1:获取访问令牌

// 初始化CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_USERPWD, '你的Client ID:你的Secret');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

$tokenResponse = curl_exec($ch);
curl_close($ch);
$tokenData = json_decode($tokenResponse, true);
$accessToken = $tokenData['access_token'] ?? '';

步骤2:创建订单并跳转

if (empty($accessToken)) die('获取访问令牌失败');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'intent' => 'CAPTURE',
    'purchase_units' => [[
        'amount' => [
            'currency_code' => 'USD',
            'value' => '10.00'
        ]
    ]],
    'application_context' => [
        'return_url' => 'https://你的域名/payment-success',
        'cancel_url' => 'https://你的域名/payment-cancel',
        'user_action' => 'PAY_NOW'
    ],
    'payment_source' => [
        'paypal' => [
            'experience_context' => [
                'allow_guest_checkout' => true,
                'brand_name' => '你的品牌名'
            ]
        ]
    ]
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $accessToken
]);

$orderResponse = curl_exec($ch);
curl_close($ch);
$orderData = json_decode($orderResponse, true);

// 跳转到PayPal支付页面
if (!empty($orderData['links'])) {
    foreach ($orderData['links'] as $link) {
        if ($link['rel'] === 'approve') {
            header('Location: ' . $link['href']);
            exit;
        }
    }
}
die('创建订单失败:' . ($orderData['message'] ?? '未知错误'));

注意事项

  • 生产环境需切换PayPal API域名(将api-m.sandbox.paypal.com改为api-m.paypal.com)
  • 若访客结账选项仍不显示,检查PayPal账户是否未开启Guest Checkout,或账户存在权限限制
  • 所有涉及金额的字段需保留两位小数,确保货币格式正确

内容的提问来源于stack exchange,提问作者RainWater

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 16:20:23