-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
239 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import path from 'path'; | ||
|
||
import { database } from '~/database'; | ||
|
||
export function mockedCheckoutEndpoints(server: FastifyInstance): void { | ||
server.get( | ||
'/mocked/checkout/:paymentId', | ||
{ | ||
schema: { hide: true }, | ||
}, | ||
async (request, reply) => { | ||
const params = request.params as { paymentId?: string }; | ||
if (!params.paymentId) { | ||
return reply.code(400).send({ | ||
error: 'Missing paymentId', | ||
}); | ||
} | ||
|
||
const query = request.query as { redirect_url?: string }; | ||
if (!query.redirect_url) { | ||
return reply.code(400).send({ | ||
error: 'Missing redirect_url', | ||
}); | ||
} | ||
|
||
const payment = await database.payments.findOne( | ||
{ _id: params.paymentId }, | ||
{ populate: ['customer', 'customer.project'] }, | ||
); | ||
if (!payment) { | ||
return reply.code(404).send({ | ||
error: 'Payment not found', | ||
}); | ||
} | ||
|
||
if (payment.customer.project.paymentProvider !== 'mocked') { | ||
return reply.code(404).send({ | ||
error: 'Payment not found', // don't leak that we are in dev mode | ||
}); | ||
} | ||
|
||
await reply.view(path.join('templates', 'mocked-checkout.hbs'), { payment, redirect_url: query.redirect_url }); | ||
}, | ||
); | ||
|
||
server.post('/mocked/checkout/:paymentId', { | ||
schema: { | ||
hide: true, | ||
summary: 'Do a checkout for a payment in development mode', | ||
tags: ['dev'], | ||
params: { | ||
type: 'object', | ||
required: ['paymentId'], | ||
additionalProperties: false, | ||
properties: { | ||
paymentId: { type: 'string' }, | ||
}, | ||
}, | ||
body: { | ||
type: 'object', | ||
required: ['status', 'redirect_url'], | ||
additionalProperties: false, | ||
properties: { | ||
status: { type: 'string' }, | ||
redirect_url: { type: 'string' }, | ||
}, | ||
}, | ||
|
||
response: { | ||
200: { | ||
$ref: 'Customer', | ||
}, | ||
400: { | ||
$ref: 'ErrorResponse', | ||
}, | ||
500: { | ||
$ref: 'ErrorResponse', | ||
}, | ||
}, | ||
}, | ||
handler: async (request, reply) => { | ||
const params = request.params as { paymentId?: string }; | ||
if (!params.paymentId) { | ||
return reply.code(400).send({ | ||
error: 'Missing paymentId', | ||
}); | ||
} | ||
|
||
const body = request.body as { redirect_url?: string; status?: 'paid' | 'failed' }; | ||
if (!body.redirect_url) { | ||
return reply.code(400).send({ | ||
error: 'Missing redirect_url', | ||
}); | ||
} | ||
if (!body.status) { | ||
return reply.code(400).send({ | ||
error: 'Missing status', | ||
}); | ||
} | ||
|
||
const payment = await database.payments.findOne( | ||
{ _id: params.paymentId }, | ||
{ populate: ['customer', 'customer.project'] }, | ||
); | ||
if (!payment) { | ||
return reply.code(404).send({ | ||
error: 'Payment not found', | ||
}); | ||
} | ||
|
||
if (payment.customer.project.paymentProvider !== 'mocked') { | ||
return reply.code(404).send({ | ||
error: 'Payment not found', // don't leak that we are in dev mode | ||
}); | ||
} | ||
|
||
const { project } = payment.customer; | ||
|
||
const response = await server.inject({ | ||
method: 'POST', | ||
headers: { | ||
authorization: `Bearer ${project.apiToken}`, | ||
}, | ||
url: `/payment/webhook/${project._id}`, | ||
payload: { | ||
paymentId: payment._id, | ||
paymentStatus: body.status, | ||
paidAt: new Date().toISOString(), | ||
}, | ||
}); | ||
|
||
if (response.statusCode !== 200) { | ||
return reply.view(path.join('templates', 'mocked-checkout.hbs'), { | ||
payment, | ||
redirect_url: body.redirect_url, | ||
error: 'Payment webhook failed', | ||
}); | ||
} | ||
|
||
await reply.redirect(body.redirect_url); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,65 @@ | ||
import { config } from '~/config'; | ||
import { Customer, Payment, PaymentMethod, Project } from '~/entities'; | ||
import { PaymentProvider } from '~/payment_providers/types'; | ||
|
||
export let customers: Customer[] = []; | ||
export const payments: { paymentId: string; status: string; customerId: string }[] = []; | ||
|
||
export class Mocked implements PaymentProvider { | ||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async chargeForegroundPayment({ | ||
payment, | ||
redirectUrl, | ||
}: { | ||
project: Project; | ||
payment: Payment; | ||
redirectUrl: string; | ||
}): Promise<{ checkoutUrl: string }> { | ||
const customer = customers.find((c) => c._id === payment.customer._id); | ||
if (!customer) { | ||
throw new Error('No customer'); | ||
} | ||
|
||
payments.push({ | ||
paymentId: payment._id, | ||
customerId: customer?._id, | ||
status: 'pending', | ||
}); | ||
|
||
const checkoutUrl = 'http://localhost:3000/checkout'; | ||
const checkoutUrl = `${config.publicUrl}/mocked/checkout/${payment._id}?redirect_url=${redirectUrl}`; | ||
|
||
return { checkoutUrl }; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async chargeBackgroundPayment({ payment }: { project: Project; payment: Payment }): Promise<void> { | ||
const customer = customers.find((c) => c._id === payment.customer._id); | ||
if (!customer) { | ||
throw new Error('No customer'); | ||
} | ||
|
||
payments.push({ | ||
paymentId: payment._id, | ||
customerId: customer?._id, | ||
status: 'pending', | ||
}); | ||
// | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async parsePaymentWebhook( | ||
payload: unknown, | ||
): Promise<{ paymentId: string; paidAt: Date | undefined; paymentStatus: 'pending' | 'paid' | 'failed' }> { | ||
const { id: paymentId } = payload as { id: string }; | ||
const { paymentId, paymentStatus, paidAt } = payload as { | ||
paymentStatus: 'pending' | 'paid' | 'failed'; | ||
paidAt: string; | ||
paymentId: string; | ||
}; | ||
|
||
return { | ||
paymentStatus: 'paid', | ||
paidAt: new Date(), | ||
paymentStatus, | ||
paidAt: new Date(paidAt), | ||
paymentId, | ||
}; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async createCustomer(customer: Customer): Promise<Customer> { | ||
customers.push(customer); | ||
|
||
return customer; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async updateCustomer(customer: Customer): Promise<Customer> { | ||
customers = customers.map((c) => (c._id === customer._id ? customer : c)); | ||
return customer; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async deleteCustomer(customer: Customer): Promise<void> { | ||
customers = customers.filter((c) => c._id !== customer._id); | ||
// | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars | ||
async getPaymentMethod(paymentId: string): Promise<PaymentMethod> { | ||
return new PaymentMethod({ | ||
paymentProviderId: 'mocked-123', | ||
type: 'mocked', | ||
name: 'mocked', | ||
name: `test **${Date.now().toString().slice(-2)}`, | ||
paymentProviderId: '123', | ||
type: 'credit_card', | ||
}); | ||
} | ||
} |
Oops, something went wrong.