<?php
/*
Plugin Name: ***** Pay Gateway
Description: Custom Payment Gateway for ***** Pay
Author: Zutov
Version: 1.0
*/
add_filter('woocommerce_payment_gateways', '*****_pay_add_gateway_class');
function ****_pay_add_gateway_class($gateways) {
$gateways[] = 'WC_****_Pay_Gateway';
return $gateways;
}
add_action('plugins_loaded', '*****_pay_init_gateway_class');
function *****_pay_init_gateway_class() {
class WC_*****_Pay_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = '*****_pay';
$this->method_title = '***** Pay';
$this->method_description = 'Custom Payment Gateway for ***** Pay';
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
$this->testmode = $this->get_option('testmode') === 'yes' ? true : false;
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
public function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => 'Включить/Выключить',
'type' => 'checkbox',
'label' => 'Включить ***** Pay Gateway',
'default' => 'no'
),
'title' => array(
'title' => 'Название',
'type' => 'text',
'description' => 'Название метода оплаты, которое видит пользователь при оформлении заказа.',
'default' => '***** Pay',
'desc_tip' => true,
),
'description' => array(
'title' => 'Описание',
'type' => 'textarea',
'description' => 'Описание метода оплаты, которое видит пользователь при оформлении заказа.',
'default' => 'Оплата с помощью ***** Pay.',
),
'testmode' => array(
'title' => 'Тестовый режим',
'type' => 'checkbox',
'label' => 'Включить тестовый режим',
'default' => 'yes',
'description' => 'В этом режиме платежи будут симулироваться, как если бы они проходили через платёжный шлюз ***** Pay.',
),
'api_key' => array(
'title' => 'API ключ',
'type' => 'text',
'description' => 'Ваш API ключ от ***** Pay.',
),
);
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
$total = $order->get_total();
$currency = get_woocommerce_currency();
$return_url = $this->get_return_url($order);
$callback_url = 'https://*******tech.com/*****_pay_callback';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://***********/payment-invoices',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"data": {
"type": "payment-invoices",
"attributes": {
"reference_id": "'. $order_id .'",
"description": "Payment by order#'. $order_id .'",
"currency": "'. $currency .'",
"amount": '. $total .',
"service": "payment_card_eur_hpp",
"return_url": "'. $return_url .'",
"callback_url": "'. $callback_url .'",
"test_mode": true
}
}
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic Y29tYV9OZ0dsU0I2VWdidUFCc1pYOjVqQWw0MkdzbGZTcjhVcTllRFF3bTBjNU1YVDM3MDFXYTZmaWh6Q00tVEU=',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
error_log('***** Pay API Response: ' . print_r($response, true));
curl_close($curl);
$response_body = json_decode($response, true);
if (isset($response_body['data']['attributes']['hpp_url'])) {
$redirect_url = $response_body['data']['attributes']['hpp_url'];
return array(
'result' => 'success',
'redirect' => $redirect_url,
);
} else {
wc_add_notice('Ошибка при обработке платежа.', 'error');
return;
}
}
}
}
// Добавление endpoint для обработки callback от ***** Pay
function add_*****_pay_callback_endpoint() {
add_rewrite_endpoint('*****_pay_callback', EP_ROOT);
}
add_action('init', 'add_*****_pay_callback_endpoint');
// Обработка запросов к endpoint
function handle_*****_pay_callback() {
global $wp_query;
if (!isset($wp_query->query_vars['*****_pay_callback'])) {
return;
}
$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);
error_log('***** Pay Callback Data: ' . print_r($data, true));
if (isset($data['status'])) {
$order_id = $data['reference_id']; // Убедитесь, что это правильный ключ для идентификатора заказа
$order = wc_get_order($order_id);
if (!$order) {
error_log('Order not found: ' . $order_id);
return;
}
switch ($data['status']) {
case 'processed':
$order->update_status('completed', 'Платеж успешно обработан через ***** Pay');
break;
case 'process_failed':
$order->update_status('failed', 'Ошибка при обработке платежа через ***** Pay');
break;
case 'process_pending':
$order->update_status('processing', 'Платёж в обработке через ***** Pay');
break;
}
}
status_header(200);
echo 'Callback received and processed';
exit;
}
add_action('template_redirect', 'handle_*****_pay_callback');
<pre>Array
(
[data] => Array
(
[type] => payment-invoices
[id] => *********
[attributes] => Array
(
[status] => process_pending
[serial_number] => ft9Y4rHkICbylNlY
[resolution] => ok
[moderation_required] =>
[amount] => 159
[payment_amount] => 159
[currency] => EUR
[service_amount] =>
[payment_service_amount] =>
[exchange_rate] =>
[service_currency] => EUR
[reference_id] => 1548
[test_mode] => 1
[description] => Payment by order#1548
[descriptor] =>
[fee] => 0
[deposit] => 159
[processed] =>
[processed_amount] =>
[refunded_amount] =>
[refunded_fee] =>
[processed_fee] =>
[processed_deposit] =>
[metadata] => Array
(
)
[flow_data] => Array
(
[action] => https://checkout******************
[method] => GET
[params] => Array
(
)
[metadata] => Array
(
[sid] => ************
[token] => ************
)
)
[flow] => hpp
[payment_flow] => charge
[return_url] => https://************/checkout/order-received/1548/?key=wc_order_iyuzm8bWMjmhg
[return_urls] => Array
(
)
[callback_url] => https://example.com/payments/callback
[created] => 1709026847
[updated] => 1709026847
[payload] =>
[original_data] => Array
(
)
[rrn] =>
[arn] =>
[approval_code] =>
[reserved_amount] =>
[reserve_expires] =>
[unreserved] =>
[source] => merchant_api
[callback_logs] => Array
(
)
[charged_back_amount] =>
[hpp_url] => https://***************/redirect/hpp/?cpi=cpi_ft9Y4rHkICbylNlY
[ledger_payload] =>
)
[relationships] => Array
(
[payment-service] => Array
(
[data] => Array
(
[type] => payment-services
[id] => payment_card_eur_hpp
)
)
[payment-method] => Array
(
[data] => Array
(
[type] => payment-methods
[id] => payment_card
)
)
[payment-request] => Array
(
[data] => Array
(
[type] => payment-requests
[id] => ***********
)
)
[active-payment] => Array
(
[data] => Array
(
[type] => payments
[id] => **************
)
)
[commerce-account] => Array
(
[data] => Array
(
[type] => commerce-accounts
[id] => coma_************
)
)
[currency-account] => Array
(
[data] => Array
(
[type] => currency-accounts
[id] => comca_***********
)
)
[customer] => Array
(
[data] =>
)
)
[links] => Array
(
[self] => /commerce/payment-invoices/************
)
)
)
</pre>{"result":"failure","messages":"<ul class=\"woocommerce-error\" role=\"alert\">\n\t\t\t<li>\n\t\t\t\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u043b\u0430\u0442\u0435\u0436\u0430.\t\t<\/li>\n\t<\/ul>\n","refresh":false,"reload":false}