PHP code example of payment-gateways / paypal-sdk

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

    

payment-gateways / paypal-sdk example snippets


use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getProducts()->toArray();

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getProduct('PROD-8R6565867F172242R')->toArray();

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;
use PaymentGateway\PayPalSdk\Products\Constants\ProductType;
use PaymentGateway\PayPalSdk\Products\Constants\ProductCategory;
use PaymentGateway\PayPalSdk\Products\Requests\StoreProductRequest;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$productRequest = new StoreProductRequest('My new product', ProductType::SERVICE);
$productRequest->setProductDescription('product description')
    ->setProductCategory(ProductCategory::SOFTWARE)
    ->setImageUrl('https://example.com/productimage.jpg')
    ->setHomeUrl('https://example.com');

// ['id' => 'PROD-XY...', 'name' => 'My new product', ...]
$response = $service->createProduct($productRequest)->toArray();

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;
use PaymentGateway\PayPalSdk\Products\Constants\ProductCategory;
use PaymentGateway\PayPalSdk\Products\Requests\UpdateProductRequest;

$service = new CatalogProductsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$productRequest = new UpdateProductRequest('PROD-XY458712546854478');
$productRequest->setProductDescription('product description')
    ->setProductCategory(ProductCategory::ACADEMIC_SOFTWARE)
    ->setImageUrl('https://example.com/productimage.jpg')
    ->setHomeUrl('https://example.com');

$service->updateProduct($productRequest);

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getPlans()->toArray();

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getPlan('P-18T532823A424032WL7NIVUA')->toArray();

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;
use PaymentGateway\PayPalSdk\Subscriptions\Frequency;
use PaymentGateway\PayPalSdk\Subscriptions\BillingCycles\BillingCycleSet;
use PaymentGateway\PayPalSdk\Subscriptions\BillingCycles\RegularBillingCycle;
use PaymentGateway\PayPalSdk\Subscriptions\Constants\CurrencyCode;
use PaymentGateway\PayPalSdk\Subscriptions\Money;
use PaymentGateway\PayPalSdk\Subscriptions\PricingSchema;
use PaymentGateway\PayPalSdk\Plans\Requests\StorePlanRequest;
use PaymentGateway\PayPalSdk\Subscriptions\Constants\IntervalUnit;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$frequency = new Frequency(IntervalUnit::MONTH, 1);
$pricingSchema = new PricingSchema(new Money(CurrencyCode::UNITED_STATES_DOLLAR, '350'));
$billingCycle = new RegularBillingCycle($frequency, $pricingSchema);
$billingCycleSet = new BillingCycleSet();
$billingCycleSet->addBillingCycle($billingCycle);
$planRequest = new StorePlanRequest('PROD-8R6565867F172242R', 'New Plan', $billingCycleSet);

// ['id' => 'P-XY...', 'product_id' => 'PROD-8R6565867F172242R', 'name' => 'My Plan', ...]
$response = $service->createPlan($planRequest)->toArray();

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;
use PaymentGateway\PayPalSdk\Subscriptions\Constants\CurrencyCode;
use PaymentGateway\PayPalSdk\Subscriptions\Money;
use PaymentGateway\PayPalSdk\Plans\Requests\UpdatePlanRequest;
use PaymentGateway\PayPalSdk\Subscriptions\PaymentPreferences;

$service = new BillingPlansApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$paymentPreferences = new PaymentPreferences();
$paymentPreferences->setSetupFee(new Money(CurrencyCode::UNITED_STATES_DOLLAR, '250'));
$planRequest = new UpdatePlanRequest('P-18T532823A424032WL7NIVUA');
$planRequest->setPaymentPreferences($paymentPreferences);

$service->updatePlan($planRequest);

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');
$response = $service->getSubscription('I-18T532823A424032WL7NIVUA')->toArray();

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;
use PaymentGateway\PayPalSdk\Subscriptions\Requests\StoreSubscriptionRequest;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->setCredentials('AeA1QIZXiflr1', 'ECYYrrSHdKfk');

$subscriptionRequest = new StoreSubscriptionRequest('P-18T532823A424032WL7NIVUA');

// ["status": "APPROVAL_PENDING", "id": "I-GFCGPH9100S7", ...]
$response = $service->createSubscription($subscriptionRequest);

$response = $service->getProducts();

if (!$response->isSuccessful()) {
    var_dump($response->toArray());     // check the errors
} else {
    // array with data
    $response->toArray();
}

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->withHandler($handler);

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;
use PaymentGateway\PayPalApiMock\PayPalApiMock;

$service = new SubscriptionsApi('https://api.sandbox.paypal.com');
$service->withHandler(new PayPalApiMock());