PHP code example of sger / laravel-paypal

1. Go to this page and download the library: Download sger/laravel-paypal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sger / laravel-paypal example snippets


Sger\Paypal\PaypalServiceProvider::class

'Paypal' => Sger\Paypal\Facades\Paypal::class

Route::resource('payments', 'PaymentsController');

$payer = new Payer;
$payer->setPaymentMethod("paypal");

$item1 = new Item();
$item1->setName('test')
	->setCurrency('EUR')
	->setQuantity(1)
	->setPrice(10);

$itemList = new ItemList();
$itemList->setItems(array($item1));

$amount = new Amount();
$amount->setCurrency('EUR')
	->setTotal(10);

$transaction = new Transaction();
$transaction->setAmount($amount)
	->setItemList($itemList)
	->setDescription("Payment description")
	->setInvoiceNumber(uniqid());

$baseUrl = \URL::to('/');

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/execute-payment?success=true")
->setCancelUrl("$baseUrl/execute-payment?success=false");

$payment = new Payment();

$payment->setIntent("sale")
	->setPayer($payer)
	->setRedirectUrls($redirectUrls)
	->setTransactions(array($transaction));

try {
	$payment->create(\Paypal::connection('sandbox'));
} catch (\PPConnectionException $ex) {
	return  "Exception: " . $ex->getMessage() . PHP_EOL;
	exit(1);
}

$approvalUrl = $payment->getApprovalLink();
// redirect user to the $approvalUrl
bash
php artisan vendor:publish