Skip to content

Commit

Permalink
feat: Add address in Reacher license (#266)
Browse files Browse the repository at this point in the history
* feat: Add address in Reacher license

* ncu

* Fix ts error
  • Loading branch information
amaury1093 authored Jul 4, 2022
1 parent 17ac05a commit 0576dc2
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 384 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"@geist-ui/react-icons": "^1.0.1",
"@reacherhq/api": "^0.3.2",
"@sentry/nextjs": "^6.19.7",
"@stripe/stripe-js": "^1.29.0",
"@supabase/supabase-js": "^1.24.0",
"@stripe/stripe-js": "^1.32.0",
"@supabase/supabase-js": "^1.35.4",
"@types/cors": "^2.8.12",
"@types/mailgun-js": "^0.22.12",
"@types/markdown-pdf": "^9.0.1",
"@types/mustache": "^4.1.2",
"@types/mustache": "^4.1.3",
"@types/react": "^17.0.43",
"@types/react-dom": "^18.0.3",
"@types/request-ip": "^0.0.37",
Expand All @@ -33,17 +33,17 @@
"mailgun-js": "^0.22.0",
"markdown-pdf": "^10.0.0",
"mustache": "^4.2.0",
"next": "^12.1.6",
"next": "^12.2.0",
"rate-limiter-flexible": "^2.3.7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"request-ip": "^2.1.3",
"stripe": "^9.2.0"
"stripe": "^9.11.0"
},
"devDependencies": {
"@amaurym/config": "^1.3.6",
"eslint-config-next": "^12.1.0",
"typescript": "^4.6.4",
"eslint-config-next": "^12.2.0",
"typescript": "^4.7.4",
"vercel": "^24.2.4"
}
}
5 changes: 3 additions & 2 deletions scripts/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ async function main(): Promise<void> {

if (myArgs.length < 2 || myArgs.length > 3) {
console.error(
'Usage: yarn gen:license [name] [email] [optional_start_date]'
'Usage: yarn gen:license [name] [email] [address] [optional_start_date]'
);
process.exit(1);
}

const startDate = myArgs[2] ? new Date(myArgs[2]) : new Date();
const startDate = myArgs[3] ? new Date(myArgs[3]) : new Date();

// Generate with dummy data.
const pdf = await generateLicense({
Expand All @@ -25,6 +25,7 @@ async function main(): Promise<void> {
stripe_buy_date: startDate,
stripe_buyer_name: myArgs[0],
stripe_buyer_email: myArgs[1],
stripe_buyer_address: myArgs[2],
});

const path = `${tmpdir()}/${pdf.filename}`;
Expand Down
40 changes: 36 additions & 4 deletions src/pages/api/stripe/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ const webhookHandler = async (
return;
}

const customerName =
invoice.customer_name || 'Reacher customer';
if (!invoice.customer_name) {
throw new Error(
'customer_name is empty in invoice'
);
}

// Generate PDF with the given info.
const stripeBuyDate = new Date(invoice.created * 1000);
Expand All @@ -150,8 +153,11 @@ const webhookHandler = async (
license_end_date: licenseEndDate,
number_devs: 8,
stripe_buy_date: stripeBuyDate,
stripe_buyer_name: customerName,
stripe_buyer_name: invoice.customer_name,
stripe_buyer_email: invoice.customer_email,
stripe_buyer_address: stripeAddressToString(
invoice.customer_address
),
});

// Send the email with the attached PDF.
Expand All @@ -162,7 +168,7 @@ const webhookHandler = async (
stripeBuyDate,
'dd/MM/yyyy'
)} to ${format(licenseEndDate, 'dd/MM/yyyy')}`,
text: `Hello ${customerName},
text: `Hello ${invoice.customer_name},
Thank you for using Reacher. You will find attached the Commercial License for the period of ${format(
stripeBuyDate,
Expand Down Expand Up @@ -211,4 +217,30 @@ Amaury`,
}
};

/**
* Convert a Stripe.Address object to string representing the address.
*/
function stripeAddressToString(addr: Stripe.Address | null): string {
if (
!addr ||
!addr.line1 ||
!addr.postal_code ||
!addr.city ||
!addr.country
) {
throw new Error(`Got invalid address: ${JSON.stringify(addr)}`);
}

return [
addr.line1,
addr.line2,
addr.postal_code,
addr.city,
addr.state,
addr.country,
]
.filter((x) => !!x)
.join(', ');
}

export default withSentry(webhookHandler);
8 changes: 6 additions & 2 deletions src/util/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mdPdf from 'markdown-pdf';
import { render } from 'mustache';

const LICENSE_TEMPLATE =
'https://raw.githubusercontent.com/reacherhq/policies/master/license/commercial.md';
'https://raw.githubusercontent.com/reacherhq/policies/master/license/commercial.en.md';

export interface LicenseMetadata {
/**
Expand All @@ -31,6 +31,10 @@ export interface LicenseMetadata {
* The date of the purchase.
*/
stripe_buy_date: Date;
/**
* The address of the buyer.
*/
stripe_buyer_address: string;
/**
* The formatted name of the buyer.
*/
Expand All @@ -57,7 +61,7 @@ export async function generateLicense(
const filledMd = render(template, {
...metadata,
license_end_date: format(metadata.license_end_date, 'MMMM dd yyyy'),
number_devs: '8 (eight) Licensed developers', // For now we hardcode to 8.
number_devs: '8 (eight)', // For now we hardcode to 8.
stripe_buy_date: format(metadata.stripe_buy_date, 'MMMM dd yyyy'),
});

Expand Down
7 changes: 4 additions & 3 deletions src/util/useUser.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
ApiError,
Provider,
Session,
User,
Expand All @@ -23,18 +24,18 @@ interface UserContext {
user: User | null;
provider?: Provider;
url?: string | null;
error: Error | null;
error: ApiError | null;
}>;
resetPassword: (
email: string
) => Promise<{ data: unknown | null; error: Error | null }>;
) => Promise<{ data: unknown | null; error: ApiError | null }>;
signOut: () => Promise<void>;
signUp: (options: UserCredentials) => Promise<{
session: Session | null;
user: User | null;
provider?: Provider;
url?: string | null;
error: Error | null;
error: ApiError | null;
}>;
subscription: SupabaseSubscription | null;
user: User | null;
Expand Down
Loading

0 comments on commit 0576dc2

Please sign in to comment.