-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedd-agate-gateway.php
executable file
·284 lines (228 loc) · 10 KB
/
edd-agate-gateway.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
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
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/*
Plugin Name: Agate Easy Digital Downloads (EDD) - Payment Gateway
Plugin URI: https://github.com/
Description: Provides a <a href="https://agate.services">agate.services</a> Payment Gateway for <a href="https://wordpress.org/plugins/easy-digital-downloads/">Easy Digital Downloads 2.4.2+</a>. Direct Integration on your website, no external payment pages opens (as other payment gateways offer). Accept payments online. You will see the payment statistics in one common table on your website. No Chargebacks, Global, Secure. All in automatic mode.
Version: 1.0.0
Author: Agate.io
Author URI: https://agate.services
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
GitHub Plugin URI: https://github.com/
*/
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;
if( !class_exists( 'EDD_Agate' ) ) {
class EDD_Agate {
private static $instance;
/**
* Get active instance
*
* @since 1.0.0
* @access public
* @static
* @return object self::$instance
*/
public static function get_instance() {
if( !self::$instance )
self::$instance = new EDD_Agate();
return self::$instance;
}
/**
* Class constructor
*
* @since 1.0.0
* @access public
* @return void
*/
public function __construct() {
// Plugin dir
define( 'EDD_AGATE_DIR', plugin_dir_path( __FILE__ ) );
// Plugin URL
define( 'EDD_AGATE_URL', plugin_dir_url( __FILE__ ) );
$this->init();
}
/**
* Run action and filter hooks
*
* @since 1.0.0
* @access private
* @return void
*/
private function init() {
// Make sure EDD is active
if( !class_exists( 'Easy_Digital_Downloads' ) ) return;
global $edd_options;
// Internationalization
add_action( 'init', array( $this, 'textdomain' ) );
// Register settings
add_filter( 'edd_settings_gateways', array( $this, 'settings' ), 1 );
// Add the gateway
add_filter( 'edd_payment_gateways', array( $this, 'register_gateway' ) );
// Register icon
add_filter('edd_accepted_payment_icons', array($this, 'pw_edd_payment_icon' ));
// Remove CC form
add_action( 'edd_agate_cc_form', '__return_false' );
// Process payment
add_action( 'edd_gateway_agate', array( $this, 'process_payment' ) );
add_action( 'init', array( $this, 'edd_listen_for_agate_ipn' ) );
add_action( 'edd_verify_agate_ipn', array( $this, 'edd_process_agate_ipn' ) );
// Display errors
add_action( 'edd_after_cc_fields', array( $this, 'errors_div' ), 999 );
}
/**
* Register the payment icon
*/
public function pw_edd_payment_icon($icons) {
$icons['http://gateway.agate.services/image/icon.png'] = 'Agate';
return $icons;
}
/**
* Internationalization
*
* @since 1.0.0
* @access public
* @static
* @return void
*/
public static function textdomain() {
// Set filter for language directory
$lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
$lang_dir = apply_filters( 'edd_agate_lang_dir', $lang_dir );
// Load translations
load_plugin_textdomain( 'edd-agate', false, $lang_dir );
}
/**
* Add settings
*
* @since 1.0.0
* @access public
* @param array $settings The existing plugin settings
* @return array
*/
public function settings( $settings ) {
$agate_settings = array(
array(
'id' => 'edd_agate_settings',
'name' => '<strong>' . __( 'Agate Settings', 'edd-agate' ) . '</strong>',
'desc' => __( 'Configure your Agate settings', 'edd-agate' ),
'type' => 'header'
),
array(
'id' => 'edd_agate_api_key',
'name' => __( 'Signature', 'edd-agate' ),
'desc' => __( 'Enter your Agate api_key', 'edd-agate' ),
'type' => 'text'
)
);
return array_merge( $settings, $agate_settings );
}
/**
* Register our new gateway
*
* @since 1.0.0
* @access public
* @param array $gateways The current gateway list
* @return array $gateways The updated gateway list
*/
public function register_gateway( $gateways ) {
$gateways['agate'] = array(
'admin_label' => 'Agate Payments',
'checkout_label' => __( 'Agate Payments - Pay with any standard currency', 'edd-agate-gateway' )
);
return $gateways;
}
/**
* Process payment submission
*
* @since 1.0.0
* @access public
* @global array $edd_options
* @param array $purchase_data The data for a specific purchase
* @return void
*/
public function process_payment( $purchase_data ) {
global $edd_options;
// Collect payment data
$payment_data = array(
'price' => $purchase_data['price'],
'date' => $purchase_data['date'],
'user_email' => $purchase_data['user_email'],
'purchase_key' => $purchase_data['purchase_key'],
'currency' => edd_get_currency(),
'downloads' => $purchase_data['downloads'],
'user_info' => $purchase_data['user_info'],
'cart_details' => $purchase_data['cart_details'],
'gateway' => 'agate',
'status' => 'pending'
);
// Record the pending payment
$payment = edd_insert_payment( $payment_data );
// Were there any errors?
if( !$payment ) {
// Record the error
edd_record_gateway_error( __( 'Payment Error', 'edd-agate' ), sprintf( __( 'Payment creation failed before sending buyer to Agate. Payment data: %s', 'edd-agate' ), json_encode( $payment_data ) ), $payment );
edd_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['edd-gateway'] );
} else {
$redirect_url = add_query_arg( 'payment-confirmation', 'agate', get_permalink( $edd_options['success_page'] ) );
$order_total = round( $purchase_data['price'] - $purchase_data['tax'], 2 );
$baseUri = "http://gateway.agate.services/" ;
$convertUrl = "http://gateway.agate.services/convert/";
$api_key = $edd_options['edd_agate_api_key'];
$currencySymbol = edd_get_currency();
$amount_iUSD = convertCurToIUSD($convertUrl, $order_total, $api_key, $currencySymbol);
}
// Redirect to Agate
redirectPayment($baseUri, $amount_iUSD, $order_total, $currencySymbol, $api_key, $redirect_url);
exit;
}
/**
* Listens for a Agate IPN requests and then sends to the processing function
*
* @since 1.0.0
* @access public
* @global array $edd_options
* @return void
*/
public function edd_listen_for_agate_ipn() {
global $edd_options;
if ( isset( $_GET['edd-listener'] ) && $_GET['edd-listener'] == 'AGATEIPN' ) {
do_action( 'edd_verify_agate_ipn' );
}
}
}
function convertCurToIUSD($url, $amount, $api_key, $currencySymbol) {
error_log("Entered into Convert CAmount");
error_log($url.'?api_key='.$api_key.'¤cy='.$currencySymbol.'&amount='. $amount);
$ch = curl_init($url.'?api_key='.$api_key.'¤cy='.$currencySymbol.'&amount='. $amount);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
$result = curl_exec($ch);
$data = json_decode( $result , true);
error_log('Response =>'. var_export($data, TRUE));
// Return the equivalent bitcoin value acquired from Agate server.
return (float) $data["result"];
}
function redirectPayment($baseUri, $amount_iUSD, $amount, $currencySymbol, $api_key, $redirect_url) {
error_log("Entered into auto submit-form");
error_log("Url ".$baseUri . "?api_key=" . $api_key);
// Using Auto-submit form to redirect user
echo "<form id='form' method='post' action='". $baseUri . "?api_key=" . $api_key."'>".
"<input type='hidden' autocomplete='off' name='amount' value='".$amount."'/>".
"<input type='hidden' autocomplete='off' name='amount_iUSD' value='".$amount_iUSD."'/>".
"<input type='hidden' autocomplete='off' name='callBackUrl' value='".$redirect_url."'/>".
"<input type='hidden' autocomplete='off' name='api_key' value='".$api_key."'/>".
"<input type='hidden' autocomplete='off' name='cur' value='".$currencySymbol."'/>".
"</form>".
"<script type='text/javascript'>".
"document.getElementById('form').submit();".
"</script>";
}
}
function edd_agate_gateway_load() {
$edd_agate = new EDD_Agate();
}
add_action( 'plugins_loaded', 'edd_agate_gateway_load' );