How to Send Custom Field Data in additional data of custom payment method magento 2?

  • author-img Nidhi Arora
  • 7 years
How to Send Custom Field Data in additional data of custom payment method magento 2

Sometime you may need to send custom field data from custom payment method of magento2. i.e. you may need to add an extra field on checkout payment page. Mainly there is three required field in custom payment module like credit card number, cvv, expiration data. If you want to send an extra field data to Model file of your module you can override magento getData method as follows.

1. Look up for a file called cc-form.js in your magento installation under vendor\magento\module-payment\view\frontend\web\js\view\payment.

2. You will see getData function which we will override in our module payment_module_code-method.js as below.

getData: function () {

var data = {

'method': this.getCode(),

'additional_data': {

'cc_cid': this.creditCardVerificationNumber(),

'cc_ss_start_month': this.creditCardSsStartMonth(),

'cc_ss_start_year': this.creditCardSsStartYear(),

'cc_ss_issue': this.creditCardSsIssue(),

'cc_type': this.creditCardType(),

'cc_exp_year': this.creditCardExpYear(),

'cc_exp_month': this.creditCardExpMonth(),

'cc_number': this.creditCardNumber(),

'cc_token': $('#' + this.getCode() + '_cc_token').val(),

}

return data;

}

You can see I am sending cc_token an custom field value with additional data to Model file of magento2 custom module. And you can fectch this value in model file as using below code.

public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)

{

$info = $this->getInfoInstance();

$paymentInfo = $info->getAdditionalInformation()['data']['additional_data'];

$paymentInfo['cc_token'];

}

If you are having any doubts, feel free to contact us at sales@envisionecommerce.com . However, if you do have any additional tips or method on this “How to”, you can suggest them in the comment section below.

Stay tuned for more “How to” blog on Magento 2. Till then Happy Magento 2 Coding!

Download Blog

ENQUIRY

Ready to Get Started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    815 Brazos St STE 500, Austin,
    TX 78701, USA