Commit b2f85023 authored by mattwire's avatar mattwire Committed by mattwire
Browse files

Add PaymentprocessorWebhook entity, API and scheduled job that allows for...

Add PaymentprocessorWebhook entity, API and scheduled job that allows for queueing and scheduling of webhooks
parent b1b1ad01
<?php
use CRM_Mjwshared_ExtensionUtil as E;
class CRM_Mjwshared_BAO_PaymentprocessorWebhook extends CRM_Mjwshared_DAO_PaymentprocessorWebhook {
}
<?php
/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
* Generated from mjwshared/xml/schema/CRM/Mjwshared/PaymentprocessorWebhook.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:f87fdd5e01150ab8d776e10bbe3fa15d)
*/
use CRM_Mjwshared_ExtensionUtil as E;
/**
* Database access object for the PaymentprocessorWebhook entity.
*/
class CRM_Mjwshared_DAO_PaymentprocessorWebhook extends CRM_Core_DAO {
const EXT = E::LONG_NAME;
const TABLE_ADDED = '';
/**
* Static instance to hold the table name.
*
* @var string
*/
public static $_tableName = 'civicrm_paymentprocessor_webhook';
/**
* Should CiviCRM log any modifications to this table in the civicrm_log table.
*
* @var bool
*/
public static $_log = TRUE;
/**
* Unique PaymentprocessorWebhook ID
*
* @var int
*/
public $id;
/**
* Payment Processor for this webhook
*
* @var int
*/
public $payment_processor_id;
/**
* Webhook event ID
*
* @var string
*/
public $event_id;
/**
* Webhook trigger event type
*
* @var string
*/
public $trigger;
/**
* When the webhook was first received by the IPN code
*
* @var timestamp
*/
public $created_date;
/**
* Has this webhook been processed yet?
*
* @var timestamp
*/
public $processed_date;
/**
* Webhook processing status
*
* @var string
*/
public $status;
/**
* @var text
*/
public $identifier;
/**
* Class constructor.
*/
public function __construct() {
$this->__table = 'civicrm_paymentprocessor_webhook';
parent::__construct();
}
/**
* Returns localized title of this entity.
*
* @param bool $plural
* Whether to return the plural version of the title.
*/
public static function getEntityTitle($plural = FALSE) {
return $plural ? E::ts('Paymentprocessor Webhooks') : E::ts('Paymentprocessor Webhook');
}
/**
* Returns foreign keys and entity references.
*
* @return array
* [CRM_Core_Reference_Interface]
*/
public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'payment_processor_id', 'civicrm_payment_processor', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
}
/**
* Returns all the column names of this table
*
* @return array
*/
public static function &fields() {
if (!isset(Civi::$statics[__CLASS__]['fields'])) {
Civi::$statics[__CLASS__]['fields'] = [
'id' => [
'name' => 'id',
'type' => CRM_Utils_Type::T_INT,
'description' => E::ts('Unique PaymentprocessorWebhook ID'),
'required' => TRUE,
'where' => 'civicrm_paymentprocessor_webhook.id',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
'payment_processor_id' => [
'name' => 'payment_processor_id',
'type' => CRM_Utils_Type::T_INT,
'title' => E::ts('Payment Processor'),
'description' => E::ts('Payment Processor for this webhook'),
'where' => 'civicrm_paymentprocessor_webhook.payment_processor_id',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'FKClassName' => 'CRM_Financial_DAO_PaymentProcessor',
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'table' => 'civicrm_payment_processor',
'keyColumn' => 'id',
'labelColumn' => 'name',
],
'add' => NULL,
],
'event_id' => [
'name' => 'event_id',
'type' => CRM_Utils_Type::T_STRING,
'description' => E::ts('Webhook event ID'),
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'where' => 'civicrm_paymentprocessor_webhook.event_id',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
'trigger' => [
'name' => 'trigger',
'type' => CRM_Utils_Type::T_STRING,
'title' => E::ts('Trigger'),
'description' => E::ts('Webhook trigger event type'),
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'where' => 'civicrm_paymentprocessor_webhook.trigger',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
'created_date' => [
'name' => 'created_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => E::ts('Created Date'),
'description' => E::ts('When the webhook was first received by the IPN code'),
'where' => 'civicrm_paymentprocessor_webhook.created_date',
'default' => 'CURRENT_TIMESTAMP',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
'processed_date' => [
'name' => 'processed_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => E::ts('Processed Date'),
'description' => E::ts('Has this webhook been processed yet?'),
'where' => 'civicrm_paymentprocessor_webhook.processed_date',
'default' => 'NULL',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
'status' => [
'name' => 'status',
'type' => CRM_Utils_Type::T_STRING,
'title' => E::ts('Status'),
'description' => E::ts('Webhook processing status'),
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'where' => 'civicrm_paymentprocessor_webhook.status',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
'identifier' => [
'name' => 'identifier',
'type' => CRM_Utils_Type::T_TEXT,
'title' => E::ts('Identifier'),
'where' => 'civicrm_paymentprocessor_webhook.identifier',
'table_name' => 'civicrm_paymentprocessor_webhook',
'entity' => 'PaymentprocessorWebhook',
'bao' => 'CRM_Mjwshared_DAO_PaymentprocessorWebhook',
'localizable' => 0,
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
return Civi::$statics[__CLASS__]['fields'];
}
/**
* Return a mapping from field-name to the corresponding key (as used in fields()).
*
* @return array
* Array(string $name => string $uniqueName).
*/
public static function &fieldKeys() {
if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {
Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
}
return Civi::$statics[__CLASS__]['fieldKeys'];
}
/**
* Returns the names of this table
*
* @return string
*/
public static function getTableName() {
return self::$_tableName;
}
/**
* Returns if this table needs to be logged
*
* @return bool
*/
public function getLog() {
return self::$_log;
}
/**
* Returns the list of fields that can be imported
*
* @param bool $prefix
*
* @return array
*/
public static function &import($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'paymentprocessor_webhook', $prefix, []);
return $r;
}
/**
* Returns the list of fields that can be exported
*
* @param bool $prefix
*
* @return array
*/
public static function &export($prefix = FALSE) {
$r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'paymentprocessor_webhook', $prefix, []);
return $r;
}
/**
* Returns the list of indices
*
* @param bool $localize
*
* @return array
*/
public static function indices($localize = TRUE) {
$indices = [];
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
}
}
<?php
use CRM_Mjwshared_ExtensionUtil as E;
/**
* Collection of upgrade steps.
*/
class CRM_Mjwshared_Upgrader extends CRM_Mjwshared_Upgrader_Base {
/**
* @return TRUE on success
* @throws Exception
*/
public function upgrade_1000() {
$this->ctx->log->info('Applying update 1000 - Add civicrm_paymentprocessor_webhook table');
if (!CRM_Core_DAO::checkTableExists('civicrm_paymentprocessor_webhook')) {
$this->executeSqlFile('sql/paymentprocessorwebhook_install.sql');
}
return TRUE;
}
}
<?php
// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
use CRM_Mjwshared_ExtensionUtil as E;
/**
* Base class which provides helpers to execute upgrade logic
*/
class CRM_Mjwshared_Upgrader_Base {
/**
* @var CRM_Mjwshared_Upgrader_Base
*/
public static $instance;
/**
* @var CRM_Queue_TaskContext
*/
protected $ctx;
/**
* @var string
* eg 'com.example.myextension'
*/
protected $extensionName;
/**
* @var string
* full path to the extension's source tree
*/
protected $extensionDir;
/**
* @var array
* sorted numerically
*/
private $revisions;
/**
* @var bool
* Flag to clean up extension revision data in civicrm_setting
*/
private $revisionStorageIsDeprecated = FALSE;
/**
* Obtain a reference to the active upgrade handler.
*/
public static function instance() {
if (!self::$instance) {
self::$instance = new CRM_Mjwshared_Upgrader(
'mjwshared',
E::path()
);
}
return self::$instance;
}
/**
* Adapter that lets you add normal (non-static) member functions to the queue.
*
* Note: Each upgrader instance should only be associated with one
* task-context; otherwise, this will be non-reentrant.
*
* ```
* CRM_Mjwshared_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
* ```
*/
public static function _queueAdapter() {
$instance = self::instance();
$args = func_get_args();
$instance->ctx = array_shift($args);
$instance->queue = $instance->ctx->queue;
$method = array_shift($args);
return call_user_func_array([$instance, $method], $args);
}
/**
* CRM_Mjwshared_Upgrader_Base constructor.
*
* @param $extensionName
* @param $extensionDir
*/
public function __construct($extensionName, $extensionDir) {
$this->extensionName = $extensionName;
$this->extensionDir = $extensionDir;
}
// ******** Task helpers ********
/**
* Run a CustomData file.
*
* @param string $relativePath
* the CustomData XML file path (relative to this extension's dir)
* @return bool
*/
public function executeCustomDataFile($relativePath) {
$xml_file = $this->extensionDir . '/' . $relativePath;
return $this->executeCustomDataFileByAbsPath($xml_file);
}
/**
* Run a CustomData file
*
* @param string $xml_file
* the CustomData XML file path (absolute path)
*
* @return bool
*/
protected function executeCustomDataFileByAbsPath($xml_file) {
$import = new CRM_Utils_Migrate_Import();
$import->run($xml_file);
return TRUE;
}
/**
* Run a SQL file.
*
* @param string $relativePath
* the SQL file path (relative to this extension's dir)
*
* @return bool
*/
public function executeSqlFile($relativePath) {
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN,
$this->extensionDir . DIRECTORY_SEPARATOR . $relativePath
);
return TRUE;
}
/**
* Run the sql commands in the specified file.
*
* @param string $tplFile
* The SQL file path (relative to this extension's dir).
* Ex: "sql/mydata.mysql.tpl".
*
* @return bool
* @throws \CRM_Core_Exception
*/
public function executeSqlTemplate($tplFile) {
// Assign multilingual variable to Smarty.
$upgrade = new CRM_Upgrade_Form();
$tplFile = CRM_Utils_File::isAbsolute($tplFile) ? $tplFile : $this->extensionDir . DIRECTORY_SEPARATOR . $tplFile;
$smarty = CRM_Core_Smarty::singleton();
$smarty->assign('domainID', CRM_Core_Config::domainID());
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN, $smarty->fetch($tplFile), NULL, TRUE
);
return TRUE;
}
/**
* Run one SQL query.
*
* This is just a wrapper for CRM_Core_DAO::executeSql, but it
* provides syntactic sugar for queueing several tasks that
* run different queries
*
* @return bool
*/
public function executeSql($query, $params = []) {
// FIXME verify that we raise an exception on error
CRM_Core_DAO::executeQuery($query, $params);
return TRUE;
}
/**
* Syntactic sugar for enqueuing a task which calls a function in this class.
*
* The task is weighted so that it is processed
* as part of the currently-pending revision.
*
* After passing the $funcName, you can also pass parameters that will go to
* the function. Note that all params must be serializable.
*/
public function addTask($title) {
$args = func_get_args();
$title = array_shift($args);
$task = new CRM_Queue_Task(
[get_class($this), '_queueAdapter'],
$args,
$title
);
return $this->queue->createItem($task, ['weight' => -1]);
}
// ******** Revision-tracking helpers ********
/**
* Determine if there are any pending revisions.
*
* @return bool
*/
public function hasPendingRevisions() {
$revisions = $this->getRevisions();
$currentRevision = $this->getCurrentRevision();
if (empty($revisions)) {
return FALSE;
}
if (empty($currentRevision)) {
return TRUE;
}
return ($currentRevision < max($revisions));
}
/**
* Add any pending revisions to the queue.
*
* @param CRM_Queue_Queue $queue
*/
public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
$this->queue = $queue;
$currentRevision = $this->getCurrentRevision();
foreach ($this->getRevisions() as $revision) {
if ($revision > $currentRevision) {
$title = E::ts('Upgrade %1 to revision %2', [
1 => $this->extensionName,
2 => $revision,
]);
// note: don't use addTask() because it sets weight=-1
$task = new CRM_Queue_Task(
[get_class($this), '_queueAdapter'],
['upgrade_' . $revision],
$title
);
$this->queue->createItem($task);
$task = new CRM_Queue_Task(
[get_class($this), '_queueAdapter'],
['setCurrentRevision', $revision],
$title
);
$this->queue->createItem($task);
}
}
}
/**
* Get a list of revisions.
*
* @return array
* revisionNumbers sorted numerically
*/
public function getRevisions() {
if (!is_array($this->revisions)) {
$this->revisions = [];
$clazz = new ReflectionClass(get_class($this));
$methods = $clazz->getMethods();
foreach ($methods as $method) {
if (preg_match('/^upgrade_(.*)/', $method->name, $matches)) {
$this->revisions[] = $matches[1];
}
}
sort($this->revisions, SORT_NUMERIC);
}
return $this->revisions;
}
public function getCurrentRevision() {
$revision = CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
if (!$revision) {
$revision = $this->getCurrentRevisionDeprecated();
}
return $revision;