Skip to content

Commit

Permalink
feat: App app and version to NFAPI call.
Browse files Browse the repository at this point in the history
  • Loading branch information
soleilcot committed Sep 30, 2024
1 parent b828fc7 commit 70a065d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 53 deletions.
6 changes: 3 additions & 3 deletions Api/Portal/RequestHandler.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace NoFraud\Connect\Api\Portal;

use org\bovigo\vfs\DirectoryIterationTestCase;

class RequestHandler extends \NoFraud\Connect\Api\Request\Handler\AbstractHandler
{
private const TRANSACTION_STATUS_ENDPOINT = 'status_by_invoice';

/**
* Build
*
Expand Down
111 changes: 61 additions & 50 deletions Api/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

class RequestHandler extends \NoFraud\Connect\Api\Request\Handler\AbstractHandler
{
private const DEFAULT_AVS_CODE = 'U';
private const DEFAULT_CVV_CODE = 'U';
private const BRAINTREE_CODE = 'braintree';
private const DEFAULT_AVS_CODE = 'U';
private const DEFAULT_CVV_CODE = 'U';
private const BRAINTREE_CODE = 'braintree';
private const MAGEDELIGHT_AUTHNET_CIM_METHOD_CODE = 'md_authorizecim';
private const PARADOXLABS_CIM_METHOD_CODE = 'authnetcim';
private const PL_MI_METHOD_CODE = 'nmi_directpost';
private const PARADOXLABS_CIM_METHOD_CODE = 'authnetcim';
private const PL_MI_METHOD_CODE = 'nmi_directpost';
private $versionHelper;

/**
* @var Currency
Expand Down Expand Up @@ -68,7 +69,8 @@ public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Model\Customer $customer,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface $orderCollectionFactory,
\Magento\Framework\Locale\ResolverInterface $localeResolver
\Magento\Framework\Locale\ResolverInterface $localeResolver,
\NoFraud\Connect\Helper\Version $versionHelper
) {

parent::__construct($logger, $curl);
Expand All @@ -78,6 +80,7 @@ public function __construct(
$this->customer = $customer;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->_localeResolver = $localeResolver;
$this->versionHelper = $versionHelper;
}

/**
Expand All @@ -91,12 +94,12 @@ public function __construct(
*/
public function build($payment, $order, $apiToken)
{
$params = $this->buildBaseParams($payment, $order, $apiToken);
$params = $this->buildBaseParams($payment, $order, $apiToken);
$params['customer'] = $this->buildCustomerParams($order);
$params['order'] = $this->buildOrderParams($order);
$params['payment'] = $this->buildPaymentParams($payment);
$params['billTo'] = $this->buildAddressParams($order->getBillingAddress(), true);
$params['shipTo'] = $this->buildAddressParams($order->getShippingAddress());
$params['order'] = $this->buildOrderParams($order);
$params['payment'] = $this->buildPaymentParams($payment);
$params['billTo'] = $this->buildAddressParams($order->getBillingAddress(), true);
$params['shipTo'] = $this->buildAddressParams($order->getShippingAddress());
$params['lineItems'] = $this->buildLineItemsParams($order->getItems());

$paramsAdditionalInfo = $this->buildParamsAdditionalInfo($payment);
Expand All @@ -117,12 +120,20 @@ protected function buildBaseParams($payment, $order, $apiToken)
{
$baseParams = [];

$baseParams['nf-token'] = $apiToken;
$baseParams['amount'] = $this->formatTotal($order->getGrandTotal());
$baseParams['currency_code'] = $order->getOrderCurrencyCode();
$baseParams['app'] = 'Magento M2 Plugin';

try {
$baseParams['version'] = $this->versionHelper->getVersion();
} catch (\Exception $e) {
$baseParams['version'] = '';
}

$baseParams['nf-token'] = $apiToken;
$baseParams['amount'] = $this->formatTotal($order->getGrandTotal());
$baseParams['currency_code'] = $order->getOrderCurrencyCode();
$baseParams['shippingAmount'] = $this->formatTotal($order->getShippingAmount());
$baseParams['avsResultCode'] = self::DEFAULT_AVS_CODE;
$baseParams['cvvResultCode'] = self::DEFAULT_CVV_CODE;
$baseParams['avsResultCode'] = self::DEFAULT_AVS_CODE;
$baseParams['cvvResultCode'] = self::DEFAULT_CVV_CODE;

if (empty($order->getXForwardedFor())) {
$baseParams['customerIP'] = $order->getRemoteIp();
Expand Down Expand Up @@ -215,11 +226,11 @@ protected function getCustomerOrders($customerId)
return $this->orderCollectionFactory->create(
$customerId
)->addFieldToSelect(
'*'
)->setOrder(
'created_at',
'desc'
)->getItems();
'*'
)->setOrder(
'created_at',
'desc'
)->getItems();
}

/**
Expand Down Expand Up @@ -247,10 +258,10 @@ protected function buildPaymentParams($payment)
{
$cc = [];

$cc['cardType'] = $this->formatCcType($payment->getCcType());
$cc['cardNumber'] = $payment->getCcNumber();
$cc['cardType'] = $this->formatCcType($payment->getCcType());
$cc['cardNumber'] = $payment->getCcNumber();
$cc['expirationDate'] = $this->buildCcExpDate($payment);
$cc['cardCode'] = $payment->getCcCid();
$cc['cardCode'] = $payment->getCcCid();

if ($last4 = $this->decryptLast4($payment)) {
$cc['last4'] = $last4;
Expand Down Expand Up @@ -316,7 +327,7 @@ protected function buildCcExpDate($payment)
$expYear = $payment->getCcExpYear();

// Pad a one-digit month with a 0;
if (isset($expMonth) && !empty($expMonth)){
if (isset($expMonth) && !empty($expMonth)) {
if (strlen($expMonth) == 1) {
$expMonth = "0" . $expMonth;
}
Expand All @@ -330,7 +341,7 @@ protected function buildCcExpDate($payment)
// NoFraud requires an expiration year;
// If year is invalid, return nothing;
// Else if year is four digits (1999), truncate it to two (99);
if (isset($expYear) && !empty($expYear)){
if (isset($expYear) && !empty($expYear)) {
if (strlen($expYear) > 4) {
return;
} elseif (strlen($expYear) == 4) {
Expand Down Expand Up @@ -358,13 +369,13 @@ protected function buildAddressParams($address, $includePhoneNumber = false)
$addressParams = [];

$addressParams['firstName'] = $address->getFirstname();
$addressParams['lastName'] = $address->getLastname();
$addressParams['company'] = $address->getCompany();
$addressParams['address'] = implode(' ', $address->getStreet());
$addressParams['city'] = $address->getCity();
$addressParams['state'] = $address->getRegionCode();
$addressParams['zip'] = $address->getPostcode();
$addressParams['country'] = $address->getCountryId();
$addressParams['lastName'] = $address->getLastname();
$addressParams['company'] = $address->getCompany();
$addressParams['address'] = implode(' ', $address->getStreet());
$addressParams['city'] = $address->getCity();
$addressParams['state'] = $address->getRegionCode();
$addressParams['zip'] = $address->getPostcode();
$addressParams['country'] = $address->getCountryId();

if ($includePhoneNumber) {
$addressParams['phoneNumber'] = $address->getTelephone();
Expand Down Expand Up @@ -394,9 +405,9 @@ protected function buildLineItemsParams($orderItems)

$lineItem = [];

$lineItem['sku'] = $item->getSku();
$lineItem['name'] = $item->getName();
$lineItem['price'] = $this->formatTotal($item->getPrice());
$lineItem['sku'] = $item->getSku();
$lineItem['name'] = $item->getName();
$lineItem['price'] = $this->formatTotal($item->getPrice());
$lineItem['quantity'] = $item->getQtyOrdered();

$lineItemsParams[] = $lineItem;
Expand All @@ -416,12 +427,12 @@ protected function formatTotal($amount)
if (empty($amount)) {
return;
}

$value = $this->currency->formatTxt($amount, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);

$separatorComa = strpos($value, ',');
$separatorDot = strpos($value, '.');
$price = $value;
$separatorDot = strpos($value, '.');
$price = $value;

if ($separatorComa !== false && $separatorDot !== false) {
if ($separatorComa > $separatorDot) {
Expand All @@ -434,13 +445,13 @@ protected function formatTotal($amount)
* It's hard code for Japan locale.
*/
$price = number_format(
(float)
(float)
str_replace(',', $locale === self::JAPAN_LOCALE_CODE ? '' : '.', $value),
2,
'.',
','
);
}
}
return $price;
}

Expand All @@ -464,10 +475,10 @@ protected function buildParamsAdditionalInfo($payment)

case \Magento\Paypal\Model\Config::METHOD_PAYFLOWPRO:
$last4 = $info['cc_details']['cc_last_4'] ?? null;
$sAvs = $info['avsaddr'] ?? null; // AVS Street Address Match
$zAvs = $info['avszip'] ?? null; // AVS Zip Code Match
$iAvs = $info['iavs'] ?? null; // International AVS Response
$cvv = $info['cvv2match'] ?? null;
$sAvs = $info['avsaddr'] ?? null; // AVS Street Address Match
$zAvs = $info['avszip'] ?? null; // AVS Zip Code Match
$iAvs = $info['iavs'] ?? null; // International AVS Response
$cvv = $info['cvv2match'] ?? null;

$params = [
"payment" => [
Expand All @@ -482,16 +493,16 @@ protected function buildParamsAdditionalInfo($payment)
break;

case self::BRAINTREE_CODE:
$last4 = substr($info['cc_number'] ?? [], -4);
$last4 = substr($info['cc_number'] ?? [], -4);
$cardType = $info['cc_type'] ?? null;
$sAvs = $info['avsStreetAddressResponseCode'] ?? null; // AVS Street Address Match
$zAvs = $info['avsPostalCodeResponseCode'] ?? null; // AVS Zip Code Match
$cvv = $info['cvvResponseCode'] ?? null;
$sAvs = $info['avsStreetAddressResponseCode'] ?? null; // AVS Street Address Match
$zAvs = $info['avsPostalCodeResponseCode'] ?? null; // AVS Zip Code Match
$cvv = $info['cvvResponseCode'] ?? null;

$params = [
"payment" => [
"creditCard" => [
"last4" => $last4,
"last4" => $last4,
"cardType" => $cardType,
],
],
Expand Down
55 changes: 55 additions & 0 deletions Helper/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Created by Nofraud Connect
* Author: Soleil Cotterell
* Date: 8/23/24
*/

namespace NoFraud\Connect\Helper;

use Magento\Framework\Module\Dir\Reader as DirReader;

class Version extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $dirReader;

/**
* Constructor
*
* @param DirReader $dirReader
* @param \Magento\Framework\App\Helper\Context $context
* @param \NoFraud\Connect\Logger\Logger $logger
*/
public function __construct(
DirReader $dirReader,
\Magento\Framework\App\Helper\Context $context,
\NoFraud\Connect\Logger\Logger $logger
) {
parent::__construct($context);
$this->logger = $logger;
$this->dirReader = $dirReader;
}

public function getVersion()
{
$installVersion = "unidentified";
$composer = $this->getComposerInformation("NoFraud_Connect");

if ($composer) {
$installVersion = $composer['version'];
}

return $installVersion;
}

public function getComposerInformation($moduleName)
{
$dir = $this->dirReader->getModuleDir("", $moduleName);

if (file_exists($dir . '/composer.json')) {
return json_decode(file_get_contents($dir . '/composer.json'), true);
}

return false;
}
}

0 comments on commit 70a065d

Please sign in to comment.