Commit afaa2197 authored by mattwire's avatar mattwire

Merge branch 'fix-webhook-screen' into 'master'

Fix webhook screen

See merge request !10
parents 7c8e345e decae834
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use CRM_AuthNetEcheck_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference
*/
class CRM_AuthorizeNet_Form_UpdateWebhook extends CRM_Core_Form {
public function buildQuickForm() {
// Defaults.
$this->assign('shouldOfferToFix', 0);
$this->assign('isStillBad', 0);
$this->assign('isAllOk', 0);
// Run check.
$messages = [];
CRM_AuthorizeNet_Webhook::check($messages);
if (!$messages) {
$this->assign('isAllOk', 1);
}
else {
$this->assign('shouldOfferToFix', 1);
$this->assignMessages($messages);
$this->addButtons(array(
array(
'type' => 'submit',
'name' => E::ts('Update / Create webhook'),
'isDefault' => TRUE,
),
));
}
// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
parent::buildQuickForm();
}
public function postProcess() {
$messages = [];
$attemptFix = TRUE;
CRM_AuthorizeNet_Webhook::check($messages, $attemptFix);
if ($messages) {
$this->assign('isStillBad', 1);
$this->assign('shouldOfferToFix', 0);
$this->assignMessages($messages);
}
else {
$this->assign('isAllOk', 1);
$this->assign('shouldOfferToFix', 0);
$this->assign('isStillBad', 0);
$this->assign('intro', E::ts('All webhooks update successfully.'));
}
parent::postProcess();
}
/**
* @param array $messages
*/
private function assignMessages($messages) {
$messagesArray = [];
foreach ($messages as $message) {
$messagesArray[] = [
'title' => $message->getTitle(),
'message' => $message->getMessage(),
];
}
$this->assign('messages', $messagesArray);
}
/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
public function getRenderableElementNames() {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = array();
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}
......@@ -112,7 +112,11 @@ class CRM_AuthorizeNet_Webhook {
* @throws \CiviCRM_API3_Exception
* @throws \JohnConde\Authnet\AuthnetInvalidJsonException
*/
public static function check(&$messages) {
public static function check(&$messages, $attemptFix = FALSE) {
$env = Civi::settings()->get('environment');
if ($env && $env !== 'Production') {
return;
}
$checkMessage = [
'name' => 'authnet_webhook',
'label' => 'AuthorizeNet',
......@@ -120,6 +124,7 @@ class CRM_AuthorizeNet_Webhook {
$result = civicrm_api3('PaymentProcessor', 'get', [
'class_name' => ['IN' => ['Payment_AuthNetCreditcard', 'Payment_AuthNeteCheck']],
'is_active' => 1,
'domain_id' => CRM_Core_Config::domainID(),
]);
foreach ($result['values'] as $paymentProcessor) {
......@@ -182,30 +187,67 @@ class CRM_AuthorizeNet_Webhook {
}
if (!$foundWebhook) {
try {
$webhookHandler->createWebhook();
}
catch (Exception $e) {
$messages[] = new CRM_Utils_Check_Message(
"{$checkMessage['name']}_webhook",
E::ts('Could not create webhook. You can review from your account dashboard.<br/>The webhook URL is: %3', [
1 => $paymentProcessor['name'],
2 => $paymentProcessor['id'],
3 => urldecode($webhook_path),
]) . ".<br/>Error from {$checkMessage['label']}: <em>" . $e->getMessage() . '</em>',
"{$checkMessage['label']} " . E::ts('Webhook: %1 (%2)', [
if ($attemptFix) {
try {
$webhookHandler->createWebhook();
}
catch (Exception $e) {
$messages[] = new CRM_Utils_Check_Message(
"{$checkMessage['name']}_webhook",
E::ts('Could not create webhook. You can review from your account dashboard.<br/>The webhook URL is: %3', [
1 => $paymentProcessor['name'],
2 => $paymentProcessor['id'],
]
3 => urldecode($webhook_path),
]) . ".<br/>Error from {$checkMessage['label']}: <em>" . $e->getMessage() . '</em>',
"{$checkMessage['label']} " . E::ts('Webhook: %1 (%2)', [
1 => $paymentProcessor['name'],
2 => $paymentProcessor['id'],
]
),
\Psr\Log\LogLevel::WARNING,
'fa-money'
);
}
}
else {
$message = new CRM_Utils_Check_Message(
__FUNCTION__ . $paymentProcessor['id'] . "{$checkMessage['name']}_webhook",
E::ts(
"{$checkMessage['label']} Webhook missing or needs update! <em>Expected webhook path is: <a href='%1' target='_blank'>%1</a></em>",
[1 => urldecode($webhook_path)]
),
self::getTitle($paymentProcessor),
\Psr\Log\LogLevel::WARNING,
'fa-money'
);
$message->addAction(
E::ts('View and fix problems'),
NULL,
'href',
['path' => 'civicrm/fix-authnet-webhook', 'query' => ['reset' => 1]]
);
$messages[] = $message;
}
}
}
}
/**
* Get the error message title for the system check
* @param array $paymentProcessor
*
* @return string
*/
private static function getTitle($paymentProcessor) {
if (!empty($paymentProcessor['is_test'])) {
$paymentProcessor['name'] .= ' (test)';
}
return E::ts('Stripe Payment Processor: %1 (%2)', [
1 => $paymentProcessor['name'],
2 => $paymentProcessor['id'],
]);
}
/**
* List of webhooks we currently handle
* @return array
......
{*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*}
<div class="crm-block crm-content-block">
{if $isAllOk}
<div class="alert alert-success">{$intro}</div>
{/if}
{if $shouldOfferToFix}
<div class="alert alert-warning status crm-warning">
<h3>Problems discovered:</h3>
<ul>
{foreach from=$messages item=message}
<li>{$message.title}: {$message.message}</li>
{/foreach}
</ul>
</div>
<div class="alert alert-info status help">
<p>Please click the Update / Create webhook button to
attempt to fix.</p>
<p>This will attempt to check and correct your Authorize.net webhooks. Note: do not
run this in a development environment unless you want a webhook set up that
points to your development domain(!).</p>
</div>
{/if}
{if $isStillBad}
<div class="alert alert-danger status crm-error">
<h3>There were errors updating the webhook(s):</h3>
<ul>
{foreach from=$messages item=message}
<li>{$message.title}: {$message.message}</li>
{/foreach}
</ul>
</div>
<div class="alert alert-info status help">
The easiest way to fix this, is to
delete your webhooks from your Authorize.net account(s) and then revisit this page
to recreate them correctly.
</div>
{/if}
{* FOOTER *}
{if $shouldOfferToFix || $isStillBad}
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
{/if}
</div>
<?xml version="1.0"?>
<menu>
<item>
<path>civicrm/fix-authnet-webhook</path>
<page_callback>CRM_AuthorizeNet_Form_UpdateWebhook</page_callback>
<title>Check and Fix Authorize Webhooks</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
</menu>
\ No newline at end of file
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