├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── lib ├── BaseAPI.php ├── MerchantAPI.php └── exceptions.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.sw* 3 | vendor 4 | .DS_Store 5 | composer.lock 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2016 Cryptonator 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Cryptonator.com Merchant API SDK 2 | 3 | ## Requirements 4 | 5 | PHP 5.3 or above 6 | 7 | 8 | ## Links 9 | 10 | 1. Cryptonator MerchantAPI Help Center: [Ru](https://cryptonator.zendesk.com/hc/ru/categories/200829259), 11 | [En](https://cryptonator.zendesk.com/hc/en-us/categories/200829259) 12 | 13 | ## Getting started 14 | 15 | ### Installation 16 | 17 | 1. Add `"cryptonator/merchant-php-sdk": "dev-master"` to `composer.json` of your application or clone repo to your project. 18 | 2. If you are using composer use `require_once 'vendor/autoload.php';` otherwise paste the following line 19 | ```php 20 | require_once '/path/to/cloned/repo/lib/MerchantAPI.php'; 21 | ``` 22 | 23 | ### Merchant API 24 | 25 | Using Cryptonator MerchantAPI SDK requires the following steps 26 | 27 | 1. Paste the following code. 28 | Note: constants `merchant_id` and `secret` you will find in your account settings once you have [set up a merchant account](https://www.cryptonator.com/auth/signup/) with Cryptonator. 29 | 30 | ```php 31 | use cryptonator\MerchantAPI; 32 | 33 | $cryptonator = new MerchantAPI(merchant_id, secret); 34 | ``` 35 | 36 | 2. Now you can use Cryptonator MerchantAPI. 37 | 38 | ```php 39 | // start payment 40 | $url = $cryptonator->startPayment(array( 41 | 'item_name' => 'Item Name', 42 | //'order_id' => 'Order ID', 43 | //'item_description' => 'Item Description', 44 | 'invoice_amount' => 'Invoice Amount', 45 | 'invoice_currency' => 'Invoice Currency', 46 | //'success_url' => 'Success URL', 47 | //'failed_url' => 'Failed URL', 48 | //'language' => 'Language', 49 | )); 50 | 51 | // create invoice 52 | $invoice = $cryptonator->createInvoice(array( 53 | 'item_name' => 'Item Name', 54 | //'order_id' => 'Order ID', 55 | //'item_description' => 'Item Description', 56 | 'checkout_currency' => 'Checkout Amount', 57 | 'invoice_amount' => 'Invoice Amount', 58 | 'invoice_currency' => 'Invoice Currency', 59 | //'success_url' => 'Success URL', 60 | //'failed_url' => 'Failed URL', 61 | //'language' => 'Language', 62 | )); 63 | 64 | // get invoice 65 | $invoice = $cryptonator->getInvoice('InvoiceID'); 66 | 67 | // list invoices 68 | $invoices = $cryptonator->listInvoices(array( 69 | //'invoice_status' => 'Invoice Status', 70 | //'invoice_currency' => 'Invoice Currency', 71 | //'checkout_currency' => 'Checkout Currency', 72 | )); 73 | 74 | // check annswer 75 | $check_server = $cryptonator->checkAnswer($_POST); 76 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cryptonator/merchant-php-sdk", 3 | "description": "Cryptonator.com Merchant API SDK for PHP", 4 | "keywords": ["cryptonator", "merchant", "php", "sdk", "payment", "cryptonator.com"], 5 | "type": "library", 6 | "license": "MIT", 7 | "require-dev": { 8 | }, 9 | "require": { 10 | "php": ">=5.3.0" 11 | }, 12 | "autoload": { 13 | "classmap": ["lib/"] 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /lib/BaseAPI.php: -------------------------------------------------------------------------------- 1 | status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 34 | $result->body = json_decode($body, true); 35 | 36 | curl_close($curl); 37 | 38 | return $this->processResult($result); 39 | } 40 | 41 | protected function processResult($result) 42 | { 43 | if ($result->status_code != 400) { 44 | return $result->body; 45 | } 46 | else { 47 | throw new Exceptions\ServerError($result->body['error'], $result->status_code); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /lib/MerchantAPI.php: -------------------------------------------------------------------------------- 1 | merchant_id = $merchant_id; 17 | $this->secret = $secret; 18 | } 19 | 20 | /** 21 | * Создание счета с возможностью выбора криптовалюты оплаты. 22 | * Возвращает ссылку на страницу платежа. 23 | * @see https://www.cryptonator.com/hc/ru/articles/207346169-Метод-startpayment 24 | * @param $options array обязательные ключи item_name, invoice_currency, invoice_amount 25 | * @return string 26 | * @throws ServerError 27 | */ 28 | public function startPayment($options) 29 | { 30 | if (isset($options['item_name'], $options['invoice_currency'], $options['invoice_amount'])) { 31 | $options['merchant_id'] = $this->merchant_id; 32 | 33 | return self::API_URL . 'startpayment?' . http_build_query($options); 34 | } 35 | else { 36 | throw new ServerError('Неверные параметры. Ообязательные параметры item_name, invoice_currency, invoice_amount', 0); 37 | } 38 | } 39 | 40 | /** 41 | * Создание счета на оплату, возвращает ID счета. 42 | * Если ответ не проходит проверку secret_hash возвращает null. 43 | * @see https://www.cryptonator.com/hc/ru/articles/208018135-Метод-createinvoice 44 | * @param $options array 45 | * @return mixed 46 | */ 47 | public function createInvoice($options) 48 | { 49 | $result_array = array( 50 | 'merchant_id' => $this->merchant_id, 51 | 'item_name' => '', 52 | 'order_id' => '', 53 | 'item_description' => '', 54 | 'checkout_currency' => '', 55 | 'invoice_amount' => '', 56 | 'invoice_currency' => '', 57 | 'success_url' => '', 58 | 'failed_url' => '', 59 | 'confirmation_policy' => '', 60 | 'language' => '', 61 | ); 62 | 63 | $this->fillResultArray($result_array, $options); 64 | $result_array['secret_hash'] = $this->generateHash($result_array); 65 | 66 | return $this->hashCheck($this->sendReqest('createinvoice', $result_array)); 67 | } 68 | 69 | /** 70 | * Получение информации о счете 71 | * @see https://www.cryptonator.com/hc/ru/articles/208018455-Метод-getinvoice 72 | * @param $id string 73 | * @return mixed 74 | */ 75 | public function getInvoice($id) 76 | { 77 | $result_array = array( 78 | 'merchant_id' => $this->merchant_id, 79 | 'invoice_id' => $id 80 | ); 81 | 82 | 83 | $result_array['secret_hash'] = $this->generateHash($result_array); 84 | 85 | return $this->sendReqest('getinvoice', $result_array); 86 | } 87 | 88 | /** 89 | * Получение списка счетов, удовлетворяющих условиию 90 | * @see https://www.cryptonator.com/hc/ru/articles/208018895-Метод-listinvoices 91 | * @param $options array 92 | * @return mixed 93 | */ 94 | public function listInvoices($options = array()) 95 | { 96 | $result_array = array( 97 | 'merchant_id' => $this->merchant_id, 98 | 'invoice_status' => '', 99 | 'invoice_currency' => '', 100 | 'checkout_currency' => '', 101 | ); 102 | 103 | $this->fillResultArray($result_array, $options); 104 | $result_array['secret_hash'] = $this->generateHash($result_array); 105 | 106 | return $this->sendReqest('listinvoices', $result_array); 107 | } 108 | 109 | /** 110 | * Проверка присланных данных о оплате счета на основании secret_hash. 111 | * true - данные прошли проверку, false - не прошли. 112 | * @see https://www.cryptonator.com/hc/ru/articles/207291239-HTTP-уведомления 113 | * @param $array array параметры POST 114 | * @return bool 115 | */ 116 | public function checkAnswer($array) 117 | { 118 | $hash = $array['secret_hash']; 119 | unset($array['secret_hash']); 120 | 121 | $generate = $this->generateHash($array); 122 | 123 | if ($generate !== null && $hash == $this->generateHash($array)) { 124 | return true; 125 | } 126 | else { 127 | return false; 128 | } 129 | } 130 | 131 | /** 132 | * Заполняет массив элементами массива-донора 133 | * если в массиве-доноре нет соответствующего ключа, то значение в массиве не изменяется 134 | * @param $result array исходный массив 135 | * @param $options array массив-донор 136 | * @return bool 137 | */ 138 | private function fillResultArray(&$result, $options) { 139 | if (is_array($result) && is_array($options)) { 140 | foreach ($result as $key => $r) { 141 | if (isset($options[$key])) { 142 | $result[$key] = $options[$key]; 143 | } 144 | 145 | unset($key, $r); 146 | } 147 | 148 | return true; 149 | } 150 | else { 151 | return false; 152 | } 153 | } 154 | 155 | /** 156 | * Генерация кэш-строки 157 | * @see https://www.cryptonator.com/hc/ru/articles/207300279-Проверка-подлинности-HTTP-уведомлений 158 | * @param $array array массив параметров для отправки на сервер 159 | * @return null|string 160 | */ 161 | private function generateHash($array) 162 | { 163 | if (is_array($array) && count($array) > 0) { 164 | $string = implode('&', $array) . '&' . $this->secret; 165 | 166 | return sha1($string); 167 | } 168 | else { 169 | return null; 170 | } 171 | } 172 | 173 | /** 174 | * Проверка присланных данных севером на основании secret_hash 175 | * @param $body 176 | * @return null 177 | */ 178 | private function hashCheck($body) 179 | { 180 | $hash = $body['secret_hash']; 181 | unset($body['secret_hash']); 182 | 183 | if ($hash == sha1(implode('&', $body) . '&' . $this->secret)) { 184 | return $body; 185 | } 186 | else { 187 | return null; 188 | } 189 | } 190 | } -------------------------------------------------------------------------------- /lib/exceptions.php: -------------------------------------------------------------------------------- 1 |