クラス
AmazonPay
Methods for setting up Amazon Pay.
ソース ソース
ファイル: src/AmazonPay.php
class AmazonPay extends Module { const L10N = 'wcexaap'; const VUE_APP_MOUNT_EL = 'wcexaap_vue'; const SDK_REQUIRED_FIELDS = [ 'public_key_id', 'private_key', 'merchant_id', 'access_key', 'secret_key', 'client_id', ]; /** * REST Routes object * * @var Routing\RestRoutes */ public $restRoutes; /** * Redirect routes object * * @var Routing\RedirectRoutes */ public $redirectRoutes; /** * Error store/handling object * * @var Errors\GenericErrorStore */ public $errors; /** * Checkout object * * @var Checkout */ public $checkout; /** * Creates Amazon Pay settlement module * * @author Evan D Shaw <evandanielshaw@gmail.com> * @inheritDoc */ public function __construct() { $cptmc = new CptmClient( 'AAP001', WCEXAAP_VERSION, WCEXAAP_PLUGIN_FILE, 'https://api.aivec.co.jp/cptmp/v1/getProvidersList/AAP001' ); $cptmc->init(); parent::__construct( Config\Constants::PAYMENT_NAME, Config\Constants::ACTING, Config\Constants::ACTING_FLAG, [ 'shipped' => ['once'], 'service' => ['once'], 'data' => ['once'], ], Config\Constants::AMZ_PAY_SUPPORTED_CURRENCY_CODES, false, $cptmc, true ); $this->errors = new Errors\GenericErrorStore($this); $this->errors->populate(); (new IPN\Listener($this))->listen(); (new Hooks())->init(); } /** * Initializes Amazon Pay. Should only be called if Welcart is activated * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void * @throws InvalidArgumentException Thrown if errors contains duplicate codes. */ public function init() { (new DeliveryPage($this))->init(); // Do not load initial confirm page on redirects if (!isset($_REQUEST['amazonCheckoutSessionId'])) { (new Views\ConfirmPageLogin\Factory())->getInstance($this)->init()->addHooks(); } if (is_admin()) { (new Admin\SettlementSettings($this))->init(); (new Admin\OrderList($this))->init()->addHooks(); (new Admin\OrderEdit($this))->init()->addHooks(); } if ($this->ready() === true) { add_action('plugins_loaded', [$this, 'initCheckoutHooks']); add_action('init', [$this, 'initializeApp'], 16); } } /** * This function loads all hooks associated with the checkout process. Note that because of when * Welcart's main class and subsequent checkout hooks are loaded, our implementation of them MUST * be initialized in this hook. Wordpresses "init" hook, for example, if used, will result in * these hooks not being called at all. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ public function initCheckoutHooks() { $this->checkout = new Checkout($this); $this->checkout->init()->addHooks(); usces_register_action( 'wcexaapCompletePurchase', 'get', 'wcexaapCompletePurchase', null, function () { // route guards $routeGuards = new Routing\RedirectRouteGuards( $this, new Routing\RestRouteGuards($this) ); $amazonRouteGuards = new Routing\AmazonRedirectRouteGuards($this); call_user_func($amazonRouteGuards->sessionIdExistsOrExit()); call_user_func($routeGuards->cartNotEmptyOrExit()); call_user_func($routeGuards->itemsInStockOrCartRedirect()); $this->checkout->handleCheckoutResult(); } ); } /** * Initializes the Amazon Pay settlement module if it is properly configured and all * dependent assets for this plugin. * * NOTE: usces' main() function is called in an init hook of priority 10. Therefore, if * class files which need the usces global are initialized before 10, everything from * usces will be null. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return void */ public function initializeApp() { $opts = $this->getActingOpts(); if ($opts['use_quickpay'] === 'on') { (new Views\CartPage\Factory())->getInstance($this); } // we only call init here to enqueue styles before template load (new Views\CartErrorPage\CartErrorPage())->init(); (new Views\MemberEditPage\MemberEditPage($this))->init(); (new Views\MemberLoginPage\MemberLoginPage($this))->init(); // initialize last so that hooks are registered before exit $this->restRoutes = new Routing\RestRoutes($this); $this->redirectRoutes = new Routing\RedirectRoutes($this); // listen for route matches $this->restRoutes->dispatcher->listen(); // PRIORITY MUST BE LOWER THAN 2 add_filter('usces_filter_template_redirect', function ($bool) { $this->redirectRoutes->dispatcher->listen(); return $bool; }, 1, 1); } /** * Returns `once` or `continue` * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return string */ public function getChargeType() { $type = 'once'; if (defined('WCEX_DLSELLER')) { if (dlseller_have_continue_charge()) { $type = 'continue'; } } return $type; } /** * Returns namespaced theme configuration * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return array */ public static function getThemeConfig() { return Theme::themeConfig('wcexaap'); } /** * Returns an associative array with all user inputed data concerning their contract with Amazon, as well * as region specific data. * * @inheritDoc * @author Evan D Shaw <evandanielshaw@gmail.com> * @param array $opts * @return array */ public function filterActingOpts($opts) { $regionmetamap = [ 'us' => [ 'jsurl' => 'https://static-na.payments-amazon.com', 'integrationCentralUrl' => 'https://sellercentral.amazon.com/gp/pyop/seller/integrationcentral/', 'ledgerCurrency' => 'USD', 'checkoutLanguage' => 'en_US', ], 'eu' => [ 'jsurl' => 'https://static-eu.payments-amazon.com', 'integrationCentralUrl' => 'https://sellercentral-europe.amazon.com/gp/pyop/seller/integrationcentral/', 'ledgerCurrency' => 'EUR', 'checkoutLanguage' => 'en_GB', ], 'uk' => [ 'jsurl' => 'https://static-eu.payments-amazon.com', 'integrationCentralUrl' => 'https://sellercentral-europe.amazon.com/gp/pyop/seller/integrationcentral/', 'ledgerCurrency' => 'GBP', 'checkoutLanguage' => 'en_GB', ], 'jp' => [ 'jsurl' => 'https://static-fe.payments-amazon.com', 'integrationCentralUrl' => 'https://sellercentral-japan.amazon.com/gp/pyop/seller/integrationcentral/', 'ledgerCurrency' => 'JPY', 'checkoutLanguage' => 'ja_JP', ], ]; $opts['region'] = isset($opts['region']) ? strtolower($opts['region']) : 'jp'; $rmap = $regionmetamap[$opts['region']]; $opts['amzjsurl'] = $rmap['jsurl']; $opts['integrationCentralUrl'] = $rmap['integrationCentralUrl']; $opts['ledgerCurrency'] = $rmap['ledgerCurrency']; $opts['checkoutLanguage'] = $rmap['checkoutLanguage']; $host = ''; $url = esc_url_raw(wp_unslash(get_home_url())); if (!empty($url)) { $scheme = wp_parse_url($url, PHP_URL_SCHEME); $host = !$scheme ? 'https://' . $url : $url; } foreach (self::SDK_REQUIRED_FIELDS as $rfieldkey) { $opts[$rfieldkey] = isset($opts[$rfieldkey]) ? trim($opts[$rfieldkey]) : ''; } $opts['ipn_endpoint'] = !empty($opts['ipn_endpoint']) ? trim($opts['ipn_endpoint']) : $host; $opts['ipn_registered'] = isset($opts['ipn_registered']) ? (bool)(int)$opts['ipn_registered'] : false; $opts['ipn_verified'] = isset($opts['ipn_verified']) ? (bool)(int)$opts['ipn_verified'] : false; $opts['transaction_processing_type'] = isset($opts['transaction_processing_type']) ? $opts['transaction_processing_type'] : 'sync'; $opts['sync_with_order_status_changes'] = isset($opts['sync_with_order_status_changes']) ? $opts['sync_with_order_status_changes'] : 'off'; $opts['use_social_login'] = isset($opts['use_social_login']) ? $opts['use_social_login'] : 'on'; $opts['use_quickpay'] = isset($opts['use_quickpay']) ? $opts['use_quickpay'] : 'on'; $opts['quickpay_btn_position'] = isset($opts['quickpay_btn_position']) ? $opts['quickpay_btn_position'] : 'below'; $opts['quickpay_notes_field'] = isset($opts['quickpay_notes_field']) ? $opts['quickpay_notes_field'] : 'on'; $opts['quickpay_custom_order_fields'] = isset($opts['quickpay_custom_order_fields']) ? $opts['quickpay_custom_order_fields'] : 'on'; $opts['quickpay_custom_delivery_fields'] = isset($opts['quickpay_custom_delivery_fields']) ? $opts['quickpay_custom_delivery_fields'] : 'on'; $opts['quickpay_default_customer_address_details'] = isset($opts['quickpay_default_customer_address_details']) ? $opts['quickpay_default_customer_address_details'] : 'off'; $opts['address_priority'] = isset($opts['address_priority']) ? $opts['address_priority'] : 'amazon'; $opts['buttonColor'] = isset($opts['buttonColor']) ? $opts['buttonColor'] : 'Gold'; $opts['amazonpay_config'] = [ 'public_key_id' => $opts['public_key_id'], // RSA Public Key ID (this is not the Merchant or Seller ID) 'private_key' => $opts['private_key'], // Path to RSA Private Key (or a string representation) 'merchant_id' => $opts['merchant_id'], // Merchant/SellerID 'client_id' => $opts['client_id'], // Login With Amazon Client ID 'region' => $opts['region'], // us, eu, uk, jp 'currency_code' => usces_crcode('return'), // USD, EUR, GBP, JPY 'sandbox' => $opts['sandbox'], ]; return $opts; } /** * Returns `true` if all fields required in order to use the Amazon Client SDK are set, `false` otherwise * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return bool */ public function isAmazonClientUsable() { $opts = $this->getActingOpts(); foreach (self::SDK_REQUIRED_FIELDS as $rfieldkey) { if (empty($opts[$rfieldkey])) { return false; } } return true; } /** * Returns variables for use in JavaScript files * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return array */ public function getScriptInjectionVariables() { $response = [ 'vueMountEL' => self::VUE_APP_MOUNT_EL, 'paymentName' => $this->getPaymentName(), 'themeColor' => self::getThemeConfig()['color'], 'formExtrasPrefix' => Config\Constants::FORM_EXTRAS_PREFIX, 'actingStatusRowId' => $this->getActing() . '-acting-status', ]; if ($this->restRoutes !== null) { $response = array_merge($this->restRoutes->getScriptInjectionVariables(), $response); } if ($this->errors !== null) { $response = array_merge($this->errors->getScriptInjectionVariables(), $response); } return $response; } /** * Initializes and returns Amazon Pay SDK V2 client object * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return Client * @throws InvalidArgumentException Thrown when 'amazonpay_config' is malformed. */ public function getAmazonClient() { return new Client($this->getActingOpts()['amazonpay_config']); } /** * Initializes and returns Amazon Pay SDK V1 client object * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return V1Client * @throws InvalidArgumentException Thrown when `$config` is malformed. */ public function getAmazonV1Client() { $config = $this->getActingOpts(); $config = [ 'merchant_id' => $config['merchant_id'], 'access_key' => $config['access_key'], 'secret_key' => $config['secret_key'], 'platform_id' => Config\Constants::PLATFORM_ID, 'region' => $config['region'] === 'eu' ? 'de' : $config['region'], 'currency_code' => usces_crcode('return'), 'sandbox' => $config['sandbox'], ]; return new V1Client($config); } }
- __construct — Creates Amazon Pay settlement module
- filterActingOpts — Returns an associative array with all user inputed data concerning their contract with Amazon, as well as region specific data.
- getAmazonClient — Initializes and returns Amazon Pay SDK V2 client object
- getAmazonV1Client — Initializes and returns Amazon Pay SDK V1 client object
- getChargeType — Returns once or continue
- getScriptInjectionVariables — Returns variables for use in JavaScript files
- getThemeConfig — Returns namespaced theme configuration
- init — Initializes Amazon Pay. Should only be called if Welcart is activated
- initCheckoutHooks — This function loads all hooks associated with the checkout process. Note that because of when Welcart's main class and subsequent checkout hooks are loaded, our implementation of them MUST be initialized in this hook. Wordpresses "init" hook, for example, if used, will result in these hooks not being called at all.
- initializeApp — Initializes the Amazon Pay settlement module if it is properly configured and all dependent assets for this plugin.
- isAmazonClientUsable — Returns true if all fields required in order to use the Amazon Client SDK are set, false otherwise