Yandex.Kassa API extension for Yii2
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., (*1)
This extension strongly in beta, so feel free to send pull requests and fix bugs, (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist mikefinch/yii2-yandex-kassa-api "*"
or add, (*5)
"mikefinch/yii2-yandex-kassa-api": "*"
to the require section of your composer.json
file., (*6)
Usage
Once the extension is installed, you have to follow next few steps:, (*7)
1. Add extension into your configuration file
'components' => [
...
'kassa' => [
'class' => 'mikefinch\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), (*8)
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'=>'mikefinch\YandexKassaAPI\actions\CreatePaymentAction',
'orderClass' => Orders::className(),
'beforePayment' => function($order) {
return $order->status == Orders::STATUS_NEW;
}
],
'notify' => [
'class'=>'mikefinch\YandexKassaAPI\actions\ConfirmPaymentAction',
'orderClass' => Orders::className(),
'beforeConfirm' => function($payment, $order) {
$order->status = Orders::STATUS_PAID;
return $order->save();
}
]
];
}
Here we have two callbacks., (*9)
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., (*10)
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., (*11)
4. Config your Yandex.Kassa notification page
, (*12)
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., (*13)
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., (*14)