Last active
March 10, 2023 14:14
-
-
Save sagittaracc/0726e53275f836247241a4a1e3b70f41 to your computer and use it in GitHub Desktop.
Yii2 Assist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace backend\components; | |
use yii\base\Component; | |
use Exception; | |
use Yii; | |
use yii\helpers\Html; | |
/** | |
* Сервис Assist | |
* | |
* @author Yuriy Arutyunyan <[email protected]> | |
*/ | |
class AssistService extends Component | |
{ | |
/** | |
* @var string URL сервиса | |
*/ | |
public $url; | |
/** | |
* @var string идентификатор мерчанта | |
*/ | |
public $merchant_id; | |
/** | |
* @var string логин для доступа к сервисам | |
*/ | |
public $login; | |
/** | |
* @var string пароль для доступа к сервисам | |
*/ | |
public $password; | |
/** | |
* @var string используемая валюта | |
*/ | |
public $currency; | |
/** | |
* Проверяет доступность сервиса оплаты | |
* @return boolean | |
*/ | |
public function isAvailable() | |
{ | |
return !empty($this->merchant_id); | |
} | |
/** | |
* Генерирует форму для перенаправления на страницу оплаты | |
* @param backend\models\Order $order данные плательщика | |
* @param array $options настройки формы | |
* @return string html форма | |
*/ | |
public function generatePaymentForm($order, $options = []) | |
{ | |
return | |
Html::beginForm("$this->url/pay/order.cfm", 'post', $options) . | |
Html::hiddenInput('Merchant_ID', $this->merchant_id) . | |
Html::hiddenInput('OrderNumber', $order->number) . | |
Html::hiddenInput('OrderAmount', $order->amount) . | |
Html::hiddenInput('OrderCurrency', $this->currency) . | |
Html::hiddenInput('FirstName', '') . | |
Html::hiddenInput('LastName', '') . | |
Html::hiddenInput('Email', '') . | |
Html::hiddenInput('OrderComment', 'Оплата квитанции') . | |
Html::endForm(); | |
} | |
/** | |
* Данные по статусу оплаты | |
* @param int $number номер квитанции | |
* @return mixed | |
*/ | |
public function getOrderState($number) | |
{ | |
$url = "$this->url/orderstate/orderstate.cfm"; | |
$headers = ["Content-Type: application/x-www-form-urlencoded"]; | |
$data = "Ordernumber=$number&Merchant_ID={$this->merchant_id}&Login={$this->login}&Password={$this->password}&Format=5"; | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_HEADER, 0); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
$resp = curl_exec($curl); | |
if ($resp === false) { | |
throw new Exception(curl_error($curl), 301); | |
} | |
curl_close($curl); | |
return $resp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment