Ejemplo n.º 1
0
/**
 * Run billing for a single membership.
 * @param $membership The membership to bill.
 * @param $until Bill up to and including this day.
 * @param $after Start billing after this day or beginning of time if not given.
 */
function _billing_bill_membership($membership, $until, $after = '')
{
    $price = payment_parse_currency($membership['plan']['price']);
    $price['value'] *= -1;
    // Day to bill on
    // TODO allow plans to specify their start date -Ed 2013-01-20
    $day_of_month = 1;
    $until_date = strtotime($until);
    $membership_start = strtotime($membership['start']);
    // Find first unbilled day
    if (empty($after) || $membership_start > strtotime($after)) {
        // Membership started on or after first billable day
        // Start billing on membership start date
        $period_start = $membership_start;
    } else {
        // Membership started before first billable date
        // Find first billing period starting after $after
        $begin = strtotime($after . ' +1 day');
        $days = _billing_days_remaining($day_of_month, getdate($begin));
        $period_start = strtotime("+{$days} days", $begin);
    }
    // Check for partial month and bill prorated
    $period_info = getdate($period_start);
    if ($period_info['mday'] != $day_of_month) {
        // Parital month, prorate
        $prorated = _billing_prorate($period_info, $day_of_month, $price);
        $payment = array('date' => date('Y-m-d', $period_start), 'description' => 'Dues: ' . $membership['plan']['name'], 'code' => $prorated['code'], 'value' => $prorated['value'], 'credit_cid' => $membership['cid'], 'method' => 'cash');
        payment_save($payment);
        // Advance to beginning of first full period
        $days = _billing_days_remaining($day_of_month, $period_info);
        $period_start = strtotime("+{$days} days", $period_start);
    }
    // Bill each full billing period
    while ($period_start < $until_date) {
        $payment = array('date' => date('Y-m-d', $period_start), 'description' => 'Dues: ' . $membership['plan']['name'], 'code' => $price['code'], 'value' => $price['value'], 'credit_cid' => $membership['cid'], 'method' => 'cash');
        payment_save($payment);
        // Advance to next billing period
        $period_start = strtotime('+1 month', $period_start);
    }
}
Ejemplo n.º 2
0
/**
 * Return themed html for prorated first month button.
 */
function theme_amazon_payment_first_month($cid)
{
    if (!function_exists('billing_revision')) {
        return 'Prorated dues payment requires billing module.';
    }
    $contact = crm_get_one('contact', array('cid' => $cid));
    // Calculate fraction of the billing period
    $mship = end($contact['member']['membership']);
    $date = getdate(strtotime($mship['start']));
    $period = billing_days_in_period($date);
    $day = $date['mday'];
    $fraction = ($period - $day + 1.0) / $period;
    // Get payment amount
    $due = payment_parse_currency($mship['plan']['price']);
    $due['value'] = ceil($due['value'] * $fraction);
    $html .= $due['value'];
    // Create button
    $html = "<fieldset><legend>First month prorated dues</legend>";
    $params = array('referenceId' => $cid, 'amount' => $due['code'] . ' ' . payment_format_currency($due, false), 'description' => 'CRM Dues Payment');
    $amount = payment_format_currency($due);
    $html .= "<p><strong>First month's dues:</strong> {$amount}</p>";
    if ($due['value'] > 0) {
        $html .= theme('amazon_payment_button', $cid, $params);
    }
    $html .= '</fieldset>';
    return $html;
}
Ejemplo n.º 3
0
// Check for success
if (strpos($result, '<VerifySignatureResult><VerificationStatus>Success</VerificationStatus></VerifySignatureResult>') === false) {
    die;
}
// Check if the payment already exists
// Skip transactions that have already been imported
$payment_opts = array('filter' => array('confirmation' => $_POST['transactionId']));
$data = crm_get_data('payment', $payment_opts);
if (count($data) > 0) {
    die;
}
// Parse the data and insert into the database
// 'USD 12.34' goes to ['USD', '1234']
$parts = explode(' ', $_POST['transactionAmount']);
file_put_contents($debug, print_r($parts, true) . "\n", FILE_APPEND);
$payment_amount = payment_parse_currency($parts[1], $parts[0]);
// Determine cid
$cid = $_POST['referenceId'];
if (empty($cid)) {
    // Check if the amazon name is linked to a contact
    $opts = array('filter' => array('amazon_name' => $_POST['buyerName']));
    $contact_data = amazon_payment_contact_data($opts);
    if (count($contact_data) > 0) {
        $cid = $contact_data[0]['cid'];
    }
}
$payment = array('date' => date('Y-m-d', $_POST['transactionDate']), 'credit_cid' => $cid, 'code' => $payment_amount['code'], 'value' => (string) $payment_amount['value'], 'description' => $_POST['paymentReason'], 'method' => 'amazon', 'confirmation' => $_POST['transactionId'], 'amazon_name' => $_POST['buyerName']);
$payment = payment_save($payment);
// Log out
$_SESSION['userId'] = 0;
session_destroy();
Ejemplo n.º 4
0
/**
 * Handle payment edit request.
 *
 * @return The url to display on completion.
 */
function command_payment_edit()
{
    // Verify permissions
    if (!user_access('payment_edit')) {
        error_register('Permission denied: payment_edit');
        return crm_url('payments');
    }
    // Parse and save payment
    $payment = $_POST;
    $value = payment_parse_currency($_POST['value'], $_POST['code']);
    $payment['code'] = $value['code'];
    $payment['value'] = $value['value'];
    payment_save($payment);
    message_register('1 payment updated.');
    return crm_url('payments');
}
Ejemplo n.º 5
0
/**
 * Handle paypal payment import request.
 *
 * @return The url to display on completion.
 */
function command_paypal_payment_import()
{
    if (!user_access('payment_edit')) {
        error_register('User does not have permission: payment_edit');
        return crm_url('payments');
    }
    if (!array_key_exists('payment-file', $_FILES)) {
        error_register('No payment file uploaded');
        return crm_url('payments&tab=import');
    }
    $csv = file_get_contents($_FILES['payment-file']['tmp_name']);
    $data = csv_parse($csv);
    $count = 0;
    foreach ($data as $row) {
        // Skip transactions that have already been imported
        $payment_opts = array('filter' => array('confirmation' => $row['Transaction ID']));
        $data = payment_data($payment_opts);
        if (count($data) > 0) {
            continue;
        }
        // Parse value
        $value = payment_parse_currency($row['Gross']);
        // Create payment object
        $payment = array('date' => date('Y-m-d', strtotime($row['Date'])), 'code' => $value['code'], 'value' => $value['value'], 'description' => $row['Name'] . ' Paypal Payment', 'method' => 'paypal', 'confirmation' => $row['Transaction ID'], 'notes' => $row['Item Title'], 'paypal_email' => $row['From Email Address']);
        // Check if the paypal email is linked to a contact
        $opts = array('filter' => array('paypal_email' => $row['From Email Address']));
        $contact_data = paypal_payment_contact_data($opts);
        if (count($contact_data) > 0) {
            $payment['credit_cid'] = $contact_data[0]['cid'];
        }
        // Save the payment
        $payment = payment_save($payment);
        $count++;
    }
    message_register("Successfully imported {$count} payment(s)");
    return crm_url('payments');
}