|
1
|
|
|
<?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' )) { exit('NO direct script access allowed'); } |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ---------------------------------------------- |
|
5
|
|
|
* |
|
6
|
|
|
* Class EEG_Paypal_Express |
|
7
|
|
|
* |
|
8
|
|
|
* @package Event Espresso |
|
9
|
|
|
* @subpackage eea-paypal-express |
|
10
|
|
|
* @author Event Espresso |
|
11
|
|
|
* @version $VID:$ |
|
12
|
|
|
* |
|
13
|
|
|
* ---------------------------------------------- |
|
14
|
|
|
*/ |
|
15
|
|
|
class EEG_Paypal_Express extends EE_Offsite_Gateway { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Merchant API Username. |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $_api_username = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Merchant API Password. |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $_api_password = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* API Signature. |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_api_signature = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Business/personal logo. |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $_image_url = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* All the currencies supported by this gateway. |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $_currencies_supported = array( |
|
46
|
|
|
'USD', |
|
47
|
|
|
'AUD', |
|
48
|
|
|
'BRL', |
|
49
|
|
|
'CAD', |
|
50
|
|
|
'CZK', |
|
51
|
|
|
'DKK', |
|
52
|
|
|
'EUR', |
|
53
|
|
|
'HKD', |
|
54
|
|
|
'HUF', |
|
55
|
|
|
'ILS', |
|
56
|
|
|
'JPY', |
|
57
|
|
|
'MYR', |
|
58
|
|
|
'MXN', |
|
59
|
|
|
'NOK', |
|
60
|
|
|
'NZD', |
|
61
|
|
|
'PHP', |
|
62
|
|
|
'PLN', |
|
63
|
|
|
'GBP', |
|
64
|
|
|
'RUB', |
|
65
|
|
|
'SGD', |
|
66
|
|
|
'SEK', |
|
67
|
|
|
'CHF', |
|
68
|
|
|
'TWD', |
|
69
|
|
|
'THB', |
|
70
|
|
|
'TRY' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Sets the gateway URL variable based on whether debug mode is enabled or not. |
|
76
|
|
|
* |
|
77
|
|
|
* @param type $settings_array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function set_settings( $settings_array ) { |
|
80
|
|
|
parent::set_settings($settings_array); |
|
81
|
|
|
// Redirect URL. |
|
82
|
|
|
if ( $this->_debug_mode ) { |
|
83
|
|
|
$this->_base_gateway_url = 'https://api-3t.sandbox.paypal.com/nvp'; |
|
|
|
|
|
|
84
|
|
|
} else { |
|
85
|
|
|
$this->_base_gateway_url = 'https://api-3t.paypal.com/nvp'; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* |
|
92
|
|
|
* @param EEI_Payment $payment |
|
93
|
|
|
* @param type $billing_info |
|
94
|
|
|
* @param type $return_url |
|
95
|
|
|
* @param type $cancel_url |
|
96
|
|
|
*/ |
|
97
|
|
|
public function set_redirection_info( $payment, $billing_info = array(), $return_url = NULL, $notify_url = NULL, $cancel_url = NULL ) { |
|
98
|
|
View Code Duplication |
if ( ! $payment instanceof EEI_Payment ) { |
|
99
|
|
|
$payment->set_gateway_response( __( 'Error. No associated payment was found.', 'event_espresso' ) ); |
|
100
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
101
|
|
|
return $payment; |
|
102
|
|
|
} |
|
103
|
|
|
$transaction = $payment->transaction(); |
|
104
|
|
View Code Duplication |
if ( ! $transaction instanceof EEI_Transaction ) { |
|
105
|
|
|
$payment->set_gateway_response( __( 'Could not process this payment because it has no associated transaction.', 'event_espresso' ) ); |
|
106
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
107
|
|
|
return $payment; |
|
108
|
|
|
} |
|
109
|
|
|
$order_description = substr( sprintf( __('Event Registrations from %s', 'event_espresso'), get_bloginfo('name') ), 0, 127 ); |
|
110
|
|
|
$primary_registration = $transaction->primary_registration(); |
|
111
|
|
|
$primary_attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() : false; |
|
112
|
|
|
$locale = explode('-', get_bloginfo('language')); |
|
113
|
|
|
|
|
114
|
|
|
// Gather request parameters. |
|
115
|
|
|
$token_request_dtls = array( |
|
116
|
|
|
'METHOD' => 'SetExpressCheckout', |
|
117
|
|
|
'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
118
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code(), |
|
119
|
|
|
'PAYMENTREQUEST_0_DESC' => $order_description, |
|
120
|
|
|
'RETURNURL' => $return_url, |
|
121
|
|
|
'CANCELURL' => $cancel_url, |
|
122
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
123
|
|
|
'SOLUTIONTYPE' => 'Sole', // Buyer does not need to create a PayPal account to check out. This is referred to as PayPal Account Optional. |
|
124
|
|
|
'BUTTONSOURCE' => 'EventEspresso_SP',//EE will blow up if you change this |
|
125
|
|
|
'LOCALECODE' => $locale[1] // Locale of the pages displayed by PayPal during Express Checkout. |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
// Show itemized list. |
|
129
|
|
|
if ( EEH_Money::compare_floats( $payment->amount(), $transaction->total(), '==' ) ) { |
|
130
|
|
|
$item_num = 0; |
|
131
|
|
|
$itemized_sum = 0; |
|
132
|
|
|
$total_line_items = $transaction->total_line_item(); |
|
133
|
|
|
// Go through each item in the list. |
|
134
|
|
|
foreach ( $total_line_items->get_items() as $line_item ) { |
|
135
|
|
|
if ( $line_item instanceof EE_Line_Item ) { |
|
136
|
|
|
// PayPal doesn't like line items with 0.00 amount, so we may skip those. |
|
137
|
|
|
if ( EEH_Money::compare_floats( $line_item->total(), '0.00', '==' ) ) { |
|
138
|
|
|
continue; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$unit_price = $line_item->unit_price(); |
|
142
|
|
|
$line_item_quantity = $line_item->quantity(); |
|
143
|
|
|
// This is a discount. |
|
144
|
|
|
if ( $line_item->is_percent() ) { |
|
145
|
|
|
$unit_price = $line_item->total(); |
|
146
|
|
|
$line_item_quantity = 1; |
|
147
|
|
|
} |
|
148
|
|
|
// Item Name. |
|
149
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr($line_item->name(), 0, 127); |
|
150
|
|
|
// Item description. |
|
151
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr($line_item->desc(), 0, 127); |
|
152
|
|
|
// Cost of individual item. |
|
153
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency( $unit_price ); |
|
154
|
|
|
// Item Number. |
|
155
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
|
156
|
|
|
// Item quantity. |
|
157
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_QTY'.$item_num] = $line_item_quantity; |
|
158
|
|
|
// Digital item is sold. |
|
159
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_ITEMCATEGORY'.$item_num] = 'Digital'; |
|
160
|
|
|
$itemized_sum += $line_item->total(); |
|
161
|
|
|
++$item_num; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
// Item's sales S/H and tax amount. |
|
165
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_ITEMAMT'] = $total_line_items->get_items_total(); |
|
166
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_TAXAMT'] = $total_line_items->get_total_tax(); |
|
167
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
168
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
169
|
|
|
|
|
170
|
|
|
$itemized_sum_diff_from_txn_total = round( $transaction->total() - $itemized_sum - $total_line_items->get_total_tax(), 2 ); |
|
171
|
|
|
// If we were not able to recognize some item like promotion, surcharge or cancellation, |
|
172
|
|
|
// add the difference as an extra line item. |
|
173
|
|
|
if ( $itemized_sum_diff_from_txn_total != 0 ) { |
|
174
|
|
|
// Item Name. |
|
175
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_NAME'.$item_num] = substr( __( 'Other (promotion/surcharge/cancellation)', 'event_espresso' ), 0, 127 ); |
|
176
|
|
|
// Item description. |
|
177
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_DESC'.$item_num] = substr( $line_item->desc(), 0, 127 ); |
|
|
|
|
|
|
178
|
|
|
// Cost of individual item. |
|
179
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_AMT'.$item_num] = $this->format_currency( $itemized_sum_diff_from_txn_total ); |
|
180
|
|
|
// Item Number. |
|
181
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_NUMBER'.$item_num] = $item_num + 1; |
|
182
|
|
|
// Item quantity. |
|
183
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_QTY'.$item_num] = 1; |
|
184
|
|
|
++$item_num; |
|
185
|
|
|
} |
|
186
|
|
|
} else { |
|
187
|
|
|
// Just one Item. |
|
188
|
|
|
// Item Name. |
|
189
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_NAME0'] = substr( sprintf( __('Payment for %1$s', 'event_espresso'), $primary_registration->reg_code() ), 0, 127 ); |
|
190
|
|
|
// Item description. |
|
191
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_DESC0'] = substr( sprintf( __('Payment of %1$s for %2$s', 'event_espresso'), $payment->amount(), $primary_registration->reg_code() ), 0, 127 ); |
|
192
|
|
|
// Cost of individual item. |
|
193
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_AMT0'] = $this->format_currency( $payment->amount() ); |
|
194
|
|
|
// Item Number. |
|
195
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_NUMBER0'] = 1; |
|
196
|
|
|
// Item quantity. |
|
197
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_QTY0'] = 1; |
|
198
|
|
|
// Digital item is sold. |
|
199
|
|
|
$token_request_dtls['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Digital'; |
|
200
|
|
|
// Item's sales S/H and tax amount. |
|
201
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_ITEMAMT'] = $this->format_currency( $payment->amount() ); |
|
202
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_TAXAMT'] = '0'; |
|
203
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPPINGAMT'] = '0'; |
|
204
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_HANDLINGAMT'] = '0'; |
|
205
|
|
|
} |
|
206
|
|
|
// Automatically filling out shipping and contact information. |
|
207
|
|
|
if ( $primary_attendee instanceof EEI_Attendee ) { |
|
208
|
|
|
$token_request_dtls['NOSHIPPING'] = '2'; // If you do not pass the shipping address, PayPal obtains it from the buyer's account profile. |
|
209
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET'] = $primary_attendee->address(); |
|
210
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTREET2'] = $primary_attendee->address2(); |
|
211
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOCITY'] = $primary_attendee->city(); |
|
212
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOSTATE'] = $primary_attendee->state_abbrev(); |
|
213
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $primary_attendee->country_ID(); |
|
214
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOZIP'] = $primary_attendee->zip(); |
|
215
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_EMAIL'] = $primary_attendee->email(); |
|
216
|
|
|
$token_request_dtls['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $primary_attendee->phone(); |
|
217
|
|
|
} |
|
218
|
|
|
// Used a business/personal logo on the PayPal page. |
|
219
|
|
|
if ( ! empty($this->_image_url) ) { |
|
220
|
|
|
$token_request_dtls['LOGOIMG'] = $this->_image_url; |
|
221
|
|
|
} |
|
222
|
|
|
// Request PayPal token. |
|
223
|
|
|
$token_request_response = $this->_ppExpress_request( $token_request_dtls, 'Payment Token', $payment ); |
|
224
|
|
|
$token_rstatus = $this->_ppExpress_check_response( $token_request_response ); |
|
225
|
|
|
$response_args = ( isset($token_rstatus['args']) ) ? $token_rstatus['args'] : array(); |
|
226
|
|
|
if ( $token_rstatus['status'] ) { |
|
227
|
|
|
// We got the Token so we may continue with the payment and redirect the client. |
|
228
|
|
|
$payment->set_details( $response_args ); |
|
229
|
|
|
$payment->set_redirect_url( 'https://www.sandbox.paypal.com/cgi-bin/webscr?useraction=commit&cmd=_express-checkout&token=' . $response_args['TOKEN'] ); |
|
230
|
|
View Code Duplication |
} else { |
|
231
|
|
|
if ( isset($response_args['L_ERRORCODE']) ) { |
|
232
|
|
|
$payment->set_gateway_response( $response_args['L_ERRORCODE'] . '; ' . $response_args['L_SHORTMESSAGE'] ); |
|
233
|
|
|
} else { |
|
234
|
|
|
$payment->set_gateway_response( __( 'Error occurred while trying to setup the Express Checkout.', 'event_espresso' ) ); |
|
235
|
|
|
} |
|
236
|
|
|
$payment->set_details( $response_args ); |
|
237
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $payment; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* |
|
246
|
|
|
* @param array $update_info { |
|
247
|
|
|
* @type string $gateway_txn_id |
|
248
|
|
|
* @type string status an EEMI_Payment status |
|
249
|
|
|
* } |
|
250
|
|
|
* @param type $transaction |
|
251
|
|
|
* @return EEI_Payment |
|
252
|
|
|
*/ |
|
253
|
|
|
public function handle_payment_update( $update_info, $transaction ) { |
|
254
|
|
|
$payment = ( $transaction instanceof EEI_Transaction ) ? $transaction->last_payment() : null; |
|
255
|
|
|
|
|
256
|
|
|
if ( $payment instanceof EEI_Payment ) { |
|
257
|
|
|
$this->log( array( 'Return from Authorization' => $update_info ), $payment ); |
|
258
|
|
|
|
|
259
|
|
|
$payment_details = $payment->details(); |
|
260
|
|
|
$transaction = $payment->transaction(); |
|
261
|
|
View Code Duplication |
if ( ! $transaction instanceof EEI_Transaction ) { |
|
262
|
|
|
$payment->set_gateway_response( __( 'Could not process this payment because it has no associated transaction.', 'event_espresso' ) ); |
|
263
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
264
|
|
|
return $payment; |
|
265
|
|
|
} |
|
266
|
|
|
$primary_registrant = $transaction->primary_registration(); |
|
267
|
|
|
|
|
268
|
|
|
// Check if we still have the token. |
|
269
|
|
|
if ( ! isset($payment_details['TOKEN']) || empty($payment_details['TOKEN']) ) { |
|
270
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
271
|
|
|
return $payment; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$cdetails_request_dtls = array( |
|
275
|
|
|
'METHOD' => 'GetExpressCheckoutDetails', |
|
276
|
|
|
'TOKEN' => $payment_details['TOKEN'] |
|
277
|
|
|
); |
|
278
|
|
|
// Request Customer Details. |
|
279
|
|
|
$cdetails_request_response = $this->_ppExpress_request( $cdetails_request_dtls, 'Customer Details', $payment ); |
|
280
|
|
|
$cdetails_rstatus = $this->_ppExpress_check_response( $cdetails_request_response ); |
|
281
|
|
|
$cdata_response_args = ( isset($cdetails_rstatus['args']) ) ? $cdetails_rstatus['args'] : array(); |
|
282
|
|
|
if ( $cdetails_rstatus['status'] ) { |
|
283
|
|
|
// We got the PayerID so now we can Complete the transaction. |
|
284
|
|
|
$docheckout_request_dtls = array( |
|
285
|
|
|
'METHOD' => 'DoExpressCheckoutPayment', |
|
286
|
|
|
'PAYERID' => $cdata_response_args['PAYERID'], |
|
287
|
|
|
'TOKEN' => $payment_details['TOKEN'], |
|
288
|
|
|
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
|
289
|
|
|
'PAYMENTREQUEST_0_AMT' => $payment->amount(), |
|
290
|
|
|
'PAYMENTREQUEST_0_CURRENCYCODE' => $payment->currency_code() |
|
291
|
|
|
); |
|
292
|
|
|
// Payment Checkout/Capture. |
|
293
|
|
|
$docheckout_request_response = $this->_ppExpress_request( $docheckout_request_dtls, 'Do Payment', $payment ); |
|
294
|
|
|
$docheckout_rstatus = $this->_ppExpress_check_response( $docheckout_request_response ); |
|
295
|
|
|
$docheckout_response_args = ( isset($docheckout_rstatus['args']) ) ? $docheckout_rstatus['args'] : array(); |
|
296
|
|
|
if ( $docheckout_rstatus['status'] ) { |
|
297
|
|
|
// All is well, payment approved. |
|
298
|
|
|
$primary_registration_code = $primary_registrant instanceof EE_Registration ? $primary_registrant->reg_code() : ''; |
|
299
|
|
|
$payment->set_extra_accntng( $primary_registration_code ); |
|
300
|
|
|
$payment->set_amount( isset($docheckout_response_args['PAYMENTINFO_0_AMT']) ? floatval( $docheckout_response_args['PAYMENTINFO_0_AMT'] ) : 0 ); |
|
301
|
|
|
$payment->set_txn_id_chq_nmbr( isset( $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] ) ? $docheckout_response_args['PAYMENTINFO_0_TRANSACTIONID'] : null ); |
|
302
|
|
|
$payment->set_details( $cdata_response_args ); |
|
303
|
|
|
$payment->set_gateway_response( isset( $docheckout_response_args['PAYMENTINFO_0_ACK'] ) ? $docheckout_response_args['PAYMENTINFO_0_ACK'] : '' ); |
|
304
|
|
|
$payment->set_status( $this->_pay_model->approved_status() ); |
|
305
|
|
View Code Duplication |
} else { |
|
306
|
|
|
if ( isset($docheckout_response_args['L_ERRORCODE']) ) { |
|
307
|
|
|
$payment->set_gateway_response( $docheckout_response_args['L_ERRORCODE'] . '; ' . $docheckout_response_args['L_SHORTMESSAGE'] ); |
|
308
|
|
|
} else { |
|
309
|
|
|
$payment->set_gateway_response( __( 'Error occurred while trying to Capture the funds.', 'event_espresso' ) ); |
|
310
|
|
|
} |
|
311
|
|
|
$payment->set_details( $docheckout_response_args ); |
|
312
|
|
|
$payment->set_status( $this->_pay_model->declined_status() ); |
|
313
|
|
|
} |
|
314
|
|
View Code Duplication |
} else { |
|
315
|
|
|
if ( isset($cdata_response_args['L_ERRORCODE']) ) { |
|
316
|
|
|
$payment->set_gateway_response( $cdata_response_args['L_ERRORCODE'] . '; ' . $cdata_response_args['L_SHORTMESSAGE'] ); |
|
317
|
|
|
} else { |
|
318
|
|
|
$payment->set_gateway_response( __( 'Error occurred while trying to get payment Details from PayPal.', 'event_espresso' ) ); |
|
319
|
|
|
} |
|
320
|
|
|
$payment->set_details( $cdata_response_args ); |
|
321
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
322
|
|
|
} |
|
323
|
|
|
} else { |
|
324
|
|
|
$payment->set_gateway_response( __( 'Error occurred while trying to process the payment.', 'event_espresso' ) ); |
|
325
|
|
|
$payment->set_status( $this->_pay_model->failed_status() ); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return $payment; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Make the Express checkout request. |
|
334
|
|
|
* |
|
335
|
|
|
* @param array $request_params |
|
336
|
|
|
* @param string $request_text |
|
337
|
|
|
* @param EEI_Payment $payment |
|
338
|
|
|
* @return mixed |
|
339
|
|
|
*/ |
|
340
|
|
|
public function _ppExpress_request( $request_params, $request_text, $payment ) { |
|
341
|
|
|
$request_dtls = array( |
|
342
|
|
|
'VERSION' => '204.0', |
|
343
|
|
|
'USER' => urlencode( $this->_api_username ), |
|
344
|
|
|
'PWD' => urlencode( $this->_api_password ), |
|
345
|
|
|
'SIGNATURE' => urlencode( $this->_api_signature ) |
|
346
|
|
|
); |
|
347
|
|
|
$dtls = array_merge( $request_dtls, $request_params ); |
|
348
|
|
|
|
|
349
|
|
|
$this->_log_clean_request( $dtls, $payment, $request_text . ' Request' ); |
|
350
|
|
|
// Request Customer Details. |
|
351
|
|
|
$request_response = wp_remote_post( |
|
352
|
|
|
$this->_base_gateway_url, |
|
353
|
|
|
array( |
|
354
|
|
|
'method' => 'POST', |
|
355
|
|
|
'timeout' => 45, |
|
356
|
|
|
'httpversion' => '1.1', |
|
357
|
|
|
'cookies' => array(), |
|
358
|
|
|
'headers' => array(), |
|
359
|
|
|
'body' => http_build_query( $dtls ) |
|
360
|
|
|
) |
|
361
|
|
|
); |
|
362
|
|
|
// Log the response. |
|
363
|
|
|
$this->log( array( $request_text . ' Response' => $request_response), $payment ); |
|
364
|
|
|
|
|
365
|
|
|
return $request_response; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* Check the response status. |
|
371
|
|
|
* |
|
372
|
|
|
* @param mixed $request_response |
|
373
|
|
|
* @return array |
|
374
|
|
|
*/ |
|
375
|
|
|
public function _ppExpress_check_response( $request_response ) { |
|
376
|
|
|
if ( ! is_wp_error( $request_response ) && $request_response['body'] && ! empty($request_response['body'] ) ) { |
|
377
|
|
|
$response_args = array(); |
|
378
|
|
|
parse_str( urldecode($request_response['body']), $response_args ); |
|
379
|
|
|
if ( isset($response_args['ACK']) |
|
380
|
|
|
&& $response_args['ACK'] === 'Success' |
|
381
|
|
|
&& ( isset($response_args['PAYERID']) |
|
382
|
|
|
|| isset($response_args['PAYMENTINFO_0_TRANSACTIONID']) |
|
383
|
|
|
|| ( isset($response_args['PAYMENTSTATUS']) && $response_args['PAYMENTSTATUS'] === 'Completed' ) |
|
384
|
|
|
|| isset($response_args['TOKEN']) |
|
385
|
|
|
) |
|
386
|
|
|
) { |
|
387
|
|
|
// Response status OK, return response parameters for further processing. |
|
388
|
|
|
return array('status' => true, 'args' => $response_args); |
|
389
|
|
|
} else { |
|
390
|
|
|
if ( isset($response_args['ACK']) ) { |
|
391
|
|
|
$errors = $this->_get_errors($response_args); |
|
392
|
|
|
return array( 'status' => false, 'args' => $errors ); |
|
393
|
|
|
} else { |
|
394
|
|
|
return array( 'status' => false, 'args' => $request_response ); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
} else { |
|
398
|
|
|
// If we got here then there was an error in this request. |
|
399
|
|
|
return array( 'status' => false, 'args' => $request_response ); |
|
400
|
|
|
} |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Log a "Cleared" request. |
|
406
|
|
|
* |
|
407
|
|
|
* @param array $response |
|
|
|
|
|
|
408
|
|
|
* @param EEI_Payment $payment |
|
409
|
|
|
* @param string $info |
|
410
|
|
|
* @return void |
|
411
|
|
|
*/ |
|
412
|
|
|
private function _log_clean_request( $request_prms, $payment, $info ) { |
|
413
|
|
|
$cleaned_request_data = $request_prms; |
|
414
|
|
|
unset($cleaned_request_data['PWD']); |
|
415
|
|
|
unset($cleaned_request_data['USER']); |
|
416
|
|
|
unset($cleaned_request_data['SIGNATURE']); |
|
417
|
|
|
$this->log( array($info => $cleaned_request_data), $payment ); |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Get error from the response data. |
|
423
|
|
|
* |
|
424
|
|
|
* @param array $data_array |
|
425
|
|
|
* @return array |
|
426
|
|
|
*/ |
|
427
|
|
|
private function _get_errors( $data_array ) { |
|
428
|
|
|
$errors = array(); |
|
429
|
|
|
$n = 0; |
|
430
|
|
|
while ( isset($data_array['L_ERRORCODE' . $n . '']) ) { |
|
431
|
|
|
$l_error_code = isset($data_array['L_ERRORCODE' . $n . '']) ? $data_array['L_ERRORCODE' . $n . ''] : ''; |
|
432
|
|
|
$l_severity_code = isset($data_array['L_SEVERITYCODE' . $n . '']) ? $data_array['L_SEVERITYCODE' . $n . ''] : ''; |
|
433
|
|
|
$l_short_message = isset($data_array['L_SHORTMESSAGE' . $n . '']) ? $data_array['L_SHORTMESSAGE' . $n . ''] : ''; |
|
434
|
|
|
$l_long_message = isset($data_array['L_LONGMESSAGE' . $n . '']) ? $data_array['L_LONGMESSAGE' . $n . ''] : ''; |
|
435
|
|
|
|
|
436
|
|
|
if ( $n == 0 ) { |
|
437
|
|
|
$errors = array( |
|
438
|
|
|
'L_ERRORCODE' => $l_error_code, |
|
439
|
|
|
'L_SHORTMESSAGE' => $l_short_message, |
|
440
|
|
|
'L_LONGMESSAGE' => $l_long_message, |
|
441
|
|
|
'L_SEVERITYCODE' => $l_severity_code |
|
442
|
|
|
); |
|
443
|
|
|
} else { |
|
444
|
|
|
$errors['L_ERRORCODE'] .= ', ' . $l_error_code; |
|
445
|
|
|
$errors['L_SHORTMESSAGE'] .= ', ' . $l_short_message; |
|
446
|
|
|
$errors['L_LONGMESSAGE'] .= ', ' . $l_long_message; |
|
447
|
|
|
$errors['L_SEVERITYCODE'] .= ', ' . $l_severity_code; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
$n++; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
return $errors; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
} |
|
457
|
|
|
// End of file EEG_Paypal_Express.gateway.php |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: