Skip to content

Instantly share code, notes, and snippets.

@reindert-vetter
Created September 1, 2022 13:44
Show Gist options
  • Save reindert-vetter/fdce9382862f9b2c994c148f95e20b8c to your computer and use it in GitHub Desktop.
Save reindert-vetter/fdce9382862f9b2c994c148f95e20b8c to your computer and use it in GitHub Desktop.

Introduction

For how to handle the exceptions correctly, see Exceptions.

Show

To load more information see Extra Information.

use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service->find(1);

$amount        = $order->amount();
$amountWithTax = $order->amountWithTax();
$createdAt     = $order->createdAt();
$deletedAt     = $order->deletedAt();
$id            = $order->id();
$invoiceNumber = $order->invoiceNumber();
$invoiceStatus = $order->invoiceStatus();
$isFirst       = $order->isFirst();
$isHidden      = $order->isHidden();
$taxExempt     = $order->taxExempt();
$mode          = $order->mode();
$reference     = $order->reference();
$source        = $order->source();
$updatedAt     = $order->updatedAt();

Index

To load more information see Extra Information.

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$orders = $service->include(OrderIncludes::BILLING, OrderIncludes::COMMENTS)->get();

Filter

use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;
use \PlugAndPay\Sdk\Filters\OrderFilter;

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

$orders = $service->get(function(OrderFilter $filter) {
    $filter
      ->affiliateId(1234)
      ->checkoutId(1234)
      ->contractId(1234)
      ->contractType(\PlugAndPay\Sdk\Enum\ContractType::SUBSCRIPTION)
      ->country('NL')
      ->direction(\PlugAndPay\Sdk\Enum\Direction::DESC)
      ->discountCode('sale123')
      ->email('[email protected]')
      ->hasBump(true)
      ->hasTax(true)
      ->invoiceStatus(\PlugAndPay\Sdk\Enum\InvoiceStatus::FINAL)
      ->isDeleted(true)
      ->isFirst(true)
      ->isHidden(true)
      ->isUpsell(true)
      ->limit(1)
      ->mode(\PlugAndPay\Sdk\Enum\OrderMode::LIVE)
      ->page(2)
      ->paymentStatus(\PlugAndPay\Sdk\Enum\PaymentStatus::PAID)
      ->productId(1234)
      ->productTag('color-blue')
      ->query('Piet Niet')
      ->sinceInvoiceDate(new DateTime())
      ->sincePaidAt(new DateTime())
      ->sort(\PlugAndPay\Sdk\Enum\OrderSortType::PAID_AT)
      ->source(\PlugAndPay\Sdk\Enum\OrderSource::API)
      ->untilInvoiceDate(new DateTime())
      ->untilPaidAt(new DateTime());
});

Create Order

When creating an order, to load more information see Extra Information.

use \PlugAndPay\Sdk\Entity\Address;
use \PlugAndPay\Sdk\Entity\Billing;
use \PlugAndPay\Sdk\Entity\Comment;
use \PlugAndPay\Sdk\Entity\Contact;
use \PlugAndPay\Sdk\Entity\Item;
use \PlugAndPay\Sdk\Entity\Order;
use \PlugAndPay\Sdk\Entity\Payment;
use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Enum\PaymentStatus;
use \PlugAndPay\Sdk\Enum\TaxExempt;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

$client  = new Client($token);
$service = new OrderService($client);
$order = (new Order())
    ->setTaxExempt(TaxExempt::REVERSE)
    ->setHidden(false)
    ->setBilling((new Billing())
        ->setAddress((new Address())
            ->setCity('Rijnsburg')
            ->setCountry('NL')
            ->setStreet('Koestraat')
            ->setHouseNumber('55 b')
            ->setZipcode('2231 JE')
        )
        ->setContact((new Contact())
            ->setCompany('RosLie B.V.')
            ->setEmail('[email protected]')
            ->setFirstName('Bilal')
            ->setLastName('de Wit')
            ->setTelephone('0678786754')
            ->setWebsite('roslie.nl')
            ->setVatIdNumber('NL000099998B57')
        )
    )
    ->setComments(
        [
            (new Comment())->setValue('the comment'),
        ]
    )
    ->setItems(
        [
            (new Item())
                ->setAmount(new Money(10.))
                ->setLabel('the-label')
                ->setQuantity(2)
                ->setTaxByRateId(123)
                ->setProductId(123),
        ]
    )
    ->setPayment((new Payment())
        ->setStatus(PaymentStatus::PROCESSING))
    )
    ->setTags(['first_tag', 'second_tag']);

$order = $service->create($order);
$orderId = $order->id();

Update Order

See Extra Information to load more information in your response. For now, you cannot update order items and discounts.

use \PlugAndPay\Sdk\Entity\Order;
use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

$client  = new Client($token);
$service = new OrderService($client);
$orderId = 12;

$order = $service->update($orderId, function(Order $order) {
    $order->setTags(['first_tag', 'second_tag']);
    $order->payment()->setStatus(PaymentStatus::PROCESSING);
    $order->billing()->contact()->setEmail('[email protected]');
})

Destroy Order

use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$orderId = 12;

$service->delete($orderId)

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\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$service
    ->include(
        OrderIncludes::BILLING,
        OrderIncludes::COMMENTS,
        OrderIncludes::DISCOUNTS,
        OrderIncludes::ITEMS,
        OrderIncludes::PAYMENT,
        OrderIncludes::TAGS,
        OrderIncludes::TAXES,
    )
    ->find(1);

Billing

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(\PlugAndPay\Sdk\Enum\OrderIncludes::BILLING)
    ->find(1);

$billing = $order->billing();

$company     = $billing->company();
$email       = $billing->email();
$firstName   = $billing->firstName();
$lastName    = $billing->lastName();
$telephone   = $billing->telephone();
$website     = $billing->website();
$city        = $billing->address()->city();
$country     = $billing->address()->country();
$street      = $billing->address()->street();
$houseNumber = $billing->address()->houseNumber();
$zipcode     = $billing->address()->zipcode();

Comments

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(\PlugAndPay\Sdk\Enum\OrderIncludes::COMMENTS)
    ->find(1);

$comment = $order->comments()[0];

$createdAt = $comment->createdAt();
$id        = $comment->id();
$updatedAt = $comment->updatedAt();
$value     = $comment->value();

Discount

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(\PlugAndPay\Sdk\Enum\OrderIncludes::DISCOUNTS)
    ->find(1);

$discount = $order->totalDiscounts()[0];
// Or receive a discount from an item:
$discount = $order->items()[0]->discounts()[0];

$value    = $discount->amount();
$currency = $discount->amount()->currency();
$code     = $discount->code();
$type     = $discount->type();

Items

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(\PlugAndPay\Sdk\Enum\OrderIncludes::ITEMS)
    ->find(1);

$item = $order->items()[0];

$discount      = $item->discounts()[0]->amount();
$discountCode  = $item->discounts()[0]->code();
$id            = $item->id();
$label         = $item->label();
$productId     = $item->productId();
$quantity      = $item->quantity();
$amount        = $item->amount();
$amountWithTax = $item->amountWithTax();

Payment

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(\PlugAndPay\Sdk\Enum\OrderIncludes::PAYMENT)
    ->find(1);

$payment = $order->payment();

$customerId    = $payment->customerId();
$mandateId     = $payment->mandateId();
$method        = $payment->method();
$orderId       = $payment->orderId();
$paidAt        = $payment->paidAt();
$provider      = $payment->provider();
$status        = $payment->status();
$transactionId = $payment->transactionId();
$type          = $payment->type();
$url           = $payment->url();

Tags

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(OrderIncludes::TAGS)
    ->find(1);

$tags = $order->tags();

Taxes

use \PlugAndPay\Sdk\Enum\OrderIncludes;
use \PlugAndPay\Sdk\Service\Client;
use \PlugAndPay\Sdk\Service\OrderService;

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

$order = $service
    ->include(OrderIncludes::TAXES)
    ->find(1);

// All taxes
$tax = $order->taxes()[0];
$tax->amount()
$tax->rate()->percentage()

// Tax per item
$item              = $order->items()[0];
$itemTaxValue      = $item->tax()->amount();
$itemTaxPercentage = $item->tax()->rate()->percentage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment