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);