-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvpos-payment-gateway.php
executable file
·219 lines (189 loc) · 7.59 KB
/
vpos-payment-gateway.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Plugin Name: vPOS - Payment Gateway
* Plugin URI: https://github.com/v-pos/vpos-woocommerce
* Description: Uma melhor forma de aceitar pagamentos.
* Version: 1.2
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: vPOS
* Author URI: https://vpos.ao
* License: GNU General Public License
* Text Domain: vpos-woocommerce-plugin
* Domain Path: /languages
*/
if (!defined('ABSPATH')) {
exit;
}
define("VPOS_DIR", plugin_dir_path(__FILE__));
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
require_once(ABSPATH . "wp-admin/includes/class-wp-filesystem-direct.php");
require_once("src/controllers/vpos_controller.php");
function register_vpos_routes()
{
$routes = new VposController();
$routes->register_routes();
}
add_action("rest_api_init", "register_vpos_routes");
function move_checkout_file_to_themes_dir()
{
$checkout_file_path = __DIR__ . "/vpos-checkout.php";
$poll_file_path = __DIR__ . "/vpos-poll.php";
$current_themes_path = get_template_directory();
$filesystem = new WP_Filesystem_Direct(false);
if ($filesystem->exists($checkout_file_path)) {
if ($filesystem->copy($checkout_file_path, $current_themes_path . "/vpos-checkout.php", true)) {
} else {
error_log("failed to copy file from " . $checkout_file_path . " to " . $current_themes_path);
}
} else {
error_log("vpos-checkout.php was not found in plugin directory");
}
if ($filesystem->exists($poll_file_path)) {
if ($filesystem->copy($poll_file_path, $current_themes_path . "/vpos-poll.php", true)) {
} else {
error_log("failed to copy file from " . $poll_file_path . " to " . $current_themes_path);
}
} else {
error_log("vpos-poll.php was not found in plugin directory");
}
}
function add_checkout_page()
{
$page_title = 'vpos-checkout';
$checkout_page = get_page_by_title($page_title, 'OBJECT', 'page');
if (empty($checkout_page)) {
wp_insert_post(
array(
'comment_status' => 'close',
'post_author' => 1,
'post_title' => ucwords($page_title),
'post_name' => strtolower(str_replace(' ', '-', trim($page_title))),
'post_status' => 'publish',
'post_content' => '',
'post_type' => 'page',
'page_template' => 'vpos-checkout.php'
)
);
}
}
function add_poll_page()
{
$page_title = 'payment';
$checkout_page = get_page_by_title($page_title, 'OBJECT', 'page');
if (empty($checkout_page)) {
wp_insert_post(
array(
'comment_status' => 'close',
'post_author' => 1,
'post_title' => ucwords($page_title),
'post_name' => strtolower(str_replace(' ', '-', trim($page_title))),
'post_status' => 'publish',
'post_content' => '',
'post_type' => 'page',
'page_template' => 'vpos-poll.php'
)
);
}
}
function hide_vpos_pages_from_website($args)
{
$vpos_checkout_page = get_page_by_title('vpos-checkout', 'OBJECT', 'page');
$vpos_poll_page = get_page_by_title('payment', 'OBJECT', 'page');
$args['exclude'] = '';
$args['exclude'] .= "" . $vpos_poll_page->ID . "," . "" . $vpos_checkout_page->ID . ",";
return $args;
}
add_filter('wp_page_menu_args', 'hide_vpos_pages_from_website', 999, 1);
function create_transactions_table()
{
global $wpdb;
$transaction_repository = new TransactionRepository($wpdb);
$transaction_repository->create_table();
}
function run_init_commands_after_installation()
{
$slug = (dirname(plugin_basename(__FILE__)));
add_option('Activated_Plugin', $slug);
move_checkout_file_to_themes_dir();
add_checkout_page();
add_poll_page();
create_transactions_table();
}
register_activation_hook(__FILE__, 'run_init_commands_after_installation');
function load_plugin()
{
$slug = (dirname(plugin_basename(__FILE__)));
if (is_admin() && get_option('Activated_Plugin') == $slug) {
delete_option('Activated_Plugin');
}
}
add_action('admin_init', 'load_plugin');
function your_plugin_settings_link($links)
{
$settings_link = '<a href="admin.php?page=wc-settings&tab=checkout§ion=vpos">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'your_plugin_settings_link');
function woocommerce_required_admin_notice()
{
echo '<div class="updated error notice"><p>';
echo _e('<b>vPOS WooCommerce</b> É necessário instalar o WooCommerce primeiro!', 'my-text-domain');
echo '</p></div>';
}
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_action('admin_notices', 'woocommerce_required_admin_notice');
} else {
add_action('plugins_loaded', 'init_vpos_gateway');
function init_vpos_gateway()
{
include "payment_gateway.php";
}
add_action('template_redirect', 'init_vpos_payment_gateway');
function init_vpos_payment_gateway()
{
if (get_query_var("payment_id") and get_query_var("id")) {
$payment_id = get_query_var("payment_id");
$payment_request_id = get_query_var("id");
include_once "src/payment_confirm.php";
}
}
add_filter("woocommerce_payment_gateways", "add_vpos");
function add_vpos($methods)
{
$methods[] = 'WP_Vpos_Gateway';
return $methods;
}
add_filter('woocommerce_available_payment_gateways', 'update_order_button');
function update_order_button($available_gateways)
{
if (!is_checkout()) {
return $available_gateways;
}
if (array_key_exists('vpos', $available_gateways)) {
$available_gateways['vpos']->order_button_text = "Proceder com Multicaixa Express";
}
return $available_gateways;
}
function put_id_in_cookies($uuid)
{
setcookie("vpos_id", $uuid, time() + 3600, "/");
}
function put_billing_phone_number_in_cookies($order_billing_telephone)
{
setcookie("vpos_order_billing_telephone", $order_billing_telephone, time() + 3600, "/");
}
function put_in_cookies($merchant, $total_amount, $order_id, $order_billing_telephone)
{
setcookie("vpos_merchant", $merchant, time() + 3600, "/");
setcookie("vpos_total_amount", $total_amount, time() + 3600, "/");
setcookie("vpos_order_id", $order_id, time() + 3600, "/");
setcookie("vpos_order_billing_telephone", $order_billing_telephone, time() + 3600, "/");
}
function formatTotalAmount($total_amount)
{
return number_format(sprintf('%0.2f', preg_replace("/[^0-9.]/", "", $total_amount)), 2, ",", ".") . " Kz";
}
}