【问题标题】:Setting PayPal PHP Application live实时设置 PayPal PHP 应用程序
【发布时间】:2019-01-14 14:40:56
【问题描述】:

我遵循了一些关于如何使用 PHP SDK 通过 PayPal 向人们收费的教程。只要我处于测试模式,这一切都像一个魅力,但如果我将我的 API 密钥更改为“实时”密钥,我只会收到 HTTP 错误 401。 我知道这是因为我必须将应用程序设置为“实时”。

我遵循了 PayPals GitHub 页面上的指南。 (https://github.com/paypal/PayPal-PHP-SDK/wiki/Going-Live)

别担心,我不为实际的 Web 做这件事,我只是想重新使用 PHP,它让我发疯,我无法配置这个脚本。

我有以下“charge.php”:

    require 'app/start.php';
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;

if(!isset($_POST['product'])){
    echo $_POST['product'];
    die("Nope");
}

    $product="Premium";
    $price="1.00";
    $shipping="0.00";
    $total = $price+$shipping;

    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $item = new Item();
    $item->setName($product)
    ->setCurrency('EUR')
    ->setQuantity(1)
    ->setPrice($price);

    $itemList = new ItemList();
    $itemList->setItems([$item]);

    $details = new Details();
    $details->setShipping($shipping)
    ->setSubtotal($price);

    $amount = new Amount();
    $amount->setCurrency('EUR')
    ->setTotal($total)
    ->setDetails($details);

    $transaction = new Transaction();
    $transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription($product)
    //UserID 
    ->setInvoiceNumber(rand());

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl('/paid.php?sucess=true')
    ->setCancelUrl('/paid.php?sucess=false');

    $payment = new Payment();
    $payment->setIntent('sale')
    ->setPayer($payer)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions([$transaction]);

    try{
        $payment->create($paypal);

    } catch (Exception $e){
        die($e);
    }

    $approvalUrl=$payment->getApprovalLink();
    header("Location: {$approvalUrl}");

以及下面的“start.php”,其中包含我的 API 凭据:

require 'vendor/autoload.php';

define('SITE_URL', '/charge.php');

$paypal = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
    'xx',
    'xx'
    )
);

谁能给我一个提示,我需要在哪里配置应用程序以使用 PayPals Live API?我在 carge.php 中尝试过,但我只得到 PHP 错误,我使用了未定义的变量。

我说的是来自 PayPals Github 的以下 Snipit:

$apiContext->setConfig(
      array(
        ...
        'mode' => 'live',
        ...
      )
);

任何帮助将不胜感激。 干杯,弗洛

【问题讨论】:

  • 401 表示凭据无效。请注意,您用于沙盒的凭据不适用于实时。因此,请再次检查您的实时模式凭据。
  • 感谢您的回答。凭据正确。问题是,我没有将缺少的名称类(用于配置)放在“Start.php”中。

标签: php paypal sdk sandbox live


【解决方案1】:

好的,我想通了。 对于有相同问题的任何人:

为了使配置数组起作用,您还必须将 Nameclass 放在那里。 所以正确的“start.php”应该是这样的:

<?php

use \PayPal\Rest\ApiContext;
use \PayPal\Auth\OAuthTokenCredential;

require 'vendor/autoload.php';

define('SITE_URL', 'charge_paypal.php');

$paypal = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
    'x',
    'x'
    )
);

$paypal->setConfig([
    'mode' => 'live',
        'log.LogEnabled' => true,
        'log.FileName' => 'PayPal.log',
        'log.LogLevel' => 'FINE'
]);

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 2014-12-02
    • 2014-05-31
    • 1970-01-01
    • 2011-10-14
    • 2013-01-30
    • 1970-01-01
    相关资源
    最近更新 更多