Commit fa013056 authored by ErikHommel's avatar ErikHommel
Browse files

fix date format from koalect and add delete for koalect log

parent 3b29135d
## Version 1.1
* fixed date format issue when coming from Koalect platform
* added delete function for koalect log
## Version 1.0
* first testable version
......@@ -12,5 +12,11 @@ class CRM_Koalecttocivi_BAO_KoalectLog extends CRM_Koalecttocivi_DAO_KoalectLog
public static function flush(DateTime $upToDate) {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_koalect_log WHERE date_received < %1", [$upToDate->format("Y-m-d H:i:s"), "String"]);
}
public static function deleteById() {
$id = CRM_Utils_Request::retrieveValue('id', "Integer");
if ($id) {
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/koalecttocivi/page/koalectlog', 'reset=1', TRUE));
CRM_Koalecttocivi_DAO_KoalectLog::deleteRecord(['id' => $id]);
}
}
}
......@@ -10,8 +10,10 @@ use CRM_Koalecttocivi_ExtensionUtil as E;
class CRM_Koalecttocivi_KoalectTransaction {
public function create(array $apiData) {
Civi::log()->debug('Ik krijg de volgende data in de create functie: ' . json_encode($apiData));
// store incoming data in log file
$this->log($apiData);
Civi::log()->debug('En ik kom ook voorbij het loggen');
// actual processing is in the Form Processor koalect_transaction
$data = $this->sanitizeIncomingParams($apiData);
if ($data) {
......@@ -124,7 +126,8 @@ class CRM_Koalecttocivi_KoalectTransaction {
* @return int $max
*/
public function log(array $apiData) {
$logDate = new DateTime('now');
$logDate = new DateTime();
Civi::log()->debug("De logdatum is " . $logDate->format('Y-m-d H:i:s'));
$entity = $apiData['entity'];
$action = $apiData['action'];
unset($apiData['entity'], $apiData['action']);
......@@ -248,7 +251,15 @@ class CRM_Koalecttocivi_KoalectTransaction {
private function formatDate(string $dateField, string $dateFormat, array &$data) {
if (isset($data[$dateField])) {
if (!$data[$dateField] instanceof DateTime) {
$data[$dateField] = new DateTime($data[$dateField]);
try {
$data[$dateField] = new DateTime($data[$dateField]);
}
catch (Exception $ex) {
if (strlen($data[$dateField]) > 23) {
$data[$dateField] = substr($data[$dateField], 0, 24);
$data[$dateField] = new DateTime($data[$dateField]);
}
}
}
$data[$dateField] = $data[$dateField]->format($dateFormat);
}
......@@ -419,6 +430,13 @@ class CRM_Koalecttocivi_KoalectTransaction {
}
return $status;
}
/**
* Method to prepare the result
*
* @param array $formProcessorResult
* @return array
*/
private function prepareResult(array $formProcessorResult) {
$result = [];
$resultFields = ['campaign_id', 'campaign_type_id', 'contact_id', 'contribution_id', 'soft_credit_id', 'transaction_id', 'user_id', 'gender_id'];
......
......@@ -14,6 +14,105 @@ class CRM_Koalecttocivi_Page_KoalectLog extends CRM_Core_Page {
* @access public
*/
function run() {
$testParams = [
"message_name" => "export_transaction",
"amount" => 1000,
"stripe_bank_account_id" => NULL,
"stripe_payout_id" => NULL,
"stripe_payout_net" => NULL,
"stripe_bank_reference" => NULL,
"total_fee" => 83,
"should_arrive_at" => "2022-03-24T15:42:40.000 00:00",
"campaign_id" => "c39ac7c4-eb76-495a-9a4a-4f94c6e11282",
"campaign_name" => "Verjaardagsacties voor Amnesty",
"category" => "donation",
"client_id" => "19accfa8-f729-4868-98eb-dbf133aa1d20",
"client_name" => "International Amnesty",
"created_at" => "2022-03-24T15:42:40.000 00:00",
"currency" => "EUR",
"customer" => [
"address" => [
"city" => "Genval",
"line1" => "Rua dos trstes",
"line2" => "3rd floor",
"state" => NULL,
"country" => "BE",
"latitude" => NULL,
"longitude" => NULL,
"postal_code" => "4232",
"box" => "Box 12",
"number" => "9"
],
"avatar" => NULL,
"birthday" => "1990-01-01T14:28:00.000+00:00",
"client_id" => "19accfa8-f729-4868-98eb-dbf133aa1d20",
"confirmed_at" => NULL,
"connector_name" => "Civi",
"deleted_at" => NULL,
"email" => "goncalo@koalect.com",
"firstname" => "Goncalo",
"iban" => NULL,
"is_koalect" => NULL,
"language" => "en-GB",
"lastname" => "Matos",
"phone_number" => "914441944",
"transaction_id" => 811,
"subscription_id" => NULL,
"user_id" => "309987ae-08da-4b15-96ae-ae88e3235ff8",
"newsletter" => NULL,
"terms_of_use" => NULL,
"business_name"=>"Koalect Inc",
"business_vat" => "BE1234567890",
"gender" => "male"
],
"is_subscription" => FALSE,
"is_subscription_active" => NULL,
"subscription_id" => NULL,
"payment_method" => "bancontact",
"bank_statement" => "INTERNATIO T873",
"procedure_id" => "353debf0-f9ae-40e1-b788-b96cc6285fbf",
"procedure_name" => [
"en-GB" => "Test CiviCRM",
"fr-FR"=>"Test CiviCRM",
"nl-NL"=>"Test CiviCRM"
],
"procedure_slug"=>"test-civicrm",
"terms_of_use" => true,
"newsletter" => false,
"project_id" => "02c8f01e-bb90-4d82-ace1-f130b3d50cca",
"project_name" => "Maxime viert zijn verjaardag voor Amnesty",
"provider_ids" => '{
"charge_id" => "py_1KgSGwGkMJeTP5WJVZSSehn8",
"source_id" => "src_1KgSGaGkMJeTP5WJhv7gRn5X",
"redirect_url" => "https://hooks.stripe.com/redirect/authenticate/src_1KgSGaGkMJeTP5WJhv7gRn5X?client_secret=src_client_secret_BiTzfM7dYGnfhPm0AcuFq1PJ",
"authorize_url" =>"https://hooks.stripe.com/redirect/authenticate/src_1KgSGaGkMJeTP5WJhv7gRn5X?client_secret=src_client_secret_BiTzfM7dYGnfhPm0AcuFq1PJ",
"balance_transaction_id" => "txn_1KgSGxGkMJeTP5WJJJvw82na",
"balance_available_at" => 1648425600,
"balance_paid_out" => false,
"source_transfer_id" => "tr_1KgSGxGkMJeTP5WJlySAJB5H",
"destination_payment_id" => "py_1KgSGx2ezygk5LMso8LvU4Ot"
}',
"status" => "succeeded",
"target_id" => "19accfa8-f729-4868-98eb-dbf133aa1d20",
"target_name" => "International Amnesty",
"target_type" => "Client",
"tax_receipt_requested" => false,
"transaction_id" => 811,
"updated_at" => "2022-03-23T13:22:41.000Z",
"user_id" => "309987ae-08da-4b15-96ae-ae88e3235ff8",
"external_reference" => NULL,
"connector_name" => "Civi",
"comment" => NULL,
"provider_name" => "stripe",
"procedure_category" => "donation",
"project_owner" => NULL,
"tax_receipt_allowed" => true,
"tax_receipt_enabled" => false
];
$resultaat = civicrm_api3('KoalectTransaction', 'create', $testParams);
CRM_Core_Error::debug('resultaat', $resultaat);
exit();
$this->setPageConfiguration();
$this->assign('logs', $this->getLogs());
parent::run();
......@@ -32,6 +131,9 @@ class CRM_Koalecttocivi_Page_KoalectLog extends CRM_Core_Page {
->addSelect('*')
->execute();
foreach ($koalectLogs as $koalectLog) {
$deleteUrl = CRM_Utils_System::url('civicrm/koalecttocivi/page/deletekoalectlog', 'reset=1&lid=' .
$koalectLog['id'], TRUE);
$koalectLog['delete_url'] = '<a class="action-item" title="Deleted" href="' . $deleteUrl . '">' . E::ts('Delete') . '</a>';
$logs[$koalectLog['id']] = $koalectLog;
}
}
......@@ -41,11 +143,14 @@ class CRM_Koalecttocivi_Page_KoalectLog extends CRM_Core_Page {
else {
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_koalect_log");
while ($dao->fetch()) {
$deleteUrl = CRM_Utils_System::url('civicrm/koalecttocivi/page/deletekoalectlog', 'reset=1&lid=' .
$dao->id, TRUE);
$log = [
'date_received' => $dao->date_received,
'api_action' => $dao->api_action,
'api_entity' => $dao->api_entity,
'request_data' => $dao->request_data,
'delete_url' => '<a class="action-item" title="Deleted" href="' . $deleteUrl . '">' . E::ts('Delete') . '</a>'
];
$logs[$dao->id] = $log;
}
......
<?php
use CRM_Koalecttocivi_ExtensionUtil as E;
/**
* KoalectLog.Delete API specification (optional)
* This is used for documentation and validation.
*
* @param array $spec description of fields supported by this API call
*
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
*/
function _civicrm_api3_koalect_log_Delete_spec(&$spec) {
$spec['id'] = [
'name' => 'id',
'title' => 'ID',
'description' => 'Unique Koalect Log ID',
'api.required' => 1,
'type' => CRM_Utils_Type::T_INT,
];
}
/**
* KoalectLog.Delete API
*
* @param array $params
*
* @return array
* API result descriptor
*
* @see civicrm_api3_create_success
*
* @throws API_Exception
*/
function civicrm_api3_koalect_log_Delete($params) {
try {
CRM_Koalecttocivi_BAO_KoalectLog::deleteRecord($params);
return civicrm_api3_create_success(["Koalect log deleted."], $params, 'KoalectLog', 'Delete');
}
catch (Exception $ex) {
return civicrm_api3_create_error($ex->getMessage(), $params);
}
}
......@@ -31,6 +31,6 @@ function civicrm_api3_koalect_log_Flush($params) {
$logDate = new DateTime('now');
$logDate->modify('-3 months');
}
CRM_Koalecttocivi_BAO_KoalectLog::flush($$logDate);
CRM_Koalecttocivi_BAO_KoalectLog::flush($logDate);
return civicrm_api3_create_success(["Koalect log records deleted when earlier than log date in params."], $params, 'KoalectLog', 'Flush');
}
......@@ -14,8 +14,8 @@
<url desc="Support">https://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2022-03-21</releaseDate>
<version>1.0</version>
<releaseDate>2022-03-24</releaseDate>
<version>1.1</version>
<develStage>beta</develStage>
<compatibility>
<ver>5.0</ver>
......
......@@ -12,6 +12,7 @@
<th id="sortable">{ts}API Entity{/ts}</th>
<th id="sortable">{ts}API Action{/ts}</th>
<th id="sortable">{ts}Data{/ts}</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
......@@ -23,6 +24,7 @@
<td>{$log.api_entity}</td>
<td>{$log.api_action}</td>
<td>{$log.request_data}</td>
<td>{$log.delete_url}</td>
</tr>
{/foreach}
</tbody>
......
......@@ -6,4 +6,10 @@
<title>KoalectLog</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/koalecttocivi/page/deletekoalectlog</path>
<page_callback>CRM_Koalecttocivi_BAO_KoalectLog::deleteById</page_callback>
<title>KoalectLog</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
</menu>
Supports Markdown
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