Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shift4-Team committed Nov 3, 2022
0 parents commit 9769b44
Show file tree
Hide file tree
Showing 53 changed files with 7,094 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build and validate
on: [ push ]

jobs:

tests:
name: Integration Tests
env:
SECRET_KEY: ${{ secrets.SECRET_KEY }}
strategy:
fail-fast: false
matrix:
node: [ lts/fermium, lts/gallium, lts/* ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test

style-check:
name: Style Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
- run: npm ci
- run: npm run stylecheck
20 changes: 20 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Publish
on:
release:
types: [ published ]

jobs:
publish:
if: github.repository == 'shift4/shift4-node'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
coverage
npm-debug.log
.idea
.env
lib/version.js
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

©2022 Shift4. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
154 changes: 154 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
Shift4 Node.js Library
===================================

[![Build](https://github.com/shift4developer/shift4-node/actions/workflows/build.yml/badge.svg)](https://github.com/shift4developer/shift4-node/actions/workflows/build.yml)

Installation
------------

```sh
npm install shift4
```

Quick start
-----------


```js
const api = require('shift4')('sk_test_my_secret_key');
(async () => {
try {
const customer = await api.customers.create({
email: 'user@example.com',
description: 'User description'
})
console.log('ID of created customer object: ', customer.id);

const card = await api.cards.create(customer.id, {
number: '4242424242424242',
expMonth: '12',
expYear: '2025',
cvc: '123',
cardholderName: 'John Smith'
})
console.log('ID of created card object: ', card.id);

const charge = await api.charges.create({
amount: 1000,
currency: "EUR",
card: card.id,
customerId: customer.id
});
console.log('ID of created charge object: ', charge.id);
} catch (e) {
console.error(e)
// handle errors
}
})();

```

API reference
-------------

Please refer to detailed API docs (linked) for all available fields

- charges
- [create(params)](https://dev.shift4.com/docs/api#charge-create)
- [get(chargeId)](https://dev.shift4.com/docs/api#charge-retrieve)
- [update(chargeId, params)](https://dev.shift4.com/docs/api#charge-update)
- [capture(chargeId)](https://dev.shift4.com/docs/api#charge-capture)
- [refund(chargeId, [params])](https://dev.shift4.com/docs/api#charge-capture)
- [list([params])](https://dev.shift4.com/docs/api#charge-list)
- customers
- [create(params)](https://dev.shift4.com/docs/api#customer-create)
- [get(customerId)](https://dev.shift4.com/docs/api#customer-retrieve)
- [update(customerId, params)](https://dev.shift4.com/docs/api#customer-update)
- [delete(customerId)](https://dev.shift4.com/docs/api#customer-delete)
- [list([params])](https://dev.shift4.com/docs/api#customer-list)
- cards
- [create(customerId, params)](https://dev.shift4.com/docs/api#card-create)
- [get(customerId, cardId)](https://dev.shift4.com/docs/api#card-retrieve)
- [update(customerId, cardId, params)](https://dev.shift4.com/docs/api#card-update)
- [delete(customerId, cardId)](https://dev.shift4.com/docs/api#card-delete)
- [list(customerId, [params])](https://dev.shift4.com/docs/api#card-list)
- subscriptions
- [create(params)](https://dev.shift4.com/docs/api#subscription-create)
- [get(subscriptionId)](https://dev.shift4.com/docs/api#subscription-retrieve)
- [update(subscriptionId, params)](https://dev.shift4.com/docs/api#subscription-update)
- [cancel(subscriptionId, [params])](https://dev.shift4.com/docs/api#subscription-cancel)
- [list([params])](https://dev.shift4.com/docs/api#subscription-list)
- plans
- [create(params)](https://dev.shift4.com/docs/api#plan-create)
- [get(planId)](https://dev.shift4.com/docs/api#plan-retrieve)
- [update(planId, params)](https://dev.shift4.com/docs/api#plan-update)
- [delete(planId)](https://dev.shift4.com/docs/api#plan-delete)
- [list([params])](https://dev.shift4.com/docs/api#plan-list)
- events
- [get(eventId)](https://dev.shift4.com/docs/api#event-retrieve)
- [list([params])](https://dev.shift4.com/docs/api#event-list)
- tokens
- [create(params)](https://dev.shift4.com/docs/api#token-create)
- [get(tokenId)](https://dev.shift4.com/docs/api#token-retrieve)
- blacklist
- [create(params)](https://dev.shift4.com/docs/api#blacklist-rule-create)
- [get(blacklistRuleId)](https://dev.shift4.com/docs/api#blacklist-rule-retrieve)
- [delete(blacklistRuleId)](https://dev.shift4.com/docs/api#blacklist-rule-delete)
- [list([params])](https://dev.shift4.com/docs/api#blacklist-rule-list)
- checkoutRequest
- [sign(checkoutRequestObjectOrJson)](https://dev.shift4.com/docs/api#checkout-request-sign)
- crossSaleOffers
- [create(params)](https://dev.shift4.com/docs/api#cross-sale-offer-create)
- [get(crossSaleOfferId)](https://dev.shift4.com/docs/api#cross-sale-offer-retrieve)
- [update(crossSaleOfferId, params)](https://dev.shift4.com/docs/api#cross-sale-offer-update)
- [delete(crossSaleOfferId)](https://dev.shift4.com/docs/api#cross-sale-offer-delete)
- [list([params])](https://dev.shift4.com/docs/api#cross-sale-offer-list)
- credits
- [create(params)](https://dev.shift4.com/docs/api#credit-create)
- [get(creditId)](https://dev.shift4.com/docs/api#credit-retrieve)
- [update(creditId, params)](https://dev.shift4.com/docs/api#credit-update)
- [list([params])](https://dev.shift4.com/docs/api#credit-list)
- disputes
- [get(disputeId)](https://dev.shift4.com/docs/api#dispute-retrieve)
- [update(disputeId, params)](https://dev.shift4.com/docs/api#dispute-update)
- [close(disputeId)](https://dev.shift4.com/docs/api#dispute-close)
- [list([params])](https://dev.shift4.com/docs/api#dispute-list)
- fileUploads
- [upload(content, params)](https://dev.shift4.com/docs/api#file-upload-create)
- [get(fileUploadId)](https://dev.shift4.com/docs/api#file-upload-retrieve)
- [list([params])](https://dev.shift4.com/docs/api#file-upload-list)
- fraudWarnings
- [get(fraudWarningId)](https://dev.shift4.com/docs/api#fraud-warning-retrieve)
- [list([params])](https://dev.shift4.com/docs/api#fraud-warning-list)
- paymentMethods
- [create(params)](https://dev.shift4.com/docs/api#payment-method-create)
- [retrieve(payment_method_id)](https://dev.shift4.com/docs/api#payment-method-retrieve)
- [delete(payment_method_id)](https://dev.shift4.com/docs/api#payment-method-delete)
- [list([params])](https://dev.shift4.com/docs/api#payment-methods-list)

For further information, please refer to our official documentation at [https://dev.shift4.com/docs](https://dev.shift4.com/docs)

Developing
----------

To connect to different backend:

```js
var api = require('shift4')({
secretKey: 'sk_test_my_secret_key',
apiUrl: 'https://api.myshift4env.com',
uploadsUrl: 'https://uploads.myshift4env.com'
});
```

To run tests:

```sh
SECRET_KEY=sk_test_my_secret_key npm run test
```

To run style check:

```sh
npm run stylecheck
```
54 changes: 54 additions & 0 deletions lib/communicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const Shift4Error = require('./shift4Error')
const version = require('./version')
const fetch = require('node-fetch')

function removeTrailingSlash (url) {
if (url.charAt(url.length - 1) === '/') {
return url.substring(0, url.length - 1)
} else {
return url
}
}

module.exports = class Communicator {
constructor ({ secretKey, url }) {
this.secretKey = secretKey
this.url = removeTrailingSlash(url)
}

async post (path, body) {
return this.call('POST', path, JSON.stringify(body), { 'Content-Type': 'application/json' })
}

async postMultipart (path, body) {
return this.call('POST', path, body)
}

async get (path, params) {
return this.call('GET', `${path}?${new URLSearchParams(params)}`)
}

async delete (path) {
return this.call('DELETE', `${path}`)
}

async call (method, path, body = undefined, headers = {}) {
// noinspection JSDeprecatedSymbols
const response = await fetch(`${this.url}${path}`, {
method,
body,
headers: {
'User-Agent': `Shift4-Node/${version} (Ruby/${process.version})`,
Authorization: `Basic ${Buffer.from(this.secretKey + ':').toString('base64')}`,
...headers
}
})
const json = await response.json()

if (response.ok) {
return json
} else {
throw new Shift4Error(json)
}
}
}
24 changes: 24 additions & 0 deletions lib/resources/blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = class Blacklist {
/**
* @param {Communicator} communicator
*/
constructor (communicator) {
this.communicator = communicator
}

create (params) {
return this.communicator.post('/blacklist', params)
}

get (blacklistRuleId) {
return this.communicator.get(`/blacklist/${blacklistRuleId}`)
}

delete (blacklistRuleId) {
return this.communicator.delete(`/blacklist/${blacklistRuleId}`)
}

list (params) {
return this.communicator.get('/blacklist', params)
}
}
28 changes: 28 additions & 0 deletions lib/resources/cards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = class Cards {
/**
* @param {Communicator} communicator
*/
constructor (communicator) {
this.communicator = communicator
}

create (customerId, params) {
return this.communicator.post(`/customers/${customerId}/cards`, params)
}

get (customerId, cardId) {
return this.communicator.get(`/customers/${customerId}/cards/${cardId}`)
}

update (customerId, cardId, params) {
return this.communicator.post(`/customers/${customerId}/cards/${cardId}`, params)
}

delete (customerId, cardId) {
return this.communicator.delete(`/customers/${customerId}/cards/${cardId}`)
}

list (customerId, params) {
return this.communicator.get(`/customers/${customerId}/cards`, params)
}
}
32 changes: 32 additions & 0 deletions lib/resources/charges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = class Charges {
/**
* @param {Communicator} communicator
*/
constructor (communicator) {
this.communicator = communicator
}

create (params) {
return this.communicator.post('/charges', params)
}

get (chargeId) {
return this.communicator.get(`/charges/${chargeId}`)
}

update (chargeId, params) {
return this.communicator.post(`/charges/${chargeId}`, params)
}

capture (chargeId, params) {
return this.communicator.post(`/charges/${chargeId}/capture`, params)
}

refund (chargeId, params) {
return this.communicator.post(`/charges/${chargeId}/refund`, params)
}

list (params) {
return this.communicator.get('/charges', params)
}
}
Loading

0 comments on commit 9769b44

Please sign in to comment.