PHP code example of beziworld / laravel-inpost-pay

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

    

beziworld / laravel-inpost-pay example snippets


use Beziworld\InpostPay\Contracts\BasketProvider;
use Beziworld\InpostPay\Contracts\OrderHandler;

$this->app->bind(BasketProvider::class, App\InpostPay\ShopBasketProvider::class);
$this->app->bind(OrderHandler::class, App\InpostPay\ShopOrderHandler::class);

use Beziworld\InpostPay\Contracts\BasketProvider;
use Beziworld\InpostPay\Data\{Basket, BasketEvent, BindingConfirmation, Consent, DeliveryOption, Price, Product};
use Beziworld\InpostPay\Enums\{ConsentRequirement, DeliveryType};
use Beziworld\InpostPay\Exceptions\MerchantApiError;

final class ShopBasketProvider implements BasketProvider
{
    public function basket(string $basketId): Basket
    {
        $cart = Cart::find($basketId) ?? throw MerchantApiError::basketNotFound($basketId);

        return new Basket(
            basePrice: Price::fromGross($cart->total, 23),
            products: $cart->items->map(fn ($item) => new Product(
                productId: (string) $item->product_id,
                name: $item->name,
                basePrice: Price::fromGross($item->price, 23),
                quantity: (string) $item->quantity,
                availableQuantity: (string) $item->stock,
                image: $item->image_url,
                ean: $item->ean,
            ))->all(),
            delivery: [
                (new DeliveryOption(DeliveryType::Apm, Price::fromGross('12.99', 23), now()->addDays(2)))
                    ->withOption('Paczka w Weekend', 'PWW', Price::fromGross('5.00', 23)),
                new DeliveryOption(DeliveryType::Courier, Price::fromGross('16.99', 23), now()->addDays(1)),
            ],
            consents: [new Consent('terms', 'Regulamin sklepu', route('terms'), '2024-01', ConsentRequirement::RequiredAlways)],
            expiresAt: now()->addDays(7),
        );
    }

    public function confirmBinding(BindingConfirmation $confirmation): Basket
    {
        // $confirmation->phoneNumber, ->browserTrusted, ->browserId, ->isSuccessful()
        return $this->basket($confirmation->basketId);
    }

    public function handleEvent(BasketEvent $event): Basket
    {
        // $event->type (PRODUCTS_QUANTITY, PROMO_CODES, RELATED_PRODUCTS), ->quantities, ->promoCodes, ->relatedProducts
        Cart::find($event->basketId)->applyInpostChanges($event->quantities, $event->promoCodes);

        return $this->basket($event->basketId);
    }

    public function unbind(string $basketId): void
    {
        Cart::find($basketId)?->forgetInpostBinding();
    }
}

use Beziworld\InpostPay\Data\OrderUpdate;
use Beziworld\InpostPay\Facades\InpostPay;

// Widget 2.0: key for the basket binding widget
$bindingKey = InpostPay::bindBasket($cart->id);

// The basket changed in the shop: send the full current basket
InpostPay::updateBasket($cart->id, $basket);

$binding = InpostPay::basketBinding($cart->id, $browserId);   // ->linked, ->browserTrusted, ->maskedPhoneNumber
InpostPay::unbindBasket($cart->id, realized: true);            // basket turned into an order in the shop

// Orders: shipment numbers are mandatory, statuses and descriptions shown in the app
InpostPay::addShipments($orderId, ['520000011395200025754311'], 'Wysłane');
InpostPay::completeOrder($orderId, 'Zrealizowane');           // ORDER_COMPLETED
InpostPay::rejectOrder($orderId, 'Anulowane');                // ORDER_REJECTED
InpostPay::updateOrder($orderId, OrderUpdate::description('W trakcie realizacji'));

// Back office
InpostPay::baskets(pageIndex: 0, pageSize: 50);
InpostPay::orders(orderId: $orderId);
InpostPay::paymentMethods();                                  // ['CARD', 'BLIK_CODE', ...]

// Transactions and refunds (signed with INPOST_PAY_MERCHANT_SECRET)
$page = InpostPay::transactions(['order_id' => $orderId, 'status' => ['CAPTURED']]);
$refund = InpostPay::refund($transactionId, amount: '49.99', externalRefundId: 'RMA-17');
$refund->status;    // RefundStatus::Pending | Success | Failed
$refund->commandId; // X-Command-ID: pass it again to repeat a refund with an unknown outcome

InpostPay::using(['client_id' => $store->inpost_client_id, 'client_secret' => $store->inpost_secret, 'sandbox' => false])
    ->addShipments($orderId, [$trackingNumber]);