Advertisement
Ostap34PHP

Untitled

Jan 15th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Payments;
  4.  
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use GuzzleHttp\Client;
  7.  
  8. class EasyPay
  9. {
  10.     /**
  11.      * @var string
  12.      */
  13.     public $PartnerKey;
  14.  
  15.     /**
  16.      * @var string
  17.      */
  18.     public $locale;
  19.  
  20.     /**
  21.      * @var string
  22.      */
  23.     private $requestedSessionId;
  24.  
  25.     /**
  26.      * @var string
  27.      */
  28.     private $pageId;
  29.  
  30.     /**
  31.      * @var string
  32.      */
  33.     private $appId;
  34.  
  35.     /**
  36.      * @var string
  37.      */
  38.     private $appId;
  39.  
  40.     private $client;
  41.  
  42.     public function __construct($payment_info = [])
  43.     {
  44.         foreach ($payment_info as $key => $info) {
  45.             $this->$key = $info;
  46.         }
  47.         $this->client = $client = new Client(['base_uri' => 'https://api.easypay.ua/']);
  48.     }
  49.  
  50.     /**
  51.      * This method should be called when the user first accesses the payment page.
  52.      * If the browser or device changes, the method is called again.
  53.      * @return mixed
  54.      * @throws GuzzleException
  55.      */
  56.     public function createApp()
  57.     {
  58.         $request = $this->client->request('POST', 'api/system/createApp', [
  59.             'headers' => [
  60.                 'PartnerKey' => $this->PartnerKey,
  61.                 'locale' => $this->locale,
  62.             ]
  63.         ]);
  64.  
  65.         $response = json_decode($request->getBody()->getContents());
  66.  
  67.         if (!$response->error) {
  68.             $this->appId = $response->appId;
  69.             $this->requestedSessionId = $response->requestedSessionId;
  70.             $this->pageId = $response->pageId;
  71.         }
  72.  
  73.         return $response;
  74.     }
  75.  
  76.  
  77.     /**
  78.      * Creates a new session instance for the user along with the data storage pages.
  79.      * The lifetime of the session 20 minutes.
  80.      * @return mixed
  81.      * @throws GuzzleException
  82.      */
  83.     public function createSession()
  84.     {
  85.         $request = $this->client->request('POST', 'api/system/createSession', [
  86.             'headers' => [
  87.                 'PartnerKey' => $this->PartnerKey,
  88.                 'locale' => $this->locale,
  89.                 'AppId' => $this->appId
  90.             ]
  91.         ]);
  92.  
  93.         $response = json_decode($request->getBody()->getContents());
  94.  
  95.         if (!$response->error) {
  96.             $this->appId = $response->appId;
  97.             $this->requestedSessionId = $response->requestedSessionId;
  98.             $this->pageId = $response->pageId;
  99.         }
  100.  
  101.         return $response;
  102.     }
  103.  
  104.     /**
  105.      * Creates a new instance of the storage page.
  106.      * When a user opens a new page for payment, a new page ID must be created.
  107.      * @return mixed
  108.      * @throws GuzzleException
  109.      */
  110.     public function createPage()
  111.     {
  112.         $request = $this->client->request('POST', 'api/system/createPage', [
  113.             'headers' => [
  114.                 'PartnerKey' => $this->PartnerKey,
  115.                 'locale' => $this->locale,
  116.                 'AppId' => $this->appId,
  117.                 'requestedSessionId' => $this->requestedSessionId
  118.             ]
  119.         ]);
  120.  
  121.         $response = json_decode($request->getBody()->getContents());
  122.  
  123.         if (!$response->error) {
  124.             $this->appId = $response->appId;
  125.             $this->requestedSessionId = $response->requestedSessionId;
  126.             $this->pageId = $response->pageId;
  127.         }
  128.  
  129.         return $response;
  130.     }
  131.  
  132.     public function createOrder()
  133.     {
  134.         $request = $this->client->request('POST', 'api/system/createPage', [
  135.             'headers' => [
  136.                 'PartnerKey' => $this->PartnerKey,
  137.                 'locale' => $this->locale,
  138.                 'AppId' => $this->appId,
  139.                 'requestedSessionId' => $this->requestedSessionId,
  140.                 'PageId' => $this->pageId,
  141.                 'Sign' => base64_encode(hash('sha256', $this->))
  142.             ]
  143.         ]);
  144.  
  145.         $response = json_decode($request->getBody()->getContents());
  146.  
  147.         return $response;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement