/**
  * 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;
 }
 /**
  * Retrieve full details from the order using 'GetBillingAgreementDetails' (if it contains a subscription).
  *
  * @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)
 {
     if (!WC_Subscriptions_Cart::cart_contains_subscription()) {
         return parent::get_amazon_order_details($amazon_reference_id);
     }
     $response = WC_Amazon_Payments_Advanced_API::request(array('Action' => 'GetBillingAgreementDetails', 'AmazonBillingAgreementId' => $amazon_reference_id));
     if (!is_wp_error($response) && isset($response->GetBillingAgreementDetailsResult->BillingAgreementDetails)) {
         return $response->GetBillingAgreementDetailsResult->BillingAgreementDetails;
     }
     return false;
 }
 /**
  * Add scripts
  */
 public function scripts()
 {
     $enqueue_scripts = is_cart() || is_checkout() || is_checkout_pay_page();
     if (!apply_filters('woocommerce_amazon_pa_enqueue_scripts', $enqueue_scripts)) {
         return;
     }
     $type = 'yes' == $this->settings['enable_login_app'] ? 'app' : 'standard';
     wp_enqueue_style('amazon_payments_advanced', plugins_url('assets/css/style.css', __FILE__));
     wp_enqueue_script('amazon_payments_advanced_widgets', WC_Amazon_Payments_Advanced_API::get_widgets_url(), array(), '1.0', true);
     wp_enqueue_script('amazon_payments_advanced', plugins_url('assets/js/amazon-' . $type . '-widgets.js', __FILE__), array(), '1.0', true);
     $redirect_page = is_cart() ? add_query_arg('amazon_payments_advanced', 'true', get_permalink(woocommerce_get_page_id('checkout'))) : add_query_arg('amazon_payments_advanced', 'true');
     $params = array('seller_id' => $this->settings['seller_id'], 'reference_id' => $this->reference_id, 'redirect' => esc_url_raw($redirect_page), 'is_checkout_pay_page' => is_checkout_pay_page(), 'is_checkout' => is_checkout(), 'access_token' => $this->access_token);
     if ('yes' == $this->settings['enable_login_app']) {
         $params['button_type'] = 'LwA';
         $params['button_color'] = 'Gold';
         $params['button_size'] = 'small';
         $params['checkout_url'] = esc_url_raw(get_permalink(woocommerce_get_page_id('checkout')));
     }
     if (class_exists('WC_Subscriptions_Cart')) {
         $cart_contains_subscription = WC_Subscriptions_Cart::cart_contains_subscription() || wcs_cart_contains_renewal();
         $change_payment_for_subscription = isset($_GET['change_payment_method']) && wcs_is_subscription(absint($_GET['change_payment_method']));
         $params['is_recurring'] = $cart_contains_subscription || $change_payment_for_subscription;
     }
     $params = array_map('esc_js', apply_filters('woocommerce_amazon_pa_widgets_params', $params));
     wp_localize_script('amazon_payments_advanced', 'amazon_payments_advanced_params', $params);
 }
    /**
     * Authorization metabox content.
     */
    public function authorization_box()
    {
        global $post, $wpdb, $theorder;
        $actions = array();
        $order_id = absint($post->ID);
        if (!is_object($theorder)) {
            $theorder = new WC_Order($order_id);
        }
        // Get ids
        $amazon_authorization_id = get_post_meta($order_id, 'amazon_authorization_id', true);
        $amazon_reference_id = get_post_meta($order_id, 'amazon_reference_id', true);
        $amazon_capture_id = get_post_meta($order_id, 'amazon_capture_id', true);
        $amazon_refund_ids = get_post_meta($order_id, 'amazon_refund_id', false);
        if ($amazon_capture_id) {
            $amazon_capture_state = WC_Amazon_Payments_Advanced_API::get_capture_state($order_id, $amazon_capture_id);
            switch ($amazon_capture_state) {
                case 'Pending':
                    echo wpautop(sprintf(__('Capture Reference %s is <strong>%s</strong>.', 'woocommerce-gateway-amazon-payments-advanced'), esc_html($amazon_capture_id), esc_html($amazon_capture_state)) . ' <a href="#" data-action="refresh" class="refresh">' . __('Refresh', 'woocommerce-gateway-amazon-payments-advanced') . '</a>');
                    // Admin will need to re-check this, so clear the stored value
                    $this->clear_stored_states($order_id);
                    break;
                case 'Declined':
                    echo wpautop(__('The capture was declined.', 'woocommerce-gateway-amazon-payments-advanced'));
                    $actions['authorize'] = array('id' => $amazon_reference_id, 'button' => __('Re-authorize?', 'woocommerce-gateway-amazon-payments-advanced'));
                    break;
                case 'Completed':
                    echo wpautop(sprintf(__('Capture Reference %s is <strong>%s</strong>.', 'woocommerce-gateway-amazon-payments-advanced'), esc_html($amazon_capture_id), esc_html($amazon_capture_state)) . ' <a href="#" class="toggle_refund">' . __('Make a refund?', 'woocommerce-gateway-amazon-payments-advanced') . '</a>');
                    // Refund form
                    ?>
					<p class="refund_form" style="display:none">
						<input type="number" step="any" style="width:100%" class="amazon_refund_amount" value="<?php 
                    echo esc_attr($theorder->get_total());
                    ?>
" />
						<input type="text" style="width:100%" class="amazon_refund_note" placeholder="<?php 
                    _e('Add a note about this refund', 'woocommerce-gateway-amazon-payments-advanced');
                    ?>
" /><br/>
						<a href="#" class="button" data-action="refund" data-id="<?php 
                    echo esc_attr($amazon_capture_id);
                    ?>
"><?php 
                    _e('Refund', 'woocommerce-gateway-amazon-payments-advanced');
                    ?>
</a>
					</form>
					<?php 
                    break;
                case 'Closed':
                    echo wpautop(sprintf(__('Capture Reference %s is <strong>%s</strong>.', 'woocommerce-gateway-amazon-payments-advanced'), esc_html($amazon_capture_id), esc_html($amazon_capture_state)));
                    break;
            }
            // Display refunds
            if ($amazon_refund_ids) {
                $refunds = (array) get_post_meta($order_id, 'amazon_refunds', true);
                foreach ($amazon_refund_ids as $amazon_refund_id) {
                    if (isset($refunds[$amazon_refund_id])) {
                        echo wpautop(sprintf(__('Refund %s of %s is <strong>%s</strong> (%s).', 'woocommerce-gateway-amazon-payments-advanced'), $amazon_refund_id, woocommerce_price($refunds[$amazon_refund_id]['amount']), $refunds[$amazon_refund_id]['state'], $refunds[$amazon_refund_id]['note']));
                    } else {
                        $response = WC_Amazon_Payments_Advanced_API::request(array('Action' => 'GetRefundDetails', 'AmazonRefundId' => $amazon_refund_id));
                        if (!is_wp_error($response) && !isset($response['Error']['Message'])) {
                            $note = (string) $response->GetRefundDetailsResult->RefundDetails->SellerRefundNote;
                            $state = (string) $response->GetRefundDetailsResult->RefundDetails->RefundStatus->State;
                            $amount = (string) $response->GetRefundDetailsResult->RefundDetails->RefundAmount->Amount;
                            echo wpautop(sprintf(__('Refund %s of %s is <strong>%s</strong> (%s).', 'woocommerce-gateway-amazon-payments-advanced'), esc_html($amazon_refund_id), woocommerce_price($amount), esc_html($state), esc_html($note)));
                            if ($state == 'Completed') {
                                $refunds[$amazon_refund_id] = array('state' => $state, 'amount' => $amount, 'note' => $note);
                            }
                        }
                    }
                }
                update_post_meta($order_id, 'amazon_refunds', $refunds);
            }
        } elseif ($amazon_authorization_id) {
            $amazon_authorization_state = WC_Amazon_Payments_Advanced_API::get_authorization_state($order_id, $amazon_authorization_id);
            echo wpautop(sprintf(__('Auth Reference %s is <strong>%s</strong>.', 'woocommerce-gateway-amazon-payments-advanced'), esc_html($amazon_reference_id), esc_html($amazon_authorization_state)) . ' <a href="#" data-action="refresh" class="refresh">' . __('Refresh', 'woocommerce-gateway-amazon-payments-advanced') . '</a>');
            switch ($amazon_authorization_state) {
                case 'Open':
                    $actions['capture'] = array('id' => $amazon_authorization_id, 'button' => __('Capture funds', 'woocommerce-gateway-amazon-payments-advanced'));
                    $actions['close_authorization'] = array('id' => $amazon_authorization_id, 'button' => __('Close Authorization', 'woocommerce-gateway-amazon-payments-advanced'));
                    break;
                case 'Pending':
                    echo wpautop(__('You cannot capture funds whilst the authorization is pending. Try again later.', 'woocommerce-gateway-amazon-payments-advanced'));
                    // Admin will need to re-check this, so clear the stored value
                    $this->clear_stored_states($order_id);
                    break;
                case 'Closed':
                case 'Declined':
                    $actions['authorize'] = array('id' => $amazon_reference_id, 'button' => __('Authorize again', 'woocommerce-gateway-amazon-payments-advanced'));
                    break;
            }
        } elseif ($amazon_reference_id) {
            $amazon_reference_state = WC_Amazon_Payments_Advanced_API::get_reference_state($order_id, $amazon_reference_id);
            echo wpautop(sprintf(__('Order Reference %s is <strong>%s</strong>.', 'woocommerce-gateway-amazon-payments-advanced'), esc_html($amazon_reference_id), esc_html($amazon_reference_state)) . ' <a href="#" data-action="refresh" class="refresh">' . __('Refresh', 'woocommerce-gateway-amazon-payments-advanced') . '</a>');
            switch ($amazon_reference_state) {
                case 'Open':
                    $actions['authorize'] = array('id' => $amazon_reference_id, 'button' => __('Authorize', 'woocommerce-gateway-amazon-payments-advanced'));
                    $actions['authorize_capture'] = array('id' => $amazon_reference_id, 'button' => __('Authorize &amp; Capture', 'woocommerce-gateway-amazon-payments-advanced'));
                    break;
                case 'Suspended':
                    echo wpautop(__('The reference has been suspended. Another form of payment is required.', 'woocommerce-gateway-amazon-payments-advanced'));
                    break;
                case 'Canceled':
                case 'Suspended':
                    echo wpautop(__('The reference has been cancelled/closed. No authorizations can be made.', 'woocommerce-gateway-amazon-payments-advanced'));
                    break;
            }
        }
        if (!empty($actions)) {
            echo '<p class="buttons">';
            foreach ($actions as $action_name => $action) {
                echo '<a href="#" class="button" data-action="' . esc_attr($action_name) . '" data-id="' . esc_attr($action['id']) . '">' . esc_html($action['button']) . '</a> ';
            }
            echo '</p>';
        }
        $js = "\n\t\t\tjQuery( '#woocommerce-amazon-payments-advanced' ).on( 'click', 'a.button, a.refresh', function() {\n\n\t\t\t\tjQuery( '#woocommerce-amazon-payments-advanced' ).block({\n\t\t\t\t\tmessage:    null,\n\t\t\t\t\toverlayCSS: {\n\t\t\t\t\t\tbackground: '#fff url(" . WC()->plugin_url() . "/assets/images/ajax-loader.gif) no-repeat center',\n\t\t\t\t\t\topacity:    0.6\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tvar data = {\n\t\t\t\t\taction:               'amazon_order_action',\n\t\t\t\t\tsecurity:             '" . wp_create_nonce('amazon_order_action') . "',\n\t\t\t\t\torder_id:             '{$order_id}',\n\t\t\t\t\tamazon_action:        jQuery( this ).data( 'action' ),\n\t\t\t\t\tamazon_id:            jQuery( this ).data( 'id' ),\n\t\t\t\t\tamazon_refund_amount: jQuery( '.amazon_refund_amount' ).val(),\n\t\t\t\t\tamazon_refund_note:   jQuery( '.amazon_refund_note' ).val(),\n\t\t\t\t};\n\n\t\t\t\t// Ajax action\n\t\t\t\tjQuery.ajax({\n\t\t\t\t\turl:     '" . admin_url('admin-ajax.php') . "',\n\t\t\t\t\tdata:    data,\n\t\t\t\t\ttype:    'POST',\n\t\t\t\t\tsuccess: function( result ) {\n\t\t\t\t\t\tlocation.reload();\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\treturn false;\n\t\t\t});\n\n\t\t\tjQuery( '#woocommerce-amazon-payments-advanced' ).on( 'click', 'a.toggle_refund', function() {\n\t\t\t\tjQuery( '.refund_form' ).slideToggle();\n\t\t\t\treturn false;\n\t\t\t});\n\t\t";
        wc_enqueue_js($js);
    }