From 02eeacd83f6f801de596c1c78eba889c21f09870 Mon Sep 17 00:00:00 2001 From: Clinton Date: Sun, 18 Aug 2024 22:56:49 +0300 Subject: [PATCH] Update docs (#3) * Update publish workflow * Update README docs * Improve API error handling --- .github/workflows/publish.yml | 2 +- README.md | 173 +++++++--------------------------- src/index.js | 12 ++- 3 files changed, 44 insertions(+), 143 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 0bc40f2..b047d18 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -19,7 +19,7 @@ jobs: npm version patch - name: Push updated package + tag to GitHub - run: git push -f && git push --tags -f + run: git push && git push --tags env: GITHUB_TOKEN: ${{ secrets.PUSH_GITHUB_TOKEN }} diff --git a/README.md b/README.md index da30a7b..74f25ce 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,13 @@ This is the official NodeJS SDK for [Cashramp's API](https://cashramp.co/commerc # NPM npm install cashramp --save +or + # Yarn yarn add cashramp ``` -### 👨🏾‍💻 Setup +### 👨🏾‍💻 Quick Start ```js const Cashramp = require("cashramp"); @@ -31,13 +33,40 @@ const cashrampAPI = new Cashramp({ env: "test", // Can be either `test` or `live` secretKey: "CSHRMP-SECK_apE0rjq1tiWl6VLB", }); + +// Example: Fetch available countries +const response = await cashrampAPI.getAvailableCountries(); +if (response.success) { + console.log(response.result); +} else { + console.log(response.error); +} ``` -### 📨 Querying the API +## API Reference + +### Queries + +- `getAvailableCountries()` +- `getMarketRate({ countryCode })` +- `getPaymentMethodTypes({ country })` +- `getRampableAssets()` +- `getRampLimits()` +- `getPaymentRequest({ reference })` +- `getAccount()` + +### Mutations + +- `confirmTransaction({ paymentRequest, transactionHash })` +- `initiateHostedPayment({ amount, paymentType, countryCode, currency, email, reference, redirectUrl, firstName, lastName })` +- `cancelHostedPayment({ paymentRequest })` +- `createCustomer({ firstName, lastName, email, country })` +- `addPaymentMethod({ customer, paymentMethodType, fields })` +- `withdrawOnchain({ address, amountUsd })` -The Cashramp SDK has methods for frequently used queries and mutations. Additionally, you can construct your own query/mutation and query the API directly. +## Custom Queries -_Here's a simple query to fetch the countries Cashramp is available in. Alternatively, you can use `Cashramp#getAvailableCountries()`._ +For custom queries, use the `sendRequest` method: ```js const query = ` @@ -64,138 +93,6 @@ if (response.success) { } ``` -## Queries - -#### Available countries - -```js -// Fetch the countries that Cashramp is available in -const response = await cashrampAPI.getAvailableCountries(); -``` - -#### Market rate - -```js -// Fetch the Cashramp market rate for a country -const response = await cashrampAPI.getMarketRate({ countryCode: "GH" }); -``` - -#### Payment method types - -```js -// Fetch the payment method types available in a country -const response = await cashrampAPI.getPaymentMethodTypes({ - country: - "VHlwZXM6OkNvdW50cnktZWMyOTY3N2QtMGYyYS00NjYzLWIzNDgtNGE5MDIxZWFmNGY3", -}); -``` - -#### Rampable assets - -```js -// Fetch the assets you can on/offramp with the Onchain Ramp -const response = await cashrampAPI.getRampableAssets(); -``` - -#### Ramp limits - -```js -// Fetch the Onchain Ramp limits -const response = await cashrampAPI.getRampLimits(); -``` - -#### Payment request - -```js -// Fetch the details of a payment request -const response = await cashrampAPI.getPaymentRequest({ - reference: "test_ref_1", -}); -``` - -#### Account - -```js -// Fetch your account's balance & deposit address -const response = await cashrampAPI.getAccount(); -``` - -## Mutations - -#### Confirm transaction - -```js -// Confirm a crypto transfer sent into Cashramp's Secure Escrow address -const response = await cashrampAPI.confirmTransaction({ - paymentRequest: - "VHlwZXM6OkNhc2hyYW1wOjpBUEk6Ok1lcmNoYW50UGF5bWVudFJlcXVlc3QtYTgwMWQ1NjAtOTYyYi00MjJkLWEyYzItZjY2NzZhZGE4NDY5", - transactionHash: - "0x9378c8f2940105debec0c436127f577440b611cf3b92d2967cca53c34c67612a", -}); -``` - -#### Initiate hosted payment +## Documentation -```js -// Initiate a payment request -const response = await cashrampAPI.initiateHostedPayment({ - amount: 20, - paymentType: "deposit", - countryCode: "NG", - currency: "usd", - email: "test@example.com", - reference: "test_ref_1", - redirectUrl: "https://example.com/order/success?id=test_ref_1", - firstName: "Jane", - lastName: "Dore", -}); -``` - -#### Cancel hosted payment - -```js -// Cancel an initiated payment request -const response = await cashrampAPI.cancelHostedPayment({ - paymentRequest: - "VHlwZXM6OkNhc2hyYW1wOjpBUEk6Ok1lcmNoYW50UGF5bWVudFJlcXVlc3QtYTY3MWVhZjQtYmVmYy00ZTEwLWI4YTQtZjk5MDZlNzZhNGUw", -}); -``` - -#### Create a customer - -```js -// Create a customer -const response = await cashrampAPI.createCustomer({ - firstName: "Jane", - lastName: "Doe", - email: "test@example.com", - country: - "VHlwZXM6OkNvdW50cnktOWY5MWE5NGYtNDUxYi00YWEyLWI2NjgtNjQ3YTM2ZDFjZmZh", -}); -``` - -#### Add payment method - -```js -// Add a payment method for an existing customer -const response = await cashrampAPI.addPaymentMethod({ - customer: - "VHlwZXM6OkNhc2hyYW1wOjpBUEk6Ok1lcmNoYW50Q3VzdG9tZXItYTI5OGVmY2UtMGZjMy00YjBlLTgyNzAtYmM3Nzg5MzVjMjI0", - paymentMethodType: - "VHlwZXM6OkNhc2hyYW1wOjpQMlBQYXltZW50TWV0aG9kVHlwZS04MjZiYzhkNC04NTFlLTRhNGMtYjY3Zi04OGEwOThiZWMyNjE=", - fields: [ - { identifier: "name", value: "Jane Doe" }, - { identifier: "phone_number", value: "0554078900" }, - ], -}); -``` - -#### Withdraw onchain - -```js -// Withdraw from your balance to an onchain wallet address -const response = await cashrampAPI.withdrawOnchain({ - address: "TQuFSvpct2FeBrKjRh8NDqtGAci2Z15RSe", - amountUsd: 20, -}); -``` +For detailed API documentation, visit [Cashramp's API docs](https://docs.cashramp.co). diff --git a/src/index.js b/src/index.js index dbfb40b..a1ddbfd 100644 --- a/src/index.js +++ b/src/index.js @@ -277,11 +277,15 @@ class Cashramp { }, }); - const json = await response.json(); - if (json.errors) { - return { success: false, error: json.errors[0].message }; + if (response.status == 200) { + const json = await response.json(); + if (json.errors) { + return { success: false, error: json.errors[0].message }; + } else { + return { success: true, result: json.data[name] }; + } } else { - return { success: true, result: json.data[name] }; + return { success: false, error: response.statusText }; } } catch (err) { console.log(err);