kllakk / yii2-yandex-kassa-api
This extensions allows you obtain money from users by new Yandex.Kassa's API
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 2
Type:yii2-extension
Requires
This package is auto-updated.
Last update: 2024-10-25 19:41:55 UTC
README
This extension allows you to obtain money from users by new Yandex.Kassa's API.
It was designed to be pretty simple to use so you don't have to deep down into Yandex.Kassa's workflow.
This extension strongly in beta, so feel free to send pull requests and fix bugs
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist kllakk/yii2-yandex-kassa-api "*"
or add
"kllakk/yii2-yandex-kassa-api": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, you have to follow next few steps:
1. Add extension into your configuration file
'components' => [ ... 'kassa' => [ 'class' => 'kllakk\YandexKassaAPI\YandexKassaAPI', 'returnUrl' => '', 'shopId' => '', 'key' => '', ],
returnUrl - User will get there after payment has got succeeded
shopId - Your shop id (from Yandex)
key - Your secret key (from Yandex)
currency - Currency obviously (RUB by default)
2. Implement OrderInterface in your Order model
class Orders extends Model implements OrderInterface { ... public function setInvoiceId($invoiceId) { $this->invoice_id = $invoiceId; } public function getInvoiceId() { return $this->invoice_id; } public function getPaymentAmount() { return $this->amount; } public function findByInvoiceId($invoiceId) { return self::find()->where(['invoice_id' => $invoiceId]); } public function findById($id) { return self::findOne($id); } }
3. Add new actions to your controller
public function actions() { return [ 'create-payment' => [ 'class'=>'kllakk\YandexKassaAPI\actions\CreatePaymentAction', 'orderClass' => Orders::className(), 'beforePayment' => function($order) { return $order->status == Orders::STATUS_NEW; } ], 'notify' => [ 'class'=>'kllakk\YandexKassaAPI\actions\ConfirmPaymentAction', 'orderClass' => Orders::className(), 'beforeConfirm' => function($payment, $order) { $order->status = Orders::STATUS_PAID; return $order->save(); } ] ]; }
Here we have two callbacks.
beforePayment checks is our order okay. You can provide some logic there and return false if something went wrong and you wanna cancel your payment.
The second one is beforeConfirm. It executes when user successfully paid your payment and now you have to confirm this order, send sms notification, etc. Be careful, if you return false is this callback, payment won't be confirmed and user will receive his money back in a few hours.
4. Config your Yandex.Kassa notification page
Set your notification page Url as same as you used in your controller.
For example, if you added your actions to SiteController, it would be https://yoursite.com/site/notify
Don't forget about SSL - Yandex sends notifications only through it.
5. Redirect user to payment action afterwards
if ($order->payment_type == $order::PAYTYPE_ONLINE) { return $this->redirect(['order/create-payment', 'id' =>$order->id]); } else { return $this->redirect(['order/success']); }
You have to pass $id to the action so it could find your model by findById method.