/** * Constructor */ public function __construct() { include_once 'includes/class-wc-amazon-payments-advanced-api.php'; $this->settings = WC_Amazon_Payments_Advanced_API::get_settings(); $this->reference_id = WC_Amazon_Payments_Advanced_API::get_reference_id(); $this->access_token = WC_Amazon_Payments_Advanced_API::get_access_token(); add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_links')); add_action('init', array($this, 'init_gateway')); add_action('wp_loaded', array($this, 'init_handlers'), 11); add_action('wp_footer', array($this, 'maybe_hide_standard_checkout_button')); }
/** * Retrieve full details from the order using 'GetOrderReferenceDetails'. * * @param string $amazon_reference_id * * @return bool|object Boolean false on failure, object of OrderReferenceDetails on success. */ function get_amazon_order_details($amazon_reference_id) { $request_args = array('Action' => 'GetOrderReferenceDetails', 'AmazonOrderReferenceId' => $amazon_reference_id); // Full address information is available to the 'GetOrderReferenceDetails' call when we're in // "login app" mode and we pass the AddressConsentToken to the API // See the "Getting the Shipping Address" section here: https://payments.amazon.com/documentation/lpwa/201749990 $settings = WC_Amazon_Payments_Advanced_API::get_settings(); if ('yes' == $settings['enable_login_app']) { $request_args['AddressConsentToken'] = $this->access_token; } $response = WC_Amazon_Payments_Advanced_API::request($request_args); if (!is_wp_error($response) && isset($response->GetOrderReferenceDetailsResult->OrderReferenceDetails)) { return $response->GetOrderReferenceDetailsResult->OrderReferenceDetails; } return false; }
/** * Use 'CloseBillingAgreement' to disallow future authorizations after cancelling a subscription. * * @param WC_Order $order */ function cancelled_subscription($order) { $amazon_billing_agreement_id = get_post_meta($order->id, 'amazon_billing_agreement_id', true); if ($amazon_billing_agreement_id) { try { // 'CloseBillingAgreement' has a maximum request quota of 10 and a restore rate of one request every second // In sandbox mode, quota = 2 and restore = one every two seconds // https://payments.amazon.com/documentation/apireference/201751710#201751950 $settings = WC_Amazon_Payments_Advanced_API::get_settings(); sleep('yes' === $settings['sandbox'] ? 2 : 1); $response = WC_Amazon_Payments_Advanced_API::request(array('Action' => 'CloseBillingAgreement', 'AmazonBillingAgreementId' => $amazon_billing_agreement_id)); $this->handle_generic_api_response_errors(__FUNCTION__, $response, $order->id, $amazon_billing_agreement_id); $this->log(__FUNCTION__, "Info: CloseBillingAgreement for order {$order->id} with billing agreement: {$amazon_billing_agreement_id}."); } catch (Exception $e) { $this->log(__FUNCTION__, "Error: Exception encountered: {$e->getMessage()}"); $order->add_order_note(sprintf(__("Exception encountered in 'CloseBillingAgreement': %s"), $e->getMessage())); } } else { $this->log(__FUNCTION__, "Error: No Amazon billing agreement found for order {$order->id}."); } }