Merge pull request #2 from rbkmoney/proc

update protocol to v2
This commit is contained in:
malkoas 2018-09-20 18:40:58 +03:00 committed by GitHub
commit e968bd6c2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 35 deletions

View File

@ -72,13 +72,17 @@ function gateway_rbkmoney_payment($separator, $sessionid)
try {
$rbk_api = new RBKmoneyPayment($params);
$invoice_id = $rbk_api->create_invoice();
$invoice_access_token = $rbk_api->create_access_token($invoice_id);
$response = $rbk_api->create_invoice();
$response_decode = json_decode($response['body'], true);
$invoice_id = !empty($response_decode['invoice']['id']) ? $response_decode['invoice']['id'] : '';
$invoice_access_token = !empty($response_decode['invoiceAccessToken']['payload']) ? $response_decode['invoiceAccessToken']['payload'] : '';
} catch (Exception $ex) {
echo $ex->getMessage();
exit();
}
$output = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
@ -90,18 +94,17 @@ function gateway_rbkmoney_payment($separator, $sessionid)
data-invoice-id="' . $invoice_id . '"
data-invoice-access-token="' . $invoice_access_token . '"
' . $company_name . '
' . $company_logo . '
' . $button_label . '
' . $description . '
>
</script>
</form></body></html>';
echo $output;
exit();
}
/**
* e.g. http{s}://{your-site}/?rbkmoney_payment_callback
*/
@ -158,7 +161,7 @@ function nzshpcrt_rbkmoney_payment_callback()
}
}
$current_shop_id = (int)trim(get_option('rbkmoney_payment_shop_id'));
$current_shop_id = trim(get_option('rbkmoney_payment_shop_id'));
if ($current_shop_id != $data[$invoice][RBKmoneyPayment::SHOP_ID]) {
_rbkmoney_payment_response_with_code_and_message(
RBKmoneyPayment::HTTP_CODE_BAD_REQUEST,
@ -337,14 +340,7 @@ function form_rbkmoney_payment()
</p>
</td>
</tr>
<tr>
<td>" . __('Logo in payment form', 'wp-e-commerce') . "</td>
<td>
<input type='text' size='60' value='" . trim(get_option('rbkmoney_payment_form_path_logo')) . "' name='rbkmoney_payment_form_path_logo' />
<p class='description'>
" . __('Your logo for payment form', 'wp-e-commerce') . "
</p>
</tr>
<tr>
<td>" . __('Company name in payment form', 'wp-e-commerce') . "</td>
<td>

View File

@ -33,7 +33,7 @@ class RBKmoneyPayment
const SESSION_ID = 'session_id';
const EVENT_TYPE = 'event_type';
private $api_url = 'https://api.rbk.money/v1/';
private $api_url = 'https://api.rbk.money/v2/';
private $merchant_private_key = '';
private $shop_id = '';
@ -254,7 +254,7 @@ class RBKmoneyPayment
]);
$data = [
'shopID' => (int)$this->getShopId(),
'shopID' => $this->getShopId(),
'amount' => (int)$this->getAmount(),
'metadata' => $this->prepare_metadata($this->getOrderId(), $this->getSessionId()),
'dueDate' => $this->prepare_due_date(),
@ -263,15 +263,19 @@ class RBKmoneyPayment
'description' => $this->getDescription(),
];
$this->validate();
$url = $this->prepare_api_url('processing/invoices');
$headers = $this->headers();
$response = $this->send($url, static::HTTP_METHOD_POST, $headers, json_encode($data, true));
$response_decode = json_decode($response['body'], true);
$invoice_id = !empty($response_decode['id']) ? $response_decode['id'] : '';
return $invoice_id;
$invoice_id = !empty($response_decode['invoice']['id']) ? $response_decode['invoice']['id'] : '';
$access_token = !empty($response_decode['invoiceAccessToken']['payload']) ? $response_decode['invoiceAccessToken']['payload'] : '';
return $response;
}
private function headers() {
$headers = [];
$headers[] = 'X-Request-ID: ' . uniqid();
@ -281,24 +285,7 @@ class RBKmoneyPayment
return $headers;
}
public function create_access_token($invoice_id)
{
if (empty($invoice_id)) {
throw new Exception('Не передан обязательный параметр invoice_id');
}
$url = $this->prepare_api_url('processing/invoices/' . $invoice_id . '/access_tokens');
$headers = $this->headers();
$response = $this->send($url, static::HTTP_METHOD_POST, $headers);
if ($response['http_code'] != static::HTTP_CODE_CREATED) {
throw new Exception('Возникла ошибка при создании токена для инвойса');
}
$response_decode = json_decode($response['body'], true);
$access_token = !empty($response_decode['payload']) ? $response_decode['payload'] : '';
return $access_token;
}
private function send($url, $method, $headers = [], $data = '')
{
if (empty($url)) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB