Skip to content

Commit

Permalink
PW-1364: review comments (#2)
Browse files Browse the repository at this point in the history
* PW-1364: added correct amount calculation, added error template page,
 show error if originKey is missing, added live endpoint url prefix

* PW-1364: added foreach to paymentcollection

* PW-1364: simplified error tpl

* PW-1364: review comments
  • Loading branch information
Aleffio committed Jul 18, 2019
1 parent fc3d237 commit 65b7e25
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 92 deletions.
5 changes: 5 additions & 0 deletions adyen.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function getContent()
$notification_password = (string)Tools::getValue('ADYEN_NOTI_PASSWORD');
$api_key_test = $this->helper_data->encrypt(Tools::getValue('ADYEN_APIKEY_TEST'));
$api_key_live = $this->helper_data->encrypt(Tools::getValue('ADYEN_APIKEY_LIVE'));
$live_endpoint_url_prefix = (string)Tools::getValue('ADYEN_LIVE_ENDPOINT_URL_PREFIX');


// validating the input
Expand All @@ -167,6 +168,7 @@ public function getContent()
Configuration::updateValue('ADYEN_MODE', $mode);
Configuration::updateValue('ADYEN_NOTI_USERNAME', $notification_username);
Configuration::updateValue('ADYEN_NOTI_PASSWORD', $notification_password);
Configuration::updateValue('ADYEN_LIVE_ENDPOINT_URL_PREFIX', $live_endpoint_url_prefix);
if (!empty($api_key_test)) {
Configuration::updateValue('ADYEN_APIKEY_TEST', $api_key_test);
}
Expand Down Expand Up @@ -303,13 +305,15 @@ public function displayForm()
$mode = (string)Tools::getValue('ADYEN_MODE');
$notification_username = (string)Tools::getValue('ADYEN_NOTI_USERNAME');
$notification_password = (string)Tools::getValue('ADYEN_NOTI_PASSWORD');
$live_endpoint_url_prefix = (string)Tools::getValue('ADYEN_LIVE_ENDPOINT_URL_PREFIX');
$api_key_test = $this->hashing->hash(Tools::getValue('ADYEN_APIKEY_TEST'), _COOKIE_KEY_);
$api_key_live = $this->hashing->hash(Tools::getValue('ADYEN_APIKEY_LIVE'), _COOKIE_KEY_);
} else {
$merchant_account = Configuration::get('ADYEN_MERCHANT_ACCOUNT');
$mode = Configuration::get('ADYEN_MODE');
$notification_username = Configuration::get('ADYEN_NOTI_USERNAME');
$notification_password = Configuration::get('ADYEN_NOTI_PASSWORD');
$live_endpoint_url_prefix = Configuration::get('ADYEN_LIVE_ENDPOINT_URL_PREFIX');
$api_key_test = $this->hashing->hash(Configuration::get('ADYEN_APIKEY_TEST'),
_COOKIE_KEY_);;
$api_key_live = $this->hashing->hash(Configuration::get('ADYEN_APIKEY_LIVE'),
Expand All @@ -323,6 +327,7 @@ public function displayForm()
$helper->fields_value['ADYEN_NOTI_PASSWORD'] = $notification_password;
$helper->fields_value['ADYEN_APIKEY_TEST'] = $api_key_test;
$helper->fields_value['ADYEN_APIKEY_LIVE'] = $api_key_live;
$helper->fields_value['ADYEN_LIVE_ENDPOINT_URL_PREFIX'] = $live_endpoint_url_prefix;

return $helper->generateForm($fields_form);
}
Expand Down
44 changes: 30 additions & 14 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public function postProcess()
{
$cart = $this->context->cart;
$client = $this->helper_data->initializeAdyenClient();
// todo: applicationInfo, uncomment before release
// $client->setAdyenPaymentSource($this->helper_data->getModuleName(), $this->helper_data->getModuleVersion());
$request = [];
$request = $this->buildCCData($request, $_REQUEST);
$request = $this->buildPaymentData($request);
Expand All @@ -59,7 +57,6 @@ public function postProcess()
$response = $service->payments($request);
} catch (\Adyen\AdyenException $e) {
$response['error'] = $e->getMessage();
die('There was an error with the payment method.');
}

$customer = new Customer($cart->id_customer);
Expand All @@ -81,14 +78,23 @@ public function postProcess()
(int)$currency->id, false, $customer->secure_key);
$new_order = new Order((int)$this->module->currentOrder);
if (Validate::isLoadedObject($new_order)) {
$payment = $new_order->getOrderPaymentCollection();
if (isset($payment[0])) {
//todo add !empty
$payment[0]->card_number = pSQL($response['additionalData']['cardBin'] . " *** " . $response['additionalData']['cardSummary']);
$payment[0]->card_brand = pSQL($response['additionalData']['paymentMethod']);
$payment[0]->card_expiration = pSQL($response['additionalData']['expiryDate']);
$payment[0]->card_holder = pSQL($response['additionalData']['cardHolderName']);
$payment[0]->save();
$paymentCollection = $new_order->getOrderPaymentCollection();
foreach ($paymentCollection as $payment) {
if (!empty($response['additionalData']['cardBin'] &&
!empty($response['additionalData']['cardSummary']))) {
$payment->card_number = pSQL($response['additionalData']['cardBin'] . " *** " . $response['additionalData']['cardSummary']);
}
if (!empty($response['additionalData']['paymentMethod'])) {
$payment->card_brand = pSQL($response['additionalData']['paymentMethod']);
}
if (!empty($response['additionalData']['expiryDate'])) {
$payment->card_expiration = pSQL($response['additionalData']['expiryDate']);

}
if (!empty($response['additionalData']['cardHolderName']) {
$payment->card_holder = pSQL($response['additionalData']['cardHolderName']);
}
$payment->save();
}
}
Tools::redirect('index.php?controller=order-confirmation&id_cart=' . $cart->id . '&id_module=' . $this->module->id . '&id_order=' . $this->module->currentOrder . '&key=' . $customer->secure_key);
Expand All @@ -97,13 +103,23 @@ public function postProcess()
//6_PS_OS_CANCELED_ : order canceled
$this->module->validateOrder($cart->id, 6, $total, $this->module->displayName, null, $extra_vars,
(int)$currency->id, false, $customer->secure_key);
die('The payment was refused');
$this->helper_data->adyenLogger()->logError("The payment was refused, id: " . $cart->id);
if ($this->helper_data->isPrestashop16()) {
return $this->setTemplate('error.tpl');
} else {
return $this->setTemplate('module:adyen/views/templates/front/error.tpl');
}
break;
default:
//8_PS_OS_ERROR_ : payment error
$this->module->validateOrder($cart->id, 8, $total, $this->module->displayName, null, $extra_vars,
(int)$currency->id, false, $customer->secure_key);
die('There was an error with the payment method.');
$this->helper_data->adyenLogger()->logError("There was an error with the payment method. id: " . $cart->id);
if ($this->helper_data->isPrestashop16()) {
return $this->setTemplate('error.tpl');
} else {
return $this->setTemplate('module:adyen/views/templates/front/error.tpl');
}
break;
}

Expand Down Expand Up @@ -188,7 +204,7 @@ public function buildPaymentData($request)
$cart = $this->context->cart;
$request['amount'] = [
'currency' => $this->context->currency->iso_code,
'value' => number_format($cart->getOrderTotal(true, 3), 2, '', '')
'value' => $this->helper_data->formatAmount($cart->getOrderTotal(true, 3), $this->context->currency->iso_code)
];


Expand Down
51 changes: 47 additions & 4 deletions helper/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function getOriginKeyForOrigin()

if (!empty($response['originKeys'][$origin])) {
$originKey = $response['originKeys'][$origin];
} else {
$this->adyenLogger()->logError("OriginKey is empty, please verify that your API key is correct");
}

return $originKey;
Expand Down Expand Up @@ -102,15 +104,14 @@ public function initializeAdyenClient()
$client = $this->createAdyenClient();
$client->setApplicationName("Prestashop plugin");
$client->setXApiKey($apiKey);
$client->setAdyenPaymentSource($this->getModuleName(), $this->getModuleVersion());
$client->setExternalPlatform("Prestashop" , _PS_VERSION_);

if ($this->isDemoMode()) {
$client->setEnvironment(\Adyen\Environment::TEST);
} else {
//todo liveendpointprefix
// $client->setEnvironment(\Adyen\Environment::LIVE, $this->getLiveEndpointPrefix($storeId));
$client->setEnvironment(\Adyen\Environment::LIVE, Configuration::get('ADYEN_LIVE_ENDPOINT_URL_PREFIX'));
}


return $client;
}

Expand Down Expand Up @@ -200,4 +201,46 @@ public function isPrestashop16()
return false;
}

/**
* Return the formatted currency. Adyen accepts the currency in multiple formats.
* @param $amount
* @param $currency
* @return string
*/
public function formatAmount($amount, $currency)
{
switch ($currency) {
case "CVE":
case "DJF":
case "GNF":
case "IDR":
case "JPY":
case "KMF":
case "KRW":
case "PYG":
case "RWF":
case "UGX":
case "VND":
case "VUV":
case "XAF":
case "XOF":
case "XPF":
$format = 0;
break;
case "BHD":
case "IQD":
case "JOD":
case "KWD":
case "LYD":
case "OMR":
case "TND":
$format = 3;
break;
default:
$format = 2;
}

return (int)number_format($amount, $format, '', '');
}

}
11 changes: 11 additions & 0 deletions views/templates/front/error.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{extends file='page.tpl'}

{block name='page_content_container'}
<h3>{l s='There was an error' mod='adyen'}</h3>

<p class="warning">
{l s='We have noticed that there is a problem with your order.' mod='adyen'}
</p>

{/block}

154 changes: 80 additions & 74 deletions views/templates/front/payment.tpl
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
<div class="row">
<div class="col-xs-12 col-md-6">
<form action="{$action}" id="payment-form" method="post">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
renderSecureFields();
});
function renderSecureFields() {
var locale = "en_US";
var cardNode = document.getElementById('cardContainer');
// we can now rely on $ within the safety of our "bodyguard" function
var checkout = new AdyenCheckout({
locale: locale,
originKey: "{$originKey}",
environment: "{$environment}",
risk: {
enabled: false
}
{if !$originKey}
<form id="payment-form" method="post">
<h5>There is an error with retrieving the originKey,
please check your API key in the Adyen Module configuration</h5>
</form>
{else}
<div class="row">
<div class="col-xs-12 col-md-6">
<form action="{$action}" id="payment-form" method="post">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
renderSecureFields();
});
var card = checkout.create('card', {
type: 'card',
hasHolderName: true,
holderNameRequired: true,
onChange: function (state, component) {
console.log(state, component);
if (state.isValid && !component.state.errors.encryptedSecurityCode) {
document.getElementById('holderName').value = state.data.paymentMethod.holderName;
document.getElementById('encryptedCardNumber').value = state.data.paymentMethod.encryptedCardNumber;
document.getElementById('encryptedExpiryMonth').value = state.data.paymentMethod.encryptedExpiryMonth;
document.getElementById('encryptedExpiryYear').value = state.data.paymentMethod.encryptedExpiryYear;
if (state.data.paymentMethod.encryptedSecurityCode) {
document.getElementById('encryptedSecurityCode').value = state.data.paymentMethod.encryptedSecurityCode;
function renderSecureFields() {
var locale = "en_US";
var cardNode = document.getElementById('cardContainer');
// we can now rely on $ within the safety of our "bodyguard" function
var checkout = new AdyenCheckout({
locale: locale,
originKey: "{$originKey}",
environment: "{$environment}",
risk: {
enabled: false
}
});
var card = checkout.create('card', {
type: 'card',
hasHolderName: true,
holderNameRequired: true,
onChange: function (state, component) {
if (state.isValid && !component.state.errors.encryptedSecurityCode) {
document.getElementById('holderName').value = state.data.paymentMethod.holderName;
document.getElementById('encryptedCardNumber').value = state.data.paymentMethod.encryptedCardNumber;
document.getElementById('encryptedExpiryMonth').value = state.data.paymentMethod.encryptedExpiryMonth;
document.getElementById('encryptedExpiryYear').value = state.data.paymentMethod.encryptedExpiryYear;
if (state.data.paymentMethod.encryptedSecurityCode) {
document.getElementById('encryptedSecurityCode').value = state.data.paymentMethod.encryptedSecurityCode;
}
document.getElementById("allValidcard").value = true;
} else {
resetFields();
}
document.getElementById("allValidcard").value = true;
} else {
resetFields();
}
}
});
card.mount(cardNode);
}
});
card.mount(cardNode);
}
function resetFields() {
document.getElementById('holderName').value = "";
document.getElementById('encryptedCardNumber').value = "";
document.getElementById('encryptedExpiryMonth').value = "";
document.getElementById('encryptedExpiryYear').value = "";
document.getElementById('encryptedSecurityCode').value = "";
document.getElementById("allValidcard").value = "";
}
</script>
<div class="checkout-container" id="cardContainer">
<div class="form-div">
<input type="hidden" name="holderName" id="holderName"
value="">
<input type="hidden" name="encryptedCardNumber" id="encryptedCardNumber"
value="">
<input type="hidden" name="encryptedExpiryMonth" id="encryptedExpiryMonth"
value="">
<input type="hidden" name="encryptedExpiryYear" id="encryptedExpiryYear"
value="">
<input type="hidden" name="encryptedSecurityCode" id="encryptedSecurityCode"
value="">
<input type="hidden" class="required-entry" name="allValidcard" id="allValidcard" value="">
<input type="hidden" name="payment[screen_width]" id="screenWidth" value="">
<input type="hidden" name="payment[screen_height]" id="screenHeight" value="">
<input type="hidden" name="payment[color_depth]" id="colorDepth" value="">
<input type="hidden" name="payment[time_zone_offset]" id="timeZoneOffset" value="">
<input type="hidden" name="payment[language]" id="language" value="">
<input type="hidden" name="payment[java_enabled]" id="javaEnabled" value="">
function resetFields() {
document.getElementById('holderName').value = "";
document.getElementById('encryptedCardNumber').value = "";
document.getElementById('encryptedExpiryMonth').value = "";
document.getElementById('encryptedExpiryYear').value = "";
document.getElementById('encryptedSecurityCode').value = "";
document.getElementById("allValidcard").value = "";
}
</script>
<div class="checkout-container" id="cardContainer">
<div class="form-div">
<input type="hidden" name="holderName" id="holderName"
value="">
<input type="hidden" name="encryptedCardNumber" id="encryptedCardNumber"
value="">
<input type="hidden" name="encryptedExpiryMonth" id="encryptedExpiryMonth"
value="">
<input type="hidden" name="encryptedExpiryYear" id="encryptedExpiryYear"
value="">
<input type="hidden" name="encryptedSecurityCode" id="encryptedSecurityCode"
value="">
<input type="hidden" class="required-entry" name="allValidcard" id="allValidcard" value="">
<input type="hidden" name="payment[screen_width]" id="screenWidth" value="">
<input type="hidden" name="payment[screen_height]" id="screenHeight" value="">
<input type="hidden" name="payment[color_depth]" id="colorDepth" value="">
<input type="hidden" name="payment[time_zone_offset]" id="timeZoneOffset" value="">
<input type="hidden" name="payment[language]" id="language" value="">
<input type="hidden" name="payment[java_enabled]" id="javaEnabled" value="">
</div>
</div>
</div>
{if $prestashop16}
<button type="submit" class="button btn btn-default standard-checkout button-medium"><span>
{if $prestashop16}
<button type="submit" class="button btn btn-default standard-checkout button-medium"><span>
{l s='Pay' mod='adyen'} <i class="icon-chevron-right right"></i> </span></button>
{/if}
</form>
{/if}
</form>
</div>
</div>
</div>
{/if}

0 comments on commit 65b7e25

Please sign in to comment.