function woogiving_process_donation() { // Setup post variables $wg_order_id = $_POST['wg_order_id']; $jg_donation_id = $_POST['jg_donation_id']; if (is_numeric($wg_order_id) && ctype_alnum($jg_donation_id)) { // Validate order ID $wg_order_id_validation = get_post((int) $wg_order_id); if ($wg_order_id_validation) { // Get options $wg_options = get_option('woocommerce_woogiving_settings'); // Include JG API and setup API require_once 'inc/JustGivingClient.php'; $jg_client = new JustGivingClient('https://api.justgiving.com/', $wg_options['app_id'], 1, $wg_options['api_login'], $wg_options['api_password']); // Check if donation exists in JG $wg_donation_id_check = $jg_client->Donation->RetrieveRef(woogiving_create_ref($wg_order_id_validation->ID)); if (count($wg_donation_id_check->donations)) { // Check if donations IDs match if ($wg_donation_id_check->donations[0]->id == (int) $jg_donation_id) { // Get donation details from JG $wg_donation_id_status = $jg_client->Donation->RetrieveStatus($jg_donation_id); $jg_amount = $wg_donation_id_status->amount; $jg_status = $wg_donation_id_status->status; // Get customer order $customer_order = new WC_Order($wg_order_id_validation->ID); // Check if amount donated is equal to higher than the amount in the order if ($jg_amount >= $customer_order->order_total && $jg_status == 'Accepted') { // Get JG fundraising page ID $wg_jg_page_check = $jg_client->Page->Retrieve($wg_options['username']); // Check if donation was made to correct charity if ($wg_jg_page_check->charity->id == $wg_donation_id_check->donations[0]->charityId) { // Check order status if ($customer_order->get_status() == 'completed') { // Order has already been completed return json_encode(array('wg_status' => 'failure', 'wg_message' => __('This donation has already been processed.', 'woogiving'))); } else { // Reduce stock levels and complete payment $customer_order->reduce_order_stock(); $customer_order->payment_complete($jg_donation_id); $customer_order->update_status('completed'); return json_encode(array('wg_status' => 'success', 'wg_redirect' => woogiving_get_return_url($customer_order))); } } else { return json_encode(array('wg_status' => 'failure', 'wg_message' => __('This donation cannot be processed, please contact us.', 'woogiving'))); } } } else { return json_encode(array('wg_status' => 'failure', 'wg_message' => __('Invalid donation ID.', 'woogiving'))); } } else { return json_encode(array('wg_status' => 'failure', 'wg_message' => __('Invalid donation ID.', 'woogiving'))); } } else { return json_encode(array('wg_status' => 'failure', 'wg_message' => __('Donation could not be processed.', 'woogiving'))); } } else { return json_encode(array('wg_status' => 'failure', 'wg_message' => __('Donation could not be processed.', 'woogiving'))); } }
public function process_payment($order_id) { global $woocommerce; // Get customer order $customer_order = new WC_Order($order_id); // Generate URL $justgiving_qstr = array('amount' => $customer_order->order_total, 'reference' => woogiving_create_ref($order_id), 'currency' => get_woocommerce_currency(), 'exitUrl' => plugin_dir_url(__FILE__) . 'process.php?wg_action=process&wg_order_id=' . str_replace('#', '', $customer_order->get_order_number()) . '&jg_donation_id=JUSTGIVING-DONATION-ID'); $justgiving_url = 'https://www.justgiving.com/' . rawurlencode($this->username) . '/4w350m3/donate/?' . http_build_query($justgiving_qstr); // Redirect to JustGiving return array('result' => 'success', 'redirect' => $justgiving_url); }