forked from woocommerce/woocommerce-gateway-amazon-pay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-gateway-amazon-payments-advanced.php
465 lines (394 loc) · 13.1 KB
/
woocommerce-gateway-amazon-payments-advanced.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php // phpcs:ignore
/*
* Plugin Name: WooCommerce Amazon Pay
* Plugin URI: https://woocommerce.com/products/pay-with-amazon/
* Description: Amazon Pay is embedded directly into your existing web site, and all the buyer interactions with Amazon Pay and Login with Amazon take place in embedded widgets so that the buyer never leaves your site. Buyers can log in using their Amazon account, select a shipping address and payment method, and then confirm their order. Requires an Amazon Pay seller account and supports USA, UK, Germany, France, Italy, Spain, Luxembourg, the Netherlands, Sweden, Portugal, Hungary, Denmark, and Japan.
* Version: 2.0.3
* Author: WooCommerce
* Author URI: https://woocommerce.com
* Text Domain: woocommerce-gateway-amazon-payments-advanced
* Domain Path: /languages/
* Tested up to: 5.7
* WC tested up to: 5.3
* WC requires at least: 2.6
*
* Copyright: © 2021 WooCommerce
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package WC_Gateway_Amazon_Pay
*/
define( 'WC_AMAZON_PAY_VERSION', '2.0.3' ); // WRCS: DEFINED_VERSION.
define( 'WC_AMAZON_PAY_VERSION_CV1', '1.13.1' );
/**
* Amazon Pay main class
*/
class WC_Amazon_Payments_Advanced {
/**
* Plugin's version.
*
* @since 1.6.0
*
* @var string
*/
public $version;
/**
* Plugin's absolute path.
*
* @var string
*/
public $path;
/**
* Plugin's includes path.
*
* @var string
*/
public $includes_path;
/**
* Plugin's URL.
*
* @since 1.6.0
*
* @var string
*/
public $plugin_url;
/**
* Plugin basename.
*
* @since 2.0.0
*
* @var string
*/
public $plugin_basename;
/**
* Amazon Pay settings.
*
* @var array
*/
private $settings;
/**
* Amazon Pay Gateway
*
* @var WC_Gateway_Amazon_Payments_Advanced|WC_Gateway_Amazon_Payments_Advanced_Legacy
*/
private $gateway;
/**
* Amazon Pay Gateway Admin Class
*
* @var WC_Amazon_Payments_Advanced_Admin
*/
private $admin;
/**
* Amazon Pay Gateway Subscriptions Class
*
* @var WC_Gateway_Amazon_Payments_Advanced_Subscriptions
*/
private $subscriptions;
/**
* WC logger instance.
*
* @var WC_Logger
*/
private $logger;
/**
* Logger prefix for the whole transaction
*
* @var string
*/
private $logger_prefix;
/**
* Amazon Pay compat handler.
*
* @since 1.6.0
* @var WC_Amazon_Payments_Advanced_Compat
*/
private $compat;
/**
* IPN handler.
*
* @since 1.8.0
* @var WC_Amazon_Payments_Advanced_IPN_Handler
*/
public $ipn_handler;
/**
* Simple Path handler.
*
* @var WC_Amazon_Payments_Advanced_Merchant_Onboarding_Handler
*/
public $onboarding_handler;
/**
* Constructor
*/
public function __construct() {
$this->version = WC_AMAZON_PAY_VERSION;
$this->path = untrailingslashit( plugin_dir_path( __FILE__ ) );
$this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
$this->plugin_basename = plugin_basename( __FILE__ );
$this->includes_path = $this->path . '/includes/';
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-merchant-onboarding-handler.php';
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-api-abstract.php';
include_once $this->includes_path . 'legacy/class-wc-amazon-payments-advanced-api-legacy.php';
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-api.php';
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-compat.php';
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-ipn-handler-abstract.php';
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-ipn-handler.php';
include_once $this->includes_path . 'legacy/class-wc-amazon-payments-advanced-ipn-handler-legacy.php';
// On install hook.
include_once $this->includes_path . 'class-wc-amazon-payments-advanced-install.php';
register_activation_hook( __FILE__, array( 'WC_Amazon_Payments_Advanced_Install', 'install' ) );
add_action( 'woocommerce_init', array( $this, 'init' ) );
// REST API support.
add_action( 'rest_api_init', array( $this, 'rest_api_register_routes' ), 11 );
add_filter( 'woocommerce_rest_prepare_shop_order', array( $this, 'rest_api_add_amazon_ref_info' ), 10, 2 );
// IPN handler.
$this->ipn_handler = new WC_Amazon_Payments_Advanced_IPN_Handler();
new WC_Amazon_Payments_Advanced_IPN_Handler_Legacy(); // TODO: Maybe register legacy hooks differently
// Simple path registration endpoint.
$this->onboarding_handler = new WC_Amazon_Payments_Advanced_Merchant_Onboarding_Handler();
// Third party compatibilities.
$this->compat = new WC_Amazon_Payments_Advanced_Compat();
}
/**
* Init.
*
* @since 1.6.0
*/
public function init() {
$this->settings = WC_Amazon_Payments_Advanced_API::get_settings();
$this->load_plugin_textdomain();
if ( is_admin() ) {
include_once $this->includes_path . 'admin/class-wc-amazon-payments-advanced-admin.php';
$this->admin = new WC_Amazon_Payments_Advanced_Admin();
}
$this->init_gateway();
do_action( 'woocommerce_amazon_pa_init' );
}
/**
* Load translations.
*
* @since 1.6.0
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'woocommerce-gateway-amazon-payments-advanced', false, dirname( $this->plugin_basename ) . '/languages' );
}
/**
* Init gateway
*/
public function init_gateway() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
include_once $this->includes_path . 'class-wc-gateway-amazon-payments-advanced-abstract.php';
include_once $this->includes_path . 'class-wc-gateway-amazon-payments-advanced-privacy.php';
$subscriptions_installed = class_exists( 'WC_Subscriptions_Order' ) && function_exists( 'wcs_create_renewal_order' );
// Check for Subscriptions 2.0, and load support if found.
if ( $subscriptions_installed ) {
include_once $this->includes_path . 'class-wc-gateway-amazon-payments-advanced-subscriptions.php';
include_once $this->includes_path . 'legacy/class-wc-gateway-amazon-payments-advanced-subscriptions-legacy.php';
$this->subscriptions = new WC_Gateway_Amazon_Payments_Advanced_Subscriptions();
new WC_Gateway_Amazon_Payments_Advanced_Subscriptions_Legacy();
}
include_once $this->includes_path . 'legacy/class-wc-gateway-amazon-payments-advanced-legacy.php';
if ( WC_Amazon_Payments_Advanced_Merchant_Onboarding_Handler::get_migration_status() ) {
include_once $this->includes_path . 'class-wc-gateway-amazon-payments-advanced.php';
$this->gateway = new WC_Gateway_Amazon_Payments_Advanced();
WC_Gateway_Amazon_Payments_Advanced_Legacy::legacy_hooks();
} else {
$this->gateway = new WC_Gateway_Amazon_Payments_Advanced_Legacy();
}
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
}
/**
* Add Amazon gateway to WC.
*
* @param array $methods List of payment methods.
*
* @return array List of payment methods.
*/
public function add_gateway( $methods ) {
$methods[] = $this->gateway;
return $methods;
}
/**
* Helper method to get a sanitized version of the site name.
*
* @return string
*/
public static function get_site_name() {
// Get site setting for blog name.
$site_name = get_bloginfo( 'name' );
return self::sanitize_string( $site_name );
}
/**
* Helper method to get a sanitized version of the site description.
*
* @return string
*/
public static function get_site_description() {
// Get site setting for blog name.
$site_description = get_bloginfo( 'description' );
return self::sanitize_string( $site_description );
}
/**
* Helper method to get a sanitized version of a string.
*
* @param string $string Sanitize some elements.
*
* @return string
*/
protected static function sanitize_string( $string ) {
// Decode HTML entities.
$string = wp_specialchars_decode( $string, ENT_QUOTES );
// ASCII-ify accented characters.
$string = remove_accents( $string );
// Remove non-printable characters.
$string = preg_replace( '/[[:^print:]]/', '', $string );
// Clean up leading/trailing whitespace.
$string = trim( $string );
return $string;
}
/**
* Write a message to log if we're in "debug" mode.
*
* @since 1.6.0
*
* @param string|WP_Error $message Log message.
* @param null|mixed $object Data to be printed for more detail about the entry.
* @param null|string $context Context for the log.
*/
public function log( $message, $object = null, $context = null ) {
if ( empty( $this->settings['debug'] ) ) {
return;
}
if ( 'yes' !== $this->settings['debug'] ) {
return;
}
if ( ! is_a( $this->logger, 'WC_Logger' ) ) {
$this->logger = new WC_Logger();
}
if ( is_wp_error( $message ) ) {
$error_data = $message->get_error_data();
if ( ! is_null( $error_data ) ) {
$object = $error_data;
}
$message = $message->get_error_message();
}
if ( empty( $context ) ) {
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
array_shift( $backtrace ); // drop current.
$context = isset( $backtrace[0]['function'] ) ? $backtrace[0]['function'] : '';
if ( isset( $backtrace[0]['class'] ) ) {
$context = $backtrace[0]['class'] . '::' . $context;
}
}
$log_message = $context . ' - ' . $message;
if ( ! is_null( $object ) ) {
if ( is_array( $object ) || is_object( $object ) ) {
$log_message .= "\n";
$log_message .= wp_json_encode( $object, JSON_PRETTY_PRINT );
} elseif ( is_string( $object ) || is_numeric( $object ) ) {
$log_message .= ' | ' . $object;
} else {
$log_message .= ' | ' . var_export( $object, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
}
}
if ( ! isset( $this->logger_prefix ) ) {
$this->logger_prefix = wp_generate_password( 6, false, false );
}
$log_message = $this->logger_prefix . ' - ' . $log_message;
$this->logger->add( 'woocommerce-gateway-amazon-payments-advanced', $log_message );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( $log_message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
}
/**
* Register REST API route for /orders/<order-id>/amazon-payments-advanced/.
*
* @since 1.6.0
*/
public function rest_api_register_routes() {
// Check to make sure WC is activated and its REST API were loaded
// first.
if ( ! function_exists( 'WC' ) ) {
return;
}
if ( ! isset( WC()->api ) ) {
return;
}
if ( ! is_a( WC()->api, 'WC_API' ) ) {
return;
}
require_once $this->includes_path . 'class-wc-amazon-payments-advanced-rest-api-controller.php';
$api_implementation = new WC_Amazon_Payments_Advanced_REST_API_Controller();
$api_implementation->register_routes();
}
/**
* Add Amazon reference information in order item response.
*
* @since 1.6.0
*
* @param WP_REST_Response $response Response object.
* @param WP_Post $post Post object.
*
* @return WP_REST_Response REST response
*/
public function rest_api_add_amazon_ref_info( $response, $post ) {
if ( 'amazon_payments_advanced' === $response->data['payment_method'] ) {
$response->data['amazon_reference'] = array(
'amazon_reference_state' => WC_Amazon_Payments_Advanced_API_Legacy::get_order_ref_state( $post->ID, 'amazon_reference_state' ),
'amazon_reference_id' => get_post_meta( $post->ID, 'amazon_reference_id', true ),
'amazon_authorization_state' => WC_Amazon_Payments_Advanced_API_Legacy::get_order_ref_state( $post->ID, 'amazon_authorization_state' ),
'amazon_authorization_id' => get_post_meta( $post->ID, 'amazon_authorization_id', true ),
'amazon_capture_state' => WC_Amazon_Payments_Advanced_API_Legacy::get_order_ref_state( $post->ID, 'amazon_capture_state' ),
'amazon_capture_id' => get_post_meta( $post->ID, 'amazon_capture_id', true ),
'amazon_refund_ids' => get_post_meta( $post->ID, 'amazon_refund_id', false ),
);
}
return $response;
}
/**
* Return instance of WC_Gateway_Amazon_Payments_Advanced.
*
* @since 2.0.0
*
* @return WC_Gateway_Amazon_Payments_Advanced|WC_Gateway_Amazon_Payments_Advanced_Legacy
*/
public function get_gateway() {
return $this->gateway;
}
}
/**
* Return instance of WC_Amazon_Payments_Advanced.
*
* @since 1.6.0
*
* @return WC_Amazon_Payments_Advanced
*/
function wc_apa() {
static $plugin;
if ( ! isset( $plugin ) ) {
$plugin = new WC_Amazon_Payments_Advanced();
}
return $plugin;
}
/**
* Get order property with compatibility for WC lt 3.0.
*
* @since 1.7.0
*
* @param WC_Order $order Order object.
* @param string $key Order property.
*
* @return mixed Value of order property.
*/
function wc_apa_get_order_prop( $order, $key ) {
switch ( $key ) {
case 'order_currency':
return is_callable( array( $order, 'get_currency' ) ) ? $order->get_currency() : $order->get_order_currency();
default:
$getter = array( $order, 'get_' . $key );
return is_callable( $getter ) ? call_user_func( $getter ) : $order->{ $key };
}
}
// Provides backward compatibility.
$GLOBALS['wc_amazon_payments_advanced'] = wc_apa();