PHP code example of mitrm / yii2-gourlio

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

    

mitrm / yii2-gourlio example snippets


'components' => [
    'gourlio' => [
        'class' => 'mitrm\gourlio\Cryptobox',
        'period' => 'NOEXPIRY',
        'all_key' => [
            'bitcoin' => ['public_key' => '', 'private_key' => ''],
            'speedcoin' => ['public_key' => '', 'private_key' => ''],
            ...
        ]
    ],
]

$options = array(
    'order_id' => $order_id,
    'user_id' => Yii::$app->user->id,
    'amount' => $sum,
    'coinName' => 'speedcoin', // bitcoin ...
);
$data_pay = Yii::$app->gourlio->load($options)->getPaymentData();

$data_pay['addr']; // Номер кошелька для перевода средств
$data_pay['amount']; // сумма к оплате

$options = array(
    'order_id' => $order_id,
    'user_id' => Yii::$app->user->id,
    'amount' => $sum,
    'coinName' => 'speedcoin', // bitcoin ...
);
if(Yii::$app->gourlio->load($options)->isPaid())  {
    // Оплата пришла
}

class PaymentsController extends Controller
{
    public $enableCsrfValidation = false;

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'result' => [
                'class' => '\mitrm\gourlio\ResultAction',
                'callback' => [$this, 'resultCallbackGourlio'],
            ]
        ];
    }

    /**
     * Обработка оповещения о платеже с gourl.io
     * @param $cryptobox Cryptobox
     * @param $return_data
     * @return string
     */
    public function resultCallbackGourlio($cryptobox, $return_data)
    {
        $model = PaymentRequest::findOne(['id' => $return_data['params']['order'], 'user_id' => $return_data['params']['user']]);
        if (!$model) {
            throw new BadRequestHttpException('Транзакция не найдена');
        }
        $data = [
            'order_id' => $model->id,
            'user_id' => Yii::$app->user->id,
            'amount' => $model->sum,
            'coinName' => $model->currency, // speedcoin, bitcoin, ...
        ];
        $cryptobox->load($data);
        if($cryptobox->isPaid()) {
            $model->sum = $return_data['params']['amount'];
            $model->status_id = PaymentRequest::STATUS_SUCCESS;
            $model->save();
        }
        return $return_data['text_return'];
    }


}