Commit 5c6d22c8 authored by mattwire's avatar mattwire

Handle deleted customer

parent 5ff3615e
......@@ -151,7 +151,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
*
* @throws \CiviCRM_API3_Exception
*/
public function stripeCatchErrors($op = 'create_customer', $stripe_params, $params, $ignores = array()) {
public function stripeCatchErrors($op, $stripe_params, $params, $ignores = array()) {
$return = FALSE;
// Check for errors before trying to submit.
try {
......@@ -179,10 +179,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
case 'retrieve_balance_transaction':
$return = \Stripe\BalanceTransaction::retrieve($stripe_params);
break;
default:
$return = \Stripe\Customer::create($stripe_params);
break;
}
}
catch (Exception $e) {
......@@ -541,17 +537,18 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// Customer was found in civicrm database, fetch from Stripe.
$stripeCustomer = $this->stripeCatchErrors('retrieve_customer', $stripeCustomerId, $params);
if (!empty($stripeCustomer)) {
if ($this->isErrorReturn($stripeCustomer)) {
if (($stripeCustomer['type'] == 'invalid_request_error') && ($stripeCustomer['code'] == 'resource_missing')) {
// Customer doesn't exist, create a new one
CRM_Stripe_Customer::delete($customerParams);
$stripeCustomer = CRM_Stripe_Customer::create($customerParams, $this);
}
if ($stripeCustomer->isDeleted()
|| ($this->isErrorReturn($stripeCustomer) && ($stripeCustomer['type'] == 'invalid_request_error') && ($stripeCustomer['code'] == 'resource_missing')
)) {
// Customer doesn't exist, create a new one
CRM_Stripe_Customer::delete($customerParams);
$stripeCustomer = CRM_Stripe_Customer::create($customerParams, $this);
if ($this->isErrorReturn($stripeCustomer)) {
// We still failed to create a customer
self::handleErrorNotification($stripeCustomer, $params['stripe_error_url']);
return $stripeCustomer;
}
}
$stripeCustomer->card = $card_token;
......
......@@ -20,6 +20,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
public $test_mode;
public $event_type = NULL;
public $subscription_id = NULL;
public $customer_id = NULL;
public $charge_id = NULL;
public $previous_plan_id = NULL;
public $plan_id = NULL;
......@@ -500,7 +501,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
financial_type_id, payment_instrument_id, contact_id
FROM civicrm_stripe_subscriptions s JOIN civicrm_contribution_recur r
ON s.contribution_recur_id = r.id
WHERE subscription_id = %1
WHERE s.subscription_id = %1
AND s.processor_id = %2";
$query_params = array(
1 => array($this->subscription_id, 'String'),
......@@ -515,7 +516,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
financial_type_id, payment_instrument_id, contact_id
FROM civicrm_stripe_subscriptions s JOIN civicrm_contribution_recur r
ON s.contribution_recur_id = r.id
WHERE customer_id = %1
WHERE s.customer_id = %1
AND s.processor_id = %2";
$query_params = array(
1 => array($this->customer_id, 'String'),
......@@ -542,7 +543,7 @@ class CRM_Core_Payment_StripeIPN extends CRM_Core_Payment_BaseIPN {
$this->payment_instrument_id = $dao->payment_instrument_id;
$this->contact_id = $dao->contact_id;
// Same approach as api repeattransaction. Find last contribution ascociated
// Same approach as api repeattransaction. Find last contribution associated
// with our recurring contribution.
$results = civicrm_api3('contribution', 'getsingle', array(
'return' => array('id', 'contribution_status_id', 'total_amount'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment