我学习了一些关于如何使用PHP SDK向PayPal用户收费的教程。只要我在Testmode中,这一切都像一个符咒,但如果我将API密钥保存到“live”密钥,我只会得到一个HTTP错误401。
我理解这是因为我必须将应用程序设置为“Live”。
别担心,我不是为acutal 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',
...
)
);
任何帮助都将不胜感激。
干杯,弗洛