PrivatController.php 3.97 KB
<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 17.01.18
     * Time: 11:45
     */
    
    namespace frontend\controllers;
    
    use GuzzleHttp\Client;
    use yii\helpers\Json;
    use yii\helpers\Url;
    use yii\web\Controller;

    class PrivatController extends Controller
    {
        public function actionIndex(){
            $st = '';
            $privat[ 'products' ][ 0 ][ 'name' ] = 'test product';
            $privat[ 'products' ][ 0 ][ 'count' ] = 1;
            $privat[ 'products' ][ 0 ][ 'price' ] =100;
            $st .= $privat[ 'products' ][ 0 ][ 'name' ] . $privat[ 'products' ][ 0 ][ 'count' ] . str_replace(
                    '.',
                    '',
                    $privat[ 'products' ][ 0 ][ 'price' ] . '.00'
                );
            
            
            $url = 'https://payparts2.privatbank.ua/ipp/v2/payment/create';
            $password = '310a8890988e478eadb5dc1175ab9297';
            $privat[ 'storeId' ] = '9F93C471A19F4DC4B5A1';
            $privat[ 'amount' ] = 100;
            $privat[ 'partsCount' ] = 10;
            $privat[ 'merchantType' ] = 'PP';
            $privat[ 'orderId' ] = date('Ymd-His');
            $privat[ 'responseUrl' ] = Url::to('/ru/privat/callback', true);
            $privat[ 'redirectUrl' ] = Url::to('/site/index', true);
    
            $privat[ 'signature' ] = base64_encode(
                sha1(
                    $password . $privat[ 'storeId' ] . $privat[ 'orderId' ] . str_replace(
                        ".",
                        "",
                        $privat[ 'amount' ] . '.00'
                    ) . $privat[ 'partsCount' ] . $privat[ 'merchantType' ] . $privat[ 'responseUrl' ] . $privat[ 'redirectUrl' ] . $st . $password,
                    true
                )
            );
    
            $headers = [
                'Accept'          => 'application/json',
                'Accept-Encoding' => 'UTF-8',
                'Content-Type'    => 'application/json;charset=utf-8',
            ];
            $client = new Client();
    
            $response = $client->post(
                $url,
                [
                    'headers' => $headers,
                    'body'    => Json::encode($privat),
                ]
            );
    
            if ($response->getStatusCode() == 200) {
                $str = (string) $response->getBody()
                                         ->getContents();
                $data = Json::decode($str);
                if ($data[ 'token' ]) {
                    header(
                        'Location: https://payparts2.privatbank.ua/ipp/v2/payment?token=' . $data[ 'token' ]
                    );  // перенаправление на нужную страницу
                    exit();
                }
            }
        }
        public function beforeAction($action)
        {
            if ($action->id == 'callback') {
                $this->enableCsrfValidation = false;
            }
        
            return parent::beforeAction($action);
        }
    
        public function actionCallback()
        {
        
            if ($_SERVER[ 'REQUEST_METHOD' ] === 'POST') {
                $data = file_get_contents('php://input');
                $file = \Yii::getAlias('@storage/callback.log');
                $myFile = fopen($file, 'a') or die('Unable to open file!');
                fwrite($myFile, "\n" . $data);
                fclose($myFile);
                                $response = Json::decode($data);
                                //$order = OrderFrontend::findOne($response[ 'orderId' ]);
                                if ($response[ 'paymentState' ] === 'SUCCESS') {
                                    print_r($response); die();
            }
//                print_r($data);
//                die();
            } else {
                print_r(Url::to('/ru/order/callback', true));
                print_r(\Yii::$app->session->get('data'));
            }
        
        
        
        }
    }