Skip to content

Commit

Permalink
CIVIEWAY-261 WIP - Add Action Provider action to EWAY to fill credit …
Browse files Browse the repository at this point in the history
…card meta data
  • Loading branch information
agileware-justin committed Oct 10, 2023
1 parent 9372117 commit 4052f66
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
42 changes: 42 additions & 0 deletions CRM/eWAYRecurring/PaymentToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,46 @@ public static function fillTokensMeta() {
}
return $results;
}

public static function fillTokenMetaSingle($payment_processor_id, $token_id) {
// Caches payment processors
$processors = [];

// Get Login details for eWAY from payment processor
$processor = $processors[$payment_processor_id] ??=
PaymentProcessor::get(FALSE)
->addWhere('id', '=', $payment_processor_id)
->execute()->first();

$eway_client = $processor['eway_client'] ??= CRM_eWAYRecurring_Utils::getEWayClient($processor);

// Exit if unable to log in to eWAY
if($eway_client->getErrors()) {
return FALSE;
}

$token_customer = $eway_client->queryCustomer($token_id);

// Exit if custom query fails
$errors = $token_customer->getErrors();
if ($errors) {
return FALSE;
}

$card_details = $token_customer->Customers[0]->CardDetails;
$card_number = $card_details->Number;

$expiry_date = new DateTime('00:00:00.000');
$expiry_date->setDate(2000 + (int) $card_details->ExpiryYear, $card_details->ExpiryMonth, 1);
$expiry_date->modify('+ 1 month - 1 second');

$expiry_date = $expiry_date->format('Ymd');

PaymentToken::update(FALSE)
->addWhere('id', '=', $token_id)
->addValue('masked_account_number', $card_number)
->addValue('expiry_date', $expiry_date)
->execute();
return TRUE;
}
}
70 changes: 70 additions & 0 deletions Civi/Ewayactions/Actions/FillPaymentTokenMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Civi\Ewayactions\Actions;

use Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\SpecificationBag;
use Civi\ActionProvider\Action\AbstractAction;
use Civi\ActionProvider\Parameter\Specification;
use CRM_eWAYRecurring_ExtensionUtil as E;
use CRM_eWAYRecurring_PaymentToken;

class FillPaymentTokenMeta extends AbstractAction {

/**
* Run the action
*
* @param ParameterBagInterface $parameters
* The parameters to this action.
* @param ParameterBagInterface $output
* The parameters this action can send back
*
* @return void
*/
protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
try {
$payment_processor_id = $this->configuration->getParameter('payment_processor_id');
$payment_token_id = $parameters->getParameter('payment_token_id');

CRM_eWAYRecurring_PaymentToken::fillTokenMetaSingle($payment_processor_id,$payment_token_id);
return TRUE;
}
catch (\Exception $e) {
return FALSE;
}
}

/**
* Returns the specification of the configuration options for the actual action.
*
* @return SpecificationBag
*/
public function getConfigurationSpecification() {
return new SpecificationBag;
}

/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag([
new Specification('payment_processor_id', 'Integer', E::ts('Payment Processor'), TRUE, NULL, NULL, NULL, FALSE),
new Specification('payment_token_id', 'Integer', E::ts('Payment Token ID'), TRUE, NULL, NULL, NULL, FALSE),
]);
}

/**
* Returns the specification of the output parameters of this action.
*
* This function could be overridden by child classes.
*
* @return SpecificationBag
*/
public function getOutputSpecification() {
return new SpecificationBag([
new Specification('execution', 'Boolean', E::ts('Execution Success')),
]);
}
}
24 changes: 24 additions & 0 deletions Civi/Ewayactions/CompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Civi\Ewayactions;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use CRM_eWAYRecurring_ExtensionUtil as E;

class CompilerPass implements CompilerPassInterface {

/**
* @inheritDoc
*/
public function process( ContainerBuilder $container ) {
if ($container->hasDefinition('action_provider')) {
$actionProviderDefinition = $container->getDefinition('action_provider');
$actionProviderDefinition->addMethodCall('addAction', [
'EwayactionsFillPaymentTokenMeta',
'Civi\Ewayactions\Actions\FillPaymentTokenMeta',
E::ts('EWAY: Fill Payment Token Metadata'),
[]]);
}
}
}
8 changes: 8 additions & 0 deletions eWAYRecurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once 'eWAYRecurring.civix.php';
require_once 'vendor/autoload.php';

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Civi\Payment\Exception\PaymentProcessorException;
use CRM_eWAYRecurring_ExtensionUtil as E;

Expand Down Expand Up @@ -371,3 +372,10 @@ function ewayrecurring_civicrm_permission(&$permissions) {
$permissions['view payment tokens'] = E::ts('CiviContribute: view payment tokens');
$permissions['edit payment tokens'] = E::ts('CiviContribute: edit payment tokens');
}

/**
* Implements hook_civicrm_container().
*/
function ewayrecurring_civicrm_container(ContainerBuilder $container) {
$container->addCompilerPass(new Civi\Ewayactions\CompilerPass());
}

0 comments on commit 4052f66

Please sign in to comment.