Products - plug-and-pay/sdk-php GitHub Wiki

Introduction

For how to handle the exceptions correctly, see Exceptions.

Index Products

The ProductService->get() method extracts all Product models.

The ProductService->include() method extracts model relation values.

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;
use PlugAndPay\Sdk\Enum\ProductIncludes;

$client = new Client($token);
$service = new ProductService($client);

$products = $service->include(
    ProductIncludes::PRICING,
    ProductIncludes::TAX_RATES,
)->get();

Filters

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;
use PlugAndPay\Sdk\Filters\ProductFilter;
use PlugAndPay\Sdk\Enum\Interval;
use PlugAndPay\Sdk\Enum\CountryCode;
use PlugAndPay\Sdk\Enum\Mode;
use PlugAndPay\Sdk\Enum\SubscriptionStatus;
use PlugAndPay\Sdk\Enum\ContractType;
use PlugAndPay\Sdk\Enum\Direction;
use PlugAndPay\Sdk\Enum\ProductSortType;
use PlugAndPay\Sdk\Enum\ContractType;

$client  = new Client($token);
$service = new ProductFilter($client);

$subscriptions = $service->get(
    (new ProductFilter())
        ->direction(Direction::ASC)
        ->hasLimitedStock(true)
        ->isDeleted(false)
        ->limit(10)
        ->page(1)
        ->query('Lorem')
        ->sort(ProductSortType::ACTIVE_SUBSCRIPTIONS)
        ->tag(['first'])
        ->type(ContractType::INSTALLMENTS)
);

Show Products

The ProductService->find() method extracts one Product models.

The ProductService->include() method extracts model relation values.

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;
use PlugAndPay\Sdk\Enum\ProductIncludes;

$client = new Client($token);
$service = new ProductService($client);

$productId = 1;
$product = $service->include(
    ProductIncludes::PRICING,
    ProductIncludes::TAX_RATES,
)->find($productId);

Store Products

The ProductService->create() method stores one Product model to the Plug and Pay dashboard.

The ProductService->include() method extracts model relation values.

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;
use PlugAndPay\Sdk\Entity\Stock;
use PlugAndPay\Sdk\Entity\TaxRate;
use PlugAndPay\Sdk\Entity\PricingTax;
use PlugAndPay\Sdk\Entity\Shipping;
use PlugAndPay\Sdk\Entity\PriceTier;
use PlugAndPay\Sdk\Entity\PriceRegular;
use PlugAndPay\Sdk\Entity\PriceFirst;
use PlugAndPay\Sdk\Entity\Price;
use PlugAndPay\Sdk\Entity\ProductPricing;
use PlugAndPay\Sdk\Entity\Product;
use PlugAndPay\Sdk\Enum\ProductIncludes;

$client = new Client($token);
$service = new ProductService($client);

$stock = (new Stock())
    ->setEnabled(true)
    ->setHidden(false)
    ->setValue(100);

$taxRate = (new TaxRate())
    ->setId(1);

$pricingTax = (new PricingTax())
    ->setRate($taxRate);

$shipping = (new Shipping())
    ->setAmount(100.00);

$tierOne = (new PriceTier())
    ->setAmount(100.00)
    ->setQuantity(10);

$tierTwo = (new PriceTier())
    ->setAmount(10.00)
    ->setQuantity(100);

$priceRegular = (new PriceRegular())
    ->setAmount(400.00);

$priceFirst = (new PriceFirst())
    ->setAmount(100.00);

$priceOne = (new Price())
    ->setFirst($priceFirst)
    ->setRegular($priceRegular)
    ->setInterval(Interval::QUARTERLY)
    ->setSuggested(true)
    ->setTiers([
        $tierOne,
        $tierTwo,
    ]);

$priceTwo = (new Price())
    ->setFirst($priceFirst)
    ->setRegular($priceRegular)
    ->setInterval(Interval::MONTHLY)
    ->setSuggested(true)
    ->setTiers([
        $tierOne,
        $tierTwo,
    ]);

$productPricing = (new ProductPricing())
    ->setTaxIncluded(true)
    ->setPrices([$priceOne, $priceTwo])
    ->setShipping($shipping)
    ->setTax($pricingTax);

$product = (new Product)
    ->setDescription('lorem ipsum')
    ->setLedger(99998)
    ->setPhysical(true)
    ->setPricing($productPricing)
    ->setPublicTitle('Lorem Ipsum')
    ->setSku('123-sku')
    ->setSlug('lorem-ipsum')
    ->setStock($stock)
    ->setTitle('Lorem Ipsum')
    ->setType(ContractType::SUBSCRIPTION);

$product = $service->include(
    ProductIncludes::PRICING,
    ProductIncludes::TAX_RATES,
)->create($product);

Update product

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;
use PlugAndPay\Sdk\Entity\Product;
use PlugAndPay\Sdk\Entity\Stock;
use PlugAndPay\Sdk\Exception\DecodeResponseException;

$client  = new Client($token);
$service = new ProductService($client);
$productId = 12;

try {
    $service->update(
        $productId,
        fn(Product $product) => $product
            ->setStock((new Stock())
                ->setHidden(true)
                ->setEnabled(true)
                ->setValue(100)
            )
    );
} catch (DecodeResponseException $e) {
    // Do your exception handling
}

Destroy product

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;

$client  = new Client($token);
$service = new ProductService($client);

$productId = 12;
$service->delete($productId);

Extra Information

By using the include method you can get more information. It is important that you only request the information you need.

use PlugAndPay\Sdk\Service\Client;
use PlugAndPay\Sdk\Service\ProductService;
use PlugAndPay\Sdk\Enum\ProductIncludes;

$client  = new Client($token);
$service = new ProductService($client);

$productId = 1;
$service->include(
    ProductIncludes::PRICING,
    ProductIncludes::SHIPPING,
)->find($productId);