Skip to content

Commit

Permalink
We should gracefully handle payment processor exceptions and provide …
Browse files Browse the repository at this point in the history
…user feedback

Use statusbounce directly

remove CRM_Core_Error stuff, rely on exceptions
  • Loading branch information
Rich Lott / Artful Robot committed Nov 1, 2019
1 parent 1244e13 commit 7ee8eb3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
14 changes: 8 additions & 6 deletions CRM/Contribute/Form/CancelSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,16 @@ public function postProcess() {

if (CRM_Utils_Array::value('send_cancel_request', $params) == 1) {
$cancelParams = ['subscriptionId' => $this->_subscriptionDetails->subscription_id];
$this->_paymentProcessorObj->setContributionRecurID($this->contributionRecurID);
$cancelSubscription = $this->_paymentProcessorObj->cancelSubscription($message, $cancelParams);
try {
$this->_paymentProcessorObj->setContributionRecurID($this->contributionRecurID);
$cancelSubscription = $this->_paymentProcessorObj->cancelSubscription($message, $cancelParams);
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
CRM_Core_Error::statusBounce($e->getMessage());
}
}

if (is_a($cancelSubscription, 'CRM_Core_Error')) {
CRM_Core_Error::displaySessionError($cancelSubscription);
}
elseif ($cancelSubscription) {
if ($cancelSubscription) {
try {
civicrm_api3('ContributionRecur', 'cancel', [
'id' => $this->_subscriptionDetails->recur_id,
Expand Down
17 changes: 9 additions & 8 deletions CRM/Contribute/Form/UpdateSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @copyright CiviCRM LLC (c) 2004-2019
*/


/**
* This class generates form components generic to recurring contributions.
*
Expand Down Expand Up @@ -224,15 +225,15 @@ public function postProcess() {
$params['subscriptionId'] = $this->_subscriptionDetails->subscription_id;
$updateSubscription = TRUE;
if ($this->_paymentProcessorObj->supports('changeSubscriptionAmount')) {
$updateSubscription = $this->_paymentProcessorObj->changeSubscriptionAmount($message, $params);
}
if (is_a($updateSubscription, 'CRM_Core_Error')) {
CRM_Core_Error::displaySessionError($updateSubscription);
$status = ts('Could not update the Recurring contribution details');
$msgTitle = ts('Update Error');
$msgType = 'error';
try {
$updateSubscription = $this->_paymentProcessorObj->changeSubscriptionAmount($message, $params);
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
CRM_Core_Error::statusBounce($e->getMessage());
}
}
elseif ($updateSubscription) {

if ($updateSubscription) {
// Handle custom data
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->contributionRecurID, 'ContributionRecur');
// save the changes
Expand Down
6 changes: 4 additions & 2 deletions CRM/Core/Payment/AuthorizeNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @author Marshal Newrock <marshal@idealso.com>
*/

use Civi\Payment\Exception\PaymentProcessorException;

/**
* NOTE:
* When looking up response codes in the Authorize.Net API, they
Expand Down Expand Up @@ -762,7 +764,7 @@ public function changeSubscriptionAmount(&$message = '', $params = []) {
// submit to authorize.net
$submit = curl_init($this->_paymentProcessor['url_recur']);
if (!$submit) {
return self::error(9002, 'Could not initiate connection to payment gateway');
throw new PaymentProcessorException('Could not initiate connection to payment gateway', 9002);
}

curl_setopt($submit, CURLOPT_RETURNTRANSFER, 1);
Expand All @@ -784,7 +786,7 @@ public function changeSubscriptionAmount(&$message = '', $params = []) {
$message = "{$responseFields['code']}: {$responseFields['text']}";

if ($responseFields['resultCode'] == 'Error') {
return self::error($responseFields['code'], $responseFields['text']);
throw new PaymentProcessorException($responseFields['text'], $responseFields['code']);
}
return TRUE;
}
Expand Down
8 changes: 4 additions & 4 deletions CRM/Core/Payment/PayPalImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ public function isSuppressSubmitButtons() {
* @param string $message
* @param array $params
*
* @return array|bool|object
* @return bool
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function cancelSubscription(&$message = '', $params = []) {
Expand All @@ -729,7 +729,7 @@ public function cancelSubscription(&$message = '', $params = []) {

$result = $this->invokeAPI($args);
if (is_a($result, 'CRM_Core_Error')) {
return $result;
throw new PaymentProcessorException(CRM_Core_Error::getMessages($result, "\n"));
}
$message = "{$result['ack']}: profileid={$result['profileid']}";
return TRUE;
Expand Down Expand Up @@ -823,7 +823,7 @@ public function updateSubscriptionBillingInfo(&$message = '', $params = []) {
* @param string $message
* @param array $params
*
* @return array|bool|object
* @return bool
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function changeSubscriptionAmount(&$message = '', $params = []) {
Expand All @@ -840,7 +840,7 @@ public function changeSubscriptionAmount(&$message = '', $params = []) {
$result = $this->invokeAPI($args);
CRM_Core_Error::debug_var('$result', $result);
if (is_a($result, 'CRM_Core_Error')) {
return $result;
throw new PaymentProcessorException(CRM_Core_Error::getMessages($result, "\n"));
}
$message = "{$result['ack']}: profileid={$result['profileid']}";
return TRUE;
Expand Down

0 comments on commit 7ee8eb3

Please sign in to comment.