-
Notifications
You must be signed in to change notification settings - Fork 0
/
redde-woocommerce-plugin.php
executable file
·232 lines (212 loc) · 7.67 KB
/
redde-woocommerce-plugin.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
<?php
/**
* Plugin Name: Redde Payment Service for WooCommerce
* Plugin URI: index.php
* Description: Secure Payment for Mobile Money and Ghana Issued Cards
* Version: 1.0
* Author: Wigal
* Developed by: Kwame Oteng Appiah-Nti
* Author URI: https://www.wigal.com.gh/
* Author Email: koteng@wigal.com.gh
* License: GPLv2 or later
* Requires at least: 4.4
* Tested up to: 5.2
*
*
* @package Redde Payment Service
* @category Plugin
* @author Kwame Oteng Appiah-Nti
* @company Wigal Limited
*
* Copyright 2019 Wigal Vision Limited
*/
/**
* Exit if accessed directly.
*/
if (!defined('ABSPATH')) {
exit();
}
/**
* Admin 'Settings' link on plugin page
**/
function redde_wc_plugin_admin_action($actions, $plugin_file)
{
if (false == strpos($plugin_file, basename(__FILE__))) {
return $actions;
}
$settings_link = '<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout§ion=redde-wc-payment') . '">Settings</a>';
array_unshift($actions, $settings_link);
return $actions;
}
add_filter('plugin_action_links', 'redde_wc_plugin_admin_action', 10, 2);
function init_redde_wc_payment_gateway()
{
if (class_exists('WC_Payment_Gateway')) {
class Redde_WC_Payment_Gateway extends WC_Payment_Gateway
{
public function __construct()
{
$this->id = 'redde-wc-payment';
$this->icon = plugins_url('assets/images/redde-pay.png', __FILE__);
$this->has_fields = true;
$this->method_title = __('Redde Payment Service', '');
$this->method_description = 'WooCommerce Payment Plugin for Redde Payment Service.';
$this->description = "Secure payment with Mobile Money or Credit Card";
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->checkout_url = 'https://api.reddeonline.com/v1/checkout/';
$this->redde_app_id = $this->get_option('app_id');
$this->redde_api_key = $this->get_option('api_key');
$this->redde_merchant_name = $this->get_option('merchant_name');
$this->redde_merchant_logo = $this->get_option('merchant_logo');
$this->redde_success_callback = $this->get_option('success_callback');
$this->redde_failure_callback = $this->get_option('failure_callback');
if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
} else {
add_action('woocommerce_update_options_payment_gateways', array($this, 'process_admin_options'));
}
}
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', ''),
'type' => 'checkbox',
'description' => __('Check in order to enable ePay WooCommerce Payment Gateway, otherwise, uncheck to disable.', ''),
'label' => __('Enable Redde Payment', ''),
'default' => 'no',
'desc_tip' => true,
),
'title' => array(
'title' => __('Title', ''),
'type' => 'text',
'class' => 'is-read-only',
'description' => __('This controls the title which the user sees during checkout.', ''),
'default' => __('Redde Checkout', ''),
'desc_tip' => true,
),
'app_id' => array(
'title' => __('App ID', ''),
'type' => 'text',
'description' => __('App id given to you by Redde Team', ''),
'default' => __('', ''),
'desc_tip' => true,
),
'api_key' => array(
'title' => __('Apikey', ''),
'type' => 'text',
'description' => __('Apikey given to you by Redde Team', ''),
'default' => __('', ''),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', ''),
'type' => 'text',
'label' => __('Enable to collect onsite payment.', ''),
'description' => __('Description for merchants payment', ''),
'default' => __('', ''),
'desc_tip' => false,
),
'merchant_name' => array(
'title' => __('Merchant Name', ''),
'type' => 'text',
'description' => __('This will display merchant name on checkout', ''),
'default' => __('', ''),
'desc_tip' => false,
),
'merchant_logo' => array(
'title' => __('Merchant Logo', ''),
'type' => 'text',
'description' => __('This will be used to display merchant logo', ''),
'default' => __('', ''),
'desc_tip' => false,
),
'success_callback' => array(
'title' => __('Success Url', ''),
'type' => 'text',
'description' => __('Set this url for successful payment made', ''),
'default' => __('', ''),
'desc_tip' => false,
),
'failure_callback' => array(
'title' => __('Failed Url', ''),
'type' => 'text',
'description' => __('Set this url for failed payment', ''),
'default' => __('', ''),
'desc_tip' => false,
),
);
}
/**
* Handle payment and process the order.
* Also tells WC where to redirect the user, and this is done with a returned array.
* Redirect to Redde
**/
function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
// Get an instance of the WC_Order object
$order = wc_get_order($order_id);
$order_data = $order->get_items();
$payload = array(
'appid' => $this->redde_app_id,
'apikey' => $this->redde_api_key,
'merchantname' => $this->redde_merchant_name,
'logolink' => esc_url($this->redde_merchant_logo),
'amount' => ($woocommerce->cart->total),
'description' => $this->get_option('description'),
'clienttransid' => $this->redde_app_id . '-' . time() . '-' . $order_id,
'failurecallback' => $this->redde_failure_callback,
'successcallback' => $this->redde_success_callback,
);
$response = wp_remote_post(
$this->checkout_url,
array(
'method' => 'POST',
'body' => json_encode($payload),
'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
'timeout' => 45,
)
);
//Get response code and body
$response_code = wp_remote_retrieve_response_code($response);
$response_body = wp_remote_retrieve_body($response);
$checkout_response = json_decode($response_body);
$checkout_response = (!is_wp_error($response)) ? json_decode($response_body) : null;
switch ($response_code) {
case 200:
$order->update_status('on-hold', 'Payment in progress');
//Set session variables
@session_start();
$_SESSION['redde_payload'] = $payload;
$_SESSION['redde_order_id'] = $order_id;
$_SESSION['redde_checkout_response'] = $checkout_response;
return array(
'result' => 'success',
'redirect' => $checkout_response->checkouturl //$checkout_url
);
break;
case 400:
wc_add_notice("HTTP STATUS: $response_code - $checkout_response->reason", "error");
break;
case 500:
wc_add_notice("HTTP STATUS: $response_code - $checkout_response->reason", "error");
break;
default:
wc_add_notice("HTTP STATUS CODE here: $response_code Error Connecting to Redde Payment Service, Please try again.", "error");
break;
}
}
} //end of class
}
}
function wc_add_redde_payment_gateway($methods)
{
$methods[] = 'Redde_WC_Payment_Gateway';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'wc_add_redde_payment_gateway');
add_action('plugins_loaded', 'init_redde_wc_payment_gateway', 0);