php copyright ITos http itos net ua license Commercial class Payment_A

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* @copyright ITos (http://itos.net.ua)
* @license Commercial
*/
class Payment_Adapter_Privat24Merchant
{
private $config = array();
protected $di;
public function __construct($config)
{
$this->config = $config;
}
/**
* @param Box_Di $di
*/
public function setDi($di)
{
$this->di = $di;
}
public static function getConfig()
{
return array(
'supports_one_time_payments' => true,
'supports_subscriptions' => false,
'description' => 'Приват24 для Бизнеса: Услуги > электронная коммерция > API',
'form' => array(
'merchant_id' => array('text', array(
'label' => 'ID',
),
),
'secret_key' => array('text', array(
'label' => 'Пароль',
),
),
),
);
}
/**
* Generate payment text
*
* @param Api_Admin $api_admin
* @param int $invoice_id
* @param bool $subscription
*
* @since BoxBilling v2.9.15
*
* @return string - html form with auto submit javascript
*/
public function getHtml($api_admin, $invoice_id)
{
$invoice = $api_admin->invoice_get(array('id' => $invoice_id));
$data = array();
$data['amt'] = $invoice['total'];
$data['ccy'] = 'UAH';
$data['merchant'] = $this->config['merchant_id'];
$data['order'] = $invoice['id'];
$data['details'] = 'Оплата счета N ' . $invoice['id'];
$data['ext_details'] = $invoice['buyer']['email'];
$data['pay_way'] = 'privat24';
$data['return_url'] = $this->config['return_url'];
$data['server_url'] = $this->config['notify_url'];
$data['signature'] = $this->getSign($data);
$url = 'https://api.privatbank.ua/p24api/ishop';
return $this->_generateForm($url, $data);
}
public function processTransaction($api_admin, $id, $data)
{
if(APPLICATION_ENV != 'testing' && !$this->_isIpnValid($data))
throw new Exception('IPN is not valid');
$ipn = $data['post'];
list($amt, $ccy, $details, $ext_details, $pay_way, $order, $merchant, $state, $date, $ref, $payCountry) = explode('&', $ipn['payment']);
list($null, $amt) = explode('=', $amt);
list($null, $ccy) = explode('=', $ccy);
list($null, $details) = explode('=', $details);
list($null, $ext_details) = explode('=', $ext_details);
list($null, $pay_way) = explode('=', $pay_way);
list($null, $order) = explode('=', $order);
list($null, $merchant) = explode('=', $merchant);
list($null, $state) = explode('=', $state);
list($null, $date) = explode('=', $date);
list($null, $ref) = explode('=', $ref);
//$payCountry = explode('=', $payCountry)[1];
$invoice = $api_admin->invoice_get(array('id' => $order));
$client_id = $invoice['client']['id'];
$checkSign = $this->checkSign($ipn['payment'], $ipn['signature']);
if ($checkSign)
{
$tx_data = array(
'id' => $id,
'invoice_id' => $order,
'txn_status' => 'complete',
'txn_id' => $ref,
'amount' => $amt,
'currency' => 'UAH',
'type' => 'payment',
'status' => 'complete',
);
$api_admin->invoice_transaction_update($tx_data);
if ($state == 'ok')
{
$bd = array(
'id' => $client_id,
'amount' => $amt,
'description' => 'Платеж через Приват24',
'type' => 'Приват24',
'rel_id' => $ref,
);
$api_admin->client_balance_add_funds($bd);
$api_admin->invoice_batch_pay_with_credits(array('client_id' => $client_id));
}
}
}
private function _isIpnValid($data)
{
$ipn = $data['post'];
if (!isset($ipn['payment']) || !isset($ipn['signature']))
return false;
return true;
}
private function _generateForm($url, $data, $method = 'post')
{
$form = '';
$form .= '<form action="'.$url.'" method="'.$method.'">' . PHP_EOL;
foreach($data as $key => $value) {
$form .= sprintf('<input type="hidden" name="%s" value="%s" />', $key, $value) . PHP_EOL;
}
$form .= '<input class="bb-button bb-button-submit" type="submit" value="Оплатить через Приват24" id="payment_button"/>'. PHP_EOL;
$form .= '</form>' . PHP_EOL . PHP_EOL;
if(isset($this->config['auto_redirect']) && $this->config['auto_redirect']) {
$form .= sprintf('<h2>%s</h2>', __('Перенаправление на Приват24...'));
$form .= "<script type='text/javascript'>$(document).ready(function(){ document.getElementById('payment_button').style.display = 'none'; document.forms[0].submit();});</script>";
}
return $form;
}
private function getSign($data) {
$signString = 'amt=' . $data['amt'] . '&ccy=' . $data['ccy'] . '&details=' . $data['details'] . '&ext_details=' . $data['ext_details'] . '&pay_way=' . $data['pay_way'] . '&order=' . $data['order'] . '&merchant=' . $data['merchant'];
return sha1(md5($signString.$this->config['secret_key']));
}
private function checkSign($data, $sign) {
return ($sign == sha1(md5($data.$this->config['secret_key'])));
}
}