This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mope.php
226 lines (198 loc) · 8.9 KB
/
mope.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
220
221
222
223
224
225
226
<?php
/*
Plugin Name: Payment Gateway for Mopé on WooCommerce
Plugin URI: https://github.com/Vokality/mope-php
Description: A Mopé Gateway Plugin for WooCommerce
Version: 1.0.4
Author: Vokality LLC
Author URI: https://github.com/Vokality
Requires at least: 5.1
Requires PHP: 7.2
License: GPLv2 or later
*/
add_filter('woocommerce_payment_gateways', 'mope_add_gateway_class');
function mope_add_gateway_class($gateways)
{
$gateways[] = 'WC_Mope_Gateway';
return $gateways;
}
add_action('plugins_loaded', 'mope_init_gateway_class');
function mope_init_gateway_class()
{
class WC_Mope_Gateway extends WC_Payment_Gateway
{
private $test_mode;
private $mope_api_key;
private $mope_api_base_url;
private $transaction_description;
private $custom_wc_request_config;
public function __construct()
{
$this->id = 'mope';
$this->mope_api_base_url = "https://api.mope.sr/api/";
$this->icon = esc_url(plugins_url('assets/mope_logo.png', __FILE__));
$this->has_fields = true;
$this->method_title = 'Mopé Payment Gateway';
$this->method_description = 'Pay quickly and securely with Mopé Mobile wallets.';
$this->supports = array(
'products'
);
$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->test_mode = 'yes' === $this->get_option('test_mode');
$this->mope_api_key = $this->test_mode ? $this->get_option('test_private_key') : $this->get_option('private_key');
$this->transaction_description = $this->get_option('transaction_description');
$this->custom_wc_request_config = array(
'timeout' => 5,
'headers' => array(
'User-Agent' => 'Mopé Php Client',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->mope_api_key
)
);
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
str_replace('https:', 'http:', add_query_arg('wc-api', 'WC_Gateway_Mope', home_url('/')));
add_action('woocommerce_api_mope', array($this, 'mope_callback'));
}
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable',
'label' => 'Enable/disable Mopé Payment Gateway',
'type' => 'checkbox',
'description' => 'Make this payment method available to your users.',
'default' => 'no'
),
'title' => array(
'title' => 'Title',
'type' => 'text',
'description' => 'Title for this payment method displayed at checkout.',
'default' => 'Mopé Mobile Wallet',
'desc_tip' => true,
),
'description' => array(
'title' => 'Description',
'type' => 'textarea',
'description' => 'A description of this payment gateway',
'default' => 'Pay quickly and securely with your Mopé Mobile wallet.',
),
'test_mode' => array(
'title' => 'Test mode',
'label' => 'Enable Test Mode',
'type' => 'checkbox',
'description' => '',
'default' => 'yes',
'desc_tip' => true,
),
'test_private_key' => array(
'title' => 'Test Mopé Key',
'type' => 'password',
),
'private_key' => array(
'title' => 'Live Mopé Key',
'type' => 'password'
),
'transaction_description' => array(
'title' => 'Transaction Description',
'type' => 'text',
'default' => site_url(),
'description' => 'Text that will be used to describe the transaction in a buyers Mopé Wallet',
)
);
}
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
$order_data = $order->get_data();
$order_id = $order_data['id'];
$order_total1 = preg_replace("/(?<!\d)([,.])(?!\d)/", "", $order->get_total());
$order_total_formatted = number_format($order_total1, 2, '', '');
# use ?wc-api=callback because it works with all permalink setups
# https://github.com/woocommerce/woocommerce/issues/23142#issuecomment-476604300
$returnURL = site_url() . '?wc-api=mope&order_id=' . $order_id;
$data = array(
'description' => $this->transaction_description,
'amount' => $order_total_formatted,
'order_id' => $order_id,
'currency' => 'SRD',
'redirect_url' => $returnURL
);
$data_string = json_encode($data);
$post_data = array_merge(array('body' => $data_string), $this->custom_wc_request_config);
$response = wp_remote_post($this->mope_api_base_url . 'shop/payment_request', $post_data);
if (is_wp_error($response)) {
wc_add_notice("An unexpected error has occurred.", 'error');
return array(
'result' => 'error',
'redirect' => wc_get_checkout_url(),
);
}
$response_status = wp_remote_retrieve_response_code($response);
if (intval($response_status) != 201) {
wc_add_notice("An error occurred communicating with Mopé. Please try again later.", 'error');
return array(
'result' => 'error',
'redirect' => wc_get_checkout_url(),
);
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
$order->update_meta_data('mope_payment_id', $data->id);
$order->save();
return array(
'result' => 'success',
'redirect' => $data->url
);
}
function redirect_to_cart()
{
wp_safe_redirect(wc_get_cart_url());
exit;
}
function mope_callback()
{
$order_id = isset($_GET['order_id']) ? sanitize_text_field($_GET['order_id']) : null;
if (!$order_id || !is_numeric($order_id)) {
wc_add_notice("Invalid order ID", 'error');
$this->redirect_to_cart();
}
$order = wc_get_order($order_id);
$payment_id = $order->get_meta('mope_payment_id');
if (!$payment_id) {
wc_add_notice('An unexpected error has occurred.', 'error');
$order->update_status('failed', 'Mopé payment ID was set on order.');
$this->redirect_to_cart();
}
$response = wp_remote_get($this->mope_api_base_url . 'shop/payment_request/' . $payment_id, $this->custom_wc_request_config);
$response_status = wp_remote_retrieve_response_code($response);
if (is_wp_error($response)) {
wc_add_notice("An unexpected error has occurred.", 'error');
$this->redirect_to_cart();
}
if ($response_status == 401 || $response_status == 403) {
wc_add_notice("We're having trouble processing your request, please try again later.", "error");
$order->update_status('failed', "Response status " . $response_status . " returned from Mopé gateway.");
$this->redirect_to_cart();
}
if ($response_status == 404) {
wc_add_notice("We're having trouble processing your request, please try again later.", "error");
$order->update_status("failed", "Unable to find Mopé payment ID associated with this order.");
$this->redirect_to_cart();
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
if ($data->status == 'paid') {
$order->update_status('completed');
wc_reduce_stock_levels($order_id);
wp_safe_redirect($order->get_checkout_order_received_url());
exit;
}
wc_add_notice('Unable to process your payment. Please try again.', 'error');
$this->redirect_to_cart();
}
}
}