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

Add worker test projects #1674

Merged
merged 4 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/stripe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,4 +686,31 @@ describe('Stripe Module', function() {
expect(runTestProject.bind(null, 'mjs-ts')).to.not.throw();
});
});

describe('stripe.worker', function() {
if (process.versions.node < '16.13') {
console.log('Wrangler requires at least node.js v16.13.0, skipping test');
return;
}
const runTestCloudflareProject = (projectName: string): void => {
const script = `
cd testProjects/${projectName} &&
npm install &&
npm run build
`;
require('child_process').execSync(script);
};

it('should build successfully in Cloudflare Workers', function() {
expect(
runTestCloudflareProject.bind(null, 'cloudflare-worker')
).to.not.throw();
});

it('should build successfully in Cloudflare Pages Functions', function() {
expect(
runTestCloudflareProject.bind(null, 'cloudflare-pages')
).to.not.throw();
});
});
});
33 changes: 33 additions & 0 deletions testProjects/cloudflare-pages/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Stripe from 'stripe';

export async function onRequestPost({env, request}) {
const sig = request.headers.get('Stripe-Signature');
const body = await request.text();

const stripe = new Stripe(env.STRIPE_API_KEY, {
apiVersion: '2022-11-15',
httpClient: Stripe.createFetchHttpClient(),
});
const webCrypto = Stripe.createSubtleCryptoProvider();

try {
const event = await stripe.webhooks.constructEventAsync(
body, // raw request body
sig, // signature header
env.STRIPE_SIGNING_SECRET,
undefined,
webCrypto
);
} catch (err) {
console.error(err);
return new Response(`Error: ${err.message}`, {
status: 400,
});
}

return new Response(JSON.stringify({ received: true }), {
headers: {
"Content-Type": "application/json;charset=utf-8",
},
});
}
17 changes: 17 additions & 0 deletions testProjects/cloudflare-pages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "cloudflare-pages",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "wrangler pages functions build && rm _worker.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"stripe": "file:../../"
},
"devDependencies": {
"wrangler": "^2.9.0"
}
}
18 changes: 18 additions & 0 deletions testProjects/cloudflare-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "cloudflare-worker",
"version": "1.0.0",
"description": "",
"main": "./src/index.js",
"scripts": {
"build": "wrangler publish --dry-run"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"stripe": "file:../../"
},
"devDependencies": {
"wrangler": "^2.9.0"
}
}
38 changes: 38 additions & 0 deletions testProjects/cloudflare-worker/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Stripe from "stripe";

async function handleRequest(request, env) {
const stripe = Stripe(env.STRIPE_API_KEY, {
// Cloudflare Workers use the Fetch API for their API requests.
httpClient: Stripe.createFetchHttpClient()
});
/*
* Sample checkout integration which redirects a customer to a checkout page
* for the specified line items.
*
* See https://stripe.com/docs/payments/accept-a-payment?integration=checkout.
*/
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'T-shirt',
},
unit_amount: 2000,
},
quantity: 1,
},
],
mode: 'payment',
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
});

return Response.redirect(session.url, 303);
};

export default {
fetch: handleRequest
}
5 changes: 5 additions & 0 deletions testProjects/cloudflare-worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "my-stripe-worker"
main = "src/index.js"
compatibility_flags = []
compatibility_date = "2022-02-02"
workers_dev = true