PHP code example of proxylyx / paypal

1. Go to this page and download the library: Download proxylyx/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/ */

    

proxylyx / paypal example snippets


  // Laravel 5.1 or up
  Proxylyx\PayPal\Providers\PayPalServiceProvider::class 
  

  // Laravel 5 or below
  'Proxylyx\PayPal\Providers\PayPalServiceProvider'
  

  // Laravel 5.1 or up
  'PayPal' => Proxylyx\PayPal\Facades\PayPal::class
  

  // Laravel 5 or below
  'PayPal' => 'Proxylyx\PayPal\Facades\PayPal'
  

  use PayPal;
  

  protected $PayPalProvider;

  public function __construct()
  {
        $this->PayPalProvider = PayPal::setProvider('express_checkout');
  }
  

  protected function paypalData()
  {
          $order = Order::find(session('orderId'));
          $data = [];
          try {
              $orderItems = $order->items()->get();
          } catch (\Exception $e) {
              throw new \Exception("Order items empty", 1);
          }
          $data['items'] = [];
          foreach ($orderItems as $item) {
              array_push($data['items'], [
                  'name' => $item->item_name,
                  'price' => $item->price,
                  'qty' => $item->quantity
              ]);
          }
          $data['invoice_id'] = $order->id;
          $data['invoice_description'] = "Order #{$data['invoice_id']} Invoice";
          $data['return_url'] = route('paymentCallback');
          $data['cancel_url'] = route('paymentCancel');

          $total = 0;
          foreach($data['items'] as $item) {
              $total += $item['price']*$item['qty'];
          }
          $data['total'] = $total;

          return $data;
  }
  

  $options = [
      'BRANDNAME' => env('APP_NAME'),
      'LOGOIMG' => url('img/logo.png'),
      'CHANNELTYPE' => 'Merchant'
  ];
  

  $data = $this->paypalData();
  $response = $this->PayPalProvider->addOptions($options)->setExpressCheckout($data);
  return redirect($response['paypal_link']);
  

  protected function paypalCallback()
  { 
      $token = isset($_GET['token']) ? $_GET['token'] : null;
      $payerID = isset($_GET['PayerID']) ? $_GET['PayerID'] : null;

      if ($token == null || $payerID == null) {
          return collect([
              'status' => 'invalid',
              'message' => "Token or Payer ID not found"
          ]);
      }

      $data = $this->paypalData();

      $response = $this->PayPalProvider->doExpressCheckoutPayment($data, $token, $payerID);

      $ack = strtolower($response['ACK']);

      if ($ack === 'success' || $ack === 'successwithwarning') {
          return collect([
              'status' => 'success',
              'transactionId' => $response['PAYMENTINFO_0_TRANSACTIONID'],
              'message' => "Payment successfully processed."
          ]);
      } else {
          return collect([
              'status' => 'failed',
              'message' => $response['L_SHORTMESSAGE0']
          ]);
      }

  }
  

$provider->setApiCredentials($config);

$provider->setCurrency('EUR')->setExpressCheckout($data);

$response = $provider->refundTransaction($transactionid);

$amount = 10;
$response = $provider->refundTransaction($transactionid, $amount);

// The $token is the value returned from SetExpressCheckout API call
$response = $provider->createBillingAgreement($token);

  // The $token is the value returned from SetExpressCheckout API call
  $startdate = Carbon::now()->toAtomString();
  $profile_desc = !empty($data['subscription_desc']) ?
  $data['subscription_desc'] : $data['invoice_description'];
  $data = [
  'PROFILESTARTDATE' => $startdate,
  'DESC' => $profile_desc,
  'BILLINGPERIOD' => 'Month', // Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year'
  'BILLINGFREQUENCY' => 1, // 
  'AMT' => 10, // Billing amount for each billing cycle
  'CURRENCYCODE' => 'USD', // Currency code 
  'TRIALBILLINGPERIOD' => 'Day',  // (Optional) Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year'
  'TRIALBILLINGFREQUENCY' => 10, // (Optional) set 12 for monthly, 52 for yearly 
  'TRIALTOTALBILLINGCYCLES' => 1, // (Optional) Change it accordingly
  'TRIALAMT' => 0, // (Optional) Change it accordingly
  ];
  $response = $provider->createRecurringPaymentsProfile($data, $token);
  

    $response = $provider->getRecurringPaymentsProfileDetails($profileid);
    

    $response = $provider->updateRecurringPaymentsProfile($data, $profileid);
    

    // Cancel recurring payment profile
    $response = $provider->cancelRecurringPaymentsProfile($profileid);
    
    // Suspend recurring payment profile
    $response = $provider->suspendRecurringPaymentsProfile($profileid);
    
    // Reactivate recurring payment profile
    $response = $provider->reactivateRecurringPaymentsProfile($profileid);    
    

PayPal::setProvider('adaptive_payments');


  // Change the values accordingly for your application
  $data = [
      'receivers'  => [
          [
              'email' => '[email protected]',
              'amount' => 10,
              'primary' => true,
          ],
          [
              'email' => '[email protected]',
              'amount' => 5,
              'primary' => false
          ]
      ],
      'payer' => 'EACHRECEIVER', // (Optional) Describes who pays PayPal fees. Allowed values are: 'SENDER', 'PRIMARYRECEIVER', 'EACHRECEIVER' (Default), 'SECONDARYONLY'
      'return_url' => url('payment/success'), 
      'cancel_url' => url('payment/cancel'),
  ];

  $response = $provider->createPayRequest($data);

  // The above API call will return the following values if successful:
  // 'responseEnvelope.ack', 'payKey', 'paymentExecStatus'

  

  $redirect_url = $provider->getRedirectUrl('approved', $response['payKey']);

  return redirect($redirect_url);
  

    Route::post('ipn/notify','PayPalController@postNotify'); // Change it accordingly in your application
    

    'ipn/notify'
    

    /**
     * Retrieve IPN Response From PayPal
     *
     * @param \Illuminate\Http\Request $request
     */
    public function postNotify(Request $request)
    {
        // Import the namespace Proxylyx\PayPal\Services\ExpressCheckout first in your controller.
        $provider = new ExpressCheckout;
        $response = (string) $provider->parsePayPalIPN($request);
        
        if ($response === 'VERIFIED') {                      
            // Your code goes here ...
        }                            
    }        
    

  // Always update the code below accordingly to your own me'  => "Monthly Subscription",
          'price' => 0,
          'qty'   => 1,
      ],
  ];

  $data['subscription_desc'] = "Monthly Subscription #1";
  $data['invoice_id'] = 1;
  $data['invoice_description'] = "Monthly Subscription #1";
  $data['return_url'] = url('/paypal/ec-checkout-success?mode=recurring');
  $data['cancel_url'] = url('/');

  $total = 0;
  foreach ($data['items'] as $item) {
      $total += $item['price'] * $item['qty'];
  }

  $data['total'] = $total;
  

  $amount = 9.99;
  $description = "Monthly Subscription #1";
  $response = $provider->createMonthlySubscription($token, $amount, $description);

  // To create recurring yearly subscription on PayPal
  $response = $provider->createYearlySubscription($token, $amount, $description);
  
bash
  php artisan vendor:publish --provider "Proxylyx\PayPal\Providers\PayPalServiceProvider"