There is an action hook processed just before any successful transaction that can be used to add (for example) a receipt_email to the current charge :

Tha action is ‘direct_stripe_before_success_redirection’

Its parameters are :

  1. Charge ID – ID of the Charge that was just created with Stripe API
  2. Post ID – ID of the log created for the current transaction
  3. Button ID – ID set with the shortcode parameter “button_id”
  4. User ID – ID of the Stripe User

Here we go :

function my_direct_stripe_action($chargeID, $post_id, $button_id, $user_id) {
 // Add the action for a precise button setting a button_id
 if( $button_id === '1') {
 // Retrieve the user to identify its email address
 $user = get_user_by( 'ID', $user_id );
 $email_address = $user->user_email;
 //Retrieve Stripe Charge and update it
 $ch = \Stripe\Charge::retrieve($chargeID);
 $ch->receipt_email = $email_address;
 $ch->save();

 }
}
// Pass the function 'my_direct_stripe_action' through the action with priority 10 and 4 parameters
add_action( 'direct_stripe_before_success_redirection', 'my_direct_stripe_action', 10, 4 );

If you are in a testing environment, stripe don’t send the receipt but you can check in the logs that the charge was updated.

4 thoughts on “Add receipt_email to charge via action hook in Direct Stripe

  1. Hi Nico,

    first of all, your plugin is great, thank you very much !
    I just have a question, when someone that is not logged in on the website makes a purchase the ‘get_user_by’ function will return no information about the user cause it doesn’t exist, so I’m wondering, is there a way to obtain at least the email that was used to make the purchase?

    I’m using the Stripe test mode, and I was trying to look at the $ch returned by the following line:
    $ch = \Stripe\Charge::retrieve($chargeID);
    but it looks like $ch is empty, does it not work in test mode? Am I missing something?

    Regards

    1. Hello Jesus,

      Thank you for using Direct Stripe.

      I realize the example above only works if you save users in the WordPress database and was made before I added an option not to save users to WordPress database…
      You can see we get the email address of the user using :

      // Retrieve the user to identify its email address
      $user = get_user_by( 'ID', $user_id );
      $email_address = $user->user_email;

      If you’re not saving users to WordPress, convert the code that is within the button ID condition to

      //Retrieve Stripe Charge and update it
      $ch = \Stripe\Charge::retrieve($chargeID);
      //Retrieve the customer
      $cu = \Stripe\Customer::retrieve($ch->customer);
      $ch->receipt_email = $cu->email;
      $ch->save();

      That way we get the email address from the customer stored in Stripe.

      Kind regards

  2. Hello,

    I’ve posted a comment on this topic elsewhere but I can’t find my way back to it.
    can you lead me to it ?

    I also know you responded to my comment with a whole solution and you’re of great help !

    Thank you very much !

    Kevin

Leave a Reply

Your email address will not be published. Required fields are marked *