Skip to content
Snippets Groups Projects
Commit ae447930 authored by Joshua Walker's avatar Joshua Walker
Browse files

Merge pull request #14 from anemirovsky/4.3-1.7

Fix error when number of installments not filled out.
Cancel previous subscription when user starts new recurring contribution and already had one.
parents dcbc94d5 e5b648c4
Branches
Tags
No related merge requests found
......@@ -433,6 +433,19 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
VALUES (%1)", $query_params);
}
// If a contact/customer has an existing active recurring
// contribution/subscription, Stripe will update the existing subscription.
// If only the amount has changed not the installments/frequency, Stripe
// will not charge the card again until the next installment is due. This
// does not work well for CiviCRM, since CiviCRM creates a new recurring
// contribution along with a new initial contribution, so it expects the
// card to be charged immediately. So, since Stripe only supports one
// subscription per customer, we have to cancel the existing active
// subscription first.
if (!empty($stripe_customer->subscription) && $stripe_customer->subscription-> status == 'active') {
$stripe_customer->cancelSubscription();
}
// Attach the Subscription to the Stripe Customer.
$stripe_response = $stripe_customer->updateSubscription(array(
'prorate' => FALSE, 'plan' => $plan_id));
......@@ -472,13 +485,21 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$query_params = array(
1 => array($stripe_customer->id, 'String'),
2 => array($invoice_id, 'String'),
3 => array($end_time, 'Integer'),
);
// Insert the new Stripe Subscription info.
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, end_time, is_live)
VALUES (%1, %2, %3, '$transaction_mode')", $query_params);
// Set end_time to NULL if installments are ongoing indefinitely
if (empty($installments)) {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, is_live)
VALUES (%1, %2, '$transaction_mode')", $query_params);
} else {
// Add the end time to the query params.
$query_params[3] = array($end_time, 'Integer');
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_subscriptions
(customer_id, invoice_id, end_time, is_live)
VALUES (%1, %2, %3, '$transaction_mode')", $query_params);
}
$params['trxn_id'] = $stripe_response->id;
$params['fee_amount'] = $stripe_response->fee / 100;
......
......@@ -139,7 +139,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
%11, %12, '1', %13)",
$query_params);
if ($time_compare > $end_time) {
if (!empty($end_time) && $time_compare > $end_time) {
$end_date = date("Y-m-d H:i:s", $end_time);
// Final payment. Recurring contribution complete.
$stripe_customer->cancelSubscription();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment