/** * Process payment * * @param int $order_id */ public function process_payment($order_id) { if (!$this->order_contains_subscription($order_id) && !wcs_is_subscription($order_id)) { return parent::process_payment($order_id); } $amazon_billing_agreement_id = isset($_POST['amazon_billing_agreement_id']) ? wc_clean($_POST['amazon_billing_agreement_id']) : ''; try { if (!$amazon_billing_agreement_id) { throw new Exception(__('An Amazon payment method was not chosen.', 'woocommerce-gateway-amazon-payments-advanced')); } $order = new WC_Order($order_id); $order_total = $order->get_total(); $this->log(__FUNCTION__, "Info: Beginning processing of payment for (subscription) order {$order_id} for the amount of {$order_total} {$order->get_order_currency()}."); // Set the Billing Agreement Details $this->set_billing_agreement_details($order, $amazon_billing_agreement_id); // Confirm the Billing Agreement $this->confirm_billing_agreement($order_id, $amazon_billing_agreement_id); // Get the Billing Agreement Details, with FULL address (now that we've confirmed) $result = $this->get_billing_agreement_details($order_id, $amazon_billing_agreement_id); // Store the subscription destination $this->store_subscription_destination($order_id, $result); // Store Billing Agreement ID on the order and it's subscriptions $result = update_post_meta($order_id, 'amazon_billing_agreement_id', $amazon_billing_agreement_id); if ($result) { $this->log(__FUNCTION__, "Info: Successfully stored billing agreement in meta for order {$order_id}."); } else { $this->log(__FUNCTION__, "Error: Failed to store billing agreement in meta for order {$order_id}."); } $subscriptions = wcs_get_subscriptions_for_order($order_id); foreach ($subscriptions as $subscription) { $result = update_post_meta($subscription->id, 'amazon_billing_agreement_id', $amazon_billing_agreement_id); if ($result) { $this->log(__FUNCTION__, "Info: Successfully stored billing agreement in meta for subscription {$subscription->id} (parent order {$order_id})."); } else { $this->log(__FUNCTION__, "Error: Failed to store billing agreement in meta for subscription {$subscription->id} (parent order {$order_id})."); } } // Authorize/Capture initial payment, if initial payment required if ($order_total > 0) { return $this->authorize_payment($order, $amazon_billing_agreement_id); } // No payment needed now, free trial or coupon used - mark order as complete $order->payment_complete(); $this->log(__FUNCTION__, "Info: Zero-total initial payment for (subscription) order {$order_id}. Payment marked as complete."); // Remove items from cart WC()->cart->empty_cart(); // Return thank you page redirect return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } catch (Exception $e) { $this->log(__FUNCTION__, "Error: Exception encountered: {$e->getMessage()}"); wc_add_notice(sprintf(__('Error: %s', 'woocommerce-gateway-amazon-payments-advanced'), $e->getMessage()), 'error'); return; } }