How to Send Custom Field Data in Additional Data of Magento 2 Custom Payment Method?

  • Posted by Envision Ecommerce
  • /
  • December 14, 2016
How to Send Custom Field Data in Additional Data of Magento 2 Custom Payment Method?

Sometimes you may need to send custom field data via your custom payment method of Magento 2 store, i.e. you may need to add an extra field on your checkout payment page. As you know there are only three mandatory fields in custom payment module such as credit card number, CVV and expiration data. Here if you want to send an extra field data to Model file of your module, you should override the Magento getData method as explained below:

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. Here you will be able to find out the getData function that you need to override in your 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;
}

In above coding, you can notice that we are sending cc_token as a custom field value with additional data to Model file of Magento 2 custom module. Furthermore, you can fetch this value in the model file by using the code mentioned below:

public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
$info = $this->getInfoInstance();
$paymentInfo = $info->getAdditionalInformation()['data']['additional_data'];
$paymentInfo['cc_token'];
}

With this How to blog on Magento 2, you will be able to send custom field data in the additional data of Magento 2 custom payment method. Hope these coding steps point you in the right direction! Let our certified Magento developers know if you have any query.

Happy Magento 2 Coding!