Ejemplo n.º 1
0
/**
 * Converted to WP 2.0
 * Archives an invoice, or multiple invoices.
 *
 * @global type $wpdb
 *
 * @param type $invoice_id
 *
 * @return type
 */
function wpi_archive_invoice($invoice_id)
{
    //** Check to see if array is passed or single. */
    if (is_array($invoice_id)) {
        $counter = 0;
        foreach ($invoice_id as $single_invoice_id) {
            $this_invoice = new WPI_Invoice();
            $this_invoice->load_invoice("id={$single_invoice_id}");
            $this_invoice->set("status=archive");
            $this_invoice->add_entry(__("Archived.", WPI));
            if ($this_invoice->save_invoice()) {
                $counter++;
            }
        }
        return __("{$counter}  invoice(s) archived.", WPI);
    } else {
        $this_invoice = new WPI_Invoice();
        $this_invoice->load_invoice("id={$invoice_id}");
        $this_invoice->set("status=archive");
        $this_invoice->add_entry(__("Archived.", WPI));
        if ($this_invoice->save_invoice()) {
            return __('Successfully archived.', WPI);
        }
    }
}
/**
    Converted to WP 2.0
    Archives an invoice, or multiple invoices.
*/
    function wpi_archive_invoice($invoice_id) {
        global $wpdb;
        // Check to see if array is passed or single.
        if(is_array($invoice_id))
        {
            $counter=0;
            foreach ($invoice_id as $single_invoice_id) {
                $this_invoice = new WPI_Invoice();
                $this_invoice->load_invoice("id=$single_invoice_id");
                $this_invoice->set("status=archive");
                $this_invoice->add_entry(__("Archived.", WPI));
                if($this_invoice->save_invoice())
                    $counter++;
            }
            return __("$counter  invoice(s) archived.", WPI);
        } else {
            $this_invoice = new WPI_Invoice();
            $this_invoice->load_invoice("id=$invoice_id");
            $this_invoice->set("status=archive");
            $this_invoice->add_entry(__("Archived.", WPI));
            if($this_invoice->save_invoice())
                return __('Successfully archived.', WPI);
        }
    }
Ejemplo n.º 3
0
 /**
  *
  */
 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&note={$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&note=" . __('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;
     }
 }
Ejemplo n.º 4
0
 /**
  * Run import process
  *
  * @global object $wpdb
  * @global array $wpi_settings
  */
 function do_import()
 {
     global $wpdb, $wpi_settings;
     /* Get plugin Singleton object */
     $core = WPI_Core::getInstance();
     /* Try to import General Plugin Settings from old version */
     $legacy_settings = self::get_legacy_settings();
     if (!empty($legacy_settings)) {
         $core->Settings->SaveSettings($legacy_settings);
         $core->Functions->log(__("Web Invoice setting options were successfully imported.", WPI));
     }
     /* Boolean variables which show 'legacy logs' data migration's status */
     $legacy_logs = false;
     $legacy_logs_import_error = false;
     /* Try to import Invoices from Web Invoice plugin */
     $legacy_invoices = self::get_legacy_invoices();
     if (is_array($legacy_invoices) && !empty($legacy_invoices)) {
         $errors = false;
         foreach ($legacy_invoices as $i) {
             $invoice_id = $core->Functions->save_invoice($i, array('type' => 'import'));
             if ($invoice_id) {
                 //* Try to get Logs of Invoices from the old version */
                 $logs = self::get_legacy_logs_by_id($invoice_id);
                 if (!empty($logs)) {
                     /* Imports logs to new table. */
                     if (self::import_logs($logs)) {
                         $legacy_logs = true;
                     } else {
                         $legacy_logs_import_error = true;
                     }
                 }
                 //* If invoice has 'paid' status we should add log of payment. */
                 if ($i['post_status'] == 'paid') {
                     $invoice = new WPI_Invoice();
                     $invoice->load_invoice("id={$invoice_id}");
                     if ($i['recurring']['active'] == 'on' && !empty($i['recurring']['cycles'])) {
                         $event_amount = $i['amount'] * $i['recurring']['cycles'];
                     } else {
                         $event_amount = $i['amount'];
                     }
                     $event_note = "Automatically created using Web Invoice log data";
                     $event_note = $core->Functions->currency_format(abs($event_amount), $invoice_id) . " paid in - {$event_note}";
                     $timestamp = time();
                     $invoice->add_entry("attribute=balance&note={$event_note}&amount={$event_amount}&type=add_payment&time={$timestamp}");
                     $invoice->save_invoice();
                 }
             } else {
                 $errors = true;
             }
         }
         if ($errors == true) {
             $core->Functions->log(__("Invoices from Web Invoice plugin were migrated with errors.", WPI));
         } else {
             $core->Functions->log(__("Invoices from Web Invoice plugin were successfully migrated.", WPI));
         }
     }
     if ($legacy_logs == true) {
         if ($legacy_logs_import_error == false) {
             $core->Functions->log(__("Log data from Web Invoice plugin were successfully migrated.", WPI));
         } else {
             $core->Functions->log(__("Log data from Web Invoice plugin were migrated with errors.", WPI));
         }
     }
     //* Mark as imported */
     update_option('wpi_web_invoice_imported', 1);
 }
Ejemplo n.º 5
0
 /**
  * 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&note={$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();
     }
 }
Ejemplo n.º 6
0
 /**
  * Pay invoice by ID
  *
  * @param type $args
  *
  * @return WP_Error|WPI_Invoice
  */
 function pay_invoice($args = array())
 {
     //** Default arguments */
     $defaults = array('ID' => false, 'amount' => false);
     //** Parse arguments */
     extract(wp_parse_args($args, $defaults));
     //** Check */
     if (!$ID) {
         return new WP_Error('wp.invoice', __('Argument "ID" is required.', WPI), $args);
     }
     if (!$amount) {
         return new WP_Error('wp.invoice', __('Argument "amount" is required.', WPI), $args);
     }
     if (!is_numeric($amount)) {
         return new WP_Error('wp.invoice', __('Argument "amount" is malformed.', WPI), $args);
     }
     //** New Invoice object */
     $invoice = new WPI_Invoice();
     //** Load invoice by ID */
     $invoice->load_invoice(array('id' => $ID));
     //** Check */
     if (!empty($invoice->error)) {
         return new WP_Error('wp.invoice', __('Invoice not found', WPI), $args);
     }
     //** Pay only if status if not paid */
     if ($invoice->data['post_status'] == 'paid') {
         return new WP_Error('wp.invoice', __('Invoice is completely paid. Payments are not acceptable anymore.', WPI), $args);
     }
     //** Check amount */
     if ((double) $invoice->data['net'] < (double) $amount) {
         return new WP_Error('wp.invoice', __('Cannot pay more that the balance is. Maximum is ' . $invoice->data['net'], WPI), $args);
     }
     //** Handle partial */
     if ((double) $invoice->data['net'] > (double) $amount) {
         if (empty($invoice->data['deposit_amount'])) {
             return new WP_Error('wp.invoice', __('Partial payments are not allowed. Pay minimum is ' . $invoice->data['net'], WPI), $args);
         }
         if ((double) $amount < (double) $invoice->data['deposit_amount']) {
             return new WP_Error('wp.invoice', __('Minimum allowed payment is ' . $invoice->data['deposit_amount'], WPI), $args);
         }
     }
     //** Add payment item */
     $invoice->add_entry(array('attribute' => 'balance', 'note' => 'Paid ' . (double) $amount . ' ' . $invoice->data['default_currency_code'] . ' via XML-RPC API', 'amount' => (double) $amount, 'type' => 'add_payment'));
     //** Save to be sure totals recalculated */
     $invoice->save_invoice();
     //** Load again to get changes */
     $invoice = new WPI_Invoice();
     $invoice->load_invoice(array('id' => $ID));
     return $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&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 ) );
    }
Ejemplo n.º 8
0
 /**
  * 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) ? '&#43;' . $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));
 }