PHP code example of cwdev-support / laravel-paypal
1. Go to this page and download the library: Download cwdev-support/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/ */
use PayPal;
use Redirect;
// ...
public function getCheckout()
{
$payer = PayPal::Payer();
$payer->setPaymentMethod('paypal');
$amount = PayPal:: Amount();
$amount->setCurrency('EUR');
$amount->setTotal(42); // This is the simple way,
// you can alternatively describe everything in the order separately;
// Reference the PayPal PHP REST SDK for details.
$transaction = PayPal::Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('What are you selling?');
$redirectUrls = PayPal:: RedirectUrls();
$redirectUrls->setReturnUrl(action('ThisController@getDone'));
$redirectUrls->setCancelUrl(action('ThisController@getCancel'));
$payment = PayPal::Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$response = $payment->create($this->_apiContext);
$redirectUrl = $response->links[1]->href;
return Redirect::to( $redirectUrl );
}
public function getDone(Request $request)
{
$id = $request->get('paymentId');
$token = $request->get('token');
$payer_id = $request->get('PayerID');
$payment = PayPal::getById($id, $this->_apiContext);
$paymentExecution = PayPal::PaymentExecution();
$paymentExecution->setPayerId($payer_id);
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
// Clear the shopping cart, write to database, send notifications, etc.
// Thank the user for the purchase
return view('checkout.done');
}
public function getCancel()
{
// Curse and humiliate the user for cancelling this most sacred payment (yours)
return view('checkout.cancel');
}
public function createWebProfile(){
$flowConfig = PayPal::FlowConfig();
$presentation = PayPal::Presentation();
$inputFields = PayPal::InputFields();
$webProfile = PayPal::WebProfile();
$flowConfig->setLandingPageType("Billing"); //Set the page type
$presentation->setLogoImage("https://www.example.com/images/logo.jpg")->setBrandName("Example ltd"); //NB: Paypal recommended to use https for the logo's address and the size set to 190x60.
$inputFields->setAllowNote(true)->setNoShipping(1)->setAddressOverride(0);
$webProfile->setName("Example " . uniqid())
->setFlowConfig($flowConfig)
// Parameters for style and presentation.
->setPresentation($presentation)
// Parameters for input field customization.
->setInputFields($inputFields);
$createProfileResponse = $webProfile->create($this->_apiContext);
return $createProfileResponse->getId(); //The new webprofile's id
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.