Skip to content
Snippets Groups Projects
Commit 73f6c7f7 authored by mattwire's avatar mattwire
Browse files

Cleanup and remove legacy error handling

parent f4778155
Branches
Tags
No related merge requests found
......@@ -261,23 +261,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
Stripe::setApiVersion(CRM_Stripe_Check::API_VERSION);
}
/**
* Handle an error from Stripe API and notify the user
*
* @param array $err
* @param string $bounceURL
*
* @return string errorMessage
*/
public function handleErrorNotification($err, $bounceURL = NULL) {
try {
self::handleError("{$err['type']} {$err['code']}", $err['message'], $bounceURL);
}
catch (Exception $e) {
return $e->getMessage();
}
}
/**
* Stripe exceptions contain a json object in the body "error". This function extracts and returns that as an array.
* @param String $op
......@@ -317,7 +300,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$plan = $this->stripeClient->plans->retrieve($planId);
}
catch (\Stripe\Exception\InvalidRequestException $e) {
$err = self::parseStripeException('plan_retrieve', $e, FALSE);
$err = self::parseStripeException('plan_retrieve', $e);
if ($err['code'] === 'resource_missing') {
$formatted_amount = CRM_Utils_Money::formatLocaleNumericRoundedByCurrency(($amount / 100), $currency);
$productName = "CiviCRM " . (isset($params['membership_name']) ? $params['membership_name'] . ' ' : '') . "every {$params['recurFrequencyInterval']} {$params['recurFrequencyUnit']}(s) {$currency}{$formatted_amount}";
......@@ -590,8 +573,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$stripeCustomer = $this->stripeClient->customers->retrieve($stripeCustomerId);
} catch (Exception $e) {
$err = self::parseStripeException('retrieve_customer', $e);
$errorMessage = $this->handleErrorNotification($err, $propertyBag->getCustomProperty('error_url'));
throw new PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $errorMessage);
\Civi::log('stripe')->error('Failed to retrieve Stripe Customer: ' . $err['message'] . '; ' . print_r($err, TRUE));
throw new PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $err['code']);
}
if ($stripeCustomer->isDeleted()) {
......@@ -602,8 +585,8 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
} catch (Exception $e) {
// We still failed to create a customer
$err = self::parseStripeException('create_customer', $e);
$errorMessage = $this->handleErrorNotification($err, $propertyBag->getCustomProperty('error_url'));
throw new PaymentProcessorException('Failed to create Stripe Customer: ' . $errorMessage);
\Civi::log('stripe')->error('Failed to create Stripe Customer: ' . $err['message'] . '; ' . print_r($err, TRUE));
throw new PaymentProcessorException('Failed to create Stripe Customer: ' . $err['code']);
}
}
}
......@@ -880,9 +863,9 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$stripeBalanceTransaction = $this->stripeClient->balanceTransactions->retrieve($stripeCharge->balance_transaction);
}
catch (Exception $e) {
$err = self::parseStripeException('retrieve_balance_transaction', $e, FALSE);
$errorMessage = $this->handleErrorNotification($err, $params['error_url']);
throw new PaymentProcessorException('Failed to retrieve Stripe Balance Transaction: ' . $errorMessage);
$err = self::parseStripeException('retrieve_balance_transaction', $e);
\Civi::log('stripe')->error('Failed to retrieve Stripe Balance Transaction: ' . $err['message'] . '; ' . print_r($err, TRUE));
throw new PaymentProcessorException('Failed to retrieve Stripe Balance Transaction: ' . $err['code']);
}
if (($stripeCharge['currency'] !== $stripeBalanceTransaction->currency)
&& (!empty($stripeBalanceTransaction->exchange_rate))) {
......@@ -1395,35 +1378,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$GLOBALS['mockStripeClient'] = $this->stripeClient = $stripeClient;
}
/**
* Handle an error and notify the user
* @fixme: Remove when min version of mjwshared is 1.2.3.
* This is a copy of the updated function that throws an exception on error instead of returning FALSE
*
* @param string $errorCode
* @param string $errorMessage
* @param string $bounceURL
*
* @throws \Civi\Payment\Exception\PaymentProcessorException
* (or statusbounce if URL is specified)
*/
private function handleError($errorCode = NULL, $errorMessage = NULL, $bounceURL = NULL) {
$errorCode = empty($errorCode) ? '' : $errorCode . ': ';
$errorMessage = empty($errorMessage) ? 'Unknown System Error.' : $errorMessage;
$message = $errorCode . $errorMessage;
Civi::log()->error($this->getPaymentTypeLabel() . ' Payment Error: ' . $message);
if ($this->handleErrorThrowsException) {
// We're in a test environment. Throw exception.
throw new \Exception('Exception thrown to avoid statusBounce because handleErrorThrowsException is set.' . $message);
}
if ($bounceURL) {
CRM_Core_Error::statusBounce($message, $bounceURL, $this->getPaymentTypeLabel());
}
throw new PaymentProcessorException($errorMessage, $errorCode);
}
/**
* Get the Fee charged by Stripe from the "balance transaction".
* If the transaction is declined, there won't be a balance_transaction_id.
......
......@@ -147,8 +147,8 @@ class CRM_Stripe_Customer {
}
catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e, FALSE);
$errorMessage = $stripe->handleErrorNotification($err, $params['error_url']);
throw new PaymentProcessorException('Failed to create Stripe Customer: ' . $errorMessage);
\Civi::log('stripe')->error('Failed to create Stripe Customer: ' . $err['message'] . '; ' . print_r($err, TRUE));
throw new PaymentProcessorException('Failed to create Stripe Customer: ' . $err['code']);
}
// Store the relationship between CiviCRM's email address for the Contact & Stripe's Customer ID.
......@@ -185,9 +185,9 @@ class CRM_Stripe_Customer {
$stripeCustomer = $stripe->stripeClient->customers->update($stripeCustomerID, $stripeCustomerParams);
}
catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e, FALSE);
$errorMessage = $stripe->handleErrorNotification($err, $params['error_url']);
throw new PaymentProcessorException('Failed to update Stripe Customer: ' . $errorMessage);
$err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e);
\Civi::log('stripe')->error('Failed to update Stripe Customer: ' . $err['message'] . '; ' . print_r($err, TRUE));
throw new PaymentProcessorException('Failed to update Stripe Customer: ' . $err['code']);
}
return $stripeCustomer;
}
......@@ -216,7 +216,7 @@ class CRM_Stripe_Customer {
$stripeCustomerParams = [
'name' => $contactDisplayName,
// Stripe does not include the Customer Name when exporting payments, just the customer
// Stripe does not include the Customer Name when exporting payments, just the customer
// description, so we stick the name in the description.
'description' => $contactDisplayName . ' (CiviCRM)',
'email' => $params['email'] ?? '',
......
......@@ -357,13 +357,18 @@ class CRM_Stripe_PaymentIntent {
}
if (!empty($params['paymentIntentID'])) {
// We already have a PaymentIntent, retrieve and attempt to confirm.
$intent = $this->paymentProcessor->stripeClient->paymentIntents->retrieve($params['paymentIntentID']);
if ($intent->status === 'requires_confirmation') {
$intent->confirm();
try {
// We already have a PaymentIntent, retrieve and attempt to confirm.
$intent = $this->paymentProcessor->stripeClient->paymentIntents->retrieve($params['paymentIntentID']);
if ($intent->status === 'requires_confirmation') {
$intent->confirm();
}
if ($params['capture'] && $intent->status === 'requires_capture') {
$intent->capture();
}
}
if ($params['capture'] && $intent->status === 'requires_capture') {
$intent->capture();
catch (Exception $e) {
\Civi::log()->debug(get_class($e) . $e->getMessage());
}
}
else {
......@@ -384,7 +389,7 @@ class CRM_Stripe_PaymentIntent {
}
$intent = $this->paymentProcessor->stripeClient->paymentIntents->create($intentParams);
} catch (Exception $e) {
// Save the "error" in the paymentIntent table in in case investigation is required.
// Save the "error" in the paymentIntent table in case investigation is required.
$stripePaymentintentParams = [
'payment_processor_id' => $this->paymentProcessor->getID(),
'status' => 'failed',
......
......@@ -14,6 +14,7 @@
*
*/
use Civi\Payment\Exception\PaymentProcessorException;
use CRM_Stripe_ExtensionUtil as E;
/**
......@@ -255,9 +256,9 @@ function civicrm_api3_stripe_customer_updatestripemetadata($params) {
try {
$paymentProcessor->stripeClient->customers->retrieve($customerId);
} catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('retrieve_customer', $e, FALSE);
$errorMessage = $paymentProcessor->handleErrorNotification($err);
throw new \Civi\Payment\Exception\PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $errorMessage);
$err = CRM_Core_Payment_Stripe::parseStripeException('retrieve_customer', $e);
\Civi::log('stripe')->error('Failed to retrieve Stripe Customer: ' . $err['message'] . '; ' . print_r($err, TRUE));
throw new PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $err['code']);
}
// Get the contact display name
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment