<?php
_class('payment_api__provider_remote');
class yf_payment_api__provider_interkassa extends yf_payment_api__provider_remote
{
public $URL = 'https://sci.interkassa.com/';
public $KEY_PUBLIC = null; // Checkout ID
public $KEY_PRIVATE = null; // secret key
public $KEY_PRIVATE_TEST = null; // secret key for test
public $HASH_METHOD = 'sha256'; // signature hash method: md5, sha256
public $PURSE_ID = null; // purse_id by currency_id
/* example for project_conf.php:
public $PURSE_ID = array( // purse_id by currency_id
'UAH' => '...',
);
*/
public $URL_API = 'https://api.interkassa.com/v1/%method';
public $API_ACCOUNT = null; // api header: Ik-Api-Account-Id
public $method_allow = [
'order' => [
'payin' => [
'interkassa',
],
'payout' => [
// 'visa_p2p_privat_uah',
'visa_p2p_tasprivat_uah',
],
],
'api' => [
// Currencies and rates used by the system
'currency' => [
'uri' => [
'%method' => 'currency',
],
// 'option' => array(
// 'active' => true,
// ),
],
// Pay-in payways enabled in Interkassa
'paysystem-input-payway' => [
'uri' => [
'%method' => 'paysystem-input-payway',
],
],
// Payout payways enabled in Interkassa
'paysystem-output-payway' => [
'uri' => [
'%method' => 'paysystem-output-payway',
],
],
// Accounts available to the user
'account' => [
'is_authorization' => true,
'uri' => [
'%method' => 'account',
],
],
// Checkouts linked to the account
'checkout' => [
'is_authorization' => true,
'is_api_account' => true,
'uri' => [
'%method' => 'checkout',
],
],
// Business checkouts linked to the account
'checkout-b' => [
'is_authorization' => true,
'is_handler' => 'checkout_b',
],
// Purses linked to the account, with their parameters
'purse' => [
'is_authorization' => true,
'is_api_account' => true,
'uri' => [
'%method' => 'purse',
],
],
// Payment export
'co-invoice' => [
'is_authorization' => true,
'is_api_account' => true,
'uri' => [
'%method' => 'co-invoice',
],
],
'co-invoice-id' => [
'is_authorization' => true,
'is_api_account' => true,
'url' => 'https://api.interkassa.com/v1/%method/%id',
'uri' => [
'%method' => 'co-invoice',
'%id' => '$id',
],
],
// GET
// - list of completed payouts
// - details of one payout
// POST
// - create a new payout
'withdraw' => [
'is_authorization' => true,
'is_api_account' => true,
'uri' => [
'%method' => 'withdraw',
],
],
'withdraw-id' => [
'is_authorization' => true,
'is_api_account' => true,
'url' => 'https://api.interkassa.com/v1/%method/%id',
'uri' => [
'%method' => 'withdraw',
'%id' => '$id',
],
],
'withdraw-calc' => [
'is_authorization' => true,
'is_api_account' => true,
'uri' => [
'%method' => 'withdraw',
],
'option' => [
'action' => 'calc',
],
],
'withdraw-process' => [
'is_authorization' => true,
'is_api_account' => true,
'uri' => [
'%method' => 'withdraw',
],
'option' => [
'action' => 'process',
// 'action' => 'calc', // DEBUG
],
],
],
'payin' => [
'interkassa' => [
'title' => 'Visa, MasterCard',
'icon' => 'interkassa',
'amount_min' => 100,
'fee' => 0, // 0.1%
'currency' => [
// 'USD' => array(
// 'currency_id' => 'USD',
// 'active' => true,
// ),
// 'EUR' => array(
// 'currency_id' => 'EUR',
// 'active' => true,
// ),
'UAH' => [
'currency_id' => 'UAH',
'active' => true,
],
],
],
],
'payout' => [
// changed: 2016-07-01 16:07:07
// 'visa_p2p_privat_uah' => [
'visa_p2p_tasprivat_uah' => [
'title' => 'Visa (Privat24, UAH)',
'icon' => 'visa',
'request_option' => [
// 'paywayId' => '52e7f883e4ae1a2406000000', // visa_p2p_privat_uah
'paywayId' => '5763bad33d1eaf04218b456a', // visa_p2p_tasprivat_uah
'calcKey' => 'psPayeeAmount',
],
'amount' => [
'min' => 5,
'max' => 200,
],
// 'is_fee' => true,
'fee' => [
'out' => [
'rt' => 1,
'fix' => 10,
],
],
'is_currency' => true,
'currency' => [
'UAH' => [
'currency_id' => 'UAH',
'active' => true,
],
],
'request_field' => [
'amount',
'paymentNo',
'purseId',
'paywayId',
],
'field' => [
'card',
],
'order' => [
'card',
],
'option' => [
'card' => 'Card number',
],
'option_validation_js' => [
'card' => [
'type' => 'text',
'required' => true,
'minlength' => 13,
'maxlength' => 16,
// 'pattern' => '^\d+$',
'pattern' => '^4((40588)|(14949)|(32339)|(32334)|(32338)|(32340)|(40535)|(73117)|(73121)|(13051)|(40509)|(24600)|(62708)|(76065)|(17649)|(32337)|(62705)|(14943)|(14961)|(14962)|(32575)|(58121)|(58122)|(14939)|(14960)|(24657)|(34156)|(32335)|(23396)|(73118)|(32336)|(40129)|(76339)|(14963)|(73114)|(04030)|(58120)|(10653))[0-9]{7}(?:[0-9]{3})?$',
],
],
'option_validation' => [
'card' => 'required|length[13,16]|regex:~^4((40588)\|(14949)\|(32339)\|(32334)\|(32338)\|(32340)\|(40535)\|(73117)\|(73121)\|(13051)\|(40509)\|(24600)\|(62708)\|(76065)\|(17649)\|(32337)\|(62705)\|(14943)\|(14961)\|(14962)\|(32575)\|(58121)\|(58122)\|(14939)\|(14960)\|(24657)\|(34156)\|(32335)\|(23396)\|(73118)\|(32336)\|(40129)\|(76339)\|(14963)\|(73114)\|(04030)\|(58120)\|(10653))[0-9]{7}(?:[0-9]{3})?$~',
],
'option_validation_message' => [
'card' => 'required, 13 to 16 digits',
],
],
// changed: 2016-07-01 16:07:07
// 'visa_p2p_notprivat_uah' => [
'visa_p2p_tasnotprivat_uah' => [
'title' => 'Visa (UAH)',
'icon' => 'visa',
'request_option' => [
// 'paywayId' => '52ef9b77e4ae1a3008000000', // visa_p2p_notprivat_uah
'paywayId' => '576658923d1eaf77398b4568', // visa_p2p_notprivat_uah
'calcKey' => 'psPayeeAmount',
],
'amount' => [
'min' => 5,
'max' => 200,
],
// 'is_fee' => true,
'fee' => [
'out' => [
'rt' => 1,
'fix' => 20,
],
],
'is_currency' => true,
'currency' => [
'UAH' => [
'currency_id' => 'UAH',
'active' => true,
],
],
'request_field' => [
'amount',
'paymentNo',
'purseId',
'paywayId',
],
'field' => [
'card',
],
'order' => [
'card',
],
'option' => [
'card' => 'Card number',
],
'option_validation_js' => [
'card' => [
'type' => 'text',
'required' => true,
'minlength' => 13,
'maxlength' => 16,
// 'pattern' => '^\d+$',
'pattern' => '^4(?:(?!(40588)|(14949)|(32339)|(32334)|(32338)|(32340)|(40535)|(73117)|(73121)|(13051)|(40509)|(24600)|(62708)|(76065)|(17649)|(32337)|(62705)|(14943)|(14961)|(14962)|(32575)|(58121)|(58122)|(14939)|(14960)|(24657)|(34156)|(32335)|(23396)|(73118)|(32336)|(40129)|(76339)|(14963)|(73114)|(04030)|(58120)|(10653)))[0-9]{12}(?:[0-9]{3})?$',
],
],
'option_validation' => [
'card' => 'required|length[13,16]|regex:~^4(?:(?!(40588)\|(14949)\|(32339)\|(32334)\|(32338)\|(32340)\|(40535)\|(73117)\|(73121)\|(13051)\|(40509)\|(24600)\|(62708)\|(76065)\|(17649)\|(32337)\|(62705)\|(14943)\|(14961)\|(14962)\|(32575)\|(58121)\|(58122)\|(14939)\|(14960)\|(24657)\|(34156)\|(32335)\|(23396)\|(73118)\|(32336)\|(40129)\|(76339)\|(14963)\|(73114)\|(04030)\|(58120)\|(10653)))[0-9]{12}(?:[0-9]{3})?$~',
],
'option_validation_message' => [
'card' => 'required, 13 to 16 digits',
],
],
// changed: 2016-07-01 16:07:07
// 'mastercard_p2p_privat_uah' => [
'mastercard_p2p_tasprivat_uah' => [
'title' => 'MasterCard (Privat24, UAH)',
'icon' => 'mastercard',
'request_option' => [
// 'paywayId' => '52efa902e4ae1a780e000001', // mastercard_p2p_privat_uah
'paywayId' => '5763bb6e3d1eaf4b218b4567', // mastercard_p2p_tasprivat_uah
'calcKey' => 'psPayeeAmount',
],
'amount' => [
'min' => 5,
'max' => 200,
],
// 'is_fee' => true,
'fee' => [
'out' => [
'rt' => 1,
'fix' => 10,
],
],
'is_currency' => true,
'currency' => [
'UAH' => [
'currency_id' => 'UAH',
'active' => true,
],
],
'request_field' => [
'amount',
'paymentNo',
'purseId',
'paywayId',
],
'field' => [
'card',
],
'order' => [
'card',
],
'option' => [
'card' => 'Card number',
],
'option_validation_js' => [
'card' => [
'type' => 'text',
'required' => true,
'minlength' => 13,
'maxlength' => 16,
// 'pattern' => '^\d+$',
'pattern' => '^((535145)|(536354)|(532957)|(521153)|(530217)|(545708)|(516915)|(558335)|(532032)|(544013)|(521152)|(516874)|(557721)|(545709)|(521857)|(516933)|(670509)|(676246)|(516875)|(516798)|(552324)|(558424)|(516936)|(513399)|(517691))[0-9]{7}(?:[0-9]{3})?$',
],
],
'option_validation' => [
'card' => 'required|length[13,16]|regex:~^((535145)\|(536354)\|(532957)\|(521153)\|(530217)\|(545708)\|(516915)\|(558335)\|(532032)\|(544013)\|(521152)\|(516874)\|(557721)\|(545709)\|(521857)\|(516933)\|(670509)\|(676246)\|(516875)\|(516798)\|(552324)\|(558424)\|(516936)\|(513399)\|(517691))[0-9]{7}(?:[0-9]{3})?$~',
],
'option_validation_message' => [
'card' => 'required, 13 to 16 digits',
],
],
// changed: 2016-07-01 16:07:07
// 'mastercard_p2p_notprivat_uah' => [
'mastercard_p2p_tasnotprivat_uah' => [
'title' => 'MasterCard (UAH)',
'icon' => 'mastercard',
'request_option' => [
// 'paywayId' => '52efa871e4ae1a3008000002', // mastercard_p2p_notprivat_uah
'paywayId' => '576658ac3d1eaf82398b4567', // mastercard_p2p_tasnotprivat_uah
'calcKey' => 'psPayeeAmount',
],
'amount' => [
'min' => 5,
'max' => 200,
],
// 'is_fee' => true,
'fee' => [
'out' => [
'rt' => 1,
'fix' => 20,
],
],
'is_currency' => true,
'currency' => [
'UAH' => [
'currency_id' => 'UAH',
'active' => true,
],
],
'request_field' => [
'amount',
'paymentNo',
'purseId',
'paywayId',
],
'field' => [
'card',
],
'order' => [
'card',
],
'option' => [
'card' => 'Card number',
],
'option_validation_js' => [
'card' => [
'type' => 'text',
'required' => true,
'minlength' => 13,
'maxlength' => 16,
// 'pattern' => '^\d+$',
'pattern' => '^(?:(?!(535145)|(536354)|(532957)|(521153)|(530217)|(545708)|(516915)|(558335)|(532032)|(544013)|(521152)|(516874)|(557721)|(545709)|(521857)|(516933)|(670509)|(676246)|(516875)|(516798)|(552324)|(558424)|(516936)|(513399)|(517691)))[5-6]{1}[0-9]{12}(?:[0-9]{3})?$',
],
],
'option_validation' => [
'card' => 'required|length[13,16]|regex:~^(?:(?!(535145)\|(536354)\|(532957)\|(521153)\|(530217)\|(545708)\|(516915)\|(558335)\|(532032)\|(544013)\|(521152)\|(516874)\|(557721)\|(545709)\|(521857)\|(516933)\|(670509)\|(676246)\|(516875)\|(516798)\|(552324)\|(558424)\|(516936)\|(513399)\|(517691)))[5-6]{1}[0-9]{12}(?:[0-9]{3})?$~',
],
'option_validation_message' => [
'card' => 'required, 13 to 16 digits',
],
],
],
];
public $_api_transform = [
'operation_id' => 'paymentNo',
'transaction_id' => 'trnId',
'account' => 'card',
];
public $_api_transform_reverse = [
'code' => 'state',
];
public $_options_transform = [
'amount' => 'ik_am',
'currency' => 'ik_cur',
'title' => 'ik_desc',
'description' => 'ik_x_desc',
'operation_id' => 'ik_pm_no',
'public_key' => 'ik_co_id',
'key_public' => 'ik_co_id',
'test' => 'test_mode',
];
public $_options_transform_reverse = [
'ik_am' => 'amount',
'ik_cur' => 'currency',
'ik_desc' => 'title',
'ik_x_desc' => 'description',
'ik_pm_no' => 'operation_id',
'ik_co_id' => 'key_public',
];
public $_status = [
'success' => 'success',
'new' => 'processing',
'waitAccept' => 'processing',
'process' => 'processing',
'fail' => 'refused',
'canceled' => 'refused',
];
public $_payin_status = [
// status description final
2 => 'processing', // Awaiting payment - No
3 => 'processing', // Processing - No
4 => 'processing', // Refund in progress - No
5 => 'expired', // Expired - Yes
7 => 'success', // Credited - Yes
8 => 'refused', // Cancelled by the payway - Yes
9 => 'refused', // Refunded - Yes
];
public $_payin_status_message = [
2 => 'Awaiting payment',
3 => 'Processing',
4 => 'Refund in progress',
5 => 'Expired',
7 => 'Credited',
8 => 'Cancelled by the payment system',
9 => 'Refunded',
];
public $_payout_status = [
// status description final
1 => 'processing', // Awaiting moderation - No
2 => 'processing', // Moderated - No
3 => 'refused', // Withdrawn by moderation - Yes
4 => 'processing', // Frozen - No
5 => 'processing', // Unfrozen - No
6 => 'processing', // Processing by the payway - No
7 => 'processing', // Crediting - No
8 => 'success', // Completed - Yes
9 => 'refused', // Cancelled - Yes
11 => 'refused', // Refunded - Yes
12 => 'processing', // Created, not completed yet - No
];
public $_payout_status_message = [
1 => 'Awaiting moderation',
2 => 'Moderated',
3 => 'Withdrawn by moderation',
4 => 'Frozen',
5 => 'Unfrozen',
6 => 'Processing by the payment system',
7 => 'Crediting',
8 => 'Completed',
9 => 'Cancelled',
11 => 'Refunded',
12 => 'Created, not completed yet',
];
public $currency_default = 'USD';
public $currency_allow = [
'USD' => [
'currency_id' => 'USD',
'active' => true,
],
'EUR' => [
'currency_id' => 'EUR',
'active' => true,
],
'UAH' => [
'currency_id' => 'UAH',
'active' => true,
],
];
// public $fee = 5; // 5%
public $service_allow = [
'Visa',
'Mastercard',
// 'LiqPay',
// 'Privat24',
// 'PerfectMoney',
// 'Anelik',
// 'Ukrainian bank',
// 'Ukrainian terminals',
// 'Test payment system',
// 'SWIFT bank transfer',
// 'OKPay',
// 'Payeer',
];
public $url_result = null;
public $url_server = null;
public function _init()
{
if ( ! $this->ENABLE) {
return null;
}
$this->payment_api = _class('payment_api');
// load api
require_once __DIR__ . '/payment_provider/interkassa/Interkassa.php';
$this->api = new Interkassa($this->KEY_PUBLIC, $this->KEY_PRIVATE, $this->KEY_PRIVATE_TEST, $this->HASH_METHOD, $this->TEST_MODE);
$this->url_result = url_user('/api/payment/provider?name=interkassa&operation=response');
$this->url_server = url_user('/api/payment/provider?name=interkassa&operation=response&server=true');
// parent
parent::_init();
}
public function key($name = 'public', $value = null)
{
if ( ! $this->ENABLE) {
return null;
}
$value = $this->api->key($name, $value);
return $value;
}
public function key_reset()
{
if ( ! $this->ENABLE) {
return null;
}
$this->key('public', $this->KEY_PUBLIC);
$this->key('private', $this->KEY_PRIVATE);
$this->key('private_test', $this->KEY_PRIVATE_TEST);
}
public function hash_method($value = null)
{
if ( ! $this->ENABLE) {
return null;
}
$value = $this->api->hash_method($value);
return $value;
}
public function hash_method_reset()
{
if ( ! $this->ENABLE) {
return null;
}
$this->api->hash_method($this->HASH_METHOD);
}
public function signature($options, $request = true)
{
if ( ! $this->ENABLE) {
return null;
}
$result = $this->api->signature($options, $request);
return $result;
}
public function _form_options($options)
{
if ( ! $this->ENABLE) {
return null;
}
$_ = $options;
// transform
foreach ((array) $this->_options_transform as $from => $to) {
if (isset($_[$from])) {
$_[$to] = $_[$from];
unset($_[$from]);
}
}
// url
if ( ! empty($_['url_result'])
|| empty($_['ik_suc_uc'])
|| empty($_['ik_pnd_u'])
|| empty($_['ik_fal_u'])
) {
$url = $this->_url($options);
$_['ik_suc_u'] = $url . '&status=success';
$_['ik_pnd_u'] = $url . '&status=pending';
$_['ik_fal_u'] = $url . '&status=fail';
$_['ik_suc_m'] = 'post';
$_['ik_pnd_m'] = 'post';
$_['ik_fal_m'] = 'post';
unset($_['url_result']);
}
if ( ! empty($_['url_server']) || empty($_['ik_ia_u'])) {
$url_server = $this->_url($options, $is_server = true);
$_['ik_ia_u'] = $url_server . '&status=interaction';
$_['ik_ia_m'] = 'post';
unset($_['url_server']);
}
// default
$_['ik_am'] = number_format($_['ik_am'], 2, '.', '');
empty($_['ik_co_id']) && $_['ik_co_id'] = $this->KEY_PUBLIC;
if (empty($_['ik_am']) || empty($_['ik_co_id'])) {
$_ = null;
}
if ( ! empty($this->TEST_MODE) || ! empty($_['test_mode'])) {
unset($_['test_mode']);
$_['ik_act'] = 'payway';
$_['ik_pw_via'] = 'test_interkassa_test_xts';
}
return $_;
}
public function _url($options, $is_server = false)
{
$_url_server = $_url_result = $_operation_id = null;
if ( ! $this->ENABLE) {
return null;
}
is_array($options) && extract($options, EXTR_PREFIX_ALL | EXTR_REFS, '');
if ($is_server) {
$url = $_url_server ?: $this->url_server;
} else {
$url = $_url_result ?: $this->url_result;
}
$result = $url . '&operation_id=' . $_operation_id;
return $result;
}
public function _form($data, $options = null)
{
if ( ! $this->ENABLE) {
return null;
}
if (empty($data)) {
return null;
}
$_ = &$options;
// START DUMP
$payment_api = $this->payment_api;
$payment_api->dump(['name' => 'Interkassa', 'operation_id' => @(int) $_['data']['operation_id']]);
$is_array = (bool) $_['is_array'];
$form_options = $this->_form_options($data);
// DUMP
$payment_api->dump(['var' => $form_options]);
$signature = $this->api->signature($form_options);
if (empty($signature)) {
return null;
}
$form_options['ik_sign'] = $signature;
$url = &$this->URL;
$result = [];
if ($is_array) {
$result['url'] = $url;
} else {
$result[] = '<form id="_js_provider_interkassa_form" method="post" accept-charset="utf-8" action="' . $url . '" class="display: none;">';
}
foreach ((array) $form_options as $key => $value) {
if ($is_array) {
$result['data'][$key] = $value;
} else {
$result[] = sprintf('<input type="hidden" name="%s" value="%s" />', $key, $value);
}
}
if ( ! $is_array) {
$result[] = '</form>';
$result = implode(PHP_EOL, $result);
}
return $result;
}
public function _api_response()
{
if ( ! $this->ENABLE) {
return null;
}
$payment_api = $this->payment_api;
$test_mode = &$this->TEST_MODE;
$is_server = ! empty($_GET['server']);
$result = null;
// check operation
$operation_id = (int) ($_GET['operation_id'] ?? null);
// START DUMP
$payment_api->dump(['name' => 'Interkassa', 'operation_id' => (int) $operation_id]);
/*
&status=success
string '$_GET' (length=5)
array (size=4)
'test_mode' => string '1' (length=1)
'status' => string 'success' (length=7)
'object' => string 'payment_test' (length=12)
'action' => string 'provider' (length=8)
string '$_POST' (length=6)
array (size=12)
'ik_co_id' => string '54be5909bf4efc7f6b8ab8f5' (length=24)
'ik_inv_id' => string '33226688' (length=8)
'ik_inv_st' => string 'success' (length=7)
'ik_inv_crt' => string '2015-01-21 13:14:26' (length=19)
'ik_inv_prc' => string '2015-01-21 13:14:26' (length=19)
'ik_pm_no' => string 'ID_4233' (length=7)
'ik_pw_via' => string 'test_interkassa_test_xts' (length=24)
'ik_am' => string '100.00' (length=6)
// Checkout Refund - amount credited to the checkout account.
'ik_co_rfn' => string '97.0000' (length=7)
// Paysystem Price - payment amount in the payway.
'ik_ps_price' => string '100.00' (length=6)
'ik_cur' => string 'USD' (length=3)
'ik_desc' => string 'Account top-up (Interkassa)' (length=27)
'ik_x_user_id' => string '3' (length=1)
&status=fail
string '$_GET' (length=5)
array (size=4)
'status' => string 'fail' (length=4)
string '$_POST' (length=6)
array (size=12)
'ik_inv_st' => string 'canceled' (length=8)
'ik_inv_prc' => string '' (length=0)
&status=pending
string '$_GET' (length=5)
array (size=4)
'status' => string 'pending' (length=7)
string '$_POST' (length=6)
array (size=12)
'ik_inv_st' => string 'waitAccept' (length=10)
'ik_inv_prc' => string '' (length=0)
*/
$response = $_POST;
// response POST:
$signature = $response['ik_sign'];
// check signature
if ( ! $test_mode && empty($signature)) {
$result = [
'status' => false,
'status_message' => 'Empty signature',
];
// DUMP
$payment_api->dump(['var' => $result]);
return $result;
}
$_signature = $this->signature($response, false);
if ( ! ($test_mode && empty($signature)) && $signature != $_signature) {
$result = [
'status' => false,
'status_message' => 'Invalid signature',
];
// DUMP
$payment_api->dump(['var' => $result]);
return $result;
}
// update operation
$_response = $this->_response_parse($response);
// check public key (ik_co_id)
$key_public = $_response['key_public'];
$_key_public = $this->key('public');
if ($key_public != $_key_public) {
$result = [
'status' => false,
'status_message' => 'Invalid key (ik_co_id)',
];
return $result;
}
// check status
$state = $_response['ik_inv_st'];
list($status_name, $status_message) = $this->_state($state);
// update account, operation data
$result = $this->_api_deposition([
'provider_name' => 'interkassa',
'response' => $_response,
'status_name' => $status_name,
'status_message' => $status_message,
]);
return $result;
}
public function _response_parse($response)
{
if ( ! $this->ENABLE) {
return null;
}
$_ = $response;
// transform
foreach ((array) $this->_options_transform_reverse as $from => $to) {
if (isset($_[$from])) {
$_[$to] = $_[$from];
unset($_[$from]);
}
}
return $_;
}
public function api_request__checkout_b($options = null)
{
// get business account_id
list($status, $account) = $this->api_request('account');
if (empty($status)
|| ! empty($account['code'])
|| empty($account['data'])
) {
$result = [
'status' => false,
'status_message' => 'Error requesting the business account',
];
return $result;
}
// find business account_id
$account_id = null;
foreach ($account['data'] as $id => $item) {
if (($item['tp'] ?? null) == 'b') {
$account_id = $item['_id'];
break;
}
}
// get business account
$request_option = [
'method_id' => 'checkout',
'header' => [
'Ik-Api-Account-Id: ' . $account_id,
],
];
$result = $this->api_request($request_option);
return $result;
}
public function api_account($options = null)
{
// var
$account_id = @$this->API_ACCOUNT;
if (empty($account_id)) {
return null;
}
// business account id
$result = [
'header' => [
'Ik-Api-Account-Id: ' . $account_id,
],
];
return $result;
}
public function api_request($options = null)
{
$_option = $_header = null;
if ( ! $this->ENABLE) {
return null;
}
// import options
if (is_string($options)) {
$_method_id = $options;
}
is_array($options) && extract($options, EXTR_PREFIX_ALL | EXTR_REFS, '');
// method
$method = $this->api_method([
'type' => 'api',
'method_id' => ($_method_id ?? null),
]);
if (empty($method)) {
$result = [
'status' => false,
'status_message' => 'Request method not found',
];
return $result;
}
// method handler
if ( ! empty($method['is_handler'])) {
$handler = 'api_request__' . $method['is_handler'];
if ( ! method_exists($this, $handler)) {
$result = [
'status' => false,
'status_message' => 'Request method handler not found',
];
return $result;
}
$result = $this->{ $handler }($options);
return $result;
}
// request
$request = [];
! empty($_option) && $request = $_option;
// add options
! empty($method['option']) && $request = array_merge_recursive(
$request,
$method['option']
);
// url
$object = $this->api_url($method, $options);
if (isset($object['status']) && $object['status'] === false) {
return $object;
}
$url = $object;
// request options
$request_option = [];
($_is_debug ?? null) && $request_option['is_debug'] = true;
// api authorization
$_request_option = $this->api_authorization($method);
is_array($_request_option) && $request_option = array_merge_recursive($request_option, $_request_option);
// api account
$_request_option = $this->api_account($method);
is_array($_request_option) && $request_option = array_merge_recursive($request_option, $_request_option);
// header
is_array($_header) && $request_option = array_merge_recursive($request_option, ['header' => $_header]);
// test
if ($this->is_test()) {
switch ($_method_id) {
case 'withdraw-process':
$request['action'] = 'calc';
break;
}
}
// request
$result = $this->_api_request($url, $request, $request_option);
return $result;
}
public function api_payout($options = null)
{
$_amount = null;
if ( ! $this->ENABLE) {
return null;
}
// import options
is_array($options) && extract($options, EXTR_PREFIX_ALL | EXTR_REFS, '');
// method
$method = $this->api_method([
'type' => 'payout',
'method_id' => ($_method_id ?? null),
]);
if (empty($method)) {
$result = [
'status' => false,
'status_message' => 'Request method not found',
];
return $result;
}
$payment_api = &$this->payment_api;
// operation_id
$_operation_id = (int) $_operation_id;
$operation_id = $_operation_id;
if (empty($_operation_id)) {
$result = [
'status' => false,
'status_message' => 'Operation ID is not defined',
];
return $result;
}
$amount = $_amount;
// amount min/max
$result = $this->amount_limit([
'amount' => $amount,
'currency_id' => $currency_id,
'method' => $method,
]);
// amount currency conversion
$result = $this->currency_conversion_payout([
'options' => $options,
'method' => $method,
'amount' => $amount,
]);
if (empty($result['status'])) {
return $result;
}
$amount_currency = $result['amount_currency'];
$amount_currency_total = $result['amount_currency_total'];
$currency_id = $result['currency_id'];
if (empty($result['status'])) {
return $result;
}
// default
$amount = ($method['is_fee'] ?? null) ? $amount_currency_total : $amount_currency;
// request
$request = [];
($method['request_option'] ?? null) && $request = $method['request_option'];
// add common fields
! ($request['purseId'] ?? null) && $request['purseId'] = $this->PURSE_ID[$currency_id];
if ( ! ($request['purseId'] ?? null)) {
$result = [
'status' => false,
'status_message' => 'Wallet configuration is required',
];
return $result;
}
$request['amount'] = $amount;
$request['operation_id'] = $operation_id;
// transform
$this->option_transform([
'option' => &$request,
'transform' => $this->_api_transform,
]);
// add details
$request['details'] = [];
$request_details = $options;
$this->option_transform([
'option' => &$request_details,
'transform' => $this->_api_transform,
]);
foreach ($method['field'] as $key) {
$value = &$request_details[$key];
if ( ! isset($value)) {
$result = [
'status' => false,
'status_message' => 'Request data is missing: ' . $key,
];
return $result;
}
$request['details'][$key] = &$request_details[$key];
}
// START DUMP
$payment_api->dump(['name' => 'Interkassa', 'operation_id' => $operation_id,
'var' => ['request' => $request],
]);
// update processing
$sql_datetime = $payment_api->sql_datetime();
$operation_options = [
'processing' => [[
'provider_name' => 'interkassa',
'datetime' => $sql_datetime,
]],
];
$operation_update_data = [
'operation_id' => $operation_id,
'datetime_update' => $sql_datetime,
'options' => $operation_options,
];
$payment_api->operation_update($operation_update_data);
// request options
$request_option = [
'method_id' => 'withdraw-process',
'option' => $request,
'is_debug' => ($_is_debug ?? null),
];
$result = $this->api_request($request_option);
// DUMP
$payment_api->dump(['var' => ['response' => $result]]);
if (empty($result)) {
$result = [
'status' => false,
'status_message' => 'Unable to send the request',
];
return $result;
}
@list($status, $response) = $result;
if ( ! @$response) {
$result = [
'status' => false,
'status_message' => 'Unable to decode the response: ' . var_export($response, true),
];
return $result;
}
// transform reverse
foreach ($this->_api_transform_reverse as $from => $to) {
if ($from != $to && isset($response[$from])) {
$response[$to] = $response[$from];
unset($response[$from]);
}
}
// result
$result = [
'status' => &$status_name,
'status_message' => &$status_message,
];
$status_name = false;
$status_message = null;
$state = (int) $response['state'];
switch ($state) {
// success
case 0:
$status_name = true;
$status_message = 'Done';
break;
// processing
case 1106:
$status_message = 'Insufficient funds';
break;
default:
$status_message = 'Error: ' . $response['message'];
break;
}
($status_message ?? null) && $response['message'] = $status_message;
if ( ! $status_name) {
return $result;
}
// data
$data = &$response['data']['withdraw'];
if ( ! is_array($data)) {
$status_name = false;
$status_message = 'Unable to decode the response: ' . var_export($response, true);
return $result;
}
$data['_transaction'] = &$response['data']['transaction'];
// test mode
$this->is_test() && $data += [
'state' => 1,
'id' => 401040, // need real interkassa operation id
];
// check status
$state = (int) $data['state'];
list($status_name, $status_message) = $this->_state(
$state,
$this->_payout_status,
$this->_payout_status_message
);
$status_message = ($status_message ?? null) ?: ($data['stateName'] ?? null);
// update account, operation data
$payment_type = 'payment';
$operation_data = [
'operation_id' => $operation_id,
'provider_force' => ($_provider_force ?? null),
'provider_name' => 'interkassa',
'state' => $state,
'status_name' => $status_name,
'status_message' => $status_message,
'payment_type' => $payment_type,
'response' => $data,
];
// DUMP
$payment_api->dump(['var' => ['payment_type' => $payment_type, 'update operation' => $operation_data]]);
$result = $this->{ '_api_' . $payment_type }($operation_data);
// DUMP
$payment_api->dump(['var' => ['update result' => $result]]);
return $result;
}
}