【问题标题】:PayPal SDK and If Statement issue [duplicate]PayPal SDK和If语句问题[重复]
【发布时间】: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'])) { 不存在时代码可以正常工作

标签: php paypal


【解决方案1】:

我想通了,“使用 PayPal\Api\Amount;”如果你只是把它移到类调用它工作正常:

// Create a payer
    $payer = new PayPal\Api\Payer();
    $payer->setPaymentMethod('paypal');
    
    // Sets amount for transaction
    $amount = new PayPal\Api\Amount();
    $amount->setCurrency($currency)->setTotal($amountPayable);
    
    // Creates a new transaction
    $transaction = new PayPal\Api\Transaction();
    $transaction->setAmount($amount)->setDescription('Some description about the payment being made')->setInvoiceNumber($invoiceNumber);
    
    // Sets the return URL's
    $redirectUrls = new PayPal\Api\RedirectUrls();
    $redirectUrls->setReturnUrl($return_url)->setCancelUrl($cancel_url);
    
    // Creates a payment using the 
    $payment = new PayPal\Api\Payment();
    $payment->setIntent('sale')->setPayer($payer)->setTransactions([$transaction])->setRedirectUrls($redirectUrls);

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 2015-07-30
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2022-10-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多