Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .coderabbit.yaml
Empty file.
6 changes: 5 additions & 1 deletion Controller/Cards/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public function execute()
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);

if (!$this->formKeyValidator->validate($this->getRequest())
if (
!$this->formKeyValidator->validate($this->getRequest())
|| !$this->getRequest()->isPost()
|| !$this->platformVaultConfig->isActive()
) {
Expand Down Expand Up @@ -147,6 +148,9 @@ private function prepareVerifyCommandSubject(array $profileData, DataObject $tra
'customer_id' => $this->customerSession->getCustomerId(),
'creditcard_month' => $profileData[PaymentProfileInterface::CREDITCARD_MONTH],
'creditcard_year' => $profileData[PaymentProfileInterface::CREDITCARD_YEAR],
'creditcard_type' => $profileData[PaymentProfileInterface::CREDITCARD_TYPE],
'creditcard_first_digits' => $profileData[PaymentProfileInterface::CREDITCARD_FIRST_DIGITS],
'creditcard_last_digits' => $profileData[PaymentProfileInterface::CREDITCARD_LAST_DIGITS],
'billing_address' => $profileData[PaymentProfileInterface::BILLING_ADDRESS],
'customer_email' => $this->customerSession->getCustomer()->getEmail(),
'browser_info' => ($profileData['browser_info'] ?? ''),
Expand Down
40 changes: 39 additions & 1 deletion Gateway/Request/WalletPaymentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public function build(array $buildSubject)
return [
PaymentProfileInterface::CREDITCARD_MONTH => $this->getCreditCardMonth($buildSubject),
PaymentProfileInterface::CREDITCARD_YEAR => $this->getCreditCardYear($buildSubject),
PaymentProfileInterface::CREDITCARD_TYPE => $this->getCreditCardType($buildSubject),
PaymentProfileInterface::CREDITCARD_LAST_DIGITS => $this->getCreditCardLastDigits($buildSubject),
PaymentProfileInterface::CREDITCARD_FIRST_DIGITS => $this->getCreditCardFirstDigits($buildSubject),
PaymentProfileInterface::BILLING_ADDRESS => $this->getBillingAddress($buildSubject),

PaymentDataBuilder::PAYMENT_METHOD_TOKEN => $this->getPaymentToken($buildSubject),
VaultConfigProvider::IS_ACTIVE_CODE => 1,
];
Expand Down Expand Up @@ -72,4 +74,40 @@ private function getPaymentToken(array $buildSubject)
}
return $buildSubject['token'];
}

/**
* @param array $buildSubject
* @return array
*/
private function getCreditCardType(array $buildSubject)
{
if (empty($buildSubject['creditcard_type'])) {
throw new \InvalidArgumentException('Credit card type is not passed.');
}
return $buildSubject['creditcard_type'];
}

/**
* @param array $buildSubject
* @return array
*/
private function getCreditCardLastDigits(array $buildSubject)
{
if (empty($buildSubject['creditcard_last_digits'])) {
throw new \InvalidArgumentException('Credit card last digits are not passed.');
}
return $buildSubject['creditcard_last_digits'];
}

/**
* @param array $buildSubject
* @return array
*/
private function getCreditCardFirstDigits(array $buildSubject)
{
if (empty($buildSubject['creditcard_first_digits'])) {
throw new \InvalidArgumentException('Credit card first digits are not passed.');
}
return $buildSubject['creditcard_first_digits'];
}
}
25 changes: 22 additions & 3 deletions view/frontend/web/js/view/vault/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ define(
defaults: {
formSelector: "#vault-edit",
formSubmitSelector: "#vault-edit .save",
isLoading: false
isLoading: false,
creditCardLastDigits: null,
creditCardFirstDigits: null,
paymentMethodToken: null,
selectedCardType: null,
},

initObservable: function () {
this._super()
.observe([
'isLoading'
'isLoading',
'creditCardLastDigits',
'creditCardFirstDigits',
'paymentMethodToken',
'selectedCardType',
]);

var self = this;
Expand All @@ -43,6 +51,12 @@ define(
},

getPaymentData: function () {
if (this.creditCardFirstDigits() && !/^\d{6}$/.test(this.creditCardFirstDigits())) {
throw new Error('Invalid credit card first digits');
}
if (this.creditCardLastDigits() && !/^\d{4}$/.test(this.creditCardLastDigits())) {
throw new Error('Invalid credit card last digits');
}
return {
'first_name': $("#first_name").val(),
'last_name': $("#last_name").val(),
Expand All @@ -56,12 +70,17 @@ define(
'zip': $("#postcode").val(),
'country': $("#country").val(),
'year': this.creditCardExpYear(),
'month': this.creditCardExpMonth()
'month': this.creditCardExpMonth(),
'creditcard_first_digits': this.creditCardFirstDigits(),
'creditcard_last_digits': this.creditCardLastDigits(),
'creditcard_type': this.selectedCardType(),
Comment on lines +73 to +76
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for credit card data.

Consider adding validation to ensure the credit card data follows expected patterns:

  • First 6 digits should be exactly 6 digits
  • Last 4 digits should be exactly 4 digits
  • Card type should be from a valid set of card types

Add validation before returning the data:

 getPaymentData: function () {
+    if (this.creditCardFirstDigits() && !/^\d{6}$/.test(this.creditCardFirstDigits())) {
+        throw new Error('Invalid credit card first digits');
+    }
+    if (this.creditCardLastDigits() && !/^\d{4}$/.test(this.creditCardLastDigits())) {
+        throw new Error('Invalid credit card last digits');
+    }
     return {
         'first_name': $("#first_name").val(),
         // ... other fields ...
         'creditcard_first_digits': this.creditCardFirstDigits(),
         'creditcard_last_digits': this.creditCardLastDigits(),
         'creditcard_type': this.selectedCardType(),
     };
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'month': this.creditCardExpMonth(),
'creditcard_first_digits': this.creditCardFirstDigits(),
'creditcard_last_digits': this.creditCardLastDigits(),
'creditcard_type': this.selectedCardType(),
getPaymentData: function () {
if (this.creditCardFirstDigits() && !/^\d{6}$/.test(this.creditCardFirstDigits())) {
throw new Error('Invalid credit card first digits');
}
if (this.creditCardLastDigits() && !/^\d{4}$/.test(this.creditCardLastDigits())) {
throw new Error('Invalid credit card last digits');
}
return {
'month': this.creditCardExpMonth(),
'creditcard_first_digits': this.creditCardFirstDigits(),
'creditcard_last_digits': this.creditCardLastDigits(),
'creditcard_type': this.selectedCardType(),
};
}

};
},

submitPayment: function () {
var cartData = $(this.formSelector).serializeJSON();
cartData.creditcard_last_digits = this.creditCardLastDigits();
cartData.creditcard_first_digits = this.creditCardFirstDigits();

if (config.isThreeDSActive()) {
cartData.browser_info = this.getThreeDSBrowserInfo();
Expand Down