0

I am setting up a payment system and when submitting the checkout page to its self with the post "paypal" the paypal sdk code will execute and redirect to paypal, the issues im having is when i use the if statement i get and error:

Parse error: syntax error, unexpected 'use' (T_USE) in /path/to/file/index.php on line 14

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);
  • Try to search for your error message before posting a question. You might find what you are looking for: https://stackoverflow.com/questions/33342994/unexpected-use-t-use-when-trying-to-use-composer – ren.rocks Feb 05 '19 at 22:43
  • I forgot to mention that the code works fine when the if (isset($_POST['paypal'])) { isnt there – Alan Tiller Feb 05 '19 at 22:47

1 Answers1

0

I figured it out, with the "use PayPal\Api\Amount;" if you just move that to the class call it works fine:

// 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);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459