public static function complete_order($order, $project_id, $amount_to_charge) { if (!is_object($order)) { $order = new WC_Order($order); } if (!WC_Sponsorship_Order::order_contains_sponsorship($order)) { return; } do_action('complete_sponsorship_payment_' . $order->payment_method, $amount_to_charge, $order, $project_id); }
/** * Process the payment */ function process_payment($order_id) { global $woocommerce; if (class_exists('WC_Sponsorship_Order') && WC_Sponsorship_Order::order_contains_sponsorship($order_id)) { $order = new WC_Order($order_id); $stripe_token = isset($_POST['stripe_token']) ? woocommerce_clean($_POST['stripe_token']) : ''; // Use Stripe CURL API for payment try { $post_data = array(); $customer_id = 0; // Check if paying via customer ID if (isset($_POST['stripe_customer_id']) && $_POST['stripe_customer_id'] !== 'new' && is_user_logged_in()) { $customer_ids = get_user_meta(get_current_user_id(), '_stripe_customer_id', false); if (isset($customer_ids[$_POST['stripe_customer_id']]['customer_id'])) { $customer_id = $customer_ids[$_POST['stripe_customer_id']]['customer_id']; } else { throw new Exception(__('Invalid card.', 'wc_stripe')); } } elseif (empty($stripe_token)) { throw new Exception(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'wc_stripe')); } $customer_response = $this->add_customer_to_order($order, $customer_id, $stripe_token); $customer_response = $this->add_customer_to_order($order, $stripe_token); if (is_wp_error($customer_response)) { throw new Exception($customer_response->get_error_message()); } else { // Mark as on-hold (we're awaiting the cheque) $order->update_status('on-hold', 'Awaiting the sponsorship project\'s goal to be met.'); // Empty awaiting payment session if (defined($_SESSION) && array_key_exists('order_awaiting_payment', $_SESSION)) { unset($_SESSION['order_awaiting_payment']); } // Remove cart $woocommerce->cart->empty_cart(); // Store token if ($stripe_token) { update_post_meta($order->id, '_stripe_token', $stripe_token); } // Return thank you page redirect return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } } catch (Exception $e) { $woocommerce->add_error(__('Error:', 'wc_stripe') . ' "' . $e->getMessage() . '"'); return; } } else { return parent::process_payment($order_id); } }
public static function cancel_project_orders($project_id) { global $wpdb; if (!$project_id) { return; } update_post_meta($project_id, '_sponsorship_cancel', true); $orders = $wpdb->get_results($wpdb->prepare("\nselect pm.post_id as OrderId, pmCID.meta_value as StripeCustomerID\nfrom {$wpdb->postmeta} pm\njoin {$wpdb->postmeta} pmCID on pm.post_id = pmCID.post_id and pmCID.meta_key = '_stripe_customer_id'\nwhere pm.meta_key = '_sponsorship_project' and pm.meta_value = %d\n\t\t\t\t\t\t\t\t\t", $project_id)); foreach ($orders as $order) { WC_Sponsorship_Order::cancel_order($order->OrderId, 'Sponsorship project goal not met, charge was cancelled.'); } }