Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crypto payments #284

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@3c975f1cb22919a28755c6541b4ca2656a690f49 # v1
uses: aws-actions/amazon-ecs-render-task-definition@cf6bad72997cc8abcbc836ea5534f2e1e3719ec6 # v1
with:
task-definition: task-definition.json
container-name: store
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@3c975f1cb22919a28755c6541b4ca2656a690f49 # v1
uses: aws-actions/amazon-ecs-render-task-definition@cf6bad72997cc8abcbc836ea5534f2e1e3719ec6 # v1
with:
task-definition: task-definition.json
container-name: store-backend
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ PRINTFUL_BASE_URL=https://api.printful.com
PRINTFUL_STORE_ID=/* store id from printful */
STRIPE_KEY=/* key for making requests to Stripe */
STRIPE_WEBHOOK_SECRET=/* key for making listening to Stripe webhooks */
RADOM_BASE_URL=https://api.radom.com
RADOM_API_KEY=/* key for making requests to Radom */
RADOM_WEBHOOK_VERIFICATION_KEY=/* key for verifying webhook events from Radom */
SENTRY_DSN=/* DSN for Sentry alerts */
```

Expand Down
4 changes: 3 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
"strict": true,
"useUnknownInCatchVariables": false,
"strictNullChecks": false
}
}
4,620 changes: 3,224 additions & 1,396 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"lint": "prettier --check --plugin-search-dir=. .",
"format": "prettier --write --plugin-search-dir=. .",
"gen": "graphql-codegen --config codegen.yml",
"postinstall": "node scripts/copy-dependencies.js && npm run gen"
"postinstall": "node scripts/copy-dependencies.js && npm run gen",
"test": "vitest"
},
"type": "module",
"dependencies": {
"@brave/leo": "github:brave/leo#56572b0a2af8b9bccfce48a760a1ed518906d679",
"@brave/leo": "github:brave/leo#24fc7856282b10fd7e826e02db98ab56dd02fb2a",
"@fontsource-variable/inter": "5.1.0",
"@fontsource/poppins": "5.1.0",
"@graphql-codegen/cli": "5.0.2",
Expand All @@ -29,8 +30,8 @@
"@sentry/node": "8.30.0",
"@stablelib/base64": "2.0.0",
"@stablelib/utf8": "2.0.0",
"@sveltejs/adapter-node": "5.2.2",
"@sveltejs/kit": "2.5.27",
"@sveltejs/adapter-node": "5.2.6",
"@sveltejs/kit": "2.7.0",
"@types/marked": "6.0.0",
"autoprefixer": "10.4.20",
"graphql": "16.9.0",
Expand All @@ -47,14 +48,16 @@
"svelte": "4.2.19",
"svelte-check": "4.0.2",
"svelte-preprocess": "5.1.4",
"tailwindcss": "3.4.11",
"tailwindcss": "3.4.13",
"tweetnacl": "1.0.3",
"typescript": "5.6.2",
"vite": "5.4.6"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "3.1.2",
"@tsconfig/svelte": "5.0.4"
"@tsconfig/svelte": "5.0.4",
"msw": "2.3.1",
"vitest": "2.0.3"
},
"overrides": {
"svelte": "4.2.19"
Expand Down
166 changes: 166 additions & 0 deletions src/lib/payment-processing/checkoutSession.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { PUBLIC_ASSETS_PATH } from '$env/static/public';
import { describe, expect, test } from 'vitest';
import { CreateCheckoutSessionError, formatCheckoutSessionParams } from './checkoutSession';
import { radomAdapter } from './providers/radom';
import { stripeAdapter } from './providers/stripe';

const requestBody = {
items: [
{ id: 'clwsmtxpp0000zo6dwkmvu76n', quantity: '1' },
{ id: 'clwsmtxrj0004zo6dsqvo4b7t', quantity: '1' }
],
shippingAddress: {
name: 'Jacob Lamont',
address1: '333 Elm Circle',
city: 'Villa Rica',
zip: '30180',
country_code: 'US',
state_code: 'GA',
calling_code: 'US+1',
phone: ''
}
};

test('Fails when no order items are included in request body', async () => {
const improperRequestBody = structuredClone(requestBody);
improperRequestBody.items = [];

await expect(async () =>
formatCheckoutSessionParams(improperRequestBody, stripeAdapter)
).rejects.toThrowError(CreateCheckoutSessionError.EMPTY_CART);
});

test('Fails when shipping data is malformed', async () => {
const improperRequestBody = structuredClone(requestBody) as any;
delete improperRequestBody.shippingAddress.address1;

await expect(async () =>
formatCheckoutSessionParams(improperRequestBody, stripeAdapter)
).rejects.toThrowError(CreateCheckoutSessionError.INVALID_SHIPPING_ADDRESS);
});

describe('Stripe', () => {
const expectedStripeCheckoutSessionData = {
line_items: [
{
price_data: {
currency: 'USD',
product_data: {
name: 'Brave | BAT Desk Mat',
images: [
`${PUBLIC_ASSETS_PATH}/b2847030ab3d174e3788ebf17ebc9e90ef26db4bbba6730b91039912666d078a.png`
],
metadata: { printfulVariantId: '3826891072', baseVariantId: 14942 }
},
unit_amount: 3500
},
quantity: 1
},
{
price_data: {
currency: 'USD',
product_data: {
name: 'Brave Lion Fleece Zip Up Hoodie / 2XL',
images: [
`${PUBLIC_ASSETS_PATH}/a996ac2653c1f945483cb2bdabb72e232de591b913148aa708107d4ac9d44053.png`
],
metadata: { printfulVariantId: '3576357951', baseVariantId: 15042 }
},
unit_amount: 4595
},
quantity: 1
}
],
mode: 'payment',
success_url: 'http://localhost:5173/success/{CHECKOUT_SESSION_ID}/?provider=stripe',
cancel_url: 'http://localhost:5173/cart/?session_id={CHECKOUT_SESSION_ID}&provider=stripe',
allow_promotion_codes: true,
shipping_options: [
{
shipping_rate_data: {
display_name: 'Standard (Estimated delivery: Jul 24-Jul 29)',
type: 'fixed_amount',
metadata: { printful_shipping_rate_id: 'STANDARD' },
fixed_amount: { amount: 399, currency: 'USD' },
delivery_estimate: {
minimum: { unit: 'day', value: 4 },
maximum: { unit: 'day', value: 7 }
}
}
}
],
invoice_creation: { enabled: true }
};

test('Converts request body to Stripe checkout session data', async () => {
expect(await formatCheckoutSessionParams(requestBody, stripeAdapter)).toMatchObject(
expectedStripeCheckoutSessionData
);
});
});

describe('Radom', () => {
const expectedRadomCheckoutSessionData = {
lineItems: [
{
itemData: {
name: 'Brave | BAT Desk Mat',
description: 'Quantity: 1',
price: 35,
imageUrl:
'https://cdn.store.bravesoftware.com/b2847030ab3d174e3788ebf17ebc9e90ef26db4bbba6730b91039912666d078a.png',
currency: 'USD'
}
},
{
itemData: {
name: 'Brave Lion Fleece Zip Up Hoodie / 2XL',
description: 'Quantity: 1',
price: 45.95,
imageUrl:
'https://cdn.store.bravesoftware.com/a996ac2653c1f945483cb2bdabb72e232de591b913148aa708107d4ac9d44053.png',
currency: 'USD'
}
},
{
itemData: {
name: 'Standard (Estimated delivery: Jul 24-Jul 29)',
price: 3.99,
currency: 'USD'
}
}
],
currency: 'USD',
gateway: {
managed: {
methods: [
{
network: 'SepoliaTestnet',
token: '0x5D684d37922dAf7Aa2013E65A22880a11C475e25',
discountPercentOff: 0.2
},
{ network: 'SepoliaTestnet', token: '0xa4fCE8264370437e718aE207805b4e6233638b9E' },
{ network: 'SepoliaTestnet', token: '0xE50d86c6dE38F9754f6777d2925377564Bf79482' },
{
network: 'PolygonTestnet',
token: '0xd445cAAbb9eA6685D3A512439256866563a16E93',
discountPercentOff: 0.2
},
{ network: 'PolygonTestnet', token: '0x8f8b1972eea072C3C228EbE8f9FEADe03927D70F' },
{ network: 'PolygonTestnet', token: '0x70BE8802e2F3C6652B7e0814B478f66Ec52d9d88' },
{ network: 'SolanaDevnet', token: '4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU' }
]
}
},
successUrl: 'http://localhost:5173/success/{CHECKOUT_SESSION_ID}/?provider=radom',
cancelUrl: 'http://localhost:5173/cart/?session_id={CHECKOUT_SESSION_ID}&provider=radom',
chargeCustomerNetworkFee: true,
customizations: { allowDiscountCodes: true }
};

test('Converts request body to Radom checkout session data', async () => {
expect(await formatCheckoutSessionParams(requestBody, radomAdapter)).toMatchObject(
expectedRadomCheckoutSessionData
);
});
});
Loading