gravityplus / gravity-forms-braintree

Braintree Gravity Forms Add-On
https://wordpress.org/plugins/gravity-forms-braintree/
3 stars 13 forks source link

add gform_braintree_payload filter #32

Open macbookandrew opened 7 years ago

macbookandrew commented 7 years ago

Allows users to specify more information to add to the payload, including the email address field, which is required if payment receipts are enabled.

If payment receipts are enabled but the email address is not passed to Braintree, the customer sees the error “Your card could not be billed. Please ensure the details you entered are correct and try again.”

Here’s a sample filter that adds email and first/last names:

add_filter( 'gform_braintree_payload', 'my_braintree_add_email', 10, 3 );
function my_braintree_add_email( $args, $entry, $form ) {
    foreach ( $form['fields'] as $field ) {
        if ( 'Email' == $field->label ) {
            // field named "Email"
            $args['customer']['email'] = $entry[$field->id];
        } elseif ( 'name' == $field->type ) {
            // field using the "name" type
            foreach ( $field['inputs'] as $input ) {
                if ( 'First' == $input['label'] ) {
                    $args['customer']['firstName'] = $entry[$input['id']];
                    $args['billing']['firstName'] = $entry[$input['id']];
                } elseif ( 'Last' == $input['label'] ) {
                    $args['customer']['lastName'] = $entry[$input['id']];
                    $args['billing']['lastName'] = $entry[$input['id']];
                }
            }
        }
    }

    return $args;
}