【问题标题】:Paypal PHP checkout贝宝 PHP 结帐
【发布时间】:2021-12-29 10:07:26
【问题描述】:

我在下面有这段代码,它运行良好,但我可以使用代理来更改所有数据(我也可以更改接收者电子邮件):/

如何使它成为 POST 结帐方法?现在它是一个 GET 方法

<?php

public function checkout()
{

    $query = [];

    $query['cmd'] = '_cart';
    $query['upload'] = 1;
    $query['business'] = $this->getCredential();

    foreach ($this->getItems() as $id => $item)
    {
        $id = $id + 1;

        $query['item_name_' . $id] = $item['name'];
        $query['amount_' . $id] = $item['amount'];
        $query['quantity_' . $id] = $item['quantity'];
    }

    $query['custom'] = $this->getReference();

    $query['<first_name>'] = $this->first_name;
    $query['<last_name>'] = $this->last_name;
    $query['<email>'] = $this->customer_email;

    $query['notify_url'] = $this->getNotificationURL();
    $query['return'] = $this->getReturnURL();
    $query['cancel_return'] = $this->getCancelURL();

    $query['rm'] = '2';
    $query['cbt'] = 'Retornar para o site';
    $query['lc'] = $this->getLocation();
    $query['currency_code'] = $this->getCurrency();

    $query_string = http_build_query($query);

    return "https://". ($this->isSandbox() ? 'sandbox' : 'www' ) .".paypal.com/cgi-bin/webscr?".$query_string;
}

我也试过下面的代码,但是当我点击完成付款时,它会返回带有令牌和payer_id的返回URL,但不要付款:C

public function checkout()
{
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, 'https://api-m.paypal.com/v2/checkout/orders');
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: ' . $this->getAccessToken(),
        'Prefer: return=representation'
    ]);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
        'brand_name' => 'yStore Plugins',
        'intent' => 'CAPTURE',
        'purchase_units' => $this->getItems(),
        'application_context' => [
            'notify_url' => $this->getNotificationURL(),
            'cancel_url' => $this->getCancelURL(),
            'return_url' => $this->getReturnURL()
        ]
    ]));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $response = json_decode(curl_exec($curl));

    curl_close($curl);

    return $response->links[1]->href;
}

【问题讨论】:

    标签: php paypal


    【解决方案1】:

    遵循Set up standard payments 指南,该指南在其“添加和修改代码”部分中解释说,您应该在服务器上创建 2 条路由,一条用于“创建订单”,一条用于“捕获订单”documented here。对于 PHP,您可以使用 Checkout-PHP-SDK(不是任何旧的或已弃用的 SDK)。当被浏览器访问时,您创建的两个路由都应该只输出 JSON 数据(没有额外的 HTML 或无法解析为 JSON 的文本)。在第二条路线中,当捕获 API 成功时,您应该将其生成的付款详细信息存储在您的数据库中(特别是 purchase_units[0].payments.captures[0].id,这是 PayPal 交易 ID)并执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)立即将您的返回 JSON 转发给前端调用者。

    将这 2 个路由与前端审批流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server,它具有重要的客户端错误处理。

    【讨论】:

      猜你喜欢
      • 2014-03-06
      • 1970-01-01
      • 2014-09-06
      • 2017-01-21
      • 2015-04-12
      • 2020-09-23
      • 2021-06-01
      • 2015-11-18
      • 2013-08-08
      相关资源
      最近更新 更多