/**
  * Force a page refresh when an order is updated to have a zero total and we're not using the "login app" mode.
  *
  * This ensures that the standard WC checkout form is rendered.
  */
 public function force_standard_mode_refresh_with_zero_order_total($cart)
 {
     // Avoid constant reload loop in the event we've forced a checkout refresh
     if (!is_ajax()) {
         unset(WC()->session->reload_checkout);
     }
     // Login app mode can handle zero-total orders
     if ('yes' === $this->settings['enable_login_app']) {
         return;
     }
     if (!$this->gateway->is_available()) {
         return;
     }
     // Get the previous cart total
     $previous_total = WC()->session->wc_amazon_previous_total;
     // Store the current total
     WC()->session->wc_amazon_previous_total = $cart->total;
     // If the total is non-zero, and we don't know what the previous total was, bail.
     if (is_null($previous_total) || $cart->needs_payment()) {
         return;
     }
     // This *wasn't* as zero-total order, but is now
     if ($previous_total > 0) {
         // Force reload, re-rendering standard WC checkout form
         WC()->session->reload_checkout = true;
     }
 }
 /**
  * Get customer details from amazon
  */
 public function get_customer_details()
 {
     try {
         // Update order reference with amounts
         $amazon = new WC_Gateway_Amazon_Payments_Advanced();
         $response = $amazon->api_request(array('Action' => 'GetOrderReferenceDetails', 'AmazonOrderReferenceId' => $this->reference_id));
         if (is_wp_error($response)) {
             throw new Exception($response->get_error_message());
         }
         if (!isset($response['GetOrderReferenceDetailsResult']['OrderReferenceDetails']['Destination']['PhysicalDestination'])) {
             return;
         }
         $address = $response['GetOrderReferenceDetailsResult']['OrderReferenceDetails']['Destination']['PhysicalDestination'];
         if (!empty($address['CountryCode'])) {
             WC()->customer->set_country($address['CountryCode']);
             WC()->customer->set_shipping_country($address['CountryCode']);
         }
         if (!empty($address['StateOrRegion'])) {
             WC()->customer->set_state($address['StateOrRegion']);
             WC()->customer->set_shipping_state($address['StateOrRegion']);
         }
         if (!empty($address['PostalCode'])) {
             WC()->customer->set_postcode($address['PostalCode']);
             WC()->customer->set_shipping_postcode($address['PostalCode']);
         }
         if (!empty($address['City'])) {
             WC()->customer->set_city($address['City']);
             WC()->customer->set_shipping_city($address['City']);
         }
     } catch (Exception $e) {
         wc_add_notice(__('Error:', 'woocommerce-gateway-amazon-payments-advanced') . ' ' . $e->getMessage(), 'error');
         return;
     }
 }
 /**
  * Refund a payment
  * @param  int $order_id
  * @param  string $capture_id
  * @param  float $amount
  */
 public function refund_payment($order_id, $capture_id, $amount, $note)
 {
     $order = new WC_Order($order_id);
     if ($order->payment_method == 'amazon_payments_advanced') {
         if ('US' == WC()->countries->get_base_country() && $amount > $order->get_total()) {
             $order->add_order_note(__('Unable to refund funds via amazon:', 'woocommerce-gateway-amazon-payments-advanced') . ' ' . __('Refund amount is greater than order total.', 'woocommerce-gateway-amazon-payments-advanced'));
             return;
         } elseif ($amount > min($order->get_total() * 1.15, $order->get_total() + 75)) {
             $order->add_order_note(__('Unable to refund funds via amazon:', 'woocommerce-gateway-amazon-payments-advanced') . ' ' . __('Refund amount is greater than the max refund amount.', 'woocommerce-gateway-amazon-payments-advanced'));
             return;
         }
         $amazon = new WC_Gateway_Amazon_Payments_Advanced();
         $response = $amazon->api_request(array('Action' => 'Refund', 'AmazonCaptureId' => $capture_id, 'RefundReferenceId' => $order->id . '-' . current_time('timestamp', true), 'RefundAmount.Amount' => $amount, 'RefundAmount.CurrencyCode' => strtoupper(get_woocommerce_currency()), 'SellerRefundNote' => $note));
         if (is_wp_error($response)) {
             $order->add_order_note(__('Unable to refund funds via amazon:', 'woocommerce-gateway-amazon-payments-advanced') . ' ' . $response->get_error_message());
         } elseif (isset($response['Error']['Message'])) {
             $order->add_order_note($response['Error']['Message']);
         } else {
             $refund_id = $response['RefundResult']['RefundDetails']['AmazonRefundId'];
             $order->add_order_note(sprintf(__('Refunded %s (%s)', 'woocommerce-gateway-amazon-payments-advanced'), woocommerce_price($amount), $note));
             add_post_meta($order_id, 'amazon_refund_id', $refund_id);
         }
     }
 }
 /**
  * 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;
 }