PHP code example of appwilio / yakassa

1. Go to this page and download the library: Download appwilio/yakassa 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/ */

    

appwilio / yakassa example snippets


// config/app.php
'providers' => [
    ...
    Appwilio\YaKassa\YaKassaServiceProvider::class,
],

// config/services.php
...
'yakassa' => [
    'test_mode'     => env('YAKASSA_TEST_MODE', true),
    'shop_id'       => env('YAKASSA_SHOP_ID', ''),
    'showcase_id'   => env('YAKASSA_SHOWCASE_ID', ''),
    'shop_password' => env('YAKASSA_SHOP_PASSWORD', ''),        
],
...

use Appwilio\YaKassa\Contracts\YaKassaOrder;

class Order implements YaKassaOrder
{
    public function getOrderSum(): float
    {
        return $this->total;
    }
    
    public function getCustomerNumber(): string
    {
        return $this->customer->id;
    }
    
    public function getOrderNumber(): ?string
    {
        return $this->id;
    }
    
    public function getPaymentType(): ?string
    {
        return 'PC';
    }
    
    public function getCustomerEmail(): ?string
    {
        return $this->customer->email;
    }
    
    public function getCustomerPhone(): ?string
    {
        return $this->customer->phone;
    }
}

use Appwilio\YaKassa\Contracts\YaKassaOrder54FZ;

class Order implements YaKassaOrder54FZ
{
    public function getOrderSum(): float
    {
        return $this->total;
    }
    
    public function getCustomerNumber(): string
    {
        return $this->customer->id;
    }
    
    public function getItems(): iterable
    {
        return $this->items; // товары/услуги в заказе
    }
    
    public function getTaxSystem(): ?int
    {
        return YaKassaOrder54FZ::TAX_OSN;
    }
    
    public function getCustomerContact(): string
    {
        return $this->customer->phone;
    }
        
    public function getOrderNumber(): ?string
    {
        return $this->id;
    }
    
    public function getPaymentType(): ?string
    {
        return 'PC';
    }
    
    public function getCustomerEmail(): ?string
    {
        return $this->customer->email;
    }
    
    public function getCustomerPhone(): ?string
    {
        return $this->customer->phone;
    }
}

use Appwilio\YaKassa\Contracts\YaKassaOrderItem54FZ;

class OrderItem implements YaKassaOrderItem54FZ
{
    public function getAmount(): float
    {
        return $this->amount;
    }

    public function getQuantity(): float
    {
        return $this->quantity;
    }

    public function getTaxRate(): int
    {
        return YaKassaOrderItem54FZ::VAT_18;
    }

    public function getCurrency(): ?string
    {
        return null; // равнозначно RUB
    }

    public function getTitle(): string
    {
        return $this->product->title;
    }
}

use Appwilio\YaKassa\YaKassa;

class OrdersController
{
    public function showPaymentForm(YaKassa $kassa, $orderId)
    {
        $order = Order::find($orderId);
        
        $paymentForm = $kassa->buildPaymentForm($order);
        
        return view('payment', ['form' => $paymentForm]);
    }    
}

blade
<form method="POST" action="{{ $form->getPaymentUrl() }}">
    @foreach ($form->toArray() as $k => $v) 
        <input type="hidden" name="{{ $k }}" value="{{ $v }}" />
    @endforeach
    
    ...
</form>