omise/omise-php

How to store Customer IDs?

Closed this issue · 6 comments

How can I retrieve a particular customer? I understand that I can use something like the following:

$customer = OmiseCustomer::retrieve('cust_test_4xtrb759599jsxlhkrb');

But how can I store the customer ID (cust_test_4x...) so that I can reference that customer later on? I am using PHP on the server end and android on the client side.

@hamzabinamin usually you gonna need to store it in your database.
For example, let's say you have a user table in your database (could be any, like, MySQL, PostgreSQL, MongoDB, and so on).

Maybe something like this:

user_id | email         | password 
1       | nam@omise.co  | ***********
2       | user@omise.co | ***********

You can alter your user table so we can save a customer id here:

user_id | email         | password    | customer_id
1       | nam@omise.co  | *********** | cust_test_4xtrb759599jsxlhkrb
2       | user@omise.co | *********** |

Then, you can now retrieve a customer id every time user logs in to your system and pass that to the OmiseCustomer::retrieve() function later on.

$loggedin_user = 'query from DB';
$customer = OmiseCustomer::retrieve($loggedin_user['customer_id']);

Note that there are various ways to store a customer id into your database depends on your DB structure.

@guzzilar I understand that I need to store that in the DB but how do I pass on a user id of my choice while the customer object gets created by omise? I tried passing the id field like this:

$customer = OmiseCustomer::create(array(
        'email' => $email,
        'id' => $id
      ));

But it doesn't store the id that I pass, it always creates a predefined id starting with cust_test.... If it's not possible to pass on a custom id then how can I know which id is being created for a new user and store that in my own db?

@hamzabinamin ic, so I guess you want to pass your user_id (that has been created from your system) to the Omise's Customer API so you can refer it back when you retrieve it later (or when you look at it at the Omise Dashboard) right?

By the way, the id attribute that you see from our API. That is the id we generate and use it as a reference so it can't be altered by any custom strings.

In this case, you can pass your user_id to the metadata attribute.
Try execute the following code:

$customer = OmiseCustomer::create([
    'email'    => 'nam@omise.co',
    'metadata' => [
        'id' => 'my_custom_id'
    ]
]);

So, it will show in the Dashboard as below:
screen shot 2561-03-12 at 23 17 36

Later on, you can retrieve it with the following code:

$customer = OmiseCustomer::retrieve('cust_test_5b8yyfcxvuowurp7zsh');
echo $customer['metadata']['id']; // 'my_custom_id'

@hamzabinamin is problem solved?
Let me know if you have any questions or feel free to create a new issue ticket, I'm going to close this ticket as resolved in soon :)

@guzzilar Yes I was able to resolve it. I realized that I could just use the customer php object to get the ID that gets created by omise and store that in my own db. Thanks for your help.

@hamzabinamin Nice 👍
Let me know if you have any questions :)