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

Merge pull request #141 from aghstrategies/multi-stripe-4.7

 Multiple Stripe processor accounts cause IPN fail (change vs. 4.7)
parents bcaeb35a 0093e39d
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,27 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
$test_mode = 1;
}
$stripe_key = CRM_Core_DAO::singleValueQuery("SELECT pp.user_name FROM civicrm_payment_processor pp INNER JOIN civicrm_payment_processor_type ppt on pp.payment_processor_type_id = ppt.id AND ppt.name = 'Stripe' WHERE is_test = '$test_mode'");
$processorId = CRM_Utils_Request::retrieve('ppid', 'Integer');
try {
if (empty($processorId)) {
$stripe_key = civicrm_api3('PaymentProcessor', 'getvalue', array(
'return' => 'user_name',
'payment_processor_type_id' => 'Stripe',
'is_test' => $test_mode,
'is_active' => 1,
'options' => array('limit' => 1),
));
}
else {
$stripe_key = civicrm_api3('PaymentProcessor', 'getvalue', array(
'return' => 'user_name',
'id' => $processorId,
));
}
}
catch (CiviCRM_API3_Exception $e) {
CRM_Core_Error::fatal('Cannot find Stripe API key: ' . $e->getMessage());
}
require_once ("packages/stripe-php/lib/Stripe.php");
Stripe::setApiKey($stripe_key);
......@@ -39,7 +59,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
}
catch(Exception $e) {
CRM_Core_Error::Fatal("Failed to retrieve Stripe charge. Message: " . $e->getMessage());
exit();
}
// Find the recurring contribution in CiviCRM by mapping it from Stripe.
......@@ -59,7 +78,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
$end_time = $rel_info_query->end_time;
} else {
CRM_Core_Error::Fatal("Error relating this customer ($customer_id) to the one in civicrm_stripe_subscriptions");
exit();
}
}
......@@ -75,18 +93,17 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
if(!$recurring_contribution['id']) {
CRM_Core_Error::Fatal("ERROR: Stripe triggered a Webhook on an invoice not found in civicrm_contribution_recur: " . $stripe_event_data);
exit();
}
// Build some params.
$stripe_customer = Stripe_Customer::retrieve($customer_id);
$transaction_id = $charge->id;
//get the balance_transaction object and retrieve the Stripe fee from it
$balance_transaction_id = $charge->balance_transaction;
$balance_transaction = Stripe_BalanceTransaction::retrieve($balance_transaction_id);
$fee = $balance_transaction->fee / 100;
//Currently (Oct 2015) contribution.repeattransaction does not
//insert an invoice_id in the civicrm_contribution table
//$new_invoice_id = $stripe_event_data->data->object->id;
......@@ -109,7 +126,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
'fee_amount' => $fee
));
return;
CRM_Utils_System::civiExit();
}
//Get the original contribution with this invoice_id
......@@ -148,7 +165,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
SET end_date = %1, contribution_status_id = '1'
WHERE invoice_id = %2", $query_params);
return;
CRM_Utils_System::civiExit();
}
// Successful charge & more to come
......@@ -162,7 +179,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
'contribution_status_id' => "In Progress"
));
return;
CRM_Utils_System::civiExit();
}
break;
......@@ -175,7 +192,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
}
catch(Exception $e) {
CRM_Core_Error::Fatal("Failed to retrieve Stripe charge. Message: " . $e->getMessage());
exit();
}
// Find the recurring contribution in CiviCRM by mapping it from Stripe.
......@@ -187,7 +203,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
WHERE customer_id = %1", $query_params);
if (empty($invoice_id)) {
CRM_Core_Error::Fatal("Error relating this customer ({$customer_id}) to the one in civicrm_stripe_subscriptions");
exit();
}
// Fetch Civi's info about this recurring object.
......@@ -202,7 +217,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
}
else {
CRM_Core_Error::Fatal("ERROR: Stripe triggered a Webhook on an invoice not found in civicrm_contribution_recur: " . $stripe_event_data);
exit();
}
// Build some params.
$recieve_date = date("Y-m-d H:i:s", $charge->created);
......@@ -249,14 +263,14 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
SET contribution_status_id = 4
WHERE invoice_id = %1", $query_params);
return;
CRM_Utils_System::civiExit();
}
else {
// This has failed more than once. Now what?
}
break;
//Subscription is cancelled
case 'customer.subscription.deleted':
......@@ -276,7 +290,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
$invoice_id = $rel_info_query->invoice_id;
} else {
CRM_Core_Error::Fatal("Error relating this customer ($customer_id) to the one in civicrm_stripe_subscriptions");
exit();
}
}
......@@ -290,7 +303,6 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
if (!$recur_contribution['id']) {
CRM_Core_Error::Fatal("ERROR: Stripe triggered a Webhook on an invoice not found in civicrm_contribution_recur: "
. $stripe_event_data);
exit();
}
//Cancel the recurring contribution
......@@ -311,7 +323,7 @@ class CRM_Stripe_Page_Webhook extends CRM_Core_Page {
// One-time donation and per invoice payment.
case 'charge.succeeded':
// Not implemented.
return;
CRM_Utils_System::civiExit();
break;
}
......
......@@ -17,6 +17,14 @@ For Drupal: https://example.com/civicrm/stripe/webhook
For Joomla: https://example.com/index.php/component/civicrm/?task=civicrm/stripe/webhook
For Wordpress: https://example.com/?page=CiviCRM&q=civicrm/stripe/webhook
If you have multiple Stripe accounts on your site, you will need to specify the payment processor ID in the webhook URL.
To find the ID, look at the URL when you are editing the payment processor in CiviCRM: it should include `id=XX`, where `XX` is your payment processor ID.
Add a URL parameter of `ppid=XX` to the webhook URL.
For example, for a payment processor ID of 3, use the following:
For Drupal: https://example.com/civicrm/stripe/webhook?ppid=3
For Joomla: https://example.com/index.php/component/civicrm/?task=civicrm/stripe/webhook&ppid=3
For Wordpress: https://example.com/?page=CiviCRM&q=civicrm/stripe/webhook&ppid=3
INSTALLATION
------------
For CiviCRM 4.4 & up:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment