From 37ab0f41682a39637be130819cc5c35666379919 Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Mon, 10 Aug 2020 18:51:44 +0100
Subject: [PATCH] Add Mjwshared.Notificationretry API

---
 api/v3/Contribution/Getbalance.php      |  3 --
 api/v3/Mjwpayment/Notificationretry.php | 55 +++++++++++++++++++++++++
 docs/api.md                             | 10 +++++
 mkdocs.yml                              | 21 ++++++++++
 4 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 api/v3/Mjwpayment/Notificationretry.php
 create mode 100644 docs/api.md
 create mode 100644 mkdocs.yml

diff --git a/api/v3/Contribution/Getbalance.php b/api/v3/Contribution/Getbalance.php
index e50244d..d523e14 100644
--- a/api/v3/Contribution/Getbalance.php
+++ b/api/v3/Contribution/Getbalance.php
@@ -23,11 +23,8 @@ function civicrm_api3_contribution_getbalance($params) {
 }
 
 /**
- * Action payment.
- *
  * @param array $params
  *
- * @return array
  */
 function _civicrm_api3_contribution_getbalance_spec(&$params) {
   $idField = civicrm_api3('Contribution', 'getfield', [
diff --git a/api/v3/Mjwpayment/Notificationretry.php b/api/v3/Mjwpayment/Notificationretry.php
new file mode 100644
index 0000000..28f0b0c
--- /dev/null
+++ b/api/v3/Mjwpayment/Notificationretry.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @param array $params
+ *   Array of parameters determined by getfields.
+ */
+function _civicrm_api3_mjwpayment_notificationretry_spec(&$params) {
+  $params = [
+    'system_log_id' => [
+      'type' => CRM_Utils_Type::T_INT,
+      'title' => 'System Log ID',
+    ],
+  ];
+}
+
+/**
+ * Process incoming payment notification
+ *
+ * @param array $params
+ *
+ * @return array
+ */
+function civicrm_api3_mjwpayment_notificationretry($params) {
+  if (!empty($params['system_log_id'])) {
+    // lets replace params with this rather than allow altering
+    $logEntry = civicrm_api3('system_log', 'getsingle', ['id' => $params['system_log_id'], 'return' => ['context', 'message']]);
+  }
+  $dataRaw = $logEntry['context'];
+
+  if (substr($logEntry['message'], 0, 34) === 'payment_notification processor_id=') {
+    $paymentProcessorId = substr($logEntry['message'], 34);
+    $paymentProcessorType = civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $paymentProcessorId]);
+  }
+  else {
+    throw new API_Exception('Unsupported payment processor');
+  }
+
+  $processorClassName = "CRM_Core_{$paymentProcessorType['class_name']}";
+  if (!method_exists($processorClassName, 'processPaymentNotification')) {
+    throw new API_Exception('Unsupported payment processor');
+  }
+
+  $result = FALSE;
+  $errorMessage = 'try checking the logs for errors';
+  try {
+    $result = $processorClassName::processPaymentNotification($paymentProcessorId, $dataRaw, FALSE);
+  }
+  catch (Exception $e) {
+    $errorMessage = $e->getMessage();
+  }
+  if ($result) {
+    return civicrm_api3_create_success(1, $params, 'Mjwpayment', 'Notificationretry');
+  }
+  return civicrm_api3_create_error("Failed to process notification - {$errorMessage}");
+}
diff --git a/docs/api.md b/docs/api.md
new file mode 100644
index 0000000..98c1ec9
--- /dev/null
+++ b/docs/api.md
@@ -0,0 +1,10 @@
+# API (v3)
+
+This extension comes with several APIs to help you troubleshoot problems. These can be run via /civicrm/api or via drush if you are using Drupal (drush cvapi Mjwpayment.XXX).
+
+### Mjwpayment.notificationretry
+
+For supported payment processors (ie. those that use Mjwshared) you can retry IPN notifications using this API.
+
+#### Parameters
+* `system_log_id`: The stored notification log entry to retry. Find using SystemLog.get API.
diff --git a/mkdocs.yml b/mkdocs.yml
new file mode 100644
index 0000000..8881213
--- /dev/null
+++ b/mkdocs.yml
@@ -0,0 +1,21 @@
+site_name: MJWShared (Extension containing shared code used by MJW extensions)
+repo_url: https://lab.civicrm.org/extensions/mjwshared
+theme:
+  name: material
+markdown_extensions:
+  - attr_list
+  - admonition
+  - def_list
+  - codehilite
+  - toc:
+      permalink: true
+  - pymdownx.superfences
+  - pymdownx.inlinehilite
+  - pymdownx.tilde
+  - pymdownx.betterem
+  - pymdownx.mark
+
+nav:
+- Overview: index.md
+- API: api.md
+- Release Notes: releasenotes.md
-- 
GitLab