Advertisement
aivavic

LiqPay

May 11th, 2017
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.96 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: viktor
  5.  * Date: 30.04.2017
  6.  * Time: 23:56
  7.  */
  8.  
  9. namespace common\components\liqpay;
  10.  
  11.  
  12. use Doctrine\Instantiator\Exception\InvalidArgumentException;
  13. use yii\base\Component;
  14. /**
  15.  * Liqpay Payment Module
  16.  *
  17.  * NOTICE OF LICENSE
  18.  *
  19.  * This source file is subject to the Open Software License (OSL 3.0)
  20.  * that is available through the world-wide-web at this URL:
  21.  * http://opensource.org/licenses/osl-3.0.php
  22.  *
  23.  * @category        LiqPay
  24.  * @package         liqpay/liqpay
  25.  * @version         3.0
  26.  * @author          Liqpay
  27.  * @copyright       Copyright (c) 2014 Liqpay
  28.  * @license         http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  29.  *
  30.  * EXTENSION INFORMATION
  31.  *
  32.  * LIQPAY API       https://www.liqpay.com/ru/doc
  33.  *
  34.  */
  35. /**
  36.  * Payment method liqpay process
  37.  *
  38.  * @author      Liqpay <support@liqpay.com>
  39.  */
  40. class Liqpay extends Component
  41.  
  42.  
  43. {
  44.     const CURRENCY_EUR = 'EUR';
  45.     const CURRENCY_USD = 'USD';
  46.     const CURRENCY_UAH = 'UAH';
  47.     const CURRENCY_RUB = 'RUB';
  48.     const CURRENCY_RUR = 'RUR';
  49.     private $_api_url = 'https://www.liqpay.com/api/';
  50.     private $_checkout_url = 'https://www.liqpay.com/api/3/checkout';
  51.     protected $_supportedCurrencies = array(
  52.         self::CURRENCY_EUR,
  53.         self::CURRENCY_USD,
  54.         self::CURRENCY_UAH,
  55.         self::CURRENCY_RUB,
  56.         self::CURRENCY_RUR,
  57.     );
  58.     private $_public_key;
  59.     private $_private_key;
  60.     private $_server_response_code = null;
  61.     /**
  62.      * Constructor.
  63.      *
  64.      * @param string $public_key
  65.      * @param string $private_key
  66.      *
  67.      * @throws InvalidArgumentException
  68.      */
  69.     public function __construct()
  70.     {
  71.         parent::__construct();
  72.  
  73.         $this->_public_key = env('LIQPAY_PUBLIC_KEY');
  74.         $this->_private_key = env('LIQPAY_PRIVATE_KEY');
  75.     }
  76.     /**
  77.      * Call API
  78.      *
  79.      * @param string $path
  80.      * @param array $params
  81.      *
  82.      * @return string
  83.      */
  84.     public function api($path, $params = array())
  85.     {
  86.         if (!isset($params['version'])) {
  87.             throw new InvalidArgumentException('version is null');
  88.         }
  89.         $url         = $this->_api_url . $path;
  90.         $public_key  = $this->_public_key;
  91.         $private_key = $this->_private_key;
  92.         $data        = base64_encode(json_encode(array_merge(compact('public_key'), $params)));
  93.         $signature   = base64_encode(sha1($private_key.$data.$private_key, 1));
  94.         $postfields  = http_build_query(array(
  95.             'data'  => $data,
  96.             'signature' => $signature
  97.         ));
  98.         $ch = curl_init();
  99.         curl_setopt($ch, CURLOPT_URL, $url);
  100.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  101.         curl_setopt($ch, CURLOPT_POST, 1);
  102.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  103.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  104.         $server_output = curl_exec($ch);
  105.         $this->_server_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  106.         curl_close($ch);
  107.         return json_decode($server_output);
  108.     }
  109.     /**
  110.      * Return last api response http code
  111.      * @return string|null
  112.      */
  113.     public function get_response_code()
  114.     {
  115.         return $this->_server_response_code;
  116.     }
  117.     /**
  118.      * cnb_form
  119.      *
  120.      * @param array $params
  121.      *
  122.      * @return string
  123.      *
  124.      * @throws InvalidArgumentException
  125.      */
  126.     public function cnb_form($params)
  127.     {
  128.         $language = 'ru';
  129.         if (isset($params['language']) && $params['language'] == 'en') {
  130.             $language = 'en';
  131.         }
  132.         $params    = $this->cnb_params($params);
  133.         $data      = base64_encode(json_encode($params));
  134.         $signature = $this->cnb_signature($params);
  135.  
  136.         return sprintf('
  137.            <form method="POST" action="%s" accept-charset="utf-8">
  138.                %s
  139.                %s
  140.                <input type="image" class="pay-button" src="//static.liqpay.com/buttons/p1%s.radius.png" name="btn_text" />
  141.            </form>
  142.            ',
  143.             $this->_checkout_url,
  144.             sprintf('<input type="hidden" name="%s" value="%s" />', 'data', $data),
  145.             sprintf('<input type="hidden" name="%s" value="%s" />', 'signature', $signature),
  146.             $language
  147.         );
  148.     }
  149.     /**
  150.      * cnb_signature
  151.      *
  152.      * @param array $params
  153.      *
  154.      * @return string
  155.      */
  156.     public function cnb_signature($params)
  157.     {
  158.         $params      = $this->cnb_params($params);
  159.         $private_key = $this->_private_key;
  160.         $json      = base64_encode(json_encode($params));
  161.         $signature = $this->str_to_sign($private_key . $json . $private_key);
  162.         return $signature;
  163.     }
  164.     /**
  165.      * cnb_params
  166.      *
  167.      * @param array $params
  168.      *
  169.      * @return array $params
  170.      */
  171.     private function cnb_params($params)
  172.     {
  173.         $params['public_key'] = $this->_public_key;
  174.         if (!isset($params['version'])) {
  175.             throw new InvalidArgumentException('version is null');
  176.         }
  177.         if (!isset($params['amount'])) {
  178.             throw new InvalidArgumentException('amount is null');
  179.         }
  180.         if (!isset($params['currency'])) {
  181.             throw new InvalidArgumentException('currency is null');
  182.         }
  183.         if (!in_array($params['currency'], $this->_supportedCurrencies)) {
  184.             throw new InvalidArgumentException('currency is not supported');
  185.         }
  186.         if ($params['currency'] == self::CURRENCY_RUR) {
  187.             $params['currency'] = self::CURRENCY_RUB;
  188.         }
  189.         if (!isset($params['description'])) {
  190.             throw new InvalidArgumentException('description is null');
  191.         }
  192.         return $params;
  193.     }
  194.     /**
  195.      * str_to_sign
  196.      *
  197.      * @param string $str
  198.      *
  199.      * @return string
  200.      */
  201.     public function str_to_sign($str)
  202.     {
  203.         $signature = base64_encode(sha1($str, 1));
  204.         return $signature;
  205.     }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement