-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaymentGateway.php
288 lines (242 loc) · 11.7 KB
/
PaymentGateway.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
<?php if ( ! defined( 'ABSPATH' ) ) exit;
if( ! class_exists( 'NF_Abstracts_PaymentGateway' ) ) return;
/**
* The PayPal Express payment gateway for the Collect Payment action.
*/
class NF_PayPalExpress_PaymentGateway extends NF_Abstracts_PaymentGateway
{
protected $_slug = 'paypal-express';
public function __construct()
{
parent::__construct();
$this->_name = __( 'PayPal Express', 'ninja-forms-paypal-express' );
add_action( 'ninja_forms_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
/*
|--------------------------------------------------------------------------
| Product Description
|--------------------------------------------------------------------------
*/
$this->_settings[ 'ppe_details' ] = array(
'name' => 'ppe_details',
'type' => 'textarea',
'placeholder' => '',
'value' => '',
'label' => __( 'Details', 'ninja-forms' ),
'width' => 'full',
'group' => 'advanced',
'deps' => array(
'payment_gateways' => $this->_slug
),
'help' => __( 'Extra information associated with the payment, such as shipping address, email, etc. This will be saved as Transaction Data in your PayPal Account.', 'ninja-forms-paypal-express' ),
'use_merge_tags' => TRUE
);
$this->_settings[ 'ppe_description' ] = array(
'name' => 'ppe_description',
'type' => 'textbox',
'label' => __( 'Note to Buyer', 'ninja-forms' ),
'width' => 'full',
'group' => 'advanced',
'deps' => array(
'payment_gateways' => $this->_slug
),
'help' => sprintf( __( 'A note from the merchant to the buyer that will be displayed in the PayPal checkout window. Limit %s characters', 'ninja-forms-paypal-express' ), '165' ),
'use_merge_tags' => TRUE
);
$this->_settings[ 'ppe_sandbox' ] = array(
'name' => 'ppe_sandbox',
'type' => 'toggle',
'label' => __( 'Sandbox Mode', 'ninja-forms' ),
'width' => 'full',
'group' => 'advanced',
'deps' => array(
'payment_gateways' => $this->_slug
),
'help' => __( 'Use PayPal Express sandbox credentials to test transaction.', 'ninja-forms-paypal-express' ),
);
$this->_settings[ 'ppe_debug' ] = array(
'name' => 'ppe_debug',
'type' => 'toggle',
'label' => __( 'Debug Mode', 'ninja-forms' ),
'width' => 'full',
'group' => 'advanced',
'deps' => array(
'payment_gateways' => $this->_slug,
'ppe_sandbox' => 1
),
'help' => __( 'Displays the response from PayPal. Does NOT complete the transaction.', 'ninja-forms-paypal-express' ),
);
}
/**
* Process
*
* The main function for processing submission data.
*
* @param array $action_settings Action specific settings.
* @param int $form_id The ID of the submitted form.
* @param array $data Form submission data.
* @return array $data Modified submission data.
*/
public function process( $action_settings, $form_id, $data )
{
if( 1 != $action_settings[ 'ppe_sandbox' ] ) $action_settings[ 'ppe_sandbox' ] = FALSE; // `1` == TRUE
//Add check to ensure the payment total isn't null and is greater than zero
if( $action_settings[ 'payment_total' ] <= 0 || NULL == $action_settings[ 'payment_total' ] ) return $data;
$api = NF_PayPalExpress()->api( $action_settings[ 'ppe_sandbox' ] );
$payment_total = number_format( $action_settings[ 'payment_total' ], 2, '.', ',' );
// If we've come back from PayPal...
if( isset( $data[ 'resume' ] ) ){
// If the response indicates a canceled payment...
if ( 'cancel' == $data[ 'resume' ][ 'nfpe_checkout' ] ) {
// Exit early.
$data[ 'errors' ][ 'form' ][ 'paypal_express' ] = __( 'PayPal authorization was cancelled. Please try again.', 'ninja-forms-paypal-express' );
return $data;
}
$currency = $this->get_currency( $data );
$token = $data[ 'resume' ][ 'token' ];
$payer_id = $data[ 'resume' ][ 'PayerID' ];
$response = $api->complete_checkout( $payment_total, $currency, $token, $payer_id );
if ( $this->is_success( $response ) ) { //Request successfully
global $wpdb;
$sql = "SELECT `post_id` FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = 'paypal_transaction_id' AND `meta_value` = '" . $response[ 'PAYMENTINFO_0_TRANSACTIONID' ] . "'";
$result = $wpdb->get_results( $sql, 'ARRAY_N' );
// If this transaction ID has already been used...
if ( ! empty( $result ) ) {
// Error the user out.
$data[ 'errors' ][ 'form' ][ 'paypal_express' ] = __( 'PayPal encountered an error in processing your transaction. Please try again.', 'ninja-forms-paypal-express' );
do_action( 'ninja_forms_checkout_failure', $response );
do_action( 'ninja_forms_paypal_express_checkout_failure', $response );
} else {
$this->update_submission( $this->get_sub_id($data), array(
'paypal_status' => $this->get_status( 'success' ),
'paypal_transaction_id' => $response[ 'PAYMENTINFO_0_TRANSACTIONID' ]
));
Ninja_Forms()->merge_tags[ 'paypal_express' ]->set_transaction_id( $response[ 'PAYMENTINFO_0_TRANSACTIONID' ] );
do_action( 'ninja_forms_checkout_success', $response );
do_action( 'ninja_forms_paypal_express_checkout_success', $response );
}
} else {
$data[ 'errors' ][ 'form' ][ 'paypal_express' ] = __( 'PayPal encountered an error in processing your transaction. Please try again.', 'ninja-forms-paypal-express' );
do_action( 'ninja_forms_checkout_failure', $response );
do_action( 'ninja_forms_paypal_express_checkout_failure', $response );
}
$data[ 'actions' ][ 'paypal_express' ][ 'ppe_debug' ] = $action_settings[ 'ppe_debug' ];
if( isset( $action_settings[ 'ppe_debug' ] ) && 1 == $action_settings[ 'ppe_debug' ] ){ // `1` == TRUE
$data[ 'actions' ][ 'paypal_express' ][ 'debug' ] = $response;
$debug_message = '<dl>';
foreach( $response as $key => $value ){
$debug_message .= "<dt>$key</dt><dd>$value</dd>";
}
$debug_message .= '</dl>';
$data[ 'debug' ][ 'form' ][ 'paypal_express' ] = $debug_message;
}
// Hide loading animation.
$data[ 'actions' ][ 'success_message' ] .= '<style> .nf-ppe-spinner { display: none !important; } </style>';
return $data;
}
$currency = $this->get_currency( $data );
// If our currency is on PayPal's blacklist...
$blacklist = array(
'ZAR',
'INR',
);
if ( in_array( $currency, $blacklist ) ) {
$data[ 'errors' ][ 'form' ][ 'paypal_express' ] = sprintf( __( 'PayPal does not support the selected currency. For more information on supported currencies, please consult the following list. %s', 'ninja-forms-paypal-express' ), '<a href="https://developer.paypal.com/docs/classic/api/currency_codes/" target="_blank" >https://developer.paypal.com/docs/classic/api/currency_codes/</a>' );
return $data;
}
$description = wp_strip_all_tags( $action_settings[ 'ppe_description' ] );
$details = wp_strip_all_tags( $action_settings[ 'ppe_details' ] );
$response = $debug[ 'response' ][] = $api->checkout( $payment_total, $currency, $form_id, $description, $details );
if ( $this->is_success( $response ) ) { //Request successfully
$token = $debug[ 'checkout_token' ][] = $response['TOKEN'];
// Set Checkout (Redirect) URL
$data[ 'halt' ] = TRUE;
$data[ 'actions' ][ 'redirect' ] = $api->get_checkout_url( $token );
$this->update_submission( $this->get_sub_id( $data ), array(
'paypal_status' => __( 'Pending', 'ninja-forms-paypal-express' ),
'paypal_total' => $payment_total
) );
ob_start();
NF_PayPalExpress::template( 'spinner.html' );
$spinner = ob_get_clean();
if( ! isset( $data['actions']['success_message'] ) ){
$data['actions']['success_message'] = $spinner;
} else {
$data['actions']['success_message'] .= $spinner;
}
} else {
$data[ 'errors' ][ 'form' ][ 'paypal_express' ] = __( 'PayPal encountered an error in processing your transaction. Please try again.', 'ninja-forms-paypal-express' );
}
return $data;
}
public function enqueue_scripts( $data )
{
// TODO: Check `$data[ 'form_id' ]`
wp_enqueue_script('nf-paypal-express-debug', NF_PayPalExpress::$url . 'assets/js/debug.js', array( 'nf-front-end' ) );
wp_enqueue_script('nf-paypal-express-response', NF_PayPalExpress::$url . 'assets/js/error-handler.js', array( 'nf-front-end' ) );
}
/**
* Is Success
*
* @param array $response
* @return bool
*/
private function is_success( $response )
{
if( ! is_array( $response ) ) return FALSE;
if( ! in_array( $response[ 'ACK' ], array( 'Success', 'SuccessWithWarning' ) ) ) return FALSE;
return TRUE;
}
/**
* Update Submission
*
* @param int $sub_id
* @param array $data
*/
private function update_submission( $sub_id, $data = array() )
{
if( ! $sub_id ) return;
$sub = Ninja_Forms()->form()->sub( $sub_id )->get();
foreach( $data as $key => $value ){
$sub->update_extra_value( $key, $value );
}
$sub->save();
}
/**
* Get Submission ID
*
* Get the submission id from the submission data, if it exists.
*
* @param array $data
* @return int|bool
*/
private function get_sub_id( $data )
{
if( isset( $data[ 'actions' ][ 'save' ][ 'sub_id' ] ) ){
return $data[ 'actions' ][ 'save' ][ 'sub_id' ];
}
return FALSE;
}
private function get_status( $status )
{
$lookup = array(
'pending' => __( 'Pending', 'ninja-forms-paypal-express' ),
'cancel' => __( 'Cancelled', 'ninja-forms-paypal-express' ),
'success' => __( 'Completed', 'ninja-forms-paypal-express' ),
);
return ( isset( $lookup[ $status ] ) ) ? $lookup[ $status ] : $lookup[ 'pending' ];
}
private function get_currency( $form_data )
{
/**
* Currency Setting Priority
*
* 3. Paypal Express Currency Setting (deprecated)
* 2. Ninja Forms Currency Setting
* 1. Form Currency Setting (default)
*/
$ppe_currency = Ninja_Forms()->get_setting( 'ppe_currency', 'USD' );
$plugin_currency = Ninja_Forms()->get_setting( 'currency', $ppe_currency );
$form_currency = ( isset( $form_data[ 'settings' ][ 'currency' ] ) && $form_data[ 'settings' ][ 'currency' ] !== '' ) ? $form_data[ 'settings' ][ 'currency' ] : $plugin_currency;
return $form_currency;
}
} // END CLASS NF_PayPalExpress_PaymentGateway