pronamic / wp-pronamic-pay-adyen

Adyen driver for the WordPress payment processing library.
https://www.wp-pay.org/gateways/adyen/
6 stars 2 forks source link

Add feature to customize the Merchant Order Reference #22

Closed remcotolsma closed 1 year ago

remcotolsma commented 1 year ago

Request from customer:

Are we able to create a custom tag to be used as Merchant Order Reference in Adyen (like a dynamic invoice number)?

Internal Help Scout conversation: https://secure.helpscout.net/conversation/2282196531/25812?folderId=1425710

We'd like to add a custom reference (reference to a contract) to the Merchant Reference field, which currently consists of the Form ID and the entry ID. Or we could use the Merchant Order Reference field (containing the entry ID right now) but that isn't the preferred field from the customers' perspective.

https://github.com/pronamic/wp-pronamic-pay-adyen/blob/701b8345e9773370ecc245f5e0cbaf9ae40b003f/src/PaymentRequestHelper.php#L30-L36

remcotolsma commented 1 year ago

Added a filter in https://github.com/pronamic/wp-pronamic-pay-adyen/commit/783900b51c6a969fcb004bb0870d58492af8c23f, could be used like this:

<?php

add_filter(
    'pronamic_pay_adyen_merchant_order_reference',
    function( $merchant_order_reference, $payment ) {
        // Check if payment source is Gravity Forms.
        if ( 'gravityformsideal' !== $payment->get_source() ) {
            return $merchant_order_reference;
        }

        // Check if Gravity Forms API is available.
        if ( ! method_exists( 'GFAPI', 'get_entry' ) ) {
            return $merchant_order_reference;
        }

        // Check if Gravity Forms entry exists.
        $entry = GFAPI::get_entry( $payment->get_source_id() );

        if ( false === $entry ) {
            return $merchant_order_reference;
        }

        // Format merchant order reference.
        $merchant_order_reference = strtr(
            $merchant_order_reference,
            [
                '{gf_form_id}'  => $entry['form_id'],
                '{gf_entry_id}' => $entry['id'],
            ]
        );

        return $merchant_order_reference;
    },
    10,
    2
);

https://github.com/pronamic/wp-pronamic-pay-adyen/blob/783900b51c6a969fcb004bb0870d58492af8c23f/docs/hooks.md#pronamic_pay_adyen_merchant_order_reference