lightrail-client
Advanced tools
Comparing version 2.0.32 to 2.1.0
@@ -29,3 +29,3 @@ "use strict"; | ||
isBrowser: false, | ||
restRoot: "https://api.lightraildev.net/v2/", | ||
restRoot: "https://api.lightrail.com/v2/", | ||
sharedSecret: null, | ||
@@ -152,3 +152,3 @@ logRequests: false, | ||
const merchantJwtPayload = jsonwebtoken.decode(exports.configuration.apiKey); | ||
if (!merchantJwtPayload || !merchantJwtPayload.g || !merchantJwtPayload.g.gui) { | ||
if (!merchantJwtPayload || !merchantJwtPayload.g || !merchantJwtPayload.g.gui || !merchantJwtPayload.g.tmi) { | ||
throw new Error("apiKey not valid"); | ||
@@ -161,2 +161,3 @@ } | ||
gmi: merchantJwtPayload.g.gmi, | ||
tmi: merchantJwtPayload.g.tmi, | ||
coi: contactId | ||
@@ -163,0 +164,0 @@ }, |
@@ -1,14 +0,28 @@ | ||
export interface PaymentSource { | ||
rail: string; | ||
[key: string]: string; | ||
export declare type TransactionParty = LightrailTransactionParty | StripeTransactionParty | InternalTransactionParty; | ||
export interface LightrailTransactionParty { | ||
rail: "lightrail"; | ||
contactId?: string; | ||
code?: string; | ||
valueId?: string; | ||
} | ||
export interface TransactionStep { | ||
rail: string; | ||
[key: string]: object | string | number; | ||
export interface StripeTransactionParty { | ||
rail: "stripe"; | ||
source?: string; | ||
customer?: string; | ||
maxAmount?: number; | ||
priority?: number; | ||
} | ||
export interface InternalTransactionParty { | ||
rail: "internal"; | ||
internalId: string; | ||
balance: number; | ||
pretax?: boolean; | ||
beforeLightrail?: boolean; | ||
} | ||
export declare type TransactionStep = LightrailTransactionStep | StripeTransactionStep | InternalTransactionStep; | ||
export interface LightrailTransactionStep { | ||
rail: string; | ||
rail: "lightrail"; | ||
valueId: string; | ||
contactId: string; | ||
code: string; | ||
contactId?: string; | ||
code?: string; | ||
balanceBefore: number; | ||
@@ -18,9 +32,15 @@ balanceAfter: number; | ||
} | ||
export interface ValueApplied { | ||
id: string; | ||
redemptionRule: string; | ||
ruleExplination: string; | ||
export interface StripeTransactionStep { | ||
rail: "stripe"; | ||
amount: number; | ||
preTax: boolean; | ||
chargeId?: string; | ||
charge?: any; | ||
} | ||
export interface InternalTransactionStep { | ||
rail: "internal"; | ||
internalId: string; | ||
balanceBefore: number; | ||
balanceAfter: number; | ||
balanceChange: number; | ||
} | ||
export interface LineTotal { | ||
@@ -47,3 +67,2 @@ subtotal: number; | ||
export interface LineItem extends LineItemBase { | ||
valuesApplied: ValueApplied[]; | ||
lineTotal: LineTotal; | ||
@@ -58,3 +77,3 @@ } | ||
} | ||
export declare type TransactionType = "debit" | "credit" | "checkout" | "transfer"; | ||
export declare type TransactionType = "debit" | "credit" | "checkout" | "transfer" | string; | ||
export interface Transaction { | ||
@@ -67,4 +86,4 @@ id: string; | ||
lineItems: LineItem[]; | ||
steps: Array<LightrailTransactionStep | TransactionStep>; | ||
paymentSources: PaymentSource[]; | ||
steps: TransactionStep[]; | ||
paymentSources: TransactionParty[]; | ||
metadata: object; | ||
@@ -71,0 +90,0 @@ } |
{ | ||
"name": "lightrail-client", | ||
"version": "2.0.32", | ||
"version": "2.1.0", | ||
"description": "A Javascript and Typescript client for Lightrail", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
265
README.md
@@ -10,5 +10,45 @@ # Lightrail Client for JavaScript and TypeScript | ||
- Account credits: create, retrieve, balance-check, and create/simulate transactions | ||
- Gift cards: create, retrieve, balance-check, and create/simulate transactions | ||
- Contacts | ||
- Create | ||
- Get | ||
- List | ||
- Update | ||
- Delete | ||
- List Values | ||
- Attach Value | ||
- Values | ||
- Create | ||
- Get by Id | ||
- Get by FullCode | ||
- List | ||
- Update | ||
- Delete | ||
- Programs | ||
- Create | ||
- Get Program | ||
- List | ||
- Update | ||
- Delete | ||
- Create Issuance | ||
- Get Issuance | ||
- List Issuances | ||
- Transactions | ||
- Checkout | ||
- Debit | ||
- Credit | ||
- Transfer | ||
- Get | ||
- List | ||
- Currencies | ||
- Create | ||
- Get | ||
- List | ||
- Update | ||
- Delete | ||
Note that the Lightrail API supports many other features and we are still working on covering them in this library. For a complete list of Lightrail API features check out the [Lightrail API documentation](https://www.lightrail.com/docs/). | ||
@@ -38,210 +78,2 @@ | ||
## Use Case: Account Credits Powered by Lightrail | ||
The remainder of this document is a quick demonstration of implementing account credits powered by Lightrail using this library. It will assume familiarity with the API and some core concepts, which are discussed in detail in the 'Powering Account Credits' section of the [API documentation](https://www.lightrail.com/docs/). | ||
### Handling Contacts | ||
#### Creating a New Contact | ||
To create a new Contact, you need to provide a client-side unique identifier known as the `userSuppliedId`. The `userSuppliedId` is a per-endpoint unique identifier used to ensure idempotence. Ensuring idempotence means that if the same request is issued more than once, it will not result in repeated actions. Optionally, you can also provide an `email`, `firstName`, and `lastName`. Here is a sample request: | ||
```javascript | ||
const Lightrail = require('lightrail'); | ||
const newContactParams = { | ||
userSuppliedId: 'customer-9f50629d', | ||
email: 'test@test.ca', | ||
firstName: 'Test', | ||
lastName: 'McTest' | ||
}; | ||
Lightrail.createContact(newContactParams).then( | ||
// called asynchronously | ||
); | ||
``` | ||
Sample response: | ||
```json | ||
{ | ||
"contactId": "contact-271a", | ||
"userSuppliedId": "customer-9f50629d", | ||
"email": "test@test.ca", | ||
"firstName": "Test", | ||
"lastName": "McTest", | ||
"dateCreated": "2017-07-26T23:50:04.000Z" | ||
} | ||
``` | ||
The response objects will include both the `userSuppliedId` and a server-generated `contactId` which you can persist and use to retrieve the Contact later. | ||
#### Retrieving a Contact | ||
You can retrieve a Contact based on its `contactId`. The response to this call will be a `contact` object similar to the one shown above. | ||
```javascript | ||
Lightrail.contacts.getContactById('contact-271a').then( | ||
// called asynchronously | ||
); | ||
``` | ||
Alternatively, you can retrieve a contact based on its `userSuppliedId`: | ||
```javascript | ||
Lightrail.contacts.getContactByUserSuppliedId('customer-9f50629d').then( | ||
// called asynchronously | ||
); | ||
``` | ||
### Handling Accounts | ||
#### Creating Accounts | ||
You can create an account for a contact based on their `contactId` (generated by Lightrail) or based on their `shopperId` (an identifier generated by your e-commerce system). You must also specify the currency that the account will be in, and provide a`userSuppliedId`, a unique identifier from your own system. Since each Contact can have only up to one Account Card per currency, you can add the currency as a suffix to the `userSuppliedId` you provided for the Contact. You may optionally include an `initialValue` for the account. | ||
```javascript | ||
const newAccountParams = { | ||
userSuppliedId: 'customer-9f50629d-USD', | ||
cardType: 'ACCOUNT_CARD', | ||
currency: 'USD', | ||
initialValue: 500, | ||
}; | ||
Lightrail.contacts.accounts.createAccount({shopperId: 'customer-9f50629d'}, newAccountParams).then( | ||
// called asynchronously | ||
); | ||
``` | ||
The response object will include both the `userSuppliedId` and a server-generated `cardId` which you can persist and use to retrieve the Account Card later. | ||
```json | ||
{ | ||
"cardId": "card-1dxxea", | ||
"userSuppliedId": "customer-9f50629d-USD", | ||
"contactId": "contact-271a", | ||
"dateCreated": "2017-07-26T23:50:04.572Z", | ||
"cardType": "ACCOUNT_CARD", | ||
"currency": "USD", | ||
"categories":[ | ||
{ | ||
"categoryId": "category-bdxx88", | ||
"key": "giftbit_order", | ||
"value": "2017-07-26" | ||
}, | ||
{ | ||
"categoryId": "category-95xxc2", | ||
"key": "giftbit_program", | ||
"value": "program-account-USD-user-088e" | ||
} | ||
] | ||
} | ||
``` | ||
#### Funding and Charging | ||
To fund or charge an account, you can once again use either the `contactId` (generated by Lightrail) or the `shopperId` (generated by your e-commerce system) to identify the customer account that you wish to transact against. | ||
You must additionally pass in the following: | ||
- The `value` of the transaction: a positive `value` will add funds to the account, while a negative `value` will post a charge to the account. This amount must be in the smallest currency unit (e.g., `500` for 5 USD) | ||
- The `currency` that the transaction is in (note that Lightrail does not handle currency conversion and the contact must have an account in the corresponding currency) | ||
- A `userSuppliedId`, which is a unique transaction identifier to ensure idempotence (for example, the order ID from your e-commerce system) | ||
```javascript | ||
const transactionParams = { | ||
value: 1200, | ||
currency: 'USD', | ||
userSuppliedId: 'tx-fe2d' | ||
}; | ||
Lightrail.contacts.accounts.createTransaction({shopperId: 'customer-9f50629d'}, transactionParams).then( | ||
// called asynchronously | ||
); | ||
``` | ||
The returned object includes both the `userSuppliedId` and a server-generated `transactionId` which you can use later to retrieve this transaction. | ||
```json | ||
{ | ||
"transactionId": "transaction-fec7", | ||
"value": 1200, | ||
"userSuppliedId": "tx-fe2d", | ||
"dateCreated": "2017-07-27T23:51:12.228Z", | ||
"transactionType": "FUND", | ||
"transactionAccessMethod": "CARDID", | ||
"cardId": "card-1dea", | ||
"currency": "USD" | ||
} | ||
``` | ||
#### Transaction Simulation and Balance Checking | ||
Before attempting to post a transaction, you may wish to do a transaction simulation to find out whether or not the account has enough funds. In the case of insufficient funds, this can also tell you the maximum value for which the transaction _would be_ successful. For example, if you simulate a $35 drawdown Transaction, the method can tell you that it _would be_ successful if it were only for $20. | ||
The parameters for this method call are almost identical to those for posting a transaction. To get the maximum value, add `nsf: false` to your transaction parameters: | ||
```javascript | ||
const simulationParams = { | ||
value: -6960, | ||
currency: 'USD', | ||
userSuppliedId: 'order-s3xx30', | ||
metadata: { | ||
cart: { | ||
total: 6960, | ||
items: [{ | ||
quantity: 3, | ||
id: 'B009L1MF7A', | ||
unit_price: 2320 | ||
}] | ||
} | ||
} | ||
}; | ||
Lightrail.contacts.accounts.simulateTransaction({shopperId: 'customer-9f50629d'}, simulationParams).then( | ||
// called asynchronously | ||
); | ||
``` | ||
The response will be similar to the following. Note that this is just a simulation and NOT an actual transaction; for instance, it does not have a `transactionId`. The response indicates that for this transaction, the maximum value this account can provide is $55. | ||
```json | ||
{ | ||
"value":-5500, | ||
"userSuppliedId":"order-s3xx30", | ||
"dateCreated":null, | ||
"transactionType":"DRAWDOWN", | ||
"transactionAccessMethod":"CARDID", | ||
"valueAvailableAfterTransaction":0, | ||
"cardId":"card-dcxx37", | ||
"currency":"USD", | ||
"transactionBreakdown":[ | ||
{ | ||
"value":-500, | ||
"valueAvailableAfterTransaction":0, | ||
"valueStoreId":"value-02xx6c" | ||
}, | ||
{ | ||
"value":-5000, | ||
"valueAvailableAfterTransaction":0, | ||
"valueStoreId":"value-66xxf2" | ||
} | ||
], | ||
"transactionId":null, | ||
"metadata":{ | ||
"cart":{ | ||
"total":6960, | ||
"items":[ | ||
{ | ||
"quantity":3, | ||
"id":"B009L1MF7A", | ||
"unit_price":2320 | ||
} | ||
] | ||
} | ||
}, | ||
"codeLastFour":"YNJC" | ||
} | ||
``` | ||
## Testing | ||
@@ -251,14 +83,5 @@ | ||
- ` LIGHTRAIL_API_KEY`: find this in to the Lightrail web app -- go to your [account settings](https://www.lightrail.com/app/#/account/profile), then click 'API keys' and 'Generate Key.' **Note** that for running tests, you should use a test mode key. | ||
- `LIGHTRAIL_API_PATH`: set to `https://api.lightrail.com/v2` | ||
- `LIGHTRAIL_API_KEY`: find this in to the Lightrail web app -- go to your [account settings](https://www.lightrail.com/app/#/account/profile), then click 'Integrations' and 'Generate Key.' **Note** that for running tests, you should use a test mode key. | ||
- `CARD_ID`: this is the `cardId` for a USD card with at least a few dollars on it. | ||
- `SHOPPER_ID`: the userSuppliedId for a Lightrail contact with a USD account | ||
- `CONTACT_ID`: the Lightrail-generated contactId for the same contact | ||
Then you can run `npm test`. | ||
@@ -265,0 +88,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1835
75392
95