function get_user_worth($user_id, $args = "") { global $wpdb; $defaults = array('format_number' => 'true'); $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); $user_email = $wpdb->get_var("SELECT user_email FROM {$wpdb->users} WHERE ID = {$user_id}"); if ($have_sales = $wpdb->get_var("\n SELECT SUM(value)\n FROM {$wpdb->prefix}wpi_object_log as log\n LEFT JOIN {$wpdb->postmeta} as invoice_meta\n ON log.object_ID = invoice_meta.post_id\n WHERE action = 'add_payment'\n AND meta_value = '{$user_email}'\n AND meta_key = 'user_email'\n ")) { if (class_exists('WPI_Functions')) { if ($args['format_number'] == 'true') { return WPI_Functions::currency_format($have_sales); } else { return $have_sales; } } else { return $have_sales; } } //echo $wpdb->last_query; return false; }
/** * */ static function server_callback() { global $wpdb; //** Get request body */ $body = @file_get_contents('php://input'); $event_object = json_decode($body); switch ($event_object->type) { //** Used only for subscriptions since single payments processed without Webhook */ case 'charge.succeeded': $post_id = $wpdb->get_col("SELECT post_id\r\n FROM {$wpdb->postmeta}\r\n WHERE meta_key = '_stripe_customer_id'\r\n AND meta_value = '{$event_object->data->object->customer}'"); $invoice_object = new WPI_Invoice(); $invoice_object->load_invoice("id=" . $post_id[0]); if (empty($invoice_object->data['ID'])) { die("Can't load invoice"); } if (!class_exists('Stripe')) { require_once WPI_Path . '/third-party/stripe/lib/Stripe.php'; } $pk = trim($invoice_object->data['billing']['wpi_stripe']['settings'][$invoice_object->data['billing']['wpi_stripe']['settings']['mode']['value'] . '_secret_key']['value']); Stripe::setApiKey($pk); $event = Stripe_Event::retrieve($event_object->id); if ($event->data->object->paid == 1) { $event_amount = (double) ($event->data->object->amount / 100); $event_note = WPI_Functions::currency_format(abs($event_amount), $invoice_object->data['invoice_id']) . ' ' . __('Stripe Subscription Payment', WPI); $event_type = 'add_payment'; $invoice_object->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); $invoice_object->save_invoice(); } break; case 'customer.subscription.deleted': $post_id = $wpdb->get_col("SELECT post_id\r\n FROM {$wpdb->postmeta}\r\n WHERE meta_key = '_stripe_customer_id'\r\n AND meta_value = '{$event_object->data->object->customer}'"); $invoice_object = new WPI_Invoice(); $invoice_object->load_invoice("id=" . $post_id[0]); if (empty($invoice_object->data['ID'])) { die("Can't load invoice"); } if (!class_exists('Stripe')) { require_once WPI_Path . '/third-party/stripe/lib/Stripe.php'; } $pk = trim($invoice_object->data['billing']['wpi_stripe']['settings'][$invoice_object->data['billing']['wpi_stripe']['settings']['mode']['value'] . '_secret_key']['value']); Stripe::setApiKey($pk); $event = Stripe_Event::retrieve($event_object->id); if ($event->data->object->status == 'canceled') { $invoice_object->add_entry("attribute=invoice¬e=" . __('Stripe Subscription has been canceled', WPI) . "&type=update"); $invoice_object->save_invoice(); wp_invoice_mark_as_paid($invoice_object->data['invoice_id']); } break; default: break; } }
/** * Handler for PayPal IPN queries * @author korotkov@ud * Full callback URL: http://domain/wp-admin/admin-ajax.php?action=wpi_gateway_server_callback&type=wpi_paypal */ static function server_callback() { if (empty($_POST)) { die(__('Direct access not allowed', WPI)); } $invoice = new WPI_Invoice(); $invoice->load_invoice("id={$_POST['invoice']}"); /** Verify callback request */ if (self::_ipn_verified($invoice)) { switch ($_POST['txn_type']) { /** New PayPal Subscription */ case 'subscr_signup': /** PayPal Subscription created */ WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['invoice']), 'invoice', 'update', '', __('PayPal Subscription created', WPI)); wp_invoice_mark_as_pending($_POST['invoice']); do_action('wpi_paypal_subscr_signup_ipn', $_POST); break; case 'subscr_cancel': /** PayPal Subscription cancelled */ WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['invoice']), 'invoice', 'update', '', __('PayPal Subscription cancelled', WPI)); do_action('wpi_paypal_subscr_cancel_ipn', $_POST); break; case 'subscr_failed': /** PayPal Subscription failed */ WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['invoice']), 'invoice', 'update', '', __('PayPal Subscription payment failed', WPI)); do_action('wpi_paypal_subscr_failed_ipn', $_POST); break; case 'subscr_payment': /** Payment of Subscription */ switch ($_POST['payment_status']) { case 'Completed': /** Add payment amount */ $event_note = sprintf(__('%1s paid for subscription %2s', WPI), WPI_Functions::currency_format(abs($_POST['mc_gross']), $_POST['invoice']), $_POST['subscr_id']); $event_amount = (double) $_POST['mc_gross']; $event_type = 'add_payment'; /** Log balance changes */ $invoice->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); $invoice->save_invoice(); send_notification($invoice->data); break; default: break; } do_action('wpi_paypal_subscr_payment_ipn', $_POST); break; case 'subscr_eot': /** PayPal Subscription end of term */ WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['invoice']), 'invoice', 'update', '', __('PayPal Subscription term is finished', WPI)); wp_invoice_mark_as_paid($_POST['invoice'], $check = false); do_action('wpi_paypal_subscr_eot_ipn', $_POST); break; case 'subscr_modify': /** PayPal Subscription modified */ WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['invoice']), 'invoice', 'update', '', __('PayPal Subscription modified', WPI)); do_action('wpi_paypal_subscr_modify_ipn', $_POST); break; case 'web_accept': /** PayPal simple button */ switch ($_POST['payment_status']) { case 'Pending': /** Mark invoice as Pending */ wp_invoice_mark_as_pending($_POST['invoice']); do_action('wpi_paypal_pending_ipn', $_POST); break; case 'Completed': /** Add payment amount */ $event_note = sprintf(__('%s paid via PayPal', WPI), WPI_Functions::currency_format(abs($_POST['mc_gross']), $_POST['invoice'])); $event_amount = (double) $_POST['mc_gross']; $event_type = 'add_payment'; /** Log balance changes */ $invoice->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); /** Log payer email */ $payer_email = sprintf(__("PayPal Payer email: %s", WPI), $_POST['payer_email']); $invoice->add_entry("attribute=invoice¬e={$payer_email}&type=update"); $invoice->save_invoice(); /** ... and mark invoice as paid */ wp_invoice_mark_as_paid($_POST['invoice'], $check = true); send_notification($invoice->data); do_action('wpi_paypal_complete_ipn', $_POST); break; default: break; } break; case 'cart': /** PayPal Cart. Used for SPC */ switch ($_POST['payment_status']) { case 'Pending': /** Mark invoice as Pending */ wp_invoice_mark_as_pending($_POST['invoice']); do_action('wpi_paypal_pending_ipn', $_POST); break; case 'Completed': /** Add payment amount */ $event_note = sprintf(__('%s paid via PayPal', WPI), WPI_Functions::currency_format(abs($_POST['mc_gross']), $_POST['invoice'])); $event_amount = (double) $_POST['mc_gross']; $event_type = 'add_payment'; /** Log balance changes */ $invoice->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); /** Log payer email */ $payer_email = sprintf(__("PayPal Payer email: %s", WPI), $_POST['payer_email']); $invoice->add_entry("attribute=invoice¬e={$payer_email}&type=update"); $invoice->save_invoice(); /** ... and mark invoice as paid */ wp_invoice_mark_as_paid($_POST['invoice'], $check = true); send_notification($invoice->data); do_action('wpi_paypal_complete_ipn', $_POST); break; default: break; } break; default: break; } echo ' '; } }
/** * Handler for Silent Post Url */ static function server_callback() { $arb = false; $fields = array(); foreach ($_REQUEST as $name => $value) { $fields[$name] = $value; if ($name == 'x_subscription_id') { $arb = true; } } // Handle recurring billing payments if ($arb == true && $fields['x_response_code'] == 1) { $paynum = $fields['x_subscription_paynum']; $subscription_id = $fields['x_subscription_id']; $amount = $fields['x_amount']; $invoice_id = wpi_post_id_to_invoice_id(wpi_subscription_id_to_post_id($subscription_id)); $invoice_obj = new WPI_Invoice(); $invoice_obj->load_invoice("id={$invoice_id}"); // Add payment amount $event_note = WPI_Functions::currency_format(abs($amount), $invoice_id) . ". ARB payment {$paynum} of {$invoice_obj->data['recurring']['wpi_authorize']['cycles']}"; $event_amount = $amount; $event_type = 'add_payment'; $invoice_obj->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); // Complete subscription if last payment done if ($invoice_obj->data['recurring']['wpi_authorize']['cycles'] <= $paynum) { WPI_Functions::log_event(wpi_invoice_id_to_post_id($invoice_id), 'invoice', 'update', '', __('Subscription completely paid', WPI)); wp_invoice_mark_as_paid($invoice_id); } $invoice_obj->save_invoice(); } }
/** Process special invoice-related event */ function process_manual_event() { global $wpdb; $invoice_id = $_REQUEST['invoice_id']; $event_type = $_REQUEST['event_type']; $event_amount = $_REQUEST['event_amount']; $event_note = $_REQUEST['event_note']; $event_date = $_REQUEST['event_date']; $event_time = $_REQUEST['event_time']; $event_tax = $_REQUEST['event_tax']; $timestamp = strtotime( $event_date.' '.$event_time ); if(empty($event_note) || empty($event_amount) || !is_numeric($event_amount)) { die( json_encode( array('success' => 'false', 'message' => __('Please enter a note and numeric amount.', WPI)) ) ); } if($event_type == 'add_payment' && !empty($event_amount)) { $event_amount = $event_amount; $event_note = WPI_Functions::currency_format(abs($event_amount), $invoice_id)." " . __('paid in', WPI) . " - $event_note"; } if($event_type == 'add_charge' && !empty($event_amount)) { $name = $event_note; $event_note = "".WPI_Functions::currency_format($event_amount, $invoice_id)." " . __('charge added', WPI) . " - $event_note"; $core = WPI_Core::getInstance(); $charge_item = $core->Functions->add_itemized_charge( $invoice_id, $name, $event_amount, $event_tax ); } if($event_type == 'do_adjustment' && !empty($event_amount)) { $event_note = WPI_Functions::currency_format($event_amount, $invoice_id)." " . __('adjusted', WPI) . " - $event_note"; } $invoice = new WPI_Invoice(); $invoice->load_invoice("id=$invoice_id"); $insert_id = $invoice->add_entry("attribute=balance¬e=$event_note&amount=$event_amount&type=$event_type&time=$timestamp"); if($insert_id) { $response = array( 'success' => 'true', 'message' => sprintf(__('Event Added: %1s.', WPI), $event_note)); } else { $response = array( 'success' => 'false', 'message' => sprintf(__('Could not save entry in invoice log. %1s', WPI), '')); } $invoice->save_invoice(); if ( !empty( $charge_item ) && $event_type == 'add_charge' ) { $response['charge_item'] = $charge_item; } die( json_encode( $response ) ); }
/** * Handler for 2Checkout Callback * @author Craig Christenson * Full callback URL: http://domain/wp-admin/admin-ajax.php?action=wpi_gateway_server_callback&type=wpi_twocheckout */ static function server_callback() { if (empty($_REQUEST)) { die(__('Direct access not allowed', WPI)); } $invoice = new WPI_Invoice(); $invoice->load_invoice("id={$_REQUEST['merchant_order_id']}"); /** Verify callback request */ if (self::_ipn_verified($invoice)) { if ($_REQUEST['key']) { $event_note = sprintf(__('%s paid via 2Checkout', WPI), WPI_Functions::currency_format(abs($_REQUEST['total']), $_REQUEST['merchant_order_id'])); $event_amount = (double) $_REQUEST['total']; $event_type = 'add_payment'; /** Log balance changes */ $invoice->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); /** Log payer email */ $payer_email = sprintf(__("2Checkout buyer email: %s", WPI), $_REQUEST['email']); $invoice->add_entry("attribute=invoice¬e={$payer_email}&type=update"); $invoice->save_invoice(); /** ... and mark invoice as paid */ wp_invoice_mark_as_paid($_REQUEST['invoice_id'], $check = true); send_notification($invoice->data); echo '<script type="text/javascript">window.location="' . get_invoice_permalink($invoice->data['ID']) . '";</script>'; /** Handle INS messages */ } elseif ($_POST['md5_hash']) { switch ($_POST['message_type']) { case 'FRAUD_STATUS_CHANGED': if ($_POST['fraud_status'] == 'pass') { WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['vendor_order_id']), 'invoice', 'update', '', __('Passed 2Checkout fraud review.', WPI)); } elseif (condition) { WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['vendor_order_id']), 'invoice', 'update', '', __('Failed 2Checkout fraud review.', WPI)); wp_invoice_mark_as_pending($_POST['vendor_order_id']); } break; case 'RECURRING_STOPPED': WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['vendor_order_id']), 'invoice', 'update', '', __('Recurring billing stopped.', WPI)); break; case 'RECURRING_INSTALLMENT_FAILED': WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['vendor_order_id']), 'invoice', 'update', '', __('Recurring installment failed.', WPI)); break; case 'RECURRING_INSTALLMENT_SUCCESS': $event_note = sprintf(__('%1s paid for subscription %2s', WPI), WPI_Functions::currency_format(abs($_POST['item_rec_list_amount_1']), $_POST['vendor_order_id']), $_POST['sale_id']); $event_amount = (double) $_POST['item_rec_list_amount_1']; $event_type = 'add_payment'; $invoice->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); $invoice->save_invoice(); send_notification($invoice->data); break; case 'RECURRING_COMPLETE': WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['vendor_order_id']), 'invoice', 'update', '', __('Recurring installments completed.', WPI)); wp_invoice_mark_as_paid($_POST['invoice'], $check = false); break; case 'RECURRING_RESTARTED': WPI_Functions::log_event(wpi_invoice_id_to_post_id($_POST['vendor_order_id']), 'invoice', 'update', '', __('Recurring sale restarted.', WPI)); break; default: break; } } } }
/** * Process special invoice-related event */ static function process_manual_event() { $invoice_id = $_REQUEST['invoice_id']; $event_type = $_REQUEST['event_type']; $event_amount = $_REQUEST['event_amount']; $event_note = $_REQUEST['event_note']; $event_date = $_REQUEST['event_date']; $event_time = $_REQUEST['event_time']; $event_tax = $_REQUEST['event_tax']; $timestamp = strtotime($event_date . ' ' . $event_time) - get_option('gmt_offset') * 60 * 60; if (empty($event_note) || empty($event_amount) || !is_numeric($event_amount)) { die(json_encode(array('success' => 'false', 'message' => __('Please enter a note and numeric amount.', WPI)))); } switch ($event_type) { case WPI_EVENT_TYPE_ADD_PAYMENT: if (!empty($event_amount)) { $event_note = WPI_Functions::currency_format(abs($event_amount), $invoice_id) . " " . __('paid in', WPI) . " - {$event_note}"; } break; case WPI_EVENT_TYPE_ADD_CHARGE: if (!empty($event_amount)) { $name = $event_note; $event_note = WPI_Functions::currency_format($event_amount, $invoice_id) . " " . (!empty($event_tax) ? '+' . $event_tax . '%' : '') . " " . __('charge added', WPI) . " - {$event_note}"; $core = WPI_Core::getInstance(); $charge_item = $core->Functions->add_itemized_charge($invoice_id, $name, $event_amount, $event_tax); } break; case WPI_EVENT_TYPE_ADD_ADJUSTMENT: if (!empty($event_amount)) { $event_note = WPI_Functions::currency_format($event_amount, $invoice_id) . " " . __('adjusted', WPI) . " - {$event_note}"; } break; case WPI_EVENT_TYPE_ADD_REFUND: if (!empty($event_amount)) { $event_amount = abs((double) $event_amount); $event_note = WPI_Functions::currency_format($event_amount, $invoice_id) . " " . __('refunded', WPI) . " - {$event_note}"; } break; default: break; } $invoice = new WPI_Invoice(); $invoice->load_invoice("id={$invoice_id}"); $insert_id = $invoice->add_entry(array('attribute' => 'balance', 'note' => $event_note, 'amount' => $event_amount, 'type' => $event_type, 'time' => $timestamp)); if ($insert_id) { $response = array('success' => 'true', 'message' => sprintf(__('Event Added: %1s.', WPI), $event_note)); } else { $response = array('success' => 'false', 'message' => sprintf(__('Could not save entry in invoice log. %1s', WPI), '')); } $invoice->save_invoice(); if (!empty($charge_item) && $event_type == 'add_charge') { $response['charge_item'] = $charge_item; } die(json_encode($response)); }
/** * Merchant CB handler */ static function server_callback() { if (empty($_POST)) { die(__('Direct access not allowed', WPI)); } $invoice = new WPI_Invoice(); $invoice->load_invoice("id={$_POST['ik_pm_no']}"); if ($_POST['ik_inv_st'] != 'success') { header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error [Cannot process payment]', true, 500); return; } if (!self::_hash_verified($invoice)) { header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error [Hash or Shop ID is wrong]', true, 500); return; } if (get_post_meta($invoice->data['ID'], 'wpi_processed_by_interkassa', 1) == 'true') { header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error [Already processed]', true, 500); return; } update_post_meta($invoice->data['ID'], 'wpi_processed_by_interkassa', 'true'); /** Add payment amount */ $event_note = sprintf(__('%s paid via InterKassa [%s]', WPI), WPI_Functions::currency_format(abs($_POST['ik_am']), $_POST['ik_pm_no']), $_POST['ik_pw_via']); $event_amount = (double) $_POST['ik_am']; $event_type = 'add_payment'; //** Log balance changes */ $invoice->add_entry("attribute=balance¬e={$event_note}&amount={$event_amount}&type={$event_type}"); //** Log payer email */ $trans_id = sprintf(__("Transaction ID: %s", WPI), $_POST['ik_trn_id']); $invoice->add_entry("attribute=invoice¬e={$trans_id}&type=update"); $invoice->save_invoice(); //** ... and mark invoice as paid */ wp_invoice_mark_as_paid($_POST['ik_pm_no'], $check = true); send_notification($invoice->data); echo 'OK'; }