/**
  * Authorize payment against a billing agreement using 'AuthorizeOnBillingAgreement' method
  * See: https://payments.amazon.com/documentation/automatic/201752090#201757380
  *
  * @param int        $order_id
  * @param string     $amazon_billing_agreement_id
  * @param bool|false $capture_now
  *
  * @return bool
  */
 public static function authorize_recurring_payment($order_id, $amazon_billing_agreement_id, $capture_now = false)
 {
     $order = new WC_Order($order_id);
     if ('amazon_payments_advanced' == $order->payment_method) {
         $response = self::request(array('Action' => 'AuthorizeOnBillingAgreement', 'AmazonBillingAgreementId' => $amazon_billing_agreement_id, 'AuthorizationReferenceId' => $order->id . '-' . current_time('timestamp', true), 'AuthorizationAmount.Amount' => $order->get_total(), 'AuthorizationAmount.CurrencyCode' => strtoupper(get_woocommerce_currency()), 'CaptureNow' => $capture_now, 'TransactionTimeout' => 0, 'SellerOrderAttributes.SellerOrderId' => $order->get_order_number(), 'SellerOrderAttributes.StoreName' => WC_Amazon_Payments_Advanced::get_site_name()));
         return self::handle_payment_authorization_response($response, $order, $capture_now, 'AuthorizeOnBillingAgreement');
     }
     return false;
 }
 /**
  * Use 'SetOrderReferenceDetails' action to update details of the order reference.
  *
  * By default, use data from the WC_Order and WooCommerce / Site settings, but offer the ability to override.
  *
  * @param WC_Order $order
  * @param string   $amazon_reference_id
  * @param array    $overrides Optional. Override values sent to the Amazon Payments API for the 'SetOrderReferenceDetails' request.
  *
  * @return WP_Error|array WP_Error or parsed response array
  * @throws Exception
  */
 function set_order_reference_details($order, $amazon_reference_id, $overrides = array())
 {
     $site_name = WC_Amazon_Payments_Advanced::get_site_name();
     $request_args = array_merge(array('Action' => 'SetOrderReferenceDetails', 'AmazonOrderReferenceId' => $amazon_reference_id, 'OrderReferenceAttributes.OrderTotal.Amount' => $order->get_total(), 'OrderReferenceAttributes.OrderTotal.CurrencyCode' => strtoupper(get_woocommerce_currency()), 'OrderReferenceAttributes.SellerNote' => sprintf(__('Order %s from %s.', 'woocommerce-gateway-amazon-payments-advanced'), $order->get_order_number(), urlencode($site_name)), 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId' => $order->get_order_number(), 'OrderReferenceAttributes.SellerOrderAttributes.StoreName' => $site_name, 'OrderReferenceAttributes.PlatformId' => 'A1BVJDFFHQ7US4'), $overrides);
     // Update order reference with amounts
     $response = WC_Amazon_Payments_Advanced_API::request($request_args);
     if (is_wp_error($response)) {
         throw new Exception($response->get_error_message());
     }
     if (isset($response->Error->Message)) {
         throw new Exception((string) $response->Error->Message);
     }
     return $response;
 }
 /**
  * Use 'SetBillingAgreementDetails' action to update details of the billing agreement.
  * See: https://payments.amazon.com/documentation/apireference/201751700
  *
  * @param WC_Order $order
  * @param string   $amazon_billing_agreement_id
  *
  * @return WP_Error|array WP_Error or parsed response array
  * @throws Exception
  */
 function set_billing_agreement_details($order, $amazon_billing_agreement_id)
 {
     $site_name = WC_Amazon_Payments_Advanced::get_site_name();
     $subscriptions = wcs_get_subscriptions_for_order($order);
     $subscription_ids = wp_list_pluck($subscriptions, 'id');
     $request_args = array('Action' => 'SetBillingAgreementDetails', 'AmazonBillingAgreementId' => $amazon_billing_agreement_id, 'BillingAgreementAttributes.SellerNote' => sprintf(__('Order %s from %s.', 'woocommerce-gateway-amazon-payments-advanced'), $order->get_order_number(), urlencode($site_name)), 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId' => sprintf(__('Subscription(s): %s.', 'woocommerce-gateway-amazon-payments-advanced'), implode(', ', $subscription_ids)), 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName' => $site_name, 'BillingAgreementAttributes.PlatformId' => 'A1BVJDFFHQ7US4');
     // Update order reference with amounts
     $response = WC_Amazon_Payments_Advanced_API::request($request_args);
     $this->handle_generic_api_response_errors(__FUNCTION__, $response, $order->id, $amazon_billing_agreement_id);
     $this->log(__FUNCTION__, "Info: SetBillingAgreementDetails for order {$order->id} with billing agreement: {$amazon_billing_agreement_id}.");
     return $response;
 }