<p>The <code>commerce_modifyGatewayRequestData</code> hook did work for my purposes. </p>

<p>One unexpected error I countered when using this hook was a missing Braintree <code>merchantId</code>, <code>publicKey</code>, <code>privateKey</code>, and <code>environment</code>. I expected these to already be set, since they're configured and working fine in Craft Commerce Payment Method settings when I do not use this hook. This error occurred even when I returned <code>$data</code> from this hook as-is.  Not sure why this is, but setting it manually with <code>Braintree_Configuration</code> inside the function did the trick:</p>

<pre><code>use Braintree_Configuration;

public function commerce_modifyGatewayRequestData($data, $type, $transaction){

    // Get the Commerce order
    $orderId = $transaction-&gt;orderId;
    $order = craft()-&gt;commerce_orders-&gt;getOrderById($orderId);

    // Set the customer data from the Commerce order
    $customer['email'] = $order-&gt;email;
    $customer['firstName'] = $order-&gt;billingAddress-&gt;firstName;
    $customer['lastName'] = $order-&gt;billingAddress-&gt;lastName;
    $customer['phone'] = $order-&gt;billingAddress-&gt;phone;

    // Add customer to $data sent to Braintree
    $data['customer'] = $customer;

    // For some reason this needs to be set manually here, 
    // Should already be set via Craft Commerce?
    Braintree_Configuration::merchantId(craft()-&gt;config-&gt;get('braintreeMerchantId'));
    Braintree_Configuration::environment(craft()-&gt;config-&gt;get('braintreeEnvironment'));
    Braintree_Configuration::publicKey(craft()-&gt;config-&gt;get('braintreePublicKey'));
    Braintree_Configuration::privateKey(craft()-&gt;config-&gt;get('braintreePrivateKey'));

    return $data;
}
</code></pre>

CC BY-SA 4.0
2018-10-17T23:39:16.520Z
Interesting - that extra config stuff really _shouldn't_ be needed there - defintiely doesn't seem right - I'd raise that as an issue on the github to puzzle that out.
Jeremy Daalder
CC BY-SA 4.0
2018-10-17T02:33:29.813Z
2018-10-22T19:19:44.307Z
2018-10-22T19:19:44.307Z
Alex Kendrick
<p>Ok so it works pretty much how the documentation would lead you to think.  </p>

<p>This is for Commerce V1 but apart from hooking up to the event, it should be much the same for V2.</p>

<pre><code>public function commerce_modifyGatewayRequestData($data,$type,$transaction)
{

        // Dump the data to see what is there...you can do what you like to $data and then you return it at the end...
        BusinessLogicPlugin::log("commerce_modifyGatewayRequestData - UNMODIFIED DATA"); 
        BusinessLogicPlugin::log("data");
        BusinessLogicPlugin::log($data);
        BusinessLogicPlugin::log("type");
        BusinessLogicPlugin::log($type);
        BusinessLogicPlugin::log("transaction");
        BusinessLogicPlugin::log($transaction);
    }

    # Example...
    # TRUNCATE any over-sized data eWay might not like....
    # Data limits come from here: https://eway.io/api-v3/#direct-connection

    if(isset($data['Customer']['FirstName']))
        $data['Customer']['FirstName'] = substr($data['Customer']['FirstName'], 0, 49);

    ...etc

        // Dump the modified data for checking if you need to...

        BusinessLogicPlugin::log("commerce_modifyGatewayRequestData - MODIFIED DATA"); 
        BusinessLogicPlugin::log("data");

    // Now actually return the modified data...
    return $data
}
</code></pre>

CC BY-SA 4.0
2018-10-17T02:19:54.793Z
Thanks, Jeremy. I discovered that it does... mostly. I found that a number of Braintree Configuration settings need to be set manually in this function, even though they are already set in Commerce settings (merchantId, publicKey, privateKey, environment). This is true even if I return $data untouched. Not sure why that is, but I've got it working now. I may answer my own question, but def upvoting. You've been giving tons of great Commerce answers here.
Alex Kendrick
CC BY-SA 4.0
2018-10-17T01:18:46.620Z
2018-10-17T01:18:46.620Z
Jeremy Daalder
<p>I'm using Braintree with Craft Commerce using this Omnipay Braintree Gateway for Craft Commerce
<a href="https://github.com/samuelbirch/craftcommerce-braintree" rel="nofollow noreferrer">https://github.com/samuelbirch/craftcommerce-braintree</a></p>

<p>It's working well, but I noticed that some customer data is not passed through to Braintree at the time of a successful purchase. Specifically, the customer information that is mapped from the <code>customer</code> parameters in the transactions sale call. </p>

<p>Working directly with Braintree API it would be done as part of the <a href="https://developers.braintreepayments.com/reference/request/transaction/sale/php#customer" rel="nofollow noreferrer">sale</a> method that includes <code>amount</code> and <code>creditCard</code> etc on the transaction.</p>

<pre><code>Braintree_Transaction::sale([...], array('customer' =&gt; array())); 
</code></pre>

<p>I'm a bit stuck trying to find where to actually make a modification to pass this data through in the context of Omnipay and Craft Commerce. Does anyone have an idea of where to start? I realize this is a vague question... please comment if there is something specific that I can clarify to make this question more answerable. </p>

<p><em>Update:</em></p>

<p>It looks like maybe what I should be using is a Craft Commerce hook. Probably <code>commerce_modifyGatewayRequestData</code>. So I'm starting there, but if anyone has tips it would be appreciated!</p>

<plugin-craftcommerce>
Craft Commerce: where to modify data sent to gateway (need to send customer data)?
# Craft Commerce: where to modify data sent to gateway (need to send customer data)?

(Asked by: username_0 on 2018-10-16)

I'm using Braintree with Craft Commerce using this Omnipay Braintree Gateway for Craft Commerce
https://github.com/samuelbirch/craftcommerce-braintree
It's working well, but I noticed that some customer data is not passed through to Braintree at the time of a successful purchase. Specifically, the customer information that is mapped from the customer parameters in the transactions sale call. 
Working directly with Braintree API it would be done as part of the sale method that includes amount and creditCard etc on the transaction.
Braintree_Transaction::sale([...], array('customer' => array())); 

I'm a bit stuck trying to find where to actually make a modification to pass this data through in the context of Omnipay and Craft Commerce. Does anyone have an idea of where to start? I realize this is a vague question... please comment if there is something specific that I can clarify to make this question more answerable. 
Update:
It looks like maybe what I should be using is a Craft Commerce hook. Probably commerce_modifyGatewayRequestData. So I'm starting there, but if anyone has tips it would be appreciated!

**Tags:** plugin-craftcommerce


---

## 2 answers

### Answer by username_0 on 2018-10-17. Score: 1 | Accepted answer

The commerce_modifyGatewayRequestData hook did work for my purposes. 
One unexpected error I countered when using this hook was a missing Braintree merchantId, publicKey, privateKey, and environment. I expected these to already be set, since they're configured and working fine in Craft Commerce Payment Method settings when I do not use this hook. This error occurred even when I returned $data from this hook as-is.  Not sure why this is, but setting it manually with Braintree_Configuration inside the function did the trick:
use Braintree_Configuration;

public function commerce_modifyGatewayRequestData($data, $type, $transaction){

    // Get the Commerce order
    $orderId = $transaction->orderId;
    $order = craft()->commerce_orders->getOrderById($orderId);

    // Set the customer data from the Commerce order
    $customer['email'] = $order->email;
    $customer['firstName'] = $order->billingAddress->firstName;
    $customer['lastName'] = $order->billingAddress->lastName;
    $customer['phone'] = $order->billingAddress->phone;

    // Add customer to $data sent to Braintree
    $data['customer'] = $customer;

    // For some reason this needs to be set manually here, 
    // Should already be set via Craft Commerce?
    Braintree_Configuration::merchantId(craft()->config->get('braintreeMerchantId'));
    Braintree_Configuration::environment(craft()->config->get('braintreeEnvironment'));
    Braintree_Configuration::publicKey(craft()->config->get('braintreePublicKey'));
    Braintree_Configuration::privateKey(craft()->config->get('braintreePrivateKey'));

    return $data;
}

> **Comments:**
> **username_1**: Interesting - that extra config stuff really _shouldn't_ be needed there - defintiely doesn't seem right - I'd raise that as an issue on the github to puzzle that out.  

---

### Answer by username_1 on 2018-10-17. Score: 2

Ok so it works pretty much how the documentation would lead you to think.  
This is for Commerce V1 but apart from hooking up to the event, it should be much the same for V2.
public function commerce_modifyGatewayRequestData($data,$type,$transaction)
{

        // Dump the data to see what is there...you can do what you like to $data and then you return it at the end...
        BusinessLogicPlugin::log("commerce_modifyGatewayRequestData - UNMODIFIED DATA"); 
        BusinessLogicPlugin::log("data");
        BusinessLogicPlugin::log($data);
        BusinessLogicPlugin::log("type");
        BusinessLogicPlugin::log($type);
        BusinessLogicPlugin::log("transaction");
        BusinessLogicPlugin::log($transaction);
    }

    # Example...
    # TRUNCATE any over-sized data eWay might not like....
    # Data limits come from here: https://eway.io/api-v3/#direct-connection

    if(isset($data['Customer']['FirstName']))
        $data['Customer']['FirstName'] = substr($data['Customer']['FirstName'], 0, 49);

    ...etc

        // Dump the modified data for checking if you need to...

        BusinessLogicPlugin::log("commerce_modifyGatewayRequestData - MODIFIED DATA"); 
        BusinessLogicPlugin::log("data");

    // Now actually return the modified data...
    return $data
}

> **Comments:**
> **username_0**: Thanks, Jeremy. I discovered that it does... mostly. I found that a number of Braintree Configuration settings need to be set manually in this function, even though they are already set in Commerce settings (merchantId, publicKey, privateKey, environment). This is true even if I return $data untouched. Not sure why that is, but I've got it working now. I may answer my own question, but def upvoting. You've been giving tons of great Commerce answers here.  

---

