We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit 38b1c397 authored by attilak's avatar attilak

Changes based on code review

parent d8991b0b
...@@ -30,12 +30,6 @@ use Magento\Payment\Gateway\Http\ClientInterface; ...@@ -30,12 +30,6 @@ use Magento\Payment\Gateway\Http\ClientInterface;
*/ */
class TransactionPayment implements ClientInterface class TransactionPayment implements ClientInterface
{ {
/**
* @var \Adyen\Client
*/
protected $client;
/** /**
* PaymentRequest constructor. * PaymentRequest constructor.
* *
...@@ -59,8 +53,6 @@ class TransactionPayment implements ClientInterface ...@@ -59,8 +53,6 @@ class TransactionPayment implements ClientInterface
$this->_adyenLogger = $adyenLogger; $this->_adyenLogger = $adyenLogger;
$this->_recurringType = $recurringType; $this->_recurringType = $recurringType;
$this->_appState = $context->getAppState(); $this->_appState = $context->getAppState();
$this->client = $this->_adyenHelper->initializeAdyenClient();
} }
/** /**
...@@ -72,7 +64,9 @@ class TransactionPayment implements ClientInterface ...@@ -72,7 +64,9 @@ class TransactionPayment implements ClientInterface
{ {
$request = $transferObject->getBody(); $request = $transferObject->getBody();
$service = new \Adyen\Service\Checkout($this->client); $client = $this->_adyenHelper->initializeAdyenClient();
$service = new \Adyen\Service\Checkout($client);
try { try {
$response = $service->payments($request); $response = $service->payments($request);
......
...@@ -1175,7 +1175,7 @@ class Data extends AbstractHelper ...@@ -1175,7 +1175,7 @@ class Data extends AbstractHelper
// initialize client // initialize client
$apiKey = $this->getAPIKey($storeId); $apiKey = $this->getAPIKey($storeId);
$client = new \Adyen\Client(); $client = $this->createAdyenClient();
$client->setApplicationName("Magento 2 plugin"); $client->setApplicationName("Magento 2 plugin");
$client->setXApiKey($apiKey); $client->setXApiKey($apiKey);
...@@ -1194,6 +1194,14 @@ class Data extends AbstractHelper ...@@ -1194,6 +1194,14 @@ class Data extends AbstractHelper
return $client; return $client;
} }
/**
* @return \Adyen\Client
* @throws \Adyen\AdyenException
*/
private function createAdyenClient() {
return new \Adyen\Client();
}
/** /**
* Retrieve origin keys for platform's base url * Retrieve origin keys for platform's base url
* *
...@@ -1209,8 +1217,7 @@ class Data extends AbstractHelper ...@@ -1209,8 +1217,7 @@ class Data extends AbstractHelper
if (!$originKey = $this->cache->load("Adyen_origin_key_for_" . $domain)) { if (!$originKey = $this->cache->load("Adyen_origin_key_for_" . $domain)) {
$originKey = ""; $originKey = "";
if ($originKeys = $this->getOriginKeys($domain)) { if ($originKey = $this->getOriginKeyForUrl($domain)) {
$originKey = $originKeys[$domain];
$this->cache->save($originKey, "Adyen_origin_key_for_" . $domain, array(), 60 * 60 * 24); $this->cache->save($originKey, "Adyen_origin_key_for_" . $domain, array(), 60 * 60 * 24);
} }
} }
...@@ -1219,13 +1226,13 @@ class Data extends AbstractHelper ...@@ -1219,13 +1226,13 @@ class Data extends AbstractHelper
} }
/** /**
* Get origin keys for a specific url using the adyen api library client * Get origin key for a specific url using the adyen api library client
* *
* @param $url * @param $url
* @return mixed * @return mixed
* @throws \Adyen\AdyenException * @throws \Adyen\AdyenException
*/ */
private function getOriginKeys($url) private function getOriginKeyForUrl($url)
{ {
$params = array( $params = array(
"originDomains" => array( "originDomains" => array(
...@@ -1237,7 +1244,12 @@ class Data extends AbstractHelper ...@@ -1237,7 +1244,12 @@ class Data extends AbstractHelper
$service = $this->adyenServiceFactory->createCheckoutUtility($client); $service = $this->adyenServiceFactory->createCheckoutUtility($client);
$respone = $service->originKeys($params); $respone = $service->originKeys($params);
return $respone['originKeys'];
if (empty($originKey = $respone['originKeys'][$url])) {
$originKey = "";
}
return $originKey;
} }
/** /**
......
...@@ -174,6 +174,10 @@ ...@@ -174,6 +174,10 @@
font-weight: 400; font-weight: 400;
} }
#adyen-cc-form .fieldset > .holdername.field{
margin-bottom: 10px;
}
#adyen-cc-form .helper-text{ #adyen-cc-form .helper-text{
font-size: 11px; font-size: 11px;
color: rgb(144, 162, 189); color: rgb(144, 162, 189);
......
var AdyenCheckout = (function () {
'use strict';
return {
oneClickForms: [],
dobDateFormat: "yy-mm-dd",
securedFieldsData: {},
allowedCards: [],
cseEncryptedForm: null,
checkout: null,
updateCardType: function ( cardType, friendlyName ) {
$( ".cse-cardtype" ).removeClass( "cse-cardtype-style-active" );
if ( cardType === "unknown" ) {
return;
}
var activeCard = document.getElementById( 'cse-card-' + cardType );
if ( activeCard !== null ) {
activeCard.className = "cse-cardtype cse-cardtype-style-active cse-cardtype-" + cardType;
}
},
enableCardTypeDetection: function ( allowedCards, cardLogosContainer ) {
var cardTypesHTML = "";
this.allowedCards = allowedCards;
for ( var i = allowedCards.length; i-- > 0; ) {
cardTypesHTML = cardTypesHTML + this.getCardSpan( allowedCards[ i ] );
}
cardLogosContainer.innerHTML = cardTypesHTML;
},
getCardSpan: function ( type ) {
return "<span id=\"cse-card-" + type + "\" class=\"cse-cardtype cse-cardtype-style-active cse-cardtype-" + type + "\"></span>";
},
getCardBrand: function () {
if ( this.securedFieldsData.brand ) {
return this.securedFieldsData.brand;
}
var creditCardNumberElement = document.getElementById( 'creditCardNumber' );
if ( creditCardNumberElement ) {
var creditCardNumber = creditCardNumberElement.value.replace( / /g, '' );
return window.adyen.cardTypes.determine( creditCardNumber );
}
},
isAllowedCard: function () {
var brand = this.getCardBrand();
return (brand !== null && this.allowedCards.indexOf( brand ) !== -1);
},
validateForm: function () {
var paymentMethod = $( 'input[type=radio][name=paymentMethod]:checked' ).val();
if ( paymentMethod === "" ) {
window.alert( "Please select a payment method" );
return false;
}
// Check if it is a valid card and encrypt
if ( paymentMethod === "adyen_cc" ) {
if ( !this.isAllowedCard() ) {
window.alert( 'This credit card is not allowed' );
return false;
}
if ( this.cseEncryptedForm ) {
// CSE
this.fillCSEToken( this.cseEncryptedForm );
} else {
// SECURED FIELDS
$( 'input[name="encryptedCardNumber"]' ).val( this.securedFieldsData.new.cardNumber );
$( 'input[name="encryptedExpiryMonth"]' ).val( this.securedFieldsData.new.expiryMonth );
$( 'input[name="encryptedExpiryYear"]' ).val( this.securedFieldsData.new.expiryYear );
$( 'input[name="encryptedSecurityCode"]' ).val( this.securedFieldsData.new.securityCode );
}
} else if ( paymentMethod.indexOf( "adyen_oneclick_" ) === 0 ) {
var recurringReference = paymentMethod.slice( "adyen_oneclick_".length );
$( "#selectedReference" ).val( recurringReference );
if ( this.oneClickForms[ recurringReference ] ) {
// CSE
this.fillCSEToken( this.oneClickForms[ recurringReference ] );
} else {
$( 'input[name="encryptedSecurityCode"]' ).val( this.securedFieldsData[ recurringReference ].securityCode );
}
} else {
// if issuerId is present let the customer select it
var issuer = $( '#p_method_adyen_hpp_' + paymentMethod + '_issuer' );
if ( issuer ) {
if ( issuer.val() === "" ) {
window.alert( 'Please select issuer' );
return false;
}
}
}
// Remove SF injected elements
$( '#encrypted-hostedCardNumberField' ).remove();
$( '#encrypted-month' ).remove();
$( '#encrypted-year' ).remove();
$( '#encrypted-hostedSecurityCodeField' ).remove();
$( 'input[name="txvariant"]' ).remove();
return true;
},
fillCSEToken: function ( form ) {
var cseToken = form.encrypt();
if ( cseToken === false ) {
window.alert( 'This credit card is not valid' );
return false;
}
$( "#cseToken" ).val( cseToken );
},
/**
* Set Custom values for certain payment methods
*/
setCustomPaymentMethodValues: function () {
var paymentMethod = $( 'input[type=radio][name=paymentMethod]:checked' ).val();
var dob = $( '#p_method_adyen_hpp_' + paymentMethod + '_dob' );
if ( dob ) {
$( "#dob" ).val( dob.val() );
}
var ssn = $( '#p_method_adyen_hpp_' + paymentMethod + '_ssn' );
if ( ssn ) {
$( "#socialSecurityNumber" ).val( ssn.val() );
}
},
createForm: function () {
// The form element to encrypt.
var form = document.getElementById( 'adyen-encrypted-form' );
// Form and encryption options. See adyen.encrypt.simple.html for details.
var options = {};
options.cvcIgnoreBins = '6703'; // Ignore CVC for BCMC
// Create the form.
// Note that the method is on the adyen object, not the adyen.encrypt object.
this.cseEncryptedForm = window.adyen.createEncryptedForm( form, options );
this.cseEncryptedForm.addCardTypeDetection( this.updateCardType );
return this.cseEncryptedForm;
},
createOneClickForm: function ( recurringReference ) {
// The form element to encrypt.
var form = document.getElementById( 'adyen-encrypted-form' );
// Form and encryption options. See adyen.encrypt.simple.html for details.
var options = {};
options.fieldNameAttribute = "data-encrypted-name-" + recurringReference;
options.enableValidations = false;
// Create the form.
// Note that the method is on the adyen object, not the adyen.encrypt object.
var encryptedForm = window.adyen.createEncryptedForm( form, options );
this.oneClickForms[ recurringReference ] = encryptedForm;
return encryptedForm;
},
/**
* Create DatePicker for Date of Birth field
*
* @param element
*/
createDobDatePicker: function ( element ) {
$( "." + element ).datepicker( {
dateFormat: this.dobDateFormat,
changeMonth: true,
changeYear: true,
yearRange: "-120:+0"
} );
},
togglePaymentMethod: function ( paymentMethod ) {
$( ".payment_method_details" ).hide();
$( ".chckt-pm__details" ).hide();
$( "#dd_method_" + paymentMethod ).show();
$( "#adyen_hpp_" + paymentMethod + "_container" ).show();
},
createDfValue: function () {
window.dfDo( "dfValue" );
},
createSecuredFieldsForm: function ( originKey, rootNode ) {
var self = this;
var securedFieldsConfiguration = {
configObject: {
originKey: originKey,
cardGroupTypes: self.allowedCards
},
rootNode: rootNode
};
var securedFields = window.csf( securedFieldsConfiguration );
securedFields.onBrand( function ( brandObject ) {
if ( brandObject.brand ) {
self.securedFieldsData.brand = brandObject.brand;
self.updateCardType( brandObject.brand, null );
}
} );
window.addEventListener( "message", this.securedFieldsListener( this ), false );
return securedFields;
},
securedFieldsListener: function ( self ) {
return function ( message ) {
if ( !("data" in message) ) {
return;
}
try {
var data = JSON.parse( message.data );
if ( "encryptionSuccess" in data && data.encryptionSuccess === true ) {
var fieldName = data.cseKey;
var encryptedData = data[ fieldName ];
var securedFieldsData = self.securedFieldsData;
var parts = fieldName.split( "-" );
var card = "new";
if ( parts.length === 2 ) {
card = parts[ 1 ];
}
if ( !(card in securedFieldsData) ) {
securedFieldsData[ card ] = {};
}
switch ( data.fieldType ) {
case "hostedCardNumberField":
securedFieldsData[ card ].cardNumber = encryptedData;
break;
case "month":
securedFieldsData[ card ].expiryMonth = encryptedData;
break;
case "year":
securedFieldsData[ card ].expiryYear = encryptedData;
break;
case "hostedSecurityCodeField":
securedFieldsData[ card ].securityCode = encryptedData;
break;
}
}
} catch (e) {
//not json data
}
};
},
initiateCheckout: function (locale) {
var configuration = {
locale: locale // shopper's locale
};
this.checkout = new Adyen.Checkout(configuration);
},
initiateIdeal: function (idealItems) {
var idealNode = document.getElementById('adyen_hpp_ideal_container');
var ideal = this.checkout.create('ideal', {
items: idealItems, // The array of issuers coming from the /paymentMethods api call
showImage: true, // Optional, defaults to true
onChange: handleChange // Gets triggered whenever a user selects a bank// Gets triggered once the state is valid
});
function handleChange(event) {
var issuerIdField = document.getElementById('issuerId');
var issuerId = event.data.issuer;
issuerIdField.value = issuerId;
}
try {
ideal.mount(idealNode);
} catch (e) {
console.log('Something went wrong trying to mount the iDEAL component: ${e}');
}
}
};
})();
...@@ -30,8 +30,7 @@ define( ...@@ -30,8 +30,7 @@ define(
'Magento_Checkout/js/model/quote', 'Magento_Checkout/js/model/quote',
'Adyen_Payment/js/model/installments', 'Adyen_Payment/js/model/installments',
'mage/url', 'mage/url',
'Adyen_Payment/js/adyen.2.0.0', 'Adyen_Payment/js/adyen.2.0.0'
'Adyen_Payment/js/adyen.checkout'
], ],
function ($, ko, Component, customer, creditCardData, additionalValidators, quote, installments, url) { function ($, ko, Component, customer, creditCardData, additionalValidators, quote, installments, url) {
......
...@@ -68,29 +68,6 @@ ...@@ -68,29 +68,6 @@
<br/> <br/>
<!-- /ko --> <!-- /ko -->
<div class="field holdername type">
<label data-bind="attr: {for: 'creditCardHolderName'}" class="label">
<span><!-- ko text: $t('Credit Card Owner')--><!-- /ko --></span>
</label>
<div class="control">
<input type="text"
class="input-text required-entry"
data-encrypted-name="holderName"
value=""
data-bind="attr: {
id: 'creditCardHolderName',
title: $t('Credit Card Owner'),
placeholder: $t('Credit Card Owner'),
'data-container': getCode() + '-cc-owner'
},
enable: isActive($parents),
value: creditCardOwner,
valueUpdate: 'keyup' "
data-validate="{required:true}"
/>
</div>
</div>
<div class="field type"> <div class="field type">
<label data-bind="attr: {for: getCode() + '_cc_type'}" class="label"> <label data-bind="attr: {for: getCode() + '_cc_type'}" class="label">
<span><!-- ko text: $t('Credit Card Type')--><!-- /ko --></span> <span><!-- ko text: $t('Credit Card Type')--><!-- /ko --></span>
...@@ -120,6 +97,30 @@ ...@@ -120,6 +97,30 @@
"> ">
</div> </div>
</div> </div>
<div class="field holdername type">
<label data-bind="attr: {for: 'creditCardHolderName'}" class="label">
<span><!-- ko text: $t('Credit Card Owner')--><!-- /ko --></span>
</label>
<div class="control">
<input type="text"
class="input-text required-entry"
data-encrypted-name="holderName"
value=""
data-bind="attr: {
id: 'creditCardHolderName',
title: $t('Credit Card Owner'),
placeholder: $t('Credit Card Owner'),
'data-container': getCode() + '-cc-owner'
},
enable: isActive($parents),
value: creditCardOwner,
valueUpdate: 'keyup' "
data-validate="{required:true}"
/>
</div>
</div>
<div class="field number cardContainerField"> <div class="field number cardContainerField">
<div afterRender="renderSecureFields()" data-bind="attr: { id: 'cardContainer'}" ></div> <div afterRender="renderSecureFields()" data-bind="attr: { id: 'cardContainer'}" ></div>
</div> </div>
......
Markdown is supported
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