0

I want to add custom data in order payment extension attribute like below :-

"payment": {
                "account_status": null,
                "additional_information": [
                    "Credit Card",
                    "M",
                    "M",
                    "I",
                    "FXZDM9",
                    "1000",
                    "Approved",
                    "xxxx-1111",
                    "Visa"
                ],
                "amount_authorized": 31.9,
                "amount_ordered": 31.9,
                "amount_paid": 31.9,
                "base_amount_authorized": 31.9,
                "base_amount_ordered": 31.9,
                "base_amount_paid": 31.9,
                "base_amount_paid_online": 31.9,
                "base_shipping_amount": 9.95,
                "base_shipping_captured": 9.95,
                "cc_exp_month": "12",
                "cc_exp_year": "2030",
                "cc_last4": "1111",
                "cc_trans_id": "m4ybzfr7",
                "cc_type": "VI",
                "entity_id": 70659,
                "last_trans_id": "m4ybzfr7",
                "method": "braintree",
                "parent_id": 80005,
                "shipping_amount": 9.95,
                "shipping_captured": 9.95,
                "extension_attributes": {
                    "vault_payment_token": {
                        "entity_id": 34386,
                        "customer_id": 76756,
                        "public_hash": "f0ad9e74cc52a78f499d318ee9e55fb479b0a757dbecb10849fbf960a5e84c5b",
                        "payment_method_code": "braintree",
                        "type": "card",
                        "created_at": "2017-11-15 13:20:54",
                        "expires_at": "2031-01-01 00:00:00",
                        "gateway_token": "7jfdwj",
                        "token_details": "{\"type\":\"VI\",\"maskedCC\":\"1111\",\"expirationDate\":\"12\\/2030\"}",
                        "is_active": true,
                        "is_visible": false
                    }
                "custom_data": {
                        "key1" => "value1",
                         "key2" => "value2"
                  }
                }
            },

as above you can see, I want to add this

"custom_data": {
                        "key1" => "value1",
                         "key2" => "value2"
                  }

I have no idea how to do it, can anyone suggest me ?

1
  • you need to use extension attribute for that – Sunil Patel Nov 21 '17 at 8:21
2

Magento\Vault\Plugin\PaymentVaultAttributesLoad should be your friend here, this is essentially doing what you want to do. It's worth looking into this module.

You will need to do the following:

  • Create a new extension_attribute via your modules etc/extension_attributes.xml for the new payment information (useful docs here http://devdocs.magento.com/guides/v2.0/extension-dev-guide/extension_attributes/adding-attributes.html)
  • Set your 'custom data' to the order payment entity (via an observer or plugin etc). You need to make sure this order payment data is saved. This may require adding a new field to the sales_order_payment table. This step may not be required if the data is not order specific.
  • Create a plugin for Magento\Sales\Api\Data\OrderPaymentInterface with method afterGetExtensionAttributes.

This plugin should essentially check the custom data is set to the order payment object, and if so, set it to the order payment as an extension attribute. Example below taken from \Magento\Vault\Plugin\PaymentVaultAttributesLoad

/**
 * Load vault payment extension attribute to order/payment entity
 *
 * @param OrderPaymentInterface $payment
 * @param OrderPaymentExtensionInterface|null $paymentExtension
 * @return OrderPaymentExtensionInterface
 */
public function afterGetExtensionAttributes(
    OrderPaymentInterface $payment,
    OrderPaymentExtensionInterface $paymentExtension = null
) {
    if ($paymentExtension === null) {
        $paymentExtension = $this->paymentExtensionFactory->create();
    }

    $paymentToken = $paymentExtension->getVaultPaymentToken();
    if ($paymentToken === null) {
        $paymentToken = $this->paymentTokenManagement->getByPaymentId($payment->getEntityId());
        if ($paymentToken instanceof PaymentTokenInterface) {
            $paymentExtension->setVaultPaymentToken($paymentToken);
        }
        $payment->setExtensionAttributes($paymentExtension);
    }

    return $paymentExtension;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.