phpwordpresswoocommercehook-woocommerceopayo

Woocommerce & Opayo: Add a custom field to the data sent to the API


Very specific issue but, I've been dragged onto an issue with our companies Payment Gateway on our Wordpress / Woocommerce website where we are using the Opayo Plugin (For Opayo Direct).

The issue is:

The guys originally doing the website then tried contacting the original developer of this now unsupported plugin to which they was sent some code to put into another plugin named PHP Injection the code was similar to below:

add_filter( 'opayo_direct_custom_field_vendordata', 'my_opayo_direct_custom_field_vendordata', 10, 2 );

function my_opayo_direct_custom_field_vendordata ( $vendordata, $order ) {

    // Get Order ID
    $order_id = $order->get_order_number();
    $reference = "test_" . $order_id;

    // Get the reference field - set '_reference_field' to the meta_key from your order
    if( isset( get_post_meta( $order_id, '_reference_field', TRUE ) ) ) {
        $vendordata = get_post_meta( $order_id , '_reference_field', TRUE );
        //$vendordata['_reference_field'] = $reference; ### Commented as I'm unsure if this is correct
    }
    
    return $vendordata;
}

After doing numerous testing and small changes, still nothing seems to be showing up in the Reference field in Opayo itself?

Please tell me someone has encountered this situation before or knows what I might be missing, it's been a while since I've touched PHP


Solution

  • Alright, so, I contacted Opayo myself to see what the Mapping was for the Reference column on the Opayo website and what data that is sent to the API is mapped to that Reference column in the table.

    Apparently the value sent to the API that corresponds to this is the VendorData (Not required, 200 Char limit, free-text)

    So, to fix this, I had to:

    1. Open the Plugin Editor on Wordpress
    2. Select the woocommerce-gateway-sagepay-form Plugin
    3. Navigate and select the woocommerce-gateway-sagepay-form/classes/direct/sagepay-direct-request-class.php file

    I then scrolled to Line 335 inside of the $end array, I then added the field of VendorData as seen by the code below:

                $end = array(
                    "CustomerEMail"     =>  $order->get_billing_email(),
                    "ClientIPAddress"   =>  $this->get_ipaddress(),
                    "AccountType"       =>  $this->accounttype,
                    "ReferrerID"        =>  $this->referrerid,
                    "Website"           =>  site_url(),
                    "VendorData"        =>  '#######',
                    "Crypt"             =>  MD5( $this->open_salt . $order->get_order_key() . $this->close_salt ),
                );
    

    And then set the ####### to the Value I needed to send to Opayo to show in the Reference column in the payments table.

    Then just to be sure it wasn't going to get pruned (Again, not touched PHP for a while, I went to (NOW) Line 378 and commented it out, see below:

    // Customiseable fields
                $end['TransType'] = apply_filters( 'opayo_direct_custom_field_transtype', '01', $order );
                //$end['VendorData'] = apply_filters( 'opayo_direct_custom_field_vendordata', '', $order );
    

    Saving this, then when an Order / Purchase was made, the Reference field on Opayo's website was populated with what I set the VendorData to.

    Quick Note: The VendorData field is max of 200 characters and only Aa or 0-9, I had a couple of failed attempts when I was trying to have _ until I searched the error I received on this page:

    https://www.opayo.co.uk/support/error-codes?keyword=3189


    I hope that my issue and resolution helps someone in the future and sorry for anyone's time I wasted!