/**
  * Create invoice when estimate is accepted.
  * @param  object $doc estimate or invoice object
  * @return int cloned invoice id.
  */
 public static function create_payment_when_invoice_marked_as_paid($doc)
 {
     if (!is_a($doc, 'SI_Invoice')) {
         return;
     }
     // Check if status changed was to approved.
     if ($doc->get_status() != SI_Invoice::STATUS_PAID) {
         return;
     }
     $balance = $doc->get_balance();
     if ($balance < 0.01) {
         return;
     }
     SI_Admin_Payment::create_admin_payment($doc->get_id(), $balance, '', 'Now', self::__('This payment was automatically added to settle the balance after it was marked as "Paid".'));
 }
        if (!isset($_REQUEST['sa_metabox_payments_nonce'])) {
            self::ajax_fail('Forget something?');
        }
        $nonce = $_REQUEST['sa_metabox_payments_nonce'];
        if (!wp_verify_nonce($nonce, SI_Controller::NONCE)) {
            self::ajax_fail('Not going to fall for it!');
        }
        if (!isset($_REQUEST['sa_metabox_invoice_id'])) {
            self::ajax_fail('Forget something?');
        }
        if (get_post_type($_REQUEST['sa_metabox_invoice_id']) != SI_Invoice::POST_TYPE) {
            self::ajax_fail('Error: Invoice PT mismatch.');
        }
        $amount = isset($_REQUEST['sa_metabox_payment_amount']) ? $_REQUEST['sa_metabox_payment_amount'] : 0;
        $number = isset($_REQUEST['sa_metabox_payment_transaction_id']) ? $_REQUEST['sa_metabox_payment_transaction_id'] : '';
        $date = isset($_REQUEST['sa_metabox_payment_date']) ? $_REQUEST['sa_metabox_payment_date'] : '';
        $notes = isset($_REQUEST['sa_metabox_payment_notes']) ? $_REQUEST['sa_metabox_payment_notes'] : '';
        if (!$amount) {
            self::ajax_fail('No payment amount set.');
        }
        self::create_admin_payment($_REQUEST['sa_metabox_invoice_id'], $amount, $number, $date, $notes);
        header('Content-type: application/json');
        if (self::DEBUG) {
            header('Access-Control-Allow-Origin: *');
        }
        echo wp_json_encode(array('response' => si__('Payment Added')));
        exit;
    }
}
SI_Admin_Payment::init();
// Since it's not a registered payment processor, init it when file is loaded.