Skip to content

Commit

Permalink
feat(sails-pay): add basic setup for hook
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Feb 18, 2024
1 parent a365611 commit a139627
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 942 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
package
936 changes: 24 additions & 912 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"name": "sails-pay",
"version": "0.0.1",
"private": true,
"keywords": [
"Lemon Squeezy",
Expand Down
23 changes: 0 additions & 23 deletions packages/sails-hook-pay/index.js

This file was deleted.

10 changes: 7 additions & 3 deletions packages/sails-lemonsqueezy/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "sails-lemonsqueezy",
"name": "@sails-pay/lemonsqueezy",
"version": "0.0.1",
"description": "Lemon Squeezy adapter for Sails Pay",
"main": "index.js",
"main": "adapter.js",
"scripts": {
"test": "npm run test"
},
Expand Down Expand Up @@ -32,5 +32,9 @@
"ecommerce"
],
"author": "Kelvin Omereshone <[email protected]>",
"license": "MIT"
"license": "MIT",
"dependencies": {
"machine": "^15.2.3",
"undici": "^6.6.2"
}
}
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions packages/sails-pay/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* pay hook
*
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
*/

module.exports = function (sails) {
function extractProviderName(fullName) {
const parts = fullName.split('/')
return parts[parts.length - 1]
}
return {
defaults: {
pay: {
provider: 'default',
providers: {}
}
},
/**
* Runs when this Sails app loads/lifts.
*/
initialize: async function () {
function getPaymentProvider(provider) {
if (!sails.config.pay.providers[provider]) {
throw new Error('The provided payment provider coult not be found.')
}
const providerName = extractProviderName(
sails.config.pay.providers[provider].adapter
)
switch (providerName) {
case 'lemonsqueezy':
console.log()
const paymentProvider = require(
sails.config.pay.providers[provider].adapter
)
paymentProvider.config = sails.config.pay.providers[provider]
return paymentProvider
default:
throw new Error(
'Invalid payment provider provided, supported stores are redis or memcached.'
)
}
}

sails.hooks.pay.paymentProvider = getPaymentProvider(
sails.config.pay.provider
)
sails.hooks.pay.paymentProvider.provider = function (provider) {
return getPaymentProvider(provider)
}

sails.pay = sails.hooks.pay.paymentProvider

sails.log.info('Initializing custom hook (`pay`)')
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "sails-hook-pay",
"name": "sails-pay",
"version": "0.0.1",
"description": "The modern payments engine for Sails applications",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion playground/config/pay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.pay = {
providers: {
processors: {
default: {
adapter: 'sails-lemonsqueezy'
}
Expand Down

0 comments on commit a139627

Please sign in to comment.