forked from gravityplus/gravity-forms-braintree
-
Notifications
You must be signed in to change notification settings - Fork 4
/
angelleye-gravity-forms-braintree.php
339 lines (289 loc) · 15.2 KB
/
angelleye-gravity-forms-braintree.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Plugin Name: Gravity Forms Braintree Payments
* Plugin URI: https://angelleye.com/products/gravity-forms-braintree-payments
* Description: Allow your customers to purchase goods and services through Gravity Forms via Braintree Payments.
* Author: Angell EYE
* Version: 5.1.2
* Author URI: https://angelleye.com
* Text Domain: angelleye-gravity-forms-braintree
*************
* Attribution
*************
* This plugin is a derivative work of the code from Plugify,
* which is licensed with GPLv2.
*/
// Ensure WordPress has been bootstrapped
if( !defined( 'ABSPATH' ) ) {
exit;
}
if (!defined('AEU_ZIP_URL')) {
define('AEU_ZIP_URL', 'https://updates.angelleye.com/ae-updater/angelleye-updater/angelleye-updater.zip');
}
if (!defined('GRAVITY_FORMS_BRAINTREE_ASSET_URL')) {
define('GRAVITY_FORMS_BRAINTREE_ASSET_URL', plugin_dir_url(__FILE__));
}
if (!defined('GRAVITY_FORMS_BRAINTREE_DIR_PATH')) {
define('GRAVITY_FORMS_BRAINTREE_DIR_PATH', plugin_dir_path(__FILE__));
}
if (!defined('PAYPAL_FOR_WOOCOMMERCE_PUSH_NOTIFICATION_WEB_URL')) {
define('PAYPAL_FOR_WOOCOMMERCE_PUSH_NOTIFICATION_WEB_URL', 'https://www.angelleye.com/');
}
require_once dirname(__FILE__) . '/includes/angelleye-gravity-braintree-activator.php';
require_once dirname(__FILE__) . '/includes/angelleye-plugin-requirement-checker.php';
class AngelleyeGravityFormsBraintree{
protected static $instance = null;
public static $plugin_base_file;
public static $version = '5.1.2';
public static function getInstance()
{
self::$plugin_base_file = plugin_basename(__FILE__);
if(self::$instance==null)
self::$instance = new AngelleyeGravityFormsBraintree();
return self::$instance;
}
public function __construct()
{
register_activation_hook( __FILE__, array(AngelleyeGravityBraintreeActivator::class,"InstallDb") );
register_deactivation_hook( __FILE__, array(AngelleyeGravityBraintreeActivator::class,"DeactivatePlugin") );
register_uninstall_hook( __FILE__, array(AngelleyeGravityBraintreeActivator::class,'Uninstall'));
add_action('plugins_loaded', [$this, 'requirementCheck']);
add_action( 'wp_enqueue_scripts', [$this, 'enqueue_scripts']);
add_action('wp_ajax_gform_payment_preview_html', [ $this, 'gform_payment_preview_html']);
add_action('wp_ajax_nopriv_gform_payment_preview_html', [ $this, 'gform_payment_preview_html']);
add_action('gform_entry_field_value', [ $this, 'gform_entry_field_value'], 10, 4);
add_action( 'gform_settings_save_button', [$this, 'gform_settings_save_button']);
}
public function enqueue_scripts() {
wp_enqueue_style('gravity-forms-braintree', GRAVITY_FORMS_BRAINTREE_ASSET_URL . 'assets/css/gravity-forms-braintree-public.css');
wp_register_script('braintreegateway-dropin', "https://js.braintreegateway.com/web/dropin/1.42.0/js/dropin.min.js");
wp_enqueue_script('braintreegateway-dropin');
}
public function requirementCheck() {
$checker = new Angelleye_Plugin_Requirement_Checker('Gravity Forms Braintree Payments', self::$version, self::$plugin_base_file);
$checker->setPHP('7.2');
$checker->setRequiredClasses(['GFForms' => 'The Gravity Forms plugin is required in order to run Gravity Forms Braintree Payments.']);
$checker->setRequiredExtensions(['xmlwriter', 'openssl', 'dom', 'hash', 'curl']);
$checker->setRequiredPlugins(['gravityforms/gravityforms.php'=>['min_version'=>'2.4', 'install_link'=>'https://rocketgenius.pxf.io/c/1331556/445235/7938', 'name'=>'Gravity Forms']]);
//$checker->setDeactivatePlugins([self::$plugin_base_file]);
if($checker->check()===true) {
$this->init();
}
}
public function init()
{
$path = trailingslashit( dirname( __FILE__ ) );
// Ensure Gravity Forms (payment addon framework) is installed and good to go
if( is_callable( array( 'GFForms', 'include_payment_addon_framework' ) ) ) {
// Bootstrap payment addon framework
GFForms::include_payment_addon_framework();
GFForms::include_addon_framework();
// Require Braintree Payments core
if(!class_exists('Braintree')) {
require_once $path . 'lib/Braintree.php';
}
// Require plugin entry point
require_once $path . 'includes/angelleye-gravity-braintree-helper.php';
require_once $path . 'lib/class.plugify-gform-braintree.php';
require_once $path . 'includes/class-angelleye-gravity-braintree-ach-field.php';
require_once $path . 'includes/class-angelleye-gravity-braintree-ach-toggle-field.php';
require_once $path . 'lib/angelleye-gravity-forms-payment-logger.php';
require_once $path . 'includes/angelleye-gravity-braintree-field-mapping.php';
require_once $path . 'includes/class-angelleye-gravity-braintree-creditcard.php';
require_once $path . 'includes/class-angelleye-gravity-braintree-reports.php';
/**
* Required functions
*/
if (!function_exists('angelleye_queue_update')) {
require_once( 'includes/angelleye-functions.php' );
}
// Fire off entry point
new Plugify_GForm_Braintree();
new AngelleyeGravityBraintreeFieldMapping();
/**
* Register the ACH form field and Payment Method toggle field
*/
GF_Fields::register( new Angelleye_Gravity_Braintree_ACH_Field() );
GF_Fields::register( new Angelleye_Gravity_Braintree_ACH_Toggle_Field() );
GF_Fields::register( new Angelleye_Gravity_Braintree_CreditCard_Field() );
AngellEYE_GForm_Braintree_Payment_Logger::instance();
}
}
public static function isBraintreeFeedActive()
{
global $wpdb;
$addon_feed_table_name = $wpdb->prefix . 'gf_addon_feed';
$is_active = $wpdb->get_var("select is_active from ".$addon_feed_table_name." where addon_slug='gravity-forms-braintree' and is_active=1");
return $is_active=='1';
}
/**
* Get Payment summary html.
*
* @return void
*/
public function gform_payment_preview_html() {
$status = false;
$preview_html = '';
$extra_fees_enable = false;
if( !empty( $_POST['nonce'] ) && wp_verify_nonce($_POST['nonce'],'preview-payment-nonce') ) {
$card_type = !empty( $_POST['card_type'] ) ? $_POST['card_type'] : '';
$form_id = !empty( $_POST['form_id'] ) ? $_POST['form_id'] : '';
$form_data = !empty( $_POST['form_data'] ) ? $_POST['form_data'] : [];
$extra_fees = angelleye_get_extra_fees( $form_id );
$extra_fees_label = !empty( $extra_fees['title'] ) ? $extra_fees['title'] : '';
if( empty( $extra_fees['is_fees_enable'] ) ) {
if( $card_type === 'ACH' ) {
wp_send_json([
'status' => true,
'extra_fees_enable' => false,
'html' => ''
]);
} else {
wp_send_json([
'status' => false,
'html' => ''
]);
}
}
$status = true;
$extra_fees_enable = true;
$product_fields = get_product_fields_by_form_id( $form_id );
$products = [];
if( !empty( $product_fields['products'] ) && is_array( $product_fields['products'] ) ) {
foreach ( $product_fields['products'] as $product_field ) {
$id = !empty( $product_field['id'] ) ? $product_field['id'] : '';
$label = !empty( $product_field['label'] ) ? $product_field['label'] : '';
$product_group = !empty( $product_field['group'] ) ? $product_field['group'] : '';
$price_id = !empty( $product_field['price_id'] ) ? $product_field['price_id'] : '';
$quantity_id = !empty( $product_field['quantity_id'] ) ? $product_field['quantity_id'] : '';
$product_price = 0;
$product_qty = 1;
if( !empty( $form_data ) && is_array( $form_data ) ) {
foreach ( $form_data as $data ) {
if( !empty( $data['name'] ) && $data['name'] == $price_id ) {
$product_price = !empty( $data['value'] ) ? get_price_without_formatter( $data['value'] ) : '';
$label = !empty( $data['value'] ) ? get_selected_product_label( $data['value'], $label ) : '';
} elseif ( !empty( $data['name'] ) && $data['name'] == $quantity_id ) {
$product_qty = !empty( $data['value'] ) ? $data['value'] : 1;
}
}
}
$products[] = [
'id' => $id,
'label' => $label,
'price' => $product_price,
'quantity' => $product_qty,
];
}
}
$cart_prices = get_gfb_prices([
'form_id' => $form_id,
'products' => $products,
'card_type' => $card_type,
]);
$extra_fee_amount = !empty( $cart_prices['extra_fee_amount'] ) ? $cart_prices['extra_fee_amount'] : '0';
ob_start();
?>
<div class="product-summary">
<h2><?php esc_html_e('Product summary','angelleye-gravity-forms-braintree'); ?></h2>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?php esc_html_e('Items','angelleye-gravity-forms-braintree'); ?></th>
<th><?php esc_html_e('Price','angelleye-gravity-forms-braintree'); ?></th>
<th><?php esc_html_e('Qty','angelleye-gravity-forms-braintree'); ?></th>
<th><?php esc_html_e('Subtotal','angelleye-gravity-forms-braintree'); ?></th>
</tr>
</thead>
<tbody>
<?php
if( !empty( $products ) && is_array( $products ) ) {
foreach ( $products as $product) {
$product_title = !empty( $product['label'] ) ? $product ['label'] : esc_html__( 'Product', 'angelleye-gravity-forms-braintree' );
$product_price = !empty( $product['price'] ) ? $product ['price'] : '';
$product_quantity = !empty( $product['quantity'] ) ? $product ['quantity'] : '';
if( !empty( $product_price ) && !empty( $product_quantity ) ) {
$subtotal = $product_price * $product_quantity;
?>
<tr>
<td><?php echo esc_html($product_title ); ?></td>
<td><?php echo get_gfb_format_price( $product_price ); ?></td>
<td><?php echo $product_quantity; ?></td>
<td><?php echo get_gfb_format_price( $subtotal ); ?></td>
</tr>
<?php
}
}
}
?>
</tbody>
<tfoot>
<tr>
<td colspan="3"><?php esc_html_e('Subtotal','angelleye-gravity-forms-braintree'); ?></td>
<td><?php echo !empty( $cart_prices['subtotal'] ) ? $cart_prices['subtotal'] : '0.00'; ?></td>
</tr>
<tr>
<td colspan="3"><?php esc_html_e( "$extra_fees_label ({$extra_fee_amount}%)",'angelleye-gravity-forms-braintree'); ?></td>
<td><?php echo !empty( $cart_prices['convenience_fee'] ) ? $cart_prices['convenience_fee'] : '0.00'; ?></td>
</tr>
<tr>
<td colspan="3"><strong><?php esc_html_e('Total','angelleye-gravity-forms-braintree'); ?></strong></td>
<td><?php echo !empty( $cart_prices['total'] ) ? $cart_prices['total'] : '0.00'; ?></td>
</tr>
</tfoot>
</table>
<div class="summary-actions">
<button type="button" class="gform_button button" name="gform_payment_cancel_<?php echo $form_id; ?>" id="gform_payment_cancel_<?php echo $form_id; ?>"><?php esc_html_e('Cancel', 'angelleye-gravity-forms-braintree'); ?></button>
<button type="button" class="gform_button button" name="gform_payment_pay_<?php echo $form_id; ?>" id="gform_payment_pay_<?php echo $form_id; ?>"><?php esc_html_e('Pay Now', 'angelleye-gravity-forms-braintree'); ?></button>
</div>
</div>
<?php
$preview_html = ob_get_contents();
ob_get_clean();
}
$response = [
'status'=> $status,
'html' => $preview_html,
'extra_fees_enable' => $extra_fees_enable
];
wp_send_json( $response);
}
/**
* Update Braintree CC entry field value.
*
* @param string $display_value Get display value
* @param array $field Get field.
* @param array $lead Get entry value
* @param array $form Get form
* @return mixed|string
*/
public function gform_entry_field_value( $display_value, $field, $lead, $form ) {
if( !empty( $display_value ) && is_array( $display_value ) && !empty( $field->type ) && $field->type === 'braintree_credit_card' ) {
$field_inputs = !empty( $field['inputs'] ) ? $field['inputs'] : '';
$value = '';
if( !empty( $field_inputs ) && is_array( $field_inputs ) ) {
foreach ( $field_inputs as $field_input ) {
$field_input_id = !empty( $field_input['id'] ) ? $field_input['id'] : '';
$field_input_label = !empty( $field_input['label'] ) ? $field_input['label'] : '';
$field_input_val = !empty( $display_value[$field_input_id] ) ? $display_value[$field_input_id] : '';
$value .= "<strong>{$field_input_label}</strong>: {$field_input_val}<br>";
}
}
$display_value = $value;
}
return $display_value;
}
/**
* Hide save changes button on braintree reports page.
*
* @param $html
* @return mixed|string
*/
public function gform_settings_save_button( $html ) {
if( !empty( $_GET['page'] ) && esc_html( $_GET['page'] ) === 'gf_settings' && !empty( $_GET['subview'] ) && esc_html( $_GET['subview'] ) === 'braintree-reports' ) {
return '';
}
return $html;
}
}
AngelleyeGravityFormsBraintree::getInstance();