-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrazorpay.php
186 lines (151 loc) · 5.92 KB
/
razorpay.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
<?php
/*
Plugin Name: Razorpay for Gravity Forms
Plugin URI: https://wordpress.org/plugins/razorpay-gravity-forms
Description: Integrates Gravity Forms with Razorpay Payments, enabling end users to purchase goods and services through Gravity Forms.
Version: 1.3.7
Stable tag: 1.3.7
Author: Team Razorpay
Author URI: https://razorpay.com
Text Domain: razorpay-gravity-forms
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
== Description ==
This is the official Razorpay payment gateway plugin for Gravity Forma. Allows you to accept credit cards, debit cards, netbanking and wallet with the gravity form plugin. It uses a seamles integration, allowing the customer to pay on your website without being redirected away from your website.
*/
define('GF_RAZORPAY_VERSION', '1.3.7');
add_action('admin_post_nopriv_gf_razorpay_webhook', "gf_razorpay_webhook_init", 10);
add_action('gform_loaded', array('GF_Razorpay_Bootstrap', 'load'), 5);
add_action('plugins_loaded', 'createRzpWebhookTables');
add_action('rzp_gf_webhook_exec_cron', 'execRzpWebhookEvents');
add_filter('cron_schedules','rzpCronSchedules');
class GF_Razorpay_Bootstrap
{
public static function load()
{
if (method_exists('GFForms', 'include_payment_addon_framework') === false)
{
return;
}
require_once('class-gf-razorpay.php');
GFAddOn::register('GFRazorpay');
add_filter('gform_currencies', function (array $currencies) {
$supported_currencies = json_decode(file_get_contents(__DIR__ . "/supported-currencies.json"), true)['supported-currencies'];
foreach ($supported_currencies as $k => $v)
{
if (in_array($v['iso_code'], $currencies) === false)
{
$currencies[$v['iso_code']] = array(
'name' => __( $v['currency_name'], 'gravityforms' ),
'code' => $v['iso_code'],
'symbol_left' => $v['iso_code'],
'symbol_right' => '',
'symbol_padding' => ' ',
'thousand_separator' => ',',
'decimal_separator' => '.',
'decimals' => $v['exponent']
);
}
}
$currencies['INR'] = array(
'name' => __( 'Indian Rupee', 'gravityforms' ),
'code' => 'INR',
'symbol_left' => '₹',
'symbol_right' => '',
'symbol_padding' => ' ',
'thousand_separator' => ',',
'decimal_separator' => '.',
'decimals' => 2
);
return $currencies;
});
}
}
function gf_razorpay()
{
return GFRazorpay::get_instance();
}
// This is set to a priority of 10
// Initialize webhook processing
function gf_razorpay_webhook_init()
{
$gf_razorpay = gf_razorpay();
$gf_razorpay->process_webhook();
}
function createRzpWebhookTables()
{
$installedVersion = get_option('GF_RAZORPAY_VERSION');
if ($installedVersion !== GF_RAZORPAY_VERSION)
{
// create table to save webhook events
global $wpdb;
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}rzp_gf_webhook_triggers` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`rzp_order_id` varchar(25) NOT NULL,
`rzp_webhook_data` text,
`rzp_webhook_notified_at` varchar(30),
`rzp_update_order_cron_status` int(11) DEFAULT 0,
PRIMARY KEY (`id`)) $charset_collate;";
if (empty(dbDelta($sql)) === false)
{
update_option('GF_RAZORPAY_VERSION', GF_RAZORPAY_VERSION);
}
// create razorpay GF cron
createRzpCron('rzp_gf_webhook_exec_cron', time(), 'rzp_gf_webhook_cron_interval');
}
}
function rzpCronSchedules($schedules)
{
if (isset($schedules["rzp_gf_webhook_cron_interval"]) === false)
{
$schedules["rzp_gf_webhook_cron_interval"] = array(
'interval' => 5 * 60,
'display' => __('Every 5 minutes'));
}
return $schedules;
}
function createRzpCron($hookName, $startTime, $recurrence)
{
if (wp_next_scheduled($hookName) === false)
{
wp_schedule_event($startTime, $recurrence, $hookName);
}
}
function execRzpWebhookEvents()
{
global $wpdb;
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
$rzp_order_processed_by_webhook = 2;
$tableName = $wpdb->prefix . 'rzp_gf_webhook_triggers';
$webhookEvents = $wpdb->get_results("SELECT order_id, rzp_order_id, rzp_webhook_data FROM $tableName WHERE rzp_webhook_notified_at < " . (string)(time() - 300) ." AND rzp_update_order_cron_status=0;");
foreach ($webhookEvents as $row)
{
$events = json_decode($row->rzp_webhook_data);
foreach ($events as $event)
{
$event = (array) $event;
switch ($event['event'])
{
case 'order.paid':
$gf_razorpay = gf_razorpay();
$gf_razorpay->order_paid($event);
$wpdb->update(
$tableName,
array(
'rzp_update_order_cron_status' => $rzp_order_processed_by_webhook
),
array(
'order_id' => $row->order_id,
'rzp_order_id' => $row->rzp_order_id
)
);
return;
default:
return;
}
}
}
}