-
Notifications
You must be signed in to change notification settings - Fork 1
add createCheckout backed by recaptcha and honeypot #23
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| 'use strict'; | ||
|
|
||
| const extrovert = require('extrovert'); | ||
|
|
||
| module.exports = extrovert.toNetlifyFunction(require('../../src/actions/createCheckout')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| 'use strict'; | ||
|
|
||
| const Archetype = require('archetype'); | ||
| const connect = require('../db'); | ||
| const recaptcha = require('../integrations/recaptcha'); | ||
| const stripe = require('../integrations/stripe'); | ||
|
|
||
| const CreateCheckoutParams = new Archetype({ | ||
| name: { | ||
| $type: 'string', | ||
| $required: true | ||
| }, | ||
| email: { | ||
| $type: 'string', | ||
| $required: true | ||
| }, | ||
| plan: { | ||
| $type: 'string', | ||
| $required: true, | ||
| $enum: ['solo', 'pro'] | ||
| }, | ||
| website: { | ||
| $type: 'string' | ||
| }, | ||
| response: { | ||
| $type: 'string' | ||
| } | ||
|
vkarpov15 marked this conversation as resolved.
|
||
| }).compile('CreateCheckoutParams'); | ||
|
|
||
| const priceIds = { | ||
| solo: process.env.STRIPE_SOLO_PRICE_ID, | ||
| pro: process.env.STRIPE_PRO_PRICE_ID | ||
| }; | ||
|
|
||
| module.exports = async function createCheckout(params) { | ||
| const { name, email, plan, website, response } = new CreateCheckoutParams(params); | ||
|
|
||
| if (website) { | ||
| throw new Error('This request was flagged as spam. If this is an error, please refresh and try again.'); | ||
| } | ||
|
|
||
| const recaptchaResult = await recaptcha.verify(response); | ||
| console.log(recaptchaResult); | ||
|
vkarpov15 marked this conversation as resolved.
|
||
| if (!recaptchaResult.success || recaptchaResult.score < 0.7 || recaptchaResult.action !== 'checkout') { | ||
| throw new Error('Captcha verification failed'); | ||
| } | ||
|
vkarpov15 marked this conversation as resolved.
|
||
|
|
||
| const db = await connect(); | ||
| const { Contact } = db.models; | ||
|
|
||
| await Contact.findOneAndUpdate( | ||
| { email: email.toLowerCase() }, | ||
| { $set: { name }, $setOnInsert: { email: email.toLowerCase() } }, | ||
| { upsert: true } | ||
| ); | ||
|
|
||
| const priceId = priceIds[plan]; | ||
| if (!priceId) { | ||
| throw new Error('Price not configured for plan: ' + plan); | ||
| } | ||
|
|
||
| const returnUrl = (process.env.PUBLIC_APP_BASE_URL || 'https://studio.mongoosejs.io') + '/my-account.html'; | ||
|
|
||
| const session = await stripe.client.checkout.sessions.create({ | ||
| ui_mode: 'embedded', | ||
| mode: 'subscription', | ||
| customer_email: email, | ||
| line_items: [{ price: priceId, quantity: 1 }], | ||
|
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| return_url: returnUrl, | ||
| metadata: { name, plan } | ||
| }); | ||
|
|
||
| return { clientSecret: session.client_secret }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 'use strict'; | ||
|
|
||
| const mongoose = require('mongoose'); | ||
|
|
||
| const contactSchema = new mongoose.Schema({ | ||
| email: { type: String, required: true, unique: true }, | ||
| name: { type: String }, | ||
| company: { type: String }, | ||
| deletedAt: { type: Date, default: null } | ||
| }, { timestamps: true }); | ||
|
|
||
| module.exports = contactSchema; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 'use strict'; | ||
|
|
||
| const qs = require('querystring'); | ||
|
|
||
| const verifyUrl = 'https://www.google.com/recaptcha/api/siteverify'; | ||
|
|
||
| exports.verify = async function verify(response, remoteIp) { | ||
| if (!response) { | ||
| return { success: false, score: 0 }; | ||
| } | ||
|
|
||
| const secret = process.env.RECAPTCHA_SECRET_KEY; | ||
|
|
||
| const body = qs.stringify({ | ||
| secret, | ||
| response, | ||
| remoteip: remoteIp | ||
| }); | ||
|
vkarpov15 marked this conversation as resolved.
|
||
|
|
||
| const res = await fetch(verifyUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/x-www-form-urlencoded' | ||
| }, | ||
| body | ||
| }); | ||
|
|
||
| if (!res.ok) { | ||
| console.log('Error verifying reCAPTCHA', await res.text()); | ||
|
vkarpov15 marked this conversation as resolved.
|
||
| throw new Error('Failed to verify reCAPTCHA'); | ||
| } | ||
|
|
||
| const data = await res.json(); | ||
|
|
||
| return data; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.