Skip to content

Commit

Permalink
New release version 2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Elavarasan Nataranjan committed Aug 22, 2019
1 parent 4801288 commit 0e57bec
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 38 deletions.
40 changes: 40 additions & 0 deletions Controller/Savedcard/EditPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function execute()
$this->_getTokenModel(),
$this->customerSession->getCustomer(),
$this->getStoreId());
$tokenInquiryResponse = $this->_tokenService->getTokenInquiry(
$this->_getTokenModel(),
$this->customerSession->getCustomer(),
$this->getStoreId());
} catch (Exception $e) {
$this->wplogger->error($e->getMessage());
$this->messageManager->addException($e, __('Error: ').$e->getMessage());
Expand All @@ -106,6 +110,14 @@ public function execute()
$this->_redirect('*/savedcard/edit', array('id' => $this->_getTokenModel()->getId()));
return;
}
if ($tokenInquiryResponse->getTokenCode()) {
$this->_applyTokenInquiry($tokenInquiryResponse);
$this->_applyVaultTokenUpdate();
} else {
$this->messageManager->addError(__('Error: the card has not been updated.'));
$this->_redirect('*/savedcard/edit', array('id' => $this->_getTokenModel()->getId()));
return;
}
$this->messageManager->addSuccess(__('The card has been updated.'));
$this->_redirect('*/savedcard');
return;
Expand Down Expand Up @@ -182,4 +194,32 @@ private function convertDetailsToJSON($details)
$json = \Zend_Json::encode($details);
return $json ? $json : '{}';
}

/**
* Update Saved Card Detail
*/
protected function _applyTokenInquiry($tokenInquiryResponse)
{
$this->_worldpayToken->updateTokenByCustomer(
$this->_getTokenModelInquiry($tokenInquiryResponse),
$this->customerSession->getCustomer()
);
}

/**
* @return Sapient/WorldPay/Model/Token
*/
protected function _getTokenModelInquiry($tokenInquiryResponse)
{
if (! $tokenId = $this->getRequest()->getParam('token_id')) {
$tokenData = $this->getRequest()->getParam('token');
$tokenId = $tokenData['id'];
}
$token = $this->savecard->create()->loadByTokenCode($tokenId);
$tokenUpdateData = $this->getRequest()->getParam('token');
if (! empty($tokenUpdateData) && ! empty($tokenInquiryResponse->isSuccess())) {
$token->setBinNumber(trim($tokenInquiryResponse->isSuccess()));
}
return $token;
}
}
6 changes: 5 additions & 1 deletion Controller/ThreeDSecure/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Context $context,
*/
public function execute()
{
$threeDSecureChallengeParams = $this->checkoutSession->get3DS2Params();
$threeDSecureChallengeParams = $this->checkoutSession->get3Ds2Params();
$threeDSecureChallengeConfig = $this->checkoutSession->get3DS2Config();
$orderId = $this->checkoutSession->getAuthOrderId();
if ($redirectData = $this->checkoutSession->get3DSecureParams()) {
Expand Down Expand Up @@ -125,6 +125,10 @@ function base64url(source) {
}
</script>');

$this->checkoutSession->uns3DS2Params();
//$this->checkoutSession->uns3DS2Config();

} else {
return $this->resultRedirectFactory->create()->setPath('checkout/onepage/success', ['_current' => true]);
}
Expand Down
4 changes: 2 additions & 2 deletions Model/Authorisation/DirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function authorizePayment(
$payment->setIsTransactionPending(1);
$this->_handle3DSecure($threeDSecureParams, $directOrderParams, $orderCode);
} else if ($threeDSecureChallengeParams) {
// Handles success response with 3DS & redirect for varification.
// Handles success response with 3DS2 & redirect for varification.
$this->checkoutSession->setauthenticatedOrderId($mageOrder->getIncrementId());
$payment->setIsTransactionPending(1);
$threeDSecureConfig = $this->get3DS2ConfigValues();
Expand All @@ -84,7 +84,7 @@ private function _handle3DSecure($threeDSecureParams, $directOrderParams, $mageO
private function _handle3Ds2($threeDSecureChallengeParams, $directOrderParams, $mageOrderId, $threeDSecureConfig)
{
$this->registryhelper->setworldpayRedirectUrl($threeDSecureChallengeParams);
$this->checkoutSession->set3DS2Params($threeDSecureChallengeParams);
$this->checkoutSession->set3Ds2Params($threeDSecureChallengeParams);
$this->checkoutSession->setDirectOrderParams($directOrderParams);
$this->checkoutSession->setAuthOrderId($mageOrderId);
$this->checkoutSession->set3DS2Config($threeDSecureConfig);
Expand Down
8 changes: 6 additions & 2 deletions Model/Authorisation/ThreeDSecureChallenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ public function continuePost3dSecure2AuthorizationProcess($directOrderParams, $t
$this->_paymentUpdate->apply($this->_order->getPayment(), $this->_order);
$this->_abortIfPaymentError($this->_paymentUpdate);
} catch (Exception $e) {
$this->wplogger->info($e->getMessage());
$this->_messageManager->addError(__($e->getMessage()));
$this->wplogger->info($e->getMessage());
if($e->getMessage() === 'Asymmetric transaction rollback.'){
$this->_messageManager->addError(__('Duplicate Entry, This card number is already saved.'));
} else {
$this->_messageManager->addError(__($e->getMessage()));
}
$this->checkoutSession->setWpResponseForwardUrl(
$this->urlBuilders->getUrl(self::CART_URL, ['_secure' => true])
);
Expand Down
26 changes: 23 additions & 3 deletions Model/Authorisation/TokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ public function authorizePayment(
$threeDSecureParams = $directResponse->get3dSecureParams();
$threeDsEnabled = $this->worldpayHelper->is3DSecureEnabled();
$threeDSecureChallengeParams = $directResponse->get3ds2Params();
$threeDSecureConfig = array();
if ($threeDSecureParams) {
// Handles success response with 3DS & redirect for varification.
$this->checkoutSession->setauthenticatedOrderId($mageOrder->getIncrementId());
$payment->setIsTransactionPending(1);
$this->_handle3DSecure($threeDSecureParams, $tokenOrderParams, $orderCode);
} else if ($threeDSecureChallengeParams) {
// Handles success response with 3DS & redirect for varification.
// Handles success response with 3DS2 & redirect for varification.
$this->checkoutSession->setauthenticatedOrderId($mageOrder->getIncrementId());
$payment->setIsTransactionPending(1);
$this->_handle3Ds2($threeDSecureChallengeParams, $tokenOrderParams, $orderCode);
$threeDSecureConfig = $this->get3DS2ConfigValues();
$this->_handle3Ds2($threeDSecureChallengeParams, $tokenOrderParams, $orderCode, $threeDSecureConfig);
} else{

// Normal order goes here.(without 3DS).
Expand All @@ -80,13 +82,14 @@ private function _handle3DSecure($threeDSecureParams, $directOrderParams, $mageO
$this->checkoutSession->setAuthOrderId($mageOrderId);
}

private function _handle3Ds2($threeDSecureChallengeParams, $directOrderParams, $mageOrderId)
private function _handle3Ds2($threeDSecureChallengeParams, $directOrderParams, $mageOrderId, $threeDSecureConfig)
{
$this->wplogger->info('HANDLING 3DS2');
$this->registryhelper->setworldpayRedirectUrl($threeDSecureChallengeParams);
$this->checkoutSession->set3Ds2Params($threeDSecureChallengeParams);
$this->checkoutSession->setDirectOrderParams($directOrderParams);
$this->checkoutSession->setAuthOrderId($mageOrderId);
$this->checkoutSession->set3DS2Config($threeDSecureConfig);
}

private function _applyPaymentUpdate(
Expand All @@ -108,4 +111,21 @@ private function _abortIfPaymentError($paymentUpdate)
throw new Exception(sprintf('Payment CANCELLED'));
}
}

// get 3ds2 params from the configuration and set to checkout session
public function get3DS2ConfigValues(){
$data = array();
$data['jwtIssuer'] = $this->worldpayHelper->isJwtIssuer();

$data['organisationalUnitId'] = $this->worldpayHelper->isOrganisationalUnitId();

$mode = $this->worldpayHelper->getEnvironmentMode();
if($mode == 'Test Mode'){
$data['challengeurl'] = $this->worldpayHelper->isTestChallengeUrl();
} else {
$data['challengeurl'] = $this->worldpayHelper->isProductionChallengeUrl();
}

return $data;
}
}
27 changes: 22 additions & 5 deletions Model/Mapping/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ class Service {

protected $_logger;
protected $savedTokenFactory;
protected $_scopeConfig;

public function __construct(
\Sapient\Worldpay\Logger\WorldpayLogger $wplogger,
\Sapient\Worldpay\Helper\Data $worldpayHelper,
SavedTokenFactory $savedTokenFactory,
\Sapient\Worldpay\Model\SavedToken $savedtoken,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Customer\Model\Session $customerSession
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->wplogger = $wplogger;
$this->savedTokenFactory = $savedTokenFactory;
$this->worldpayHelper = $worldpayHelper;
$this->customerSession = $customerSession;
$this->savedtoken = $savedtoken;
$this->_urlBuilder = $urlBuilder;
$this->_scopeConfig = $scopeConfig;
}

public function collectVaultOrderParameters(
Expand Down Expand Up @@ -48,7 +51,7 @@ public function collectVaultOrderParameters(
'billingAddress' => $this->_getBillingAddress($quote),
'method' => $paymentDetails['method'],
'orderStoreId' => $orderStoreId,
'shopperId' => $quote->getCustomerId()
'shopperId' => $quote->getCustomerId()
);
}

Expand Down Expand Up @@ -77,9 +80,22 @@ public function collectDirectOrderParameters(
'billingAddress' => $this->_getBillingAddress($quote),
'method' => $paymentDetails['method'],
'orderStoreId' => $orderStoreId,
'shopperId' => $quote->getCustomerId()
'shopperId' => $quote->getCustomerId(),
'cusDetails' => $this->getCustomerDetailkfor3DS2()
);
}

public function getCustomerDetailkfor3DS2 () {
$cusDetails = array();
$cusDetails['created_at'] = $this->customerSession->getCustomer()->getCreatedAt();
$cusDetails['updated_at'] = $this->customerSession->getCustomer()->getUpdatedAt();

//check risk data is enabled

$cusDetails['is_risk_data_enabled'] = $this->_scopeConfig->getValue('worldpay/general_config/risk_data', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

return $cusDetails;
}

public function collectRedirectOrderParameters(
$orderCode,
Expand Down Expand Up @@ -169,7 +185,8 @@ public function collectTokenOrderParameters(
'billingAddress' => $this->_getBillingAddress($quote),
'method' => $paymentDetails['method'],
'orderStoreId' => $orderStoreId,
'shopperId' => $quote->getCustomerId()
'shopperId' => $quote->getCustomerId(),
'cusDetails' => $this->getCustomerDetailkfor3DS2()
);
}

Expand Down Expand Up @@ -377,7 +394,7 @@ private function _getPaymentDetailsUsingToken($paymentDetails,$quote)
$details['dynamicInteractionType'] = $this->worldpayHelper->getDynamicIntegrationType($paymentDetails['method']);
// 3DS2 value
if (isset($paymentDetails['additional_data']['dfReferenceId'])) {
$details['dfReferenceId'] = isset($paymentDetails['additional_data']['dfReferenceId']) ? $paymentDetails['additional_data']['dfReferenceId'] : '' ;
$details['dfReferenceId'] = $paymentDetails['additional_data']['dfReferenceId'];
}
return $details;
}
Expand Down
4 changes: 4 additions & 0 deletions Model/Payment/UpdateWorldpayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function saveTokenData($tokenElement, $payment, $merchantCode)
->getFirstItem()->getData();

if (empty($tokenDataExist)) {
$binNumber = $tokenElement[0]->paymentInstrument[0]->cardDetails[0]->derived[0]->bin[0];
$savedTokenFactory->setTokenCode($tokenElement[0]->tokenDetails[0]->paymentTokenID[0]);
$dateNode = $tokenElement[0]->tokenDetails->paymentTokenExpiry->date;
$tokenexpirydate = (int)$dateNode['year'].'-'.(int)$dateNode['month'].'-'.(int)$dateNode['dayOfMonth'];
Expand All @@ -144,6 +145,9 @@ public function saveTokenData($tokenElement, $payment, $merchantCode)
$savedTokenFactory->setMerchantCode($merchantCode[0]);
$savedTokenFactory->setCustomerId($tokenElement[0]->authenticatedShopperID[0]);
$savedTokenFactory->setAuthenticatedShopperID($tokenElement[0]->authenticatedShopperID[0]);
if($binNumber){
$savedTokenFactory->setBinNumber($tokenElement[0]->paymentInstrument[0]->cardDetails[0]->derived[0]->bin[0]);
}
$savedTokenFactory->save();
} else {
$this->_messageManager->addNotice(__("You already appear to have this card number stored, if your card details have changed, you can update these via the 'my cards' section"));
Expand Down
36 changes: 34 additions & 2 deletions Model/Request/PaymentServiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public function order($directOrderParams)
$directOrderParams['userAgentHeader'],
$directOrderParams['shippingAddress'],
$directOrderParams['billingAddress'],
$directOrderParams['shopperId']
$directOrderParams['shopperId'],
$directOrderParams['cusDetails']
);
return $this->_sendRequest(
dom_import_simplexml($orderSimpleXml)->ownerDocument,
Expand Down Expand Up @@ -128,7 +129,8 @@ public function orderToken($tokenOrderParams)
$tokenOrderParams['userAgentHeader'],
$tokenOrderParams['shippingAddress'],
$tokenOrderParams['billingAddress'],
$tokenOrderParams['shopperId']
$tokenOrderParams['shopperId'],
$tokenOrderParams['cusDetails']
);
return $this->_sendRequest(
dom_import_simplexml($orderSimpleXml)->ownerDocument,
Expand Down Expand Up @@ -555,4 +557,34 @@ public function order3Ds2Secure($directOrderParams)
$this->worldpayhelper->getXmlPassword($paymentType)
);
}

/**
* Send token inquiry XML to Worldpay server
*
* @param SavedToken $tokenModel
* @param \Magento\Customer\Model\Customer $customer
* @param int $storeId
* @return mixed
*/
public function tokenInquiry(
SavedToken $tokenModel,
\Magento\Customer\Model\Customer $customer,
$storeId
) {
$this->_wplogger->info('########## Submitting token inquiry. TokenId: ' . $tokenModel->getId() . ' ##########');
$requestParameters = array(
'tokenModel' => $tokenModel,
'customer' => $customer,
'merchantCode' => $this->worldpayhelper->getMerchantCode($tokenModel->getMethod()),
);
/** @var SimpleXMLElement $simpleXml */
$this->tokenInquiryXml = new \Sapient\Worldpay\Model\XmlBuilder\TokenInquiry($requestParameters);
$tokenInquirySimpleXml = $this->tokenInquiryXml->build();

return $this->_sendRequest(
dom_import_simplexml($tokenInquirySimpleXml)->ownerDocument,
$this->worldpayhelper->getXmlUsername($tokenModel->getMethod()),
$this->worldpayhelper->getXmlPassword($tokenModel->getMethod())
);
}
}
41 changes: 41 additions & 0 deletions Model/Token/InquiryXml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @copyright 2017 Sapient
*/
namespace Sapient\Worldpay\Model\Token;

use Sapient\Worldpay\Model\SavedToken;
/**
* read from WP's token update response
*/
class InquiryXml implements UpdateInterface
{
/**
* @var SimpleXMLElement
*/
private $_xml;

/**
* @param SimpleXMLElement $xml
*/
public function __construct(\SimpleXMLElement $xml)
{
$this->_xml = $xml;
}

/**
* @return string
*/
public function getTokenCode()
{
return (string)$this->_xml->reply->token->tokenDetails->paymentTokenID;
}

/**
* @return bool
*/
public function isSuccess()
{
return (string)$this->_xml->reply->token->paymentInstrument->cardDetails->derived->bin;
}
}
18 changes: 18 additions & 0 deletions Model/Token/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,22 @@ public function getTokenDelete(
$xml = simplexml_load_string($rawXml);
return new DeleteXml($xml);
}

/**
* Send token inquiry request to WP server and gives back the answer
*
* @param Sapient\Worldpay\Model\Token $tokenModel
* @param \Magento\Customer\Model\Customer $customer
* @param $storeId
* @return Sapient\Worldpay\Model\Token\InquiryXml
*/
public function getTokenInquiry(
SavedToken $tokenModel,
\Magento\Customer\Model\Customer $customer,
$storeId
) {
$rawXml = $this->_paymentServiceRequest->tokenInquiry($tokenModel, $customer, $storeId);
$xml = simplexml_load_string($rawXml);
return new InquiryXml($xml);
}
}
5 changes: 5 additions & 0 deletions Model/Token/StateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ public function getTokenReason();
* @return string
*/
public function getTokenEvent();

/**
* @return string
*/
public function getBin();
}
Loading

0 comments on commit 0e57bec

Please sign in to comment.