This is my first time working with Paypal and I've got this code running just fine on my local machine, but I'm having troubles with it once it's live on a server. When it hits the checkout page it should process the payment and forward the user to another page, but when I click on it it doesn't forward the user. Any ideas guys?
This is the start file
<?php
require 'vendor/autoload.php';
define('SITE_URL', 'http://localhost/paypal');
//user name and password
$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'**************************************',
'**************************************')
);
?>
This is the checkout file
<?php
session_start();
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;
require 'app/start.php';
if(!isset($_SESSION['total'], $_SESSION['shippingtotal'])) {
die("You must enter the required information.");
}
$product = 'WisconsinDairyFarmers Purchase';
$price = (float)$_SESSION['total'];
$shipping = (float)$_SESSION['shippingtotal'];
$total = $price + $shipping;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName($product)
->setCurrency('USD')
->setQuantity(1)
->setPrice($price);
$itemList = new ItemList();
// can send multiple items example: $item, $item2, $item3
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping($shipping)
->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($total)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription('WisconsinDairyFarmers Payment')
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(SITE_URL . '/pay.php?success=true')
->setCancelUrl(SITE_URL . '/pay.php?success=false');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
try{
$payment->create($paypal);
} catch(Exception $e) {
$data = json_decode($e->getData());
echo $data->message;
die();
}
$approvalUrl = $payment->getApprovalLink();
header("Location: {$approvalUrl}");
?>