1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PayPal Standard Payment Gateway. |
4
|
|
|
* |
5
|
|
|
* Provides a PayPal Standard Payment Gateway. |
6
|
|
|
* |
7
|
|
|
* @class WC_Gateway_Paypal |
8
|
|
|
* @extends WC_Payment_Gateway |
9
|
|
|
* @version 2.3.0 |
10
|
|
|
* @package WooCommerce/Classes/Payment |
11
|
|
|
* @author WooThemes |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
15
|
|
|
exit; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* WC_Gateway_Paypal Class. |
20
|
|
|
*/ |
21
|
|
|
class WC_Gateway_Paypal extends WC_Payment_Gateway { |
22
|
|
|
|
23
|
|
|
/** @var bool Whether or not logging is enabled */ |
24
|
|
|
public static $log_enabled = false; |
25
|
|
|
|
26
|
|
|
/** @var WC_Logger Logger instance */ |
27
|
|
|
public static $log = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor for the gateway. |
31
|
|
|
*/ |
32
|
|
|
public function __construct() { |
33
|
|
|
$this->id = 'paypal'; |
34
|
|
|
$this->has_fields = false; |
35
|
|
|
$this->order_button_text = __( 'Proceed to PayPal', 'woocommerce' ); |
36
|
|
|
$this->method_title = __( 'PayPal', 'woocommerce' ); |
37
|
|
|
$this->method_description = sprintf( __( 'PayPal standard sends customers to PayPal to enter their payment information. PayPal IPN requires fsockopen/cURL support to update order statuses after payment. Check the %ssystem status%s page for more details.', 'woocommerce' ), '<a href="' . admin_url( 'admin.php?page=wc-status' ) . '">', '</a>' ); |
38
|
|
|
$this->supports = array( |
39
|
|
|
'products', |
40
|
|
|
'refunds' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
// Load the settings. |
44
|
|
|
$this->init_form_fields(); |
45
|
|
|
$this->init_settings(); |
46
|
|
|
|
47
|
|
|
// Define user set variables. |
48
|
|
|
$this->title = $this->get_option( 'title' ); |
49
|
|
|
$this->description = $this->get_option( 'description' ); |
50
|
|
|
$this->testmode = 'yes' === $this->get_option( 'testmode', 'no' ); |
51
|
|
|
$this->debug = 'yes' === $this->get_option( 'debug', 'no' ); |
52
|
|
|
$this->email = $this->get_option( 'email' ); |
53
|
|
|
$this->receiver_email = $this->get_option( 'receiver_email', $this->email ); |
54
|
|
|
$this->identity_token = $this->get_option( 'identity_token' ); |
55
|
|
|
|
56
|
|
|
self::$log_enabled = $this->debug; |
57
|
|
|
|
58
|
|
|
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
59
|
|
|
add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'capture_payment' ) ); |
60
|
|
|
add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'capture_payment' ) ); |
61
|
|
|
|
62
|
|
|
if ( ! $this->is_valid_for_use() ) { |
63
|
|
|
$this->enabled = 'no'; |
64
|
|
|
} else { |
65
|
|
|
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-ipn-handler.php' ); |
66
|
|
|
new WC_Gateway_Paypal_IPN_Handler( $this->testmode, $this->receiver_email ); |
67
|
|
|
|
68
|
|
|
if ( $this->identity_token ) { |
69
|
|
|
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-pdt-handler.php' ); |
70
|
|
|
new WC_Gateway_Paypal_PDT_Handler( $this->testmode, $this->identity_token ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Logging method. |
77
|
|
|
* @param string $message |
78
|
|
|
*/ |
79
|
|
|
public static function log( $message ) { |
80
|
|
|
if ( self::$log_enabled ) { |
81
|
|
|
if ( empty( self::$log ) ) { |
82
|
|
|
self::$log = new WC_Logger(); |
83
|
|
|
} |
84
|
|
|
self::$log->add( 'paypal', $message ); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get gateway icon. |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function get_icon() { |
93
|
|
|
$icon_html = ''; |
94
|
|
|
$icon = (array) $this->get_icon_image( WC()->countries->get_base_country() ); |
95
|
|
|
|
96
|
|
|
foreach ( $icon as $i ) { |
97
|
|
|
$icon_html .= '<img src="' . esc_attr( $i ) . '" alt="' . esc_attr__( 'PayPal Acceptance Mark', 'woocommerce' ) . '" />'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$icon_html .= sprintf( '<a href="%1$s" class="about_paypal" onclick="javascript:window.open(\'%1$s\',\'WIPaypal\',\'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=1060, height=700\'); return false;" title="' . esc_attr__( 'What is PayPal?', 'woocommerce' ) . '">' . esc_attr__( 'What is PayPal?', 'woocommerce' ) . '</a>', esc_url( $this->get_icon_url( WC()->countries->get_base_country() ) ) ); |
101
|
|
|
|
102
|
|
|
return apply_filters( 'woocommerce_gateway_icon', $icon_html, $this->id ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the link for an icon based on country. |
107
|
|
|
* @param string $country |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function get_icon_url( $country ) { |
111
|
|
|
$url = 'https://www.paypal.com/' . strtolower( $country ); |
112
|
|
|
$home_counties = array( 'BE', 'CZ', 'DK', 'HU', 'IT', 'JP', 'NL', 'NO', 'ES', 'SE', 'TR'); |
113
|
|
|
$countries = array( 'DZ', 'AU', 'BH', 'BQ', 'BW', 'CA', 'CN', 'CW', 'FI', 'FR', 'DE', 'GR', 'HK', 'IN', 'ID', 'JO', 'KE', 'KW', 'LU', 'MY', 'MA', 'OM', 'PH', 'PL', 'PT', 'QA', 'IE', 'RU', 'BL', 'SX', 'MF', 'SA', 'SG', 'SK', 'KR', 'SS', 'TW', 'TH', 'AE', 'GB', 'US', 'VN' ); |
114
|
|
|
|
115
|
|
|
if ( in_array( $country, $home_counties ) ) { |
116
|
|
|
return $url . '/webapps/mpp/home'; |
117
|
|
|
} else if ( in_array( $country, $countries ) ) { |
118
|
|
|
return $url . '/webapps/mpp/paypal-popup'; |
119
|
|
|
} else { |
120
|
|
|
return $url . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside'; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get PayPal images for a country. |
126
|
|
|
* @param string $country |
127
|
|
|
* @return array of image URLs |
128
|
|
|
*/ |
129
|
|
|
protected function get_icon_image( $country ) { |
130
|
|
|
switch ( $country ) { |
131
|
|
|
case 'US' : |
132
|
|
|
case 'NZ' : |
133
|
|
|
case 'CZ' : |
134
|
|
|
case 'HU' : |
135
|
|
|
case 'MY' : |
136
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg'; |
137
|
|
|
break; |
138
|
|
|
case 'TR' : |
139
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg'; |
140
|
|
|
break; |
141
|
|
|
case 'GB' : |
142
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png'; |
143
|
|
|
break; |
144
|
|
|
case 'MX' : |
145
|
|
|
$icon = array( |
146
|
|
|
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png', |
147
|
|
|
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif' |
148
|
|
|
); |
149
|
|
|
break; |
150
|
|
|
case 'FR' : |
151
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg'; |
152
|
|
|
break; |
153
|
|
|
case 'AU' : |
154
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg'; |
155
|
|
|
break; |
156
|
|
|
case 'DK' : |
157
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg'; |
158
|
|
|
break; |
159
|
|
|
case 'RU' : |
160
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg'; |
161
|
|
|
break; |
162
|
|
|
case 'NO' : |
163
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg'; |
164
|
|
|
break; |
165
|
|
|
case 'CA' : |
166
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg'; |
167
|
|
|
break; |
168
|
|
|
case 'HK' : |
169
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg'; |
170
|
|
|
break; |
171
|
|
|
case 'SG' : |
172
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg'; |
173
|
|
|
break; |
174
|
|
|
case 'TW' : |
175
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg'; |
176
|
|
|
break; |
177
|
|
|
case 'TH' : |
178
|
|
|
$icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg'; |
179
|
|
|
break; |
180
|
|
|
case 'JP' : |
181
|
|
|
$icon = 'https://www.paypal.com/ja_JP/JP/i/bnr/horizontal_solution_4_jcb.gif'; |
182
|
|
|
break; |
183
|
|
|
default : |
184
|
|
|
$icon = WC_HTTPS::force_https_url( WC()->plugin_url() . '/includes/gateways/paypal/assets/images/paypal.png' ); |
185
|
|
|
break; |
186
|
|
|
} |
187
|
|
|
return apply_filters( 'woocommerce_paypal_icon', $icon ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Check if this gateway is enabled and available in the user's country. |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
public function is_valid_for_use() { |
195
|
|
|
return in_array( get_woocommerce_currency(), apply_filters( 'woocommerce_paypal_supported_currencies', array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB' ) ) ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Admin Panel Options. |
200
|
|
|
* - Options for bits like 'title' and availability on a country-by-country basis. |
201
|
|
|
* |
202
|
|
|
* @since 1.0.0 |
203
|
|
|
*/ |
204
|
|
|
public function admin_options() { |
205
|
|
|
if ( $this->is_valid_for_use() ) { |
206
|
|
|
parent::admin_options(); |
207
|
|
|
} else { |
208
|
|
|
?> |
209
|
|
|
<div class="inline error"><p><strong><?php _e( 'Gateway Disabled', 'woocommerce' ); ?></strong>: <?php _e( 'PayPal does not support your store currency.', 'woocommerce' ); ?></p></div> |
210
|
|
|
<?php |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Initialise Gateway Settings Form Fields. |
216
|
|
|
*/ |
217
|
|
|
public function init_form_fields() { |
218
|
|
|
$this->form_fields = include( 'includes/settings-paypal.php' ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the transaction URL. |
223
|
|
|
* @param WC_Order $order |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function get_transaction_url( $order ) { |
227
|
|
|
if ( $this->testmode ) { |
228
|
|
|
$this->view_transaction_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s'; |
229
|
|
|
} else { |
230
|
|
|
$this->view_transaction_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s'; |
231
|
|
|
} |
232
|
|
|
return parent::get_transaction_url( $order ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Process the payment and return the result. |
237
|
|
|
* @param int $order_id |
238
|
|
|
* @return array |
239
|
|
|
*/ |
240
|
|
|
public function process_payment( $order_id ) { |
241
|
|
|
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-request.php' ); |
242
|
|
|
|
243
|
|
|
$order = wc_get_order( $order_id ); |
244
|
|
|
$paypal_request = new WC_Gateway_Paypal_Request( $this ); |
245
|
|
|
|
246
|
|
|
return array( |
247
|
|
|
'result' => 'success', |
248
|
|
|
'redirect' => $paypal_request->get_request_url( $order, $this->testmode ) |
|
|
|
|
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Can the order be refunded via PayPal? |
254
|
|
|
* @param WC_Order $order |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
public function can_refund_order( $order ) { |
258
|
|
|
return $order && $order->get_transaction_id(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Init the API class and set the username/password etc. |
263
|
|
|
*/ |
264
|
|
|
protected function init_api() { |
265
|
|
|
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-api-handler.php' ); |
266
|
|
|
|
267
|
|
|
WC_Gateway_Paypal_API_Handler::$api_username = $this->get_option( 'api_username' ); |
268
|
|
|
WC_Gateway_Paypal_API_Handler::$api_password = $this->get_option( 'api_password' ); |
269
|
|
|
WC_Gateway_Paypal_API_Handler::$api_signature = $this->get_option( 'api_signature' ); |
270
|
|
|
WC_Gateway_Paypal_API_Handler::$sandbox = $this->testmode; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Process a refund if supported. |
275
|
|
|
* @param int $order_id |
276
|
|
|
* @param float $amount |
277
|
|
|
* @param string $reason |
278
|
|
|
* @return bool True or false based on success, or a WP_Error object |
279
|
|
|
*/ |
280
|
|
|
public function process_refund( $order_id, $amount = null, $reason = '' ) { |
281
|
|
|
$order = wc_get_order( $order_id ); |
282
|
|
|
|
283
|
|
|
if ( ! $this->can_refund_order( $order ) ) { |
|
|
|
|
284
|
|
|
$this->log( 'Refund Failed: No transaction ID' ); |
285
|
|
|
return new WP_Error( 'error', __( 'Refund Failed: No transaction ID', 'woocommerce' ) ); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->init_api(); |
289
|
|
|
|
290
|
|
|
$result = WC_Gateway_Paypal_API_Handler::refund_transaction( $order, $amount, $reason ); |
|
|
|
|
291
|
|
|
|
292
|
|
|
if ( is_wp_error( $result ) ) { |
293
|
|
|
$this->log( 'Refund Failed: ' . $result->get_error_message() ); |
294
|
|
|
return new WP_Error( 'error', $result->get_error_message() ); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$this->log( 'Refund Result: ' . print_r( $result, true ) ); |
298
|
|
|
|
299
|
|
|
switch ( strtolower( $result->ACK ) ) { |
300
|
|
|
case 'success': |
301
|
|
|
case 'successwithwarning': |
302
|
|
|
$order->add_order_note( sprintf( __( 'Refunded %s - Refund ID: %s', 'woocommerce' ), $result->GROSSREFUNDAMT, $result->REFUNDTRANSACTIONID ) ); |
303
|
|
|
return true; |
304
|
|
|
break; |
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return isset( $result->L_LONGMESSAGE0 ) ? new WP_Error( 'error', $result->L_LONGMESSAGE0 ) : false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Capture payment when the order is changed from on-hold to complete or processing |
312
|
|
|
* |
313
|
|
|
* @param int $order_id |
314
|
|
|
*/ |
315
|
|
|
public function capture_payment( $order_id ) { |
316
|
|
|
$order = wc_get_order( $order_id ); |
317
|
|
|
|
318
|
|
|
if ( 'paypal' === $order->payment_method && 'pending' === get_post_meta( $order->id, '_paypal_status', true ) && $order->get_transaction_id() ) { |
319
|
|
|
$this->init_api(); |
320
|
|
|
$result = WC_Gateway_Paypal_API_Handler::do_capture( $order ); |
|
|
|
|
321
|
|
|
|
322
|
|
|
if ( is_wp_error( $result ) ) { |
323
|
|
|
$this->log( 'Capture Failed: ' . $result->get_error_message() ); |
324
|
|
|
$order->add_order_note( sprintf( __( 'Payment could not captured: %s', 'woocommerce' ), $result->get_error_message() ) ); |
325
|
|
|
return; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$this->log( 'Capture Result: ' . print_r( $result, true ) ); |
329
|
|
|
|
330
|
|
|
if ( ! empty( $result->PAYMENTSTATUS ) ) { |
331
|
|
|
switch ( $result->PAYMENTSTATUS ) { |
332
|
|
|
case 'Completed' : |
333
|
|
|
$order->add_order_note( sprintf( __( 'Payment of %s was captured - Auth ID: %s, Transaction ID: %s', 'woocommerce' ), $result->AMT, $result->AUTHORIZATIONID, $result->TRANSACTIONID ) ); |
334
|
|
|
update_post_meta( $order->id, '_paypal_status', $result->PAYMENTSTATUS ); |
335
|
|
|
update_post_meta( $order->id, '_transaction_id', $result->TRANSACTIONID ); |
336
|
|
|
break; |
337
|
|
|
default : |
338
|
|
|
$order->add_order_note( sprintf( __( 'Payment could not captured - Auth ID: %s, Status: %s', 'woocommerce' ), $result->AUTHORIZATIONID, $result->PAYMENTSTATUS ) ); |
339
|
|
|
break; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: