/** * 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); } }
/** * Handle amazon payment import request. * * @return The url to display on completion. */ function command_amazon_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; message_register("Processing " . count($data) . " row(s)"); foreach ($data as $row) { // Ignore withdrawals, holds, and failures if (strtolower($row['Type']) !== 'payment') { message_register("Ignoring row of type: " . $row['Type']); continue; } if (strtolower($row['To/From']) !== 'from') { message_register("Ignoring outgoing payment"); continue; } if (strtolower($row['Status']) !== 'completed') { message_register("Ignoring payment with status: " . $row['Status']); continue; } // 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) { message_register("Skipping previously imported payment: " . $row['Transaction ID']); continue; } // Parse value $value = payment_parse_currency($row['Amount']); // Create payment object $payment = array('date' => date('Y-m-d', strtotime($row['Date'])), 'code' => $value['code'], 'value' => $value['value'], 'description' => $row['Name'] . ' Amazon Payment', 'method' => 'amazon', 'confirmation' => $row['Transaction ID'], 'notes' => $row['notes'], 'amazon_name' => $row['Name']); // Check if the amazon name is linked to a contact $opts = array('filter' => array('amazon_name' => $row['Name'])); $contact_data = amazon_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'); }
/** * 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'); }
edit_save(); break; case 'payments': display_payments_form(); break; case 'edit_payment': admin_check_permissions('manage_payments'); edit_payment(); break; case 'del_payment': admin_check_permissions('manage_payments'); del_payment(); break; case 'payment_save': admin_check_permissions('manage_payments'); payment_save(); break; case 'payment_add': admin_check_permissions('manage_payments'); payment_add(); break; case 'actions': display_actions(); break; case 'move': admin_check_permissions('edit_users'); move_user(); break; case 'email': email_to_user_from_admin(); break;
// 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();
/** * 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'); }