### Promise Support Example Source: https://github.com/omise/omise-node/blob/master/README.md An example demonstrating the use of Promises for asynchronous operations, chaining customer creation with charge creation. ```APIDOC ## Promise Chaining Example ### Description This example shows how to use Promises to chain multiple asynchronous operations, such as retrieving a token, creating a customer, and then creating a charge. ### Method POST (for create operations) GET (for retrieve operations) ### Endpoint /tokens, /customers, /charges ### Request Example ```javascript omise.tokens.retrieve('tokn_test_4xs9408a642a1htto8z').then(function(token) { return omise.customers.create({ email: 'john.doe@example.com', description: 'John Doe (id: 30)', card: token.id }); }).then(function(customer) { // And we make a charge to actually charge the customer for something. console.log(customer.id); return omise.charges.create({ amount: 10000, currency: 'thb', customer: customer.id }); }).then(function(charge) { // This function will be called after a charge is created. console.log('Charge created:', charge); }).catch(function(err) { // Put error handling code here. console.error('An error occurred:', err); }).finally(); ``` ``` -------------------------------- ### Install Omise Node.js Library Source: https://github.com/omise/omise-node/blob/master/README.md Install the Omise Node.js library using NPM. This is the first step to integrating Omise payments into your Node.js application. ```bash npm install omise ``` -------------------------------- ### Define a New API Resource Source: https://github.com/omise/omise-node/blob/master/README.md Create a new resource file (e.g., `lib/resourceName.js`) to handle API interactions. This example defines a resource with standard actions like create, list, retrieve, destroy, and update. ```javascript var resource = require('../apiResources'); var resourceName = function(config) { return resource.resourceActions( 'resourceName', ['create', 'list', 'retrieve', 'destroy', 'update'], {'key': config['secretKey']} ); } module.exports = resourceName; ``` -------------------------------- ### TypeScript Usage Source: https://context7.com/omise/omise-node/llms.txt Leverages TypeScript definitions for type-safe development. This example shows creating a customer and a charge using async/await. ```typescript import Omise from 'omise'; const omise = Omise({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Types are automatically inferred async function createPayment(): Promise { const customer = await omise.customers.create({ email: 'customer@example.com', description: 'Test customer' }); const charge = await omise.charges.create({ amount: 10000, currency: 'thb', customer: customer.id }); console.log('Charge status:', charge.status); console.log('Paid:', charge.paid); } ``` -------------------------------- ### Create and Manage Scheduled Charges Source: https://context7.com/omise/omise-node/llms.txt Set up recurring charges on a schedule for subscriptions. Examples include daily, monthly charges, and deleting a schedule. Requires the Omise client to be initialized. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Charge every 2 days omise.schedules.create({ every: 2, period: 'day', start_date: '2024-01-01', end_date: '2024-12-31', charge: { customer: 'cust_test_...', card: 'card_test_...', amount: 99900, description: 'Subscription fee' } }, function(err, schedule) { if (err) return console.log('Error:', err); console.log('Schedule ID:', schedule.id); console.log('Status:', schedule.status); console.log('Next occurrences:', schedule.next_occurrences_on); }); ``` ```javascript // Monthly charge on the 1st of each month omise.schedules.create({ every: 1, period: 'month', on: { days_of_month: [1] }, start_date: '2024-01-01', end_date: '2024-12-31', charge: { customer: 'cust_test_...', amount: 299900, description: 'Monthly membership' } }, function(err, schedule) { if (err) return console.log('Error:', err); console.log('Monthly schedule created:', schedule.id); }); ``` ```javascript // Delete a schedule omise.schedules.destroy('schd_test_...', function(err, result) { if (err) return console.log('Error:', err); console.log('Schedule deleted:', result.deleted); }); ``` -------------------------------- ### Promise-Based API Usage Source: https://context7.com/omise/omise-node/llms.txt Utilizes Promise chains for a cleaner asynchronous flow with comprehensive error handling. This example demonstrates a full payment flow from customer creation to charge. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Full payment flow with promises omise.customers.create({ email: 'john.doe@example.com', description: 'John Doe (id: 30)', card: 'tokn_test_...' }) .then(function(customer) { console.log('Customer created:', customer.id); return omise.charges.create({ amount: 10000, currency: 'thb', customer: customer.id }); }) .then(function(charge) { console.log('Charge successful:', charge.id); console.log('Amount:', charge.amount); console.log('Status:', charge.status); }) .catch(function(err) { // Handle API errors console.log('Error code:', err.code); console.log('Error message:', err.message); }) .finally(function() { console.log('Payment flow complete'); }); ``` -------------------------------- ### Retrieve Omise Account Balance Source: https://context7.com/omise/omise-node/llms.txt Get the current Omise account balance, including total, reserve, and transferable amounts. Requires the Omise client to be initialized with credentials. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.balance.retrieve(function(err, balance) { if (err) return console.log('Error:', err); console.log('Total balance:', balance.total / 100, balance.currency); console.log('Reserve:', balance.reserve / 100, balance.currency); console.log('Transferable:', balance.transferable / 100, balance.currency); }); ``` -------------------------------- ### Create Charge with Source (PromptPay, TrueMoney) Source: https://context7.com/omise/omise-node/llms.txt Initiate a charge using alternative payment methods like PromptPay or TrueMoney. This involves first creating a source, then using its ID to create the charge. The QR code URL for payment is provided in the response. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // First create a source, then use it for the charge omise.sources.create({ type: 'promptpay', amount: 500000, // 5,000.00 THB currency: 'thb' }).then(function(source) { return omise.charges.create({ amount: 500000, source: source.id, currency: 'thb', return_uri: 'https://example.com/checkout/complete' }); }).then(function(charge) { console.log('Charge created:', charge.id); console.log('QR Code URL:', charge.source.scannable_code.image.download_uri); }).catch(function(err) { console.log('Error:', err); }); ``` -------------------------------- ### Initialize Omise Client Source: https://context7.com/omise/omise-node/llms.txt Configure the Omise client with your public and secret API keys and specify the API version. This is the first step before making any API requests. ```javascript const omise = require('omise')({ publicKey: 'pkey_test_...', secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); ``` -------------------------------- ### Testing with Actual API Connection Source: https://github.com/omise/omise-node/blob/master/README.md To test against the live Omise API, export your public and secret keys as environment variables and run tests with `NOCK_OFF=true npm test`. ```bash $ export OMISE_PUBLIC_KEY= $ export OMISE_SECRET_KEY= $ NOCK_OFF=true npm test ``` -------------------------------- ### Create Customer with Associated Card Source: https://github.com/omise/omise-node/blob/master/README.md Demonstrates how to create a new customer and associate a card with them using a token ID obtained from omise.js. ```APIDOC ## POST /customers ### Description Creates a new customer and associates a card with it using a token ID. ### Method POST ### Endpoint /customers ### Parameters #### Request Body - **email** (string) - Required - The email address of the customer. - **description** (string) - Optional - A description for the customer. - **card** (string) - Required - The token ID of the card to associate with the customer. ### Request Example ```json { "email": "john.doe@example.com", "description": "John Doe (id: 30)", "card": "tokn_test_4xs9408a642a1htto8z" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the customer. - **description** (string) - The description of the customer. #### Response Example ```json { "id": "cust_xxxxxxxxxxxxxxxxx", "email": "john.doe@example.com", "description": "John Doe (id: 30)", "cards": { "object": "list", "data": [ { "id": "card_xxxxxxxxxxxxxxxxx", "object": "card", "country": "us", "last_digits": "4242", "brand": "Visa", "expiration_month": 12, "expiration_year": 2020, "name": "John Doe", "security_code_check": true, "created": "2023-01-01T12:00:00Z" } ], "offset": 0, "limit": 20, "total": 1 }, "created": "2023-01-01T12:00:00Z" } ``` ``` -------------------------------- ### Promise-based Customer Creation and Charge Source: https://github.com/omise/omise-node/blob/master/README.md Demonstrates using Promises for asynchronous operations. This snippet retrieves a token, creates a customer with the tokenized card, and then creates a charge for that customer. Error handling is included via `.catch()`. ```javascript omise.tokens.retrieve('tokn_test_4xs9408a642a1htto8z', function(error, token) { return omise.customers.create({ email: 'john.doe@example.com', description: 'John Doe (id: 30)', card: token.id }); }).then(function(customer) { // And we make a charge to actually charge the customer for something. console.log(customer.id); return omise.charges.create({ amount: 10000, currency: 'thb', customer: customer.id }); }).then(function(charge) { // This function will be called after a charge is created. }).catch(function(err) { // Put error handling code here. }).finally(); ``` -------------------------------- ### Create a Charge with Omise Node.js Source: https://github.com/omise/omise-node/blob/master/README.md Configure the Omise library with your secret key and version, then use it to create a charge. Ensure the 'card' parameter is a valid token obtained from the client-side. Set 'capture' to false for manual capture later. Handle potential errors and successful responses. ```javascript var omise = require('omise')({ 'secretKey': 'skey_test_...', 'omiseVersion': '2019-05-29' }); omise.charges.create({ 'description': 'Charge for order ID: 888', 'amount': '100000', // 1,000 Baht 'currency': 'thb', 'capture': false, 'card': tokenId }, function(err, resp) { if (resp.paid) { //Success } else { //Handle failure throw resp.failure_code; } }); ``` -------------------------------- ### Set Up Git Pre-commit Hook Source: https://github.com/omise/omise-node/blob/master/README.md Alias the provided script to the Git pre-commit hook to automatically run style checks and tests before each commit. ```bash ln -s ./pre-commit.sh .git/hooks/pre-commit ``` -------------------------------- ### List All Customers Source: https://github.com/omise/omise-node/blob/master/README.md Retrieve a list of all created customers using `omise.customers.list`. The customer data is available under the `data` attribute of the returned list object. ```javascript omise.customers.list(function(err, list) { console.log(list.data); }); ``` -------------------------------- ### Testing Modes Source: https://github.com/omise/omise-node/blob/master/README.md Instructions for running tests, both with and without connecting to the remote API server. ```APIDOC ## Testing ### Test Without Remote API Connection To run tests without connecting to the Omise API server, use the following command: ```console $ npm test ``` ### Test With Remote API Connection To test by connecting to the actual API server, you must first set your public and secret keys as environment variables: ```console $ export OMISE_PUBLIC_KEY= $ export OMISE_SECRET_KEY= $ NOCK_OFF=true npm test ``` ``` -------------------------------- ### Run Code Style and Tests Source: https://github.com/omise/omise-node/blob/master/README.md Before submitting a pull request, execute these npm commands to verify coding styles and ensure all tests pass. ```console npm run jscs npm test ``` -------------------------------- ### Manual Capture (Pre-Authorization) Source: https://context7.com/omise/omise-node/llms.txt Handle payments that require manual capture. First, create a pre-authorized charge, then capture the full or partial amount later. This is useful for scenarios where the final amount is not known at the time of authorization. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Create pre-authorized charge (not captured) omise.charges.create({ amount: 10000, currency: 'thb', card: 'tokn_test_...', authorization_type: 'pre_auth', capture: false }).then(function(charge) { console.log('Pre-authorized charge:', charge.id); console.log('Authorized amount:', charge.authorized_amount); // Later, capture the full amount return omise.charges.capture(charge.id); }).then(function(capturedCharge) { console.log('Captured charge:', capturedCharge.id); console.log('Captured amount:', capturedCharge.captured_amount); }).catch(function(err) { console.log('Error:', err); }); // Partial capture example omise.charges.capture('chrg_test_...', { capture_amount: 5000 // Capture only 50.00 THB of a 100.00 THB authorization }, function(err, charge) { if (err) return console.log('Error:', err); console.log('Partially captured:', charge.captured_amount); }); ``` -------------------------------- ### Create Shareable Payment Link Source: https://context7.com/omise/omise-node/llms.txt Generate a shareable payment link for customers to pay without direct integration. Configure amount, currency, title, description, and whether multiple uses are allowed. Initialize the Omise client first. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.links.create({ amount: 19000, // 190.00 THB currency: 'thb', title: 'Cappuccino', description: 'Freshly brewed coffee', multiple: true // Allow multiple uses }, function(err, link) { if (err) return console.log('Error:', err); console.log('Link ID:', link.id); console.log('Payment URL:', link.payment_uri); console.log('Amount:', link.amount); }); ``` -------------------------------- ### Create Customer with Associated Card Source: https://github.com/omise/omise-node/blob/master/README.md Use `omise.customers.create` with a `tokenId` from omise.js to associate a card with a new customer. The `customerId` is logged upon successful creation. ```javascript omise.customers.create({ 'email': 'john.doe@example.com', 'description': 'John Doe (id: 30)', 'card': 'tokn_test_4xs9408a642a1htto8z' //tokenId }, function(err, customer) { var customerId = customer.id; console.log(customerId); }); ``` -------------------------------- ### Sources API Source: https://github.com/omise/omise-node/blob/master/README.md Method for creating sources. ```APIDOC ## Sources API ### Create Source #### Method POST #### Endpoint /sources #### Parameters - **data** (object) - Required - Object containing source details. ``` -------------------------------- ### List All Customers Source: https://github.com/omise/omise-node/blob/master/README.md Retrieves a list of all customers associated with the account. ```APIDOC ## GET /customers ### Description Retrieves a list of all customers. ### Method GET ### Endpoint /customers ### Parameters #### Query Parameters - **offset** (integer) - Optional - The number of customers to skip. - **limit** (integer) - Optional - The maximum number of customers to return. ### Response #### Success Response (200) - **object** (string) - The type of object, which is 'list'. - **data** (array) - An array of customer objects. - **offset** (integer) - The offset used for pagination. - **limit** (integer) - The limit used for pagination. - **total** (integer) - The total number of customers. #### Response Example ```json { "object": "list", "data": [ { "id": "cust_xxxxxxxxxxxxxxxxx", "email": "john.doe@example.com", "description": "John Doe (id: 30)", "created": "2023-01-01T12:00:00Z" } ], "offset": 0, "limit": 20, "total": 1 } ``` ``` -------------------------------- ### Customers API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for creating, listing, retrieving, updating, and deleting customers, as well as managing their associated cards. ```APIDOC ## Customers API ### Create Customer #### Method POST #### Endpoint /customers #### Parameters - **data** (object) - Required - Object containing customer details (e.g., email, description, card). ### List Customers #### Method GET #### Endpoint /customers #### Parameters - **data** (object) - Optional - Object containing query parameters for filtering and pagination. ### Update Customer #### Method PUT #### Endpoint /customers/{customerId} #### Parameters - **customerId** (string) - Required - The ID of the customer to update. - **data** (object) - Optional - Object containing customer update data. ### Destroy Customer #### Method DELETE #### Endpoint /customers/{customerId} #### Parameters - **customerId** (string) - Required - The ID of the customer to delete. ### Retrieve Customer #### Method GET #### Endpoint /customers/{customerId} #### Parameters - **customerId** (string) - Required - The ID of the customer to retrieve. ### List Cards for Customer #### Method GET #### Endpoint /customers/{customerId}/cards #### Parameters - **customerId** (string) - Required - The ID of the customer. - **data** (object) - Optional - Object containing query parameters. ### Retrieve Card for Customer #### Method GET #### Endpoint /customers/{customerId}/cards/{cardId} #### Parameters - **customerId** (string) - Required - The ID of the customer. - **cardId** (string) - Required - The ID of the card to retrieve. ### Update Card for Customer #### Method PUT #### Endpoint /customers/{customerId}/cards/{cardId} #### Parameters - **customerId** (string) - Required - The ID of the customer. - **cardId** (string) - Required - The ID of the card to update. - **data** (object) - Optional - Object containing card update data. ### Destroy Card for Customer #### Method DELETE #### Endpoint /customers/{customerId}/cards/{cardId} #### Parameters - **customerId** (string) - Required - The ID of the customer. - **cardId** (string) - Required - The ID of the card to delete. ``` -------------------------------- ### Create Omise Customer Record Source: https://context7.com/omise/omise-node/llms.txt Create a customer record with optional card attachment for recurring payments. Provide customer email, description, and optionally a token for a card. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.customers.create({ email: 'john.doe@example.com', description: 'John Doe (id: 30)', card: 'tokn_test_4xs9408a642a1htto8z', // Optional: attach card metadata: { user_id: '30', plan: 'premium' } }, function(err, customer) { if (err) return console.log('Error:', err); console.log('Customer ID:', customer.id); console.log('Email:', customer.email); console.log('Default card:', customer.default_card); console.log('Cards count:', customer.cards.total); }); ``` -------------------------------- ### Retrieve Account and Capability Source: https://context7.com/omise/omise-node/llms.txt Fetches merchant account information and available payment capabilities. Ensure your API keys are correctly configured. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Get account info omise.account.retrieve(function(err, account) { if (err) return console.log('Error:', err); console.log('Email:', account.email); console.log('Currency:', account.currency); console.log('Country:', account.country); }); // Get payment capabilities omise.capability.retrieve(function(err, capability) { if (err) return console.log('Error:', err); console.log('Country:', capability.country); console.log('Available payment methods:'); capability.payment_methods.forEach(function(method) { console.log('-', method.name, method.currencies); }); }); ``` -------------------------------- ### Retrieve a Specific Customer Source: https://github.com/omise/omise-node/blob/master/README.md Fetch details of a specific customer by providing their `customerId` to the `omise.customers.retrieve` method. The customer's description is logged upon successful retrieval. ```javascript omise.customers.retrieve(customerId, function(err, resp) { console.log(resp.description); }); ``` -------------------------------- ### Links API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for creating, listing, and retrieving links. ```APIDOC ## Links API ### Create Link #### Method POST #### Endpoint /links #### Parameters - **data** (object) - Required - Object containing link details. ### List Links #### Method GET #### Endpoint /links #### Parameters - **data** (object) - Optional - Object containing query parameters. ### Retrieve Link #### Method GET #### Endpoint /links/{linkId} #### Parameters - **linkId** (string) - Required - The ID of the link to retrieve. ``` -------------------------------- ### Tokens API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for creating and retrieving tokens. ```APIDOC ## Tokens API ### Create Token #### Method POST #### Endpoint /tokens #### Parameters - **data** (object) - Required - Object containing token creation data. ### Retrieve Token #### Method GET #### Endpoint /tokens/{tokenId} #### Parameters - **tokenId** (string) - Required - The ID of the token to retrieve. ``` -------------------------------- ### List and Manage Disputes Source: https://context7.com/omise/omise-node/llms.txt Handles payment disputes and chargebacks. You can list all disputes, only open ones, or update a specific dispute with a response. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // List all disputes omise.disputes.list(function(err, disputes) { if (err) return console.log('Error:', err); disputes.data.forEach(function(d) { console.log(d.id, d.status, d.amount); }); }); // List open disputes omise.disputes.listOpen(function(err, disputes) { if (err) return console.log('Error:', err); console.log('Open disputes:', disputes.total); }); // Update dispute with response message omise.disputes.update('dspt_test_...', { message: 'Customer received the product as described.' }, function(err, dispute) { if (err) return console.log('Error:', err); console.log('Dispute updated:', dispute.id); }); ``` -------------------------------- ### Create and Manage Refunds for Omise Charges Source: https://context7.com/omise/omise-node/llms.txt Create full or partial refunds for completed charges. You can also list all refunds for a charge or retrieve details of a specific refund using their respective IDs. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); const chargeId = 'chrg_test_...'; // Create a partial refund omise.charges.createRefund(chargeId, { amount: 50000, // Refund 500.00 THB metadata: { reason: 'Customer request' } }, function(err, refund) { if (err) return console.log('Error:', err); console.log('Refund created:', refund.id); console.log('Refund amount:', refund.amount); }); ``` ```javascript // List all refunds for a charge omise.charges.listRefunds(chargeId, function(err, refunds) { if (err) return console.log('Error:', err); refunds.data.forEach(function(refund) { console.log(refund.id, refund.amount, refund.created_at); }); }); ``` ```javascript // Retrieve specific refund omise.charges.retrieveRefund(chargeId, 'rfnd_test_...', function(err, refund) { if (err) return console.log('Error:', err); console.log('Refund details:', refund); }); ``` -------------------------------- ### Create Charge with Token Source: https://context7.com/omise/omise-node/llms.txt Process a payment using a token obtained from Omise.js on the frontend. Ensure the token is valid and the charge details are correct. This method is suitable for direct credit card payments. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Create charge from token (token obtained from Omise.js frontend) omise.charges.create({ amount: 100000, // 1,000.00 THB (amount in smallest currency unit) currency: 'thb', capture: true, card: 'tokn_test_4xs9408a642a1htto8z', description: 'Charge for order ID: 888', return_uri: 'https://example.com/checkout/complete' }, function(err, charge) { if (err) { console.log('Error:', err.code, err.message); return; } if (charge.status === 'successful') { console.log('Payment successful:', charge.id); } else { console.log('Payment failed:', charge.failure_code, charge.failure_message); } }); ``` -------------------------------- ### List and Retrieve Events Source: https://context7.com/omise/omise-node/llms.txt Accesses webhook events for monitoring payment activities. Use event IDs to retrieve specific event details. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // List events omise.events.list({ limit: 10 }, function(err, events) { if (err) return console.log('Error:', err); events.data.forEach(function(event) { console.log(event.id, event.key, event.created_at); }); }); // Retrieve specific event omise.events.retrieve('evnt_test_...', function(err, event) { if (err) return console.log('Error:', err); console.log('Event:', event.key); console.log('Data:', JSON.stringify(event.data, null, 2)); }); ``` -------------------------------- ### Charges API Source: https://github.com/omise/omise-node/blob/master/README.md Comprehensive methods for creating, listing, retrieving, capturing, refunding, and managing charges. ```APIDOC ## Charges API ### Create Charge #### Method POST #### Endpoint /charges #### Parameters - **data** (object) - Required - Object containing charge details (e.g., amount, currency, customer). ### List Charges #### Method GET #### Endpoint /charges #### Parameters - **data** (object) - Optional - Object containing query parameters for filtering and pagination. ### Retrieve Charge #### Method GET #### Endpoint /charges/{chargeId} #### Parameters - **chargeId** (string) - Required - The ID of the charge to retrieve. ### Capture Charge #### Method POST #### Endpoint /charges/{chargeId}/capture #### Parameters - **chargeId** (string) - Required - The ID of the charge to capture. ### Create Refund #### Method POST #### Endpoint /charges/{chargeId}/refunds #### Parameters - **chargeId** (string) - Required - The ID of the charge to refund. - **data** (object) - Optional - Object containing refund details. ### Update Charge #### Method PUT #### Endpoint /charges/{chargeId} #### Parameters - **chargeId** (string) - Required - The ID of the charge to update. - **data** (object) - Optional - Object containing charge update data. ### Reverse Charge #### Method POST #### Endpoint /charges/{chargeId}/reverse #### Parameters - **chargeId** (string) - Required - The ID of the charge to reverse. ### Expire Charge #### Method POST #### Endpoint /charges/{chargeId}/expire #### Parameters - **chargeId** (string) - Required - The ID of the charge to expire. ### List Schedules for Charge #### Method GET #### Endpoint /charges/schedules #### Parameters - **data** (object) - Optional - Object containing query parameters. ``` -------------------------------- ### Retrieve a Customer Source: https://github.com/omise/omise-node/blob/master/README.md Fetches the details of a specific customer using their customer ID. ```APIDOC ## GET /customers/{customerId} ### Description Retrieves a specific customer by their ID. ### Method GET ### Endpoint /customers/{customerId} ### Parameters #### Path Parameters - **customerId** (string) - Required - The unique identifier of the customer. ### Response #### Success Response (200) - **id** (string) - The unique identifier for the customer. - **description** (string) - The description of the customer. #### Response Example ```json { "id": "cust_xxxxxxxxxxxxxxxxx", "email": "john.doe@example.com", "description": "John Doe (id: 30)", "created": "2023-01-01T12:00:00Z" } ``` ``` -------------------------------- ### Error Handling Source: https://github.com/omise/omise-node/blob/master/README.md Information on how to handle errors, both for invalid requests and for valid requests with failures. ```APIDOC ## Error Handling ### Description This section details how to handle errors returned by the Omise API. It covers checking for invalid requests and for failures in otherwise valid requests. ### Invalid Requests For invalid requests, check the `Error` object which includes `code` and `message` attributes. Refer to [Omise Errors](https://www.omise.co/api/errors) for details. ### Valid Requests with Failures For valid requests that result in a failure (e.g., a charge that is declined), check the `failure_code` and `failure_message` attributes. A successful operation requires both `failure_code` and `failure_message` to be `null`. ### Example Error Structure (Conceptual) ```json { "error": { "code": "invalid_request", "message": "The request is invalid." } } ``` ### Example Failure Structure (Conceptual) ```json { "id": "chrg_xxxxxxxxxxxxxxxxx", "status": "failed", "failure_code": "card_declined", "failure_message": "The card was declined by the bank." } ``` ``` -------------------------------- ### Create Recipient for Fund Transfers Source: https://context7.com/omise/omise-node/llms.txt Create a recipient account for receiving fund transfers. Requires recipient details including bank account information. The Omise client must be initialized. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.recipients.create({ name: 'John Doe', email: 'john@example.com', description: 'Merchant payout account', type: 'individual', // or 'corporation' bank_account: { brand: 'bbl', // Bank code number: '1234567890', name: 'JOHN DOE' }, metadata: { merchant_id: '12345' } }, function(err, recipient) { if (err) return console.log('Error:', err); console.log('Recipient ID:', recipient.id); console.log('Bank account:', recipient.bank_account.last_digits); console.log('Active:', recipient.active); }); ``` -------------------------------- ### Testing without Remote API Connection Source: https://github.com/omise/omise-node/blob/master/README.md Run tests locally without connecting to the Omise API server by executing `npm test` in your terminal. ```bash $ npm test ``` -------------------------------- ### Balance API Source: https://github.com/omise/omise-node/blob/master/README.md Method for retrieving the current balance. ```APIDOC ## Balance API ### Retrieve Balance #### Method GET #### Endpoint /balance ``` -------------------------------- ### Schedules API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for creating, destroying, and retrieving schedules. ```APIDOC ## Schedules API ### Create Schedule #### Method POST #### Endpoint /schedules #### Parameters - **data** (object) - Required - Object containing schedule details. ### Destroy Schedule #### Method DELETE #### Endpoint /schedules/{scheduleId} #### Parameters - **scheduleId** (string) - Required - The ID of the schedule to delete. ### Retrieve Schedule #### Method GET #### Endpoint /schedules/{scheduleId} #### Parameters - **scheduleId** (string) - Optional - The ID of the schedule to retrieve. If omitted, lists all schedules. ``` -------------------------------- ### Account API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for retrieving and updating account information. ```APIDOC ## Account API ### Retrieve Account #### Method GET #### Endpoint /account ### Update Account #### Method PUT #### Endpoint /account #### Parameters - **data** (object) - Optional - Object containing account update data. ``` -------------------------------- ### Events API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for listing and retrieving events. ```APIDOC ## Events API ### List Events #### Method GET #### Endpoint /events #### Parameters - **data** (object) - Optional - Object containing query parameters. ### Retrieve Event #### Method GET #### Endpoint /events/{eventId} #### Parameters - **eventId** (string) - Required - The ID of the event to retrieve. ``` -------------------------------- ### List Charges Source: https://context7.com/omise/omise-node/llms.txt Retrieve a list of past charges with support for pagination and filtering by date range, order, and limits. This is useful for auditing and reporting purposes. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.charges.list({ limit: 20, offset: 0, order: 'reverse_chronological', from: '2024-01-01T00:00:00Z', to: '2024-12-31T23:59:59Z' }, function(err, list) { if (err) return console.log('Error:', err); console.log('Total charges:', list.total); list.data.forEach(function(charge) { console.log(charge.id, charge.amount, charge.status); }); }); ``` -------------------------------- ### Update and Delete Omise Customer Source: https://context7.com/omise/omise-node/llms.txt Update customer information such as email, description, or metadata, or delete a customer record entirely. Ensure you have the correct customer ID. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Update customer omise.customers.update('cust_test_...', { email: 'newemail@example.com', description: 'Updated description', metadata: { status: 'active' } }, function(err, customer) { if (err) return console.log('Error:', err); console.log('Customer updated:', customer.email); }); ``` ```javascript // Delete customer omise.customers.destroy('cust_test_...', function(err, result) { if (err) return console.log('Error:', err); console.log('Customer deleted:', result.deleted); }); ``` -------------------------------- ### Update Customer Information Source: https://github.com/omise/omise-node/blob/master/README.md Modify customer details using `omise.customers.update`. Pass the `customerId` and an object containing the fields to be updated. The updated description is logged. ```javascript omise.customers.update(customerId, { description: 'Customer for john.doe@example.com' }, function(err, resp) { console.log(resp.description); }); ``` -------------------------------- ### Transfers API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for creating, listing, retrieving, and updating transfers. ```APIDOC ## Transfers API ### Create Transfer #### Method POST #### Endpoint /transfers #### Parameters - **data** (object) - Required - Object containing transfer details. ### List Transfers #### Method GET #### Endpoint /transfers #### Parameters - **data** (object) - Optional - Object containing query parameters. ### Retrieve Transfer #### Method GET #### Endpoint /transfers/{transferId} #### Parameters - **transferId** (string) - Required - The ID of the transfer to retrieve. ### Update Transfer #### Method PUT #### Endpoint /transfers/{transferId} #### Parameters - **transferId** (string) - Required - The ID of the transfer to update. - **data** (object) - Optional - Object containing transfer update data. ### List Schedules for Transfer #### Method GET #### Endpoint /transfers/schedules #### Parameters - **data** (object) - Optional - Object containing query parameters. ``` -------------------------------- ### Manage Omise Customer Cards Source: https://context7.com/omise/omise-node/llms.txt List, retrieve, update, and delete cards associated with a customer. You will need the customer ID and the specific card ID for these operations. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); const customerId = 'cust_test_...'; // List customer cards omise.customers.listCards(customerId, { limit: 10 }, function(err, cards) { if (err) return console.log('Error:', err); cards.data.forEach(function(card) { console.log(card.id, card.brand, card.last_digits); }); }); ``` ```javascript // Retrieve specific card omise.customers.retrieveCard(customerId, 'card_test_...', function(err, card) { if (err) return console.log('Error:', err); console.log('Card:', card.brand, '**** **** ****', card.last_digits); }); ``` ```javascript // Update card details omise.customers.updateCard(customerId, 'card_test_...', { name: 'JOHN DOE', expiration_month: 12, expiration_year: 2026 }, function(err, card) { if (err) return console.log('Error:', err); console.log('Card updated:', card.id); }); ``` ```javascript // Delete a card omise.customers.destroyCard(customerId, 'card_test_...', function(err, card) { if (err) return console.log('Error:', err); console.log('Card deleted:', card.deleted); }); ``` -------------------------------- ### Search API Source: https://github.com/omise/omise-node/blob/master/README.md Method for searching resources. ```APIDOC ## Search API ### List Search Results #### Method GET #### Endpoint /search #### Parameters - **data** (object) - Required - Object containing search query parameters. ``` -------------------------------- ### Create Transfer to Recipient Source: https://context7.com/omise/omise-node/llms.txt Transfer funds to a pre-created recipient's bank account. Supports immediate failure if funds are insufficient. Ensure the Omise client is initialized. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.transfers.create({ amount: 100000, // 1,000.00 THB recipient: 'recp_test_...', fail_fast: true, // Fail immediately if insufficient funds metadata: { payout_id: 'PAY-001' } }, function(err, transfer) { if (err) return console.log('Error:', err); console.log('Transfer ID:', transfer.id); console.log('Amount:', transfer.amount); console.log('Fee:', transfer.fee); console.log('Net:', transfer.net); console.log('Sent:', transfer.sent); if (transfer.failure_code) { console.log('Failed:', transfer.failure_code, transfer.failure_message); } }); ``` -------------------------------- ### List Transactions Source: https://context7.com/omise/omise-node/llms.txt Retrieves transaction history for account reconciliation. Allows specifying limits and order for the transaction list. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); omise.transactions.list({ limit: 50, order: 'reverse_chronological' }, function(err, transactions) { if (err) return console.log('Error:', err); transactions.data.forEach(function(txn) { console.log( txn.id, txn.type, txn.amount, txn.currency, txn.direction ); }); }); ``` -------------------------------- ### Charge Customer's Saved Card Source: https://context7.com/omise/omise-node/llms.txt Create a charge using a customer's default saved card or a specific card ID for recurring payments. Ensure the Omise client is initialized with your secret key and API version. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Charge using customer's default card omise.charges.create({ amount: 99900, currency: 'thb', customer: 'cust_test_...', description: 'Monthly subscription' }, function(err, charge) { if (err) return console.log('Error:', err); console.log('Charge successful:', charge.id); }); ``` ```javascript // Charge using specific card omise.charges.create({ amount: 99900, currency: 'thb', customer: 'cust_test_...', card: 'card_test_...', // Specific card ID description: 'Monthly subscription' }, function(err, charge) { if (err) return console.log('Error:', err); console.log('Charge successful:', charge.id); }); ``` -------------------------------- ### Update a Customer Source: https://github.com/omise/omise-node/blob/master/README.md Modifies the information of an existing customer. ```APIDOC ## PUT /customers/{customerId} ### Description Updates an existing customer's information. ### Method PUT ### Endpoint /customers/{customerId} ### Parameters #### Path Parameters - **customerId** (string) - Required - The unique identifier of the customer to update. #### Request Body - **description** (string) - Optional - The new description for the customer. - **email** (string) - Optional - The new email address for the customer. ### Request Example ```json { "description": "Customer for john.doe@example.com" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the customer. - **description** (string) - The updated description of the customer. #### Response Example ```json { "id": "cust_xxxxxxxxxxxxxxxxx", "email": "john.doe@example.com", "description": "Customer for john.doe@example.com", "created": "2023-01-01T12:00:00Z" } ``` ``` -------------------------------- ### Reverse or Expire Omise Charges Source: https://context7.com/omise/omise-node/llms.txt Use these methods to reverse an uncaptured charge or expire a pending charge that has not been completed. Ensure you have the correct charge ID. ```javascript const omise = require('omise')({ secretKey: 'skey_test_...', omiseVersion: '2019-05-29' }); // Reverse an uncaptured (pre-authorized) charge omise.charges.reverse('chrg_test_...', function(err, charge) { if (err) return console.log('Error:', err); console.log('Charge reversed:', charge.reversed); }); ``` ```javascript // Expire a pending charge (e.g., abandoned PromptPay payment) omise.charges.expire('chrg_test_...', function(err, charge) { if (err) return console.log('Error:', err); console.log('Charge expired:', charge.expired); }); ``` -------------------------------- ### Disputes API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for listing and retrieving disputes, and updating dispute information. ```APIDOC ## Disputes API ### List Disputes #### Method GET #### Endpoint /disputes #### Parameters - **data** (object) - Optional - Object containing query parameters. ### List Closed Disputes #### Method GET #### Endpoint /disputes/closed ### List Open Disputes #### Method GET #### Endpoint /disputes/open ### List Pending Disputes #### Method GET #### Endpoint /disputes/pending ### Retrieve Dispute #### Method GET #### Endpoint /disputes/{disputeId} #### Parameters - **disputeId** (string) - Required - The ID of the dispute to retrieve. ### Update Dispute #### Method PUT #### Endpoint /disputes/{disputeId} #### Parameters - **disputeId** (string) - Required - The ID of the dispute to update. - **data** (object) - Optional - Object containing dispute update data. ``` -------------------------------- ### Transactions API Source: https://github.com/omise/omise-node/blob/master/README.md Methods for listing and retrieving transactions. ```APIDOC ## Transactions API ### List Transactions #### Method GET #### Endpoint /transactions #### Parameters - **data** (object) - Optional - Object containing query parameters. ### Retrieve Transaction #### Method GET #### Endpoint /transactions/{transactionId} #### Parameters - **transactionId** (string) - Required - The ID of the transaction to retrieve. ```