Skip to content

Commit

Permalink
Merge CIVIEWAY-255 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-fj committed Jul 28, 2023
2 parents e251396 + d36cf8b commit cca20cd
Show file tree
Hide file tree
Showing 12 changed files with 832 additions and 715 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<?php

require_once 'CRM/Core/Payment.php';
require_once 'eWAYRecurring.process.inc';
use Civi\Payment\Exception\PaymentProcessorException;
use CRM_eWAYRecurring_ExtensionUtil as E;

// Include eWay SDK.
require_once extensionPath('vendor/autoload.php');
require_once E::path('vendor/autoload.php');

use Civi\Payment\Exception\PaymentProcessorException;
use CRM_eWAYRecurring_ExtensionUtil as E;
class CRM_Core_Payment_eWAYRecurring extends CRM_Core_Payment {
use CRM_eWAYRecurring_ProcessTrait;

class au_com_agileware_ewayrecurring extends CRM_Core_Payment {
public const RECEIPT_SUBJECT_TITLE = 'Recurring Donation';
private $jsEmbedded = FALSE;

private $jsEmbedded = FALSE;
private static $instances = [];

private $eWayClient;
public static function getInstance(&$paymentProcessor) {
[ 'domain_id' => $domain, 'id' => $id ] = $paymentProcessor;

if(empty(self::$instances["$domain/$id"])) {
$mode = empty( $paymentProcessor['is_test'] ) ? 'test' : 'live';
self::$instances["$domain/$id"] = new self( $mode, $paymentProcessor );
}

return self::$instances["$domain/$id"];
}

/**
* Constructor
Expand All @@ -28,24 +38,6 @@ function __construct($mode, &$paymentProcessor) {
$this->_processorName = ts('eWay Recurring');
}

/**
* Create eWay client using credentials from payment processor.
*
* @return \Eway\Rapid\Contract\Client
*/
function getEWayClient() {
if(!$this->eWayClient) {
$eWayApiKey = $this->_paymentProcessor['user_name']; // eWay Api Key
$eWayApiPassword = $this->_paymentProcessor['password']; // eWay Api Password
$eWayEndPoint = ($this->_paymentProcessor['is_test']) ? \Eway\Rapid\Client::MODE_SANDBOX : \Eway\Rapid\Client::MODE_PRODUCTION;


$this->eWayClient = \Eway\Rapid::createClient($eWayApiKey, $eWayApiPassword, $eWayEndPoint);
}

return $this->eWayClient;
}

/**
* Validate contribution on successful response.
*
Expand Down Expand Up @@ -118,7 +110,7 @@ function getPaymentFormFieldsMetadata() {
$jsSetting .= "CRM.eway.ppid = {$this->_paymentProcessor['id']};";
$jsSetting .= 'CRM.eway.paymentTokenInitialize();';

CRM_Core_Resources::singleton()->addScript($jsSetting);
CRM_Core_Resources::singleton()->addScript($jsSetting, ['weight' => 10, 'region' => 'page-footer']);
$this->jsEmbedded = TRUE;
}
return [
Expand Down Expand Up @@ -669,7 +661,7 @@ function checkConfig() {
* @return bool
*/
function handlePaymentCron() {
return process_recurring_payments($this->_paymentProcessor, $this);
return $this->process_recurring_payments( $this->_paymentProcessor );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions CRM/eWAYRecurring/Page/VerifyPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function run() {

if (count($paymentProcessorInfo) > 0) {
$paymentProcessorInfo = $paymentProcessorInfo[0];
$paymentProcessor = new au_com_agileware_ewayrecurring(($paymentProcessorInfo['is_test']) ? 'test' : 'live', $paymentProcessorInfo);
$paymentProcessor = new CRM_Core_Payment_eWAYRecurring(($paymentProcessorInfo['is_test']) ? 'test' : 'live', $paymentProcessorInfo);

try {
// This function will do redirect if the payment failed
$response = validateEwayContribution($paymentProcessor, $contributionInvoiceID);
$response = CRM_eWAYRecurring_Utils::validateEwayContribution($paymentProcessor, $contributionInvoiceID);
} catch (CRM_Core_Exception $e) {
} catch (CiviCRM_API3_Exception $e) {
}
Expand Down
88 changes: 83 additions & 5 deletions CRM/eWAYRecurring/PaymentToken.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php
require_once 'vendor/autoload.php';

use Civi\Api4\PaymentProcessor;
use Civi\Api4\PaymentToken;
use CRM_eWAYRecurring_ExtensionUtil as E;

require_once E::path('vendor/autoload.php');

class CRM_eWAYRecurring_PaymentToken {

Expand All @@ -10,6 +15,8 @@ class CRM_eWAYRecurring_PaymentToken {
* @throws \CiviCRM_API3_Exception
*/
public static function createToken() {
$store = NULL;

$contact_id = CRM_Utils_Request::retrieve('contact_id', 'String', $store, TRUE);
$pp_id = CRM_Utils_Request::retrieve('pp_id', 'String', $store, TRUE);

Expand All @@ -18,7 +25,8 @@ public static function createToken() {
$billingDetails['first_name'] = CRM_Utils_Request::retrieve('billing_first_name', 'String');
$billingDetails['middle_name'] = CRM_Utils_Request::retrieve('billing_middle_name', 'String');
$billingDetails['last_name'] = CRM_Utils_Request::retrieve('billing_last_name', 'String');
foreach ($_POST as $key => $data) {
foreach (array_keys($_POST) as $key) {
$data = CRM_Utils_Request::retrieve($key, 'String');
if (strpos($key, 'billing_street_address') !== FALSE) {
$billingDetails['billing_street_address'] = $data;
}
Expand Down Expand Up @@ -288,7 +296,77 @@ public static function getPaymentProcessorById($id) {
return NULL;
}
$paymentProcessorInfo = $paymentProcessorInfo[0];
$paymentProcessor = new au_com_agileware_ewayrecurring(($paymentProcessorInfo['is_test']) ? 'test' : 'live', $paymentProcessorInfo);
return $paymentProcessor->getPaymentProcessor();

return CRM_Core_Payment_eWAYRecurring::getInstance($paymentProcessorInfo)
->getPaymentProcessor();
}

public static function fillTokensMeta() {
// Caches payment processors
$processors = [];

$results = ['count' => 0];

$tokens = PaymentToken::get(FALSE)
->addWhere('payment_processor_id.payment_processor_type_id.name', '=', 'eWay_Recurring')
// Unretrieved tokens can end up with a saved token with id 0
// ->addWhere('token', '!=', '0')
->addClause('OR', [
'expiry_date',
'IS NULL'
], [
'masked_account_number',
'IS NULL'
])
->execute();

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

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

// Skip if unable to log in to eWAY
if($eway_client->getErrors()) {
continue;
}

$token_customer = $eway_client->queryCustomer($token['token']);

// Skip if custom query fails
$errors = $token_customer->getErrors();

if($errors){
foreach($errors as &$error) {
$error = E::ts('Error retrieving data for token id %1: %2', [1 => $token['id'], 2 => $error]);
}

$result['errors'] = array_merge($result['errors'] ?? [], $errors);
$result['is_error'] = TRUE;
continue;
}

$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');

$update = PaymentToken::update(FALSE)
->addWhere('id', '=', $token['id'])
->addValue('masked_account_number', $card_number)
->addValue('expiry_date', $expiry_date)
->execute();

$results['count']++;
}
return $results;
}
}
}
Loading

0 comments on commit cca20cd

Please sign in to comment.