This repository is a demo application for integrating tru.ID's Payment API into your backend webserver to automatically top up credit when/if you've run out.
- A tru.ID account.
- Create a tru.ID project
- A mobile phone with a SIM card that has an active mobile data connection.
To use this application you will need a tru.ID account. To signup for one head over to the signup page.
Then, in your tru.ID console, navigate to the Settings page to create yourself some credentials for your Workspace. Be sure to save the file somewhere secure and make a note of the client_id and client_secret because this is needed later when setting up the tru.ID CLI.
Note once you close this tab, you will no longer be able to retrieve this specific set of credentials in the tru.ID console.
To create the tru.ID project, first install the tru.ID CLI, by running the following command in your terminal:
npm install -g @tru_id/cliYou'll then need to enter your tru.ID credentials, which you can find in the console. Once you've found your credentials, in your terminal, run the following command:
tru setup:credentials {YOUR_CLIENT_ID} {YOUR_CLIENT_SECRET} EUNow you'll need a project on tru.ID. So to create your project in your terminal run the following command (automatic-topups can be whatever name you wish):
tru projects:create automatic-topupsThis will create a new directory automatic-topups, and within this directory will be a tru.json file containing all the information on your project. It also has your projects credentials so keep it safe.
To clone this repository in your terminal run the following command:
git clone -b main git@github.com:tru-ID/payment-api-auto-topup-tutorial.gitMake a copy of the .env.example file and call it .env. Now, populate the environment variables with the following:
TRU_PROJECT_CLIENT_ID= # YOUR tru.ID PROJECT CLIENT ID
TRU_PROJECT_SECRET= # YOUR tru.ID PROJECT SECRET
TRU_WORKSPACE_ID= # YOUR tru.ID WORKSPACE ID
TRU_WORKSPACE_CLIENT_ID= # YOUR tru.ID WORKSPACE CLIENT ID
TRU_WORKSPACE_SECRET= # YOUR tru.ID WORKSPACE SECRET
TRU_TOP_UP_CURRENCY= # The currency you're paying in
TRU_TOP_UP_AMOUNT= # How much you would like to be automatically topped upIn your Terminal, navigate to the project root directory and run the following to install any third party libraries used in this project, and then start the web server:
npm install
npm run dev
With the starter-files branch checked out, you can now create a POST request to your web server and the path /sim-check containing a JSON body for your phone_number. Assuming you have credit and your credentials are correct, you'll receive a JSON object response similar to what's shown in the API specifications page. You can see a sample CURL POST request below:
curl --location 'localhost:8080/sim-check' \
--header 'Content-Type: application/json' \
--data '{
"phone_number": "447500000001"
}'A successful SIMCheck will be similar to what's shown below:
{
"check_id": "c69bc0e6-a429-11ea-bb37-0242ac130002",
"status": "COMPLETED",
"no_sim_change": false,
"sim_change_within": 43201,
"no_sim_change_period": 10080,
"charge_amount": 1,
"charge_currency": "API",
"created_at": "2020-06-01T16:43:30+00:00",
"network_id": "23599",
"_links": {
"self": {}
},
"snapshot_balance": 100
}Distributed under the MIT license. See LICENSE for more information.