Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe making multiple customers with same email address

I have stripe check out with php. It creates customers and charges them. I want to create a donation form where if same customer comes back and gives with same email address that Stripe doesn't create another customer but charges the existing customer with additional payments. Is this possible? Or does the checkout always create new customer with new customer id?

Here is my charge.php

<?php     require_once('config.php');      $token  = $_POST['stripeToken'];      if($_POST) {       $error = NULL;        try{         if(!isset($_POST['stripeToken']))           throw new Exception("The Stripe Token was not generated correctly");             $customer = Stripe_Customer::create(array(               'card'  => $token,               'email' =>  $_POST['stripeEmail'],               'description' => 'Thrive General Donor'             ));              $charge = Stripe_Charge::create(array(               'customer' => $customer->id,               'amount'   => $_POST['donationAmount'] * 100,               'currency' => 'usd'             ));       }       catch(Exception $e) {         $eror = $e->getMessage();       }       }  ?> 
like image 420
Thrive Ministry Avatar asked Oct 15 '14 21:10

Thrive Ministry


People also ask

How do I change my customer email on Stripe?

Edit a customerFind the customer you want to modify and click the name on the Customers page. In the account information page, select Actions > Edit information. Make your changes to the customer profile.

How do you turn a guest into a Stripe customer?

Login to Stripe > Click on Customers > Locate the customer and click on it. Scroll down to make sure a card has been added and locate the Create Payment button. Enter the correct amount, description and charge the card.

When can Stripe email Customers?

Stripe can automatically send email receipts after a successful payment, or when you refund one. This is done by providing an email address when making the API request, using the email address of a Customer object, or updating a PaymentIntent with a customer's email address after checkout.


2 Answers

You will need to store the relationship between email address and Stripe customer ID in a database. I've determined this by looking at Stripe's API on Customers.

First, when creating a new customer every field is optional. This leads me to believe that every single time you POST to /v1/customers, it will "[create] a new customer object."

Also, when retrieving a customer the only field available is the id. This leads me to believe that you cannot retrieve a customer based on an email address or other field.


If you can't store this information in a database, you can always list all the customers with GET /v1/customers. This will require you to paginate through and check all customer objects until you find one with a matching email address. You can see how this would be quite inefficient if done every time you tried to create a customer.

like image 173
Sam Avatar answered Sep 23 '22 12:09

Sam


You can list all users for a given email address. https://stripe.com/docs/api#list_customers

In JavaScript you could do this:

const customerAlreadyExists = (email)=>{     return  doGet(email)                 .then(response => response.data.length > 0); }  const doGet = (url: string)=>{     return fetch('https://api.stripe.com/v1/customers' + '?email=' + email, {         method: 'GET',         headers: {             Accept: 'application/json',             Authorization: 'Bearer ' + STRIPE_API_KEY         }     }).then(function (response) {         return response.json();     }).catch(function (error) {         console.error('Error:', error);     }); } 
like image 31
Jorciney Avatar answered Sep 21 '22 12:09

Jorciney