You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.9 KiB
64 lines
1.9 KiB
2 years ago
|
import { Payment } from '../../lib/models.js';
|
||
|
import { API, developer_admin } from "../../lib/api.js";
|
||
|
import logging from '../../lib/logging.js';
|
||
|
import dayjs from 'dayjs';
|
||
|
import { product_id, fake_payments, register_enabled } from "../../client/config.js";
|
||
|
import assert from 'assert';
|
||
|
import * as queues from "../../lib/queues.js";
|
||
|
|
||
|
const log = logging.create("api/payments/fake.js");
|
||
|
|
||
|
const rules = {}; // TODO: have some kind of confirm key
|
||
|
|
||
|
export const post = async (req, res) => {
|
||
|
const api = new API(req, res);
|
||
|
|
||
|
if(!register_enabled) {
|
||
|
return api.error(500, {errors: { main: "Registration is disabled."}});
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
assert(developer_admin, "Fake payments api only available with developer_admin set to true.");
|
||
|
assert(fake_payments, "You are using FakePayment but have fake_payments in secrets/config.json");
|
||
|
|
||
|
const msg = api.validate(rules);
|
||
|
|
||
|
if(msg._valid) {
|
||
|
let internal_id = Payment.gen_internal_id();
|
||
|
|
||
|
// TODO: see if we can use date-fns instead of dayjs here
|
||
|
let payment = await Payment.insert({
|
||
|
user_id: api.user.id,
|
||
|
system: 'fake',
|
||
|
status: 'complete',
|
||
|
internal_id,
|
||
|
sys_primary_id: Payment.gen_internal_id(),
|
||
|
sys_secondary_id: Payment.gen_internal_id(),
|
||
|
sys_created_on: dayjs(msg.sys_created_on)
|
||
|
});
|
||
|
|
||
|
assert(payment && payment.internal_id, "Failed to save payment.");
|
||
|
|
||
|
let result = {
|
||
|
sys_primary_id: payment.sys_primary_id,
|
||
|
status: payment.status,
|
||
|
internal_id: payment.internal_id
|
||
|
}
|
||
|
|
||
|
queues.send_welcome(api.user);
|
||
|
queues.send_receipt(api.user, payment.id, product_id);
|
||
|
|
||
|
log.debug(result);
|
||
|
return api.reply(200, result);
|
||
|
} else {
|
||
|
log.error(msg._errors, "FakePayment validation failure");
|
||
|
return api.validation_error(res, msg);
|
||
|
}
|
||
|
} catch(error) {
|
||
|
log.error(error);
|
||
|
return api.error(403, 'FakePayment configuration failed');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
post.authenticated = true;
|