-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforminator-paystack-integration.php
More file actions
272 lines (232 loc) · 11.1 KB
/
Copy pathforminator-paystack-integration.php
File metadata and controls
272 lines (232 loc) · 11.1 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Plugin Name: Forminator Paystack Integration
* Description: Integrates Paystack payment with Forminator forms.
* Version: 1.0.1
* Author: Frank Orac
*/
// Define plugin constants
define('FORMINATOR_PAYSTACK_VERSION', '1.0.1');
define('FORMINATOR_PAYSTACK_PATH', plugin_dir_path(__FILE__));
define('FORMINATOR_PAYSTACK_URL', plugin_dir_url(__FILE__));
define('FORMINATOR_PAYSTACK_OPTION', 'forminator_paystack_settings');
// Load settings
function forminator_paystack_get_settings() {
$defaults = [
'form_id' => 8539,
'amount_field' => 'calculation-2',
'email_field' => 'email-1',
'public_key' => 'pk_test_',
'secret_key' => 'sk_test_',
'callback_url' => 'https://',
'test_mode' => 'yes',
];
$settings = get_option(FORMINATOR_PAYSTACK_OPTION, []);
return wp_parse_args($settings, $defaults);
}
// Enqueue frontend scripts
add_action('wp_enqueue_scripts', function () {
$settings = forminator_paystack_get_settings();
wp_enqueue_script('forminator-paystack', FORMINATOR_PAYSTACK_URL . 'assets/js/forminator-paystack.js', ['jquery'], FORMINATOR_PAYSTACK_VERSION, true);
wp_localize_script('forminator-paystack', 'FormPaystack', [
'form_id' => $settings['form_id'],
'amount_field' => $settings['amount_field'],
'email_field' => $settings['email_field'],
'public_key' => $settings['public_key'],
'callback_url' => $settings['callback_url'],
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('forminator_paystack_nonce')
]);
});
// Register AJAX handlers for verification
add_action('wp_ajax_forminator_paystack_verify', 'handle_forminator_paystack_verify');
add_action('wp_ajax_nopriv_forminator_paystack_verify', 'handle_forminator_paystack_verify');
// Add new AJAX handler for initialization
add_action('wp_ajax_forminator_paystack_initialize', 'handle_forminator_paystack_initialize');
add_action('wp_ajax_nopriv_forminator_paystack_initialize', 'handle_forminator_paystack_initialize');
function handle_forminator_paystack_initialize() {
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'forminator_paystack_nonce')) {
wp_send_json_error(['message' => 'Invalid nonce']);
}
$email = sanitize_email($_POST['email']);
$amount = floatval($_POST['amount']);
$form_id = intval($_POST['form_id']);
if (!$email || !is_email($email)) {
wp_send_json_error(['message' => 'Invalid email address']);
}
if (!$amount || $amount <= 0) {
wp_send_json_error(['message' => 'Invalid amount']);
}
$settings = forminator_paystack_get_settings();
$secret_key = $settings['secret_key'];
$callback_url = $settings['callback_url'];
// Amount should be in kobo (multiply by 100)
$amount_in_kobo = round($amount * 100);
$reference = 'FORMINATOR-' . $form_id . '-' . time();
$response = wp_remote_post('https://api.paystack.co/transaction/initialize', [
'headers' => [
'Authorization' => 'Bearer ' . $secret_key,
'Content-Type' => 'application/json',
'Cache-Control' => 'no-cache'
],
'body' => json_encode([
'email' => $email,
'amount' => $amount_in_kobo,
'reference' => $reference,
'callback_url' => $callback_url
])
]);
if (is_wp_error($response)) {
wp_send_json_error(['message' => 'Connection error: ' . $response->get_error_message()]);
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['status']) && $body['status']) {
wp_send_json_success($body['data']);
} else {
wp_send_json_error(['message' => $body['message'] ?? 'Payment initialization failed']);
}
}
function handle_forminator_paystack_verify() {
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'forminator_paystack_nonce')) {
wp_send_json_error(['message' => 'Invalid nonce']);
}
$reference = sanitize_text_field($_POST['reference']);
if (!$reference) {
wp_send_json_error(['message' => 'Missing reference']);
}
$settings = forminator_paystack_get_settings();
$paystack_secret = $settings['secret_key'];
$response = wp_remote_get("https://api.paystack.co/transaction/verify/{$reference}", [
'headers' => [
'Authorization' => 'Bearer ' . $paystack_secret
]
]);
if (is_wp_error($response)) {
wp_send_json_error(['message' => 'Unable to verify payment']);
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['data']) && $body['data']['status'] === 'success') {
wp_send_json_success(['message' => 'Payment verified successfully']);
} else {
wp_send_json_error(['message' => $body['message'] ?? 'Verification failed']);
}
}
add_shortcode('forminator_paystack_button', function ($atts = []) {
$settings = forminator_paystack_get_settings();
$defaults = [
'form_id' => $settings['form_id']
];
$atts = shortcode_atts($defaults, $atts);
$form_id = intval($atts['form_id']);
return '<div style="margin-top: 20px;">
<button type="button" id="forminator-paystack-button-' . $form_id . '" class="forminator-button forminator-button-submit">Pay Now</button>
<div id="forminator-paystack-error-' . $form_id . '" style="color:red; display:none; margin-top:10px;"></div>
</div>';
});
/**
* Admin Settings Page
*/
// Add admin menu: always under Settings
add_action('admin_menu', function() {
add_options_page(
'Forminator Paystack Integration', // Page title
'Forminator Paystack', // Menu title
'manage_options', // Capability
'forminator-paystack-settings', // Menu slug
'forminator_paystack_settings_page' // Callback function
);
});
// Register settings
add_action('admin_init', function() {
register_setting('forminator_paystack_settings', FORMINATOR_PAYSTACK_OPTION);
});
// Settings page callback
function forminator_paystack_settings_page() {
$settings = forminator_paystack_get_settings();
// Save settings
if (isset($_POST['forminator_paystack_save_settings']) && check_admin_referer('forminator_paystack_settings_nonce')) {
$new_settings = [
'form_id' => intval($_POST['form_id']),
'amount_field' => sanitize_text_field($_POST['amount_field']),
'email_field' => sanitize_text_field($_POST['email_field']),
'public_key' => sanitize_text_field($_POST['public_key']),
'secret_key' => sanitize_text_field($_POST['secret_key']),
'callback_url' => esc_url_raw($_POST['callback_url']),
'test_mode' => isset($_POST['test_mode']) ? 'yes' : 'no',
];
update_option(FORMINATOR_PAYSTACK_OPTION, $new_settings);
echo '<div class="notice notice-success"><p>Settings saved successfully!</p></div>';
$settings = $new_settings;
}
// Display settings form
?>
<div class="wrap">
<h1>Forminator Paystack Integration Settings</h1>
<form method="post" action="">
<?php wp_nonce_field('forminator_paystack_settings_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="form_id">Forminator Form ID</label></th>
<td>
<input type="number" name="form_id" id="form_id" value="<?php echo esc_attr($settings['form_id']); ?>" class="regular-text">
<p class="description">The ID of your Forminator form.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="amount_field">Amount Field ID</label></th>
<td>
<input type="text" name="amount_field" id="amount_field" value="<?php echo esc_attr($settings['amount_field']); ?>" class="regular-text">
<p class="description">The field ID for the payment amount (e.g., calculation-1).</p>
</td>
</tr>
<tr>
<th scope="row"><label for="email_field">Email Field ID</label></th>
<td>
<input type="text" name="email_field" id="email_field" value="<?php echo esc_attr($settings['email_field']); ?>" class="regular-text">
<p class="description">The field ID for the customer email (e.g., email-1).</p>
</td>
</tr>
<tr>
<th scope="row"><label for="public_key">Paystack Public Key</label></th>
<td>
<input type="text" name="public_key" id="public_key" value="<?php echo esc_attr($settings['public_key']); ?>" class="regular-text">
<p class="description">Your Paystack public key.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="secret_key">Paystack Secret Key</label></th>
<td>
<input type="password" name="secret_key" id="secret_key" value="<?php echo esc_attr($settings['secret_key']); ?>" class="regular-text">
<p class="description">Your Paystack secret key.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="callback_url">Callback URL</label></th>
<td>
<input type="url" name="callback_url" id="callback_url" value="<?php echo esc_url($settings['callback_url']); ?>" class="regular-text">
<p class="description">The URL to redirect to after payment (e.g., your thank you page).</p>
</td>
</tr>
<tr>
<th scope="row">Test Mode</th>
<td>
<label for="test_mode">
<input type="checkbox" name="test_mode" id="test_mode" <?php checked($settings['test_mode'], 'yes'); ?>>
Enable test mode
</label>
<p class="description">Check this to use Paystack test keys instead of live keys.</p>
</td>
</tr>
</table>
<h2>Shortcode Usage</h2>
<p>Use this shortcode to display the payment button after your Forminator form:</p>
<code>[forminator_paystack_button]</code>
<p>Or specify a different form ID:</p>
<code>[forminator_paystack_button form_id="1234"]</code>
<p class="submit">
<input type="submit" name="forminator_paystack_save_settings" class="button-primary" value="Save Settings">
</p>
</form>
</div>
<?php
}