【发布时间】:2019-06-29 20:16:24
【问题描述】:
我正在设置一个支付系统,当我使用“paypal”后将结帐页面提交给它自己时,paypal sdk 代码将执行并重定向到 paypal,我遇到的问题是当我使用我得到的 if 语句时错误:
解析错误:语法错误,第 14 行 /path/to/file/index.php 中的意外“使用”(T_USE)
require('' . $_SERVER['DOCUMENT_ROOT'] . '/assets/include/config.php');
if (isset($_POST['paypal'])) {
// Work out price and add to db
$total = 0.00;
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
$total = $total + $item['Itemprice'];
}
// Set some example data for the payment.
$currency = 'GBP';
$amountPayable = $total;
$invoiceNumber = uniqid();
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
// Includes PayPal Config
require $_SERVER['DOCUMENT_ROOT'] . '/assets/include/pp-bootstrap.php';
// Create a payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');
// Sets ammount for transaction
$amount = new Amount();
$amount->setCurrency($currency)->setTotal($amountPayable);
// Creates a new transaction
$transaction = new Transaction();
$transaction->setAmount($amount)->setDescription('Some description about the payment being made')->setInvoiceNumber($invoiceNumber);
// Sets the return URL's
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($return_url)->setCancelUrl($cancel_url);
// Creates a payment using the
$payment = new Payment();
$payment->setIntent('sale')->setPayer($payer)->setTransactions([$transaction])->setRedirectUrls($redirectUrls);
try {
$payment->create($apiContext);
} catch (Exception $e) {
throw new Exception('Unable to create a link to paypal, this may be due to an issue with PayPal, please try again later.');
}
header('location:' . $payment->getApprovalLink());
exit(1);
【问题讨论】:
-
在发布问题之前尝试搜索您的错误消息。你可能会找到你要找的东西:stackoverflow.com/questions/33342994/…
-
我忘了提到 if (isset($_POST['paypal'])) { 不存在时代码可以正常工作