payment-gateways/paypal-sdk

v1.2.0 2021-02-03 13:56 UTC

This package is auto-updated.

Last update: 2024-06-17 19:03:45 UTC


README

Build Status Code Quality Code Coverage

PayPal SDK

This is an SDK for PayPal REST APIS. The following APIs are currently supported.

Bugs Bugs Bugs

Installation

Use following command to install this library:

composer require payment-gateways/paypal-sdk

Usage

Authentication

Go to PayPal Developer site and get the Client ID and Secret for your app. These credentials can be used in the service authentication as follows:

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

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

Catalog Products API

Merchants can use the Catalog Products API to create products, which are goods and services.

List products

To get all products use the getProducts method.

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;

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

Get a product

To get a single products use the getProduct method.

use PaymentGateway\PayPalSdk\Api\CatalogProductsApi;

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

Create a product

To create a product use the createProduct method.

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();

Update a product

To update a product use the updateProduct method.

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);

Billing Plans API

You can use billing plans and billing agreements to create an agreement for a recurring PayPal or debit card payment for goods or services.

List plans

To get all plans use the getPlans method.

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;

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

Get a plan

To get a single plan use the getPlan method.

use PaymentGateway\PayPalSdk\Api\BillingPlansApi;

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

Create a plan

To create a product use the createPlan method.

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();

Update a plan

To update a product use the updatePlan method.

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);

Subscriptions API

You can use this API to create subscriptions that process recurring PayPal payments for physical or digital goods, or services.

Get a subscription

To get a single subscription use the getSubscription method.

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

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

Create a subscription

To create a subscription use the createSubscription method.

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);

Error Handling

In general, You can use the toArray method in all responses to get the service response data (except for patch operations), otherwise you'll get an array with errors if something fails. You can check for a successful response using the isSuccessful method. .

$response = $service->getProducts();

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

Mocking

You can create your own PayPal API Mock for use with PayPalService like this

use PaymentGateway\PayPalSdk\Api\SubscriptionsApi;

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

The handler must be a callable type. If you prefer, you can use the PayPal API Mock adding the following to your project

composer require --dev payment-gateways/paypal-api-mock

After, you could use the class PayPalApiMock as a handler

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

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