/**
  * Perform order actions for amazon
  */
 public function order_actions()
 {
     check_ajax_referer('amazon_order_action', 'security');
     $order_id = absint($_POST['order_id']);
     $id = isset($_POST['amazon_id']) ? woocommerce_clean($_POST['amazon_id']) : '';
     $action = sanitize_title($_POST['amazon_action']);
     switch ($action) {
         case 'refresh':
             $this->clear_stored_states($order_id);
             break;
         case 'authorize':
             // Delete old
             delete_post_meta($order_id, 'amazon_authorization_id');
             delete_post_meta($order_id, 'amazon_capture_id');
             WC_Amazon_Payments_Advanced_API::authorize_payment($order_id, $id, false);
             $this->clear_stored_states($order_id);
             break;
         case 'authorize_capture':
             // Delete old
             delete_post_meta($order_id, 'amazon_authorization_id');
             delete_post_meta($order_id, 'amazon_capture_id');
             WC_Amazon_Payments_Advanced_API::authorize_payment($order_id, $id, true);
             $this->clear_stored_states($order_id);
             break;
         case 'close_authorization':
             WC_Amazon_Payments_Advanced_API::close_authorization($order_id, $id);
             $this->clear_stored_states($order_id);
             break;
         case 'capture':
             WC_Amazon_Payments_Advanced_API::capture_payment($order_id, $id);
             $this->clear_stored_states($order_id);
             break;
         case 'refund':
             $amazon_refund_amount = floatval(woocommerce_clean($_POST['amazon_refund_amount']));
             $amazon_refund_note = woocommerce_clean($_POST['amazon_refund_note']);
             WC_Amazon_Payments_Advanced_API::refund_payment($order_id, $id, $amazon_refund_amount, $amazon_refund_note);
             $this->clear_stored_states($order_id);
             break;
     }
     die;
 }
 /**
  * Process payment
  *
  * @param int $order_id
  */
 public function process_payment($order_id)
 {
     $order = new WC_Order($order_id);
     $amazon_reference_id = isset($_POST['amazon_reference_id']) ? wc_clean($_POST['amazon_reference_id']) : '';
     try {
         if (!$amazon_reference_id) {
             throw new Exception(__('An Amazon payment method was not chosen.', 'woocommerce-gateway-amazon-payments-advanced'));
         }
         // Update order reference with amounts
         $this->set_order_reference_details($order, $amazon_reference_id);
         // Confirm order reference
         $this->confirm_order_reference($amazon_reference_id);
         // Get FULL address details and save them to the order
         $order_details = $this->get_amazon_order_details($amazon_reference_id);
         if ($order_details) {
             $this->store_order_address_details($order_id, $order_details);
         }
         // Store reference ID in the order
         add_post_meta($order_id, 'amazon_reference_id', $amazon_reference_id, true);
         switch ($this->payment_capture) {
             case 'manual':
                 // Mark as on-hold
                 $order->update_status('on-hold', __('Amazon order opened. Use the "Amazon Payments Advanced" box to authorize and/or capture payment. Authorized payments must be captured within 7 days.', 'woocommerce-gateway-amazon-payments-advanced'));
                 // Reduce stock levels
                 $order->reduce_order_stock();
                 break;
             case 'authorize':
                 // Authorize only
                 $result = WC_Amazon_Payments_Advanced_API::authorize_payment($order_id, $amazon_reference_id, false);
                 if ($result) {
                     // Mark as on-hold
                     $order->update_status('on-hold', __('Amazon order opened. Use the "Amazon Payments Advanced" box to authorize and/or capture payment. Authorized payments must be captured within 7 days.', 'woocommerce-gateway-amazon-payments-advanced'));
                     // Reduce stock levels
                     $order->reduce_order_stock();
                 } else {
                     $order->update_status('failed', __('Could not authorize Amazon payment.', 'woocommerce-gateway-amazon-payments-advanced'));
                 }
                 break;
             default:
                 // Capture
                 $result = WC_Amazon_Payments_Advanced_API::authorize_payment($order_id, $amazon_reference_id, true);
                 if ($result) {
                     // Payment complete
                     add_post_meta($order_id, '_transaction_id', $amazon_reference_id, true);
                     $order->payment_complete();
                 } else {
                     $order->update_status('failed', __('Could not authorize Amazon payment.', 'woocommerce-gateway-amazon-payments-advanced'));
                 }
                 break;
         }
         // Remove cart
         WC()->cart->empty_cart();
         // Return thank you page redirect
         return array('result' => 'success', 'redirect' => $this->get_return_url($order));
     } catch (Exception $e) {
         wc_add_notice(__('Error:', 'woocommerce-gateway-amazon-payments-advanced') . ' ' . $e->getMessage(), 'error');
         return;
     }
 }