-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoneyunify-wooc.php
234 lines (192 loc) · 8.8 KB
/
moneyunify-wooc.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
227
228
229
230
231
232
233
234
<?php
/*
Plugin Name: MoneyUnify WooCommerce Payment Gateway
Plugin URI: https://github.com/MoneyUnify/MoneyUnify-Woocommerce
Description: Take payments via MoneyUnify on your WooCommerce store.
Version: 0.0.1
Author: Kazasim Kuzasuwat
Author URI: https://moneyunify.com
Requires at least: 6.4.3
Requires PHP: 7.0
Requires WooCommerce: 5.0
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
add_filter('woocommerce_payment_gateways', 'add_moneyunify_gateway_class');
function add_moneyunify_gateway_class($gateways)
{
$gateways[] = 'WC_MoneyUnify_Gateway';
return $gateways;
}
add_action('plugins_loaded', 'init_moneyunify_gateway_class');
function init_moneyunify_gateway_class()
{
class WC_MoneyUnify_Gateway extends WC_Payment_Gateway
{
public function __construct()
{
$this->id = 'moneyunify';
$this->icon = ''; // Icon URL
$this->has_fields = true;
$this->method_title = __('MoneyUnify', 'moneyunify-pay-woocommerce');
$this->method_description = __('Take payments via MoneyUnify', 'moneyunify-pay-woocommerce');
$this->supports = array(
'products'
);
// Load the settings
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
$this->uuid = $this->get_option('uuid');
// Save settings
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' => __('Enable/Disable', 'moneyunify-pay-woocommerce'),
'type' => 'checkbox',
'label' => __('Enable MoneyUnify Gateway', 'moneyunify-pay-woocommerce'),
'default' => 'no'
),
'title' => array(
'title' => __('Title', 'moneyunify-pay-woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'moneyunify-pay-woocommerce'),
'default' => __('MoneyUnify', 'moneyunify-pay-woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'moneyunify-pay-woocommerce'),
'type' => 'textarea',
'description' => __('This controls the description which the user sees during checkout.', 'moneyunify-pay-woocommerce'),
'default' => __('Pay via MoneyUnify; you can pay with your mobile money account.', 'moneyunify-pay-woocommerce'),
),
'uuid' => array(
'title' => __('MoneyUnify UUID', 'moneyunify-pay-woocommerce'),
'type' => 'text',
'description' => __('Enter your MoneyUnify UUID here.', 'moneyunify-pay-woocommerce'),
'default' => '',
),
);
}
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
// Retrieve customer details from the order
$phone = $order->get_billing_phone();
$amount = $order->get_total();
$email = $order->get_billing_email();
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$transaction = uniqid(); // Generate a unique transaction ID
// MoneyUnify API Request - Request Payment
$response = $this->moneyunify_request_payment($phone, $amount, $email, $first_name, $last_name, $transaction);
// Check for API errors
if ($response === false || isset($response['error'])) {
// Payment initiation failed, handle accordingly
wc_add_notice('Payment initiation failed. Please try again.', 'error');
return;
}
// Check if the transaction request is successful
if (isset($response['data']['status']) && $response['data']['status'] === 'TXN_AUTH_SUCCESSFUL') {
// Save the MoneyUnify reference and phone number in the order for verification
update_post_meta($order_id, '_moneyunify_reference', $response['data']['reference']);
update_post_meta($order_id, '_moneyunify_phone_number', $phone);
// Mark the order as on-hold
$order->update_status('on-hold', __('Awaiting payment confirmation from MoneyUnify.', 'moneyunify-pay-woocommerce'));
// Redirect to the order completed page
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order),
);
} else {
// Payment initiation failed, handle accordingly
wc_add_notice('Payment initiation failed: ' . $response['message'], 'error');
return;
}
}
private function moneyunify_request_payment($phone, $amount, $email, $first_name, $last_name, $transaction)
{
$url = 'https://api.moneyunify.com/moneyunify/request_payment';
$body = array(
'muid' => $this->uuid,
'phone_number' => $phone,
'transaction_details'=> 'Order payment',
'amount' => $amount,
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
);
// Initialize cURL session
$curl = curl_init();
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
));
// Execute cURL request
$response = curl_exec($curl);
// Check for errors
if ($response === false) {
// Log cURL error
error_log('cURL Error: ' . curl_error($curl));
return false;
}
// Decode JSON response
$result = json_decode($response, true);
// Close cURL session
curl_close($curl);
return $result;
}
public function payment_fields()
{
echo '<div class="form-row form-row-wide">';
echo '<label for="moneyunify_phone_number">' . __('Phone Number', 'moneyunify-pay-woocommerce') . ' <span class="required">*</span></label>';
echo '<input type="text" class="input-text" name="moneyunify_phone_number" id="moneyunify_phone_number" placeholder="' . __('Enter your phone number', 'moneyunify-pay-woocommerce') . '" />';
echo '</div>';
}
public function verify_transaction($reference)
{
// MoneyUnify API endpoint for transaction verification
$url = 'https://api.moneyunify.com/moneyunify/verify_transaction';
// Prepare request body
$body = array(
'muid' => $this->uuid,
'reference' => $reference,
);
// Initialize cURL session
$curl = curl_init();
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => false,
));
// Execute cURL request
$response = curl_exec($curl);
if ($response === false) {
error_log('cURL Error: ' . curl_error($curl));
return false;
}
$result = json_decode($response, true);
curl_close($curl);
return $result;
}
}
}
?>