mirror of
https://github.com/haveno-dex/haveno-ts.git
synced 2024-12-26 15:59:30 -05:00
convert form id to payment method string with validation
This commit is contained in:
parent
9fd95e81f6
commit
0266147d74
@ -1040,8 +1040,8 @@ test("Can get payment accounts (CI)", async () => {
|
|||||||
// TODO: FieldId represented as number
|
// TODO: FieldId represented as number
|
||||||
test("Can validate payment account forms (CI, sanity check)", async () => {
|
test("Can validate payment account forms (CI, sanity check)", async () => {
|
||||||
|
|
||||||
// supported payment methods
|
// expected payment methods
|
||||||
const expectedPaymentMethods = ["BLOCK_CHAINS", "REVOLUT", "SEPA", "SEPA_INSTANT", "TRANSFERWISE", "ZELLE", "SWIFT", "F2F", "STRIKE", "MONEY_GRAM", "FASTER_PAYMENTS", "UPHOLD", "PAXUM", "PAY_BY_MAIL"];
|
const expectedPaymentMethods = Object.keys(PaymentAccountForm.FormId);
|
||||||
|
|
||||||
// get payment methods
|
// get payment methods
|
||||||
const paymentMethods = await user1.getPaymentMethods();
|
const paymentMethods = await user1.getPaymentMethods();
|
||||||
@ -1097,7 +1097,7 @@ test("Can validate payment account forms (CI, sanity check)", async () => {
|
|||||||
test("Can create fiat payment accounts (CI)", async () => {
|
test("Can create fiat payment accounts (CI)", async () => {
|
||||||
|
|
||||||
// get payment account form
|
// get payment account form
|
||||||
const paymentMethodId = 'REVOLUT';
|
const paymentMethodId = HavenoUtils.getPaymentMethodId(PaymentAccountForm.FormId.REVOLUT);
|
||||||
const accountForm = await user1.getPaymentAccountForm(paymentMethodId);
|
const accountForm = await user1.getPaymentAccountForm(paymentMethodId);
|
||||||
|
|
||||||
// edit form
|
// edit form
|
||||||
@ -3296,8 +3296,8 @@ function getCryptoAddress(currencyCode: string): string|undefined {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createPaymentAccount(trader: HavenoClient, assetCodes: string, paymentMethodId?: string) {
|
async function createPaymentAccount(trader: HavenoClient, assetCodes: string, paymentMethodId?: string | PaymentAccountForm.FormId) {
|
||||||
if (!paymentMethodId) paymentMethodId = isCrypto(assetCodes!) ? "block_chains" : "revolut";
|
if (!paymentMethodId) paymentMethodId = isCrypto(assetCodes!) ? PaymentAccountForm.FormId.BLOCK_CHAINS : PaymentAccountForm.FormId.REVOLUT;
|
||||||
const accountForm = await trader.getPaymentAccountForm(paymentMethodId);
|
const accountForm = await trader.getPaymentAccountForm(paymentMethodId);
|
||||||
for (const field of accountForm.getFieldsList()) field.setValue(getValidFormInput(accountForm, field.getId()));
|
for (const field of accountForm.getFieldsList()) field.setValue(getValidFormInput(accountForm, field.getId()));
|
||||||
HavenoUtils.setFormValue(PaymentAccountFormField.FieldId.TRADE_CURRENCIES, assetCodes, accountForm);
|
HavenoUtils.setFormValue(PaymentAccountFormField.FieldId.TRADE_CURRENCIES, assetCodes, accountForm);
|
||||||
|
@ -860,11 +860,12 @@ export default class HavenoClient {
|
|||||||
/**
|
/**
|
||||||
* Get a form for the given payment method to complete and create a new payment account.
|
* Get a form for the given payment method to complete and create a new payment account.
|
||||||
*
|
*
|
||||||
* @param {string} paymentMethodId - the id of the payment method
|
* @param {string | PaymentAccountForm.FormId} paymentMethodId - the id of the payment method
|
||||||
* @return {PaymentAccountForm} the payment account form
|
* @return {PaymentAccountForm} the payment account form
|
||||||
*/
|
*/
|
||||||
async getPaymentAccountForm(paymentMethodId: string): Promise<PaymentAccountForm> {
|
async getPaymentAccountForm(paymentMethodId: string | PaymentAccountForm.FormId): Promise<PaymentAccountForm> {
|
||||||
try {
|
try {
|
||||||
|
paymentMethodId = HavenoUtils.getPaymentMethodId(paymentMethodId); // validate and normalize
|
||||||
return (await this._paymentAccountsClient.getPaymentAccountForm(new GetPaymentAccountFormRequest().setPaymentMethodId(paymentMethodId), {password: this._password})).getPaymentAccountForm()!;
|
return (await this._paymentAccountsClient.getPaymentAccountForm(new GetPaymentAccountFormRequest().setPaymentMethodId(paymentMethodId), {password: this._password})).getPaymentAccountForm()!;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new HavenoError(e.message, e.code);
|
throw new HavenoError(e.message, e.code);
|
||||||
|
@ -74,53 +74,6 @@ export default class HavenoUtils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Stringify a payment account form.
|
|
||||||
*
|
|
||||||
* @param form - form to stringify
|
|
||||||
* @return {string} the stringified form
|
|
||||||
*/
|
|
||||||
static formToString(form: PaymentAccountForm): string {
|
|
||||||
let str = "";
|
|
||||||
for (const field of form.getFieldsList()) {
|
|
||||||
str += field.getId() + ": " + this.getFormValue(form, field.getId()) + "\n";
|
|
||||||
}
|
|
||||||
return str.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a form field value.
|
|
||||||
*
|
|
||||||
* @param {PaymentAccountForm} form - form to get the field value from
|
|
||||||
* @param {PaymentAccountFormField.FieldId} fieldId - id of the field to get the value from
|
|
||||||
* @return {string} the form field value
|
|
||||||
*/
|
|
||||||
// TODO: attach getter and setter to PaymentAccountForm prototype in typescript?
|
|
||||||
static getFormValue(form: PaymentAccountForm, fieldId: PaymentAccountFormField.FieldId): string {
|
|
||||||
for (const field of form.getFieldsList()) {
|
|
||||||
if (field.getId() === fieldId) return field.getValue();
|
|
||||||
}
|
|
||||||
throw new Error("PaymentAccountForm does not have field " + fieldId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a form field value.
|
|
||||||
*
|
|
||||||
* @param {PaymentAccountFormField.FieldId} fieldId - id of the field to set the value of
|
|
||||||
* @param {string} value - field value to set
|
|
||||||
* @param {PaymentAccountForm} form - form to get the field from
|
|
||||||
* @return {string} the form field value
|
|
||||||
*/
|
|
||||||
static setFormValue(fieldId: PaymentAccountFormField.FieldId, value: string, form: PaymentAccountForm): void {
|
|
||||||
for (const field of form.getFieldsList()) {
|
|
||||||
if (field.getId() === fieldId) {
|
|
||||||
field.setValue(value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error("PaymentAccountForm does not have field " + fieldId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait for the duration.
|
* Wait for the duration.
|
||||||
*
|
*
|
||||||
@ -172,4 +125,80 @@ export default class HavenoUtils {
|
|||||||
const remainder: bigint = amountAtomicUnits as bigint % HavenoUtils.AU_PER_XMR;
|
const remainder: bigint = amountAtomicUnits as bigint % HavenoUtils.AU_PER_XMR;
|
||||||
return Number(quotient) + Number(remainder) / Number(HavenoUtils.AU_PER_XMR);
|
return Number(quotient) + Number(remainder) / Number(HavenoUtils.AU_PER_XMR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------- PAYMENT ACCOUNT FORMS --------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a validated payment method id from a string or form id.
|
||||||
|
*
|
||||||
|
* @param {string | PaymentAccountForm.FormId} id - identifies the payment method
|
||||||
|
* @returns {string} the payment method id
|
||||||
|
*/
|
||||||
|
static getPaymentMethodId(id: string | PaymentAccountForm.FormId) {
|
||||||
|
if (typeof id === "string") {
|
||||||
|
id = id.toUpperCase();
|
||||||
|
if (!(id in PaymentAccountForm.FormId)) throw Error("Invalid payment method: " + id);
|
||||||
|
return id;
|
||||||
|
} else {
|
||||||
|
let keyByValue = getKeyByValue(PaymentAccountForm.FormId, id);
|
||||||
|
if (!keyByValue) throw Error("No payment method id with form id " + id);
|
||||||
|
return keyByValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stringify a payment account form.
|
||||||
|
*
|
||||||
|
* @param form - form to stringify
|
||||||
|
* @return {string} the stringified form
|
||||||
|
*/
|
||||||
|
static formToString(form: PaymentAccountForm): string {
|
||||||
|
let str = "";
|
||||||
|
for (const field of form.getFieldsList()) {
|
||||||
|
str += field.getId() + ": " + this.getFormValue(form, field.getId()) + "\n";
|
||||||
|
}
|
||||||
|
return str.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a form field value.
|
||||||
|
*
|
||||||
|
* @param {PaymentAccountForm} form - form to get the field value from
|
||||||
|
* @param {PaymentAccountFormField.FieldId} fieldId - id of the field to get the value from
|
||||||
|
* @return {string} the form field value
|
||||||
|
*/
|
||||||
|
// TODO: attach getter and setter to PaymentAccountForm prototype in typescript?
|
||||||
|
static getFormValue(form: PaymentAccountForm, fieldId: PaymentAccountFormField.FieldId): string {
|
||||||
|
for (const field of form.getFieldsList()) {
|
||||||
|
if (field.getId() === fieldId) return field.getValue();
|
||||||
|
}
|
||||||
|
throw new Error("PaymentAccountForm does not have field " + fieldId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a form field value.
|
||||||
|
*
|
||||||
|
* @param {PaymentAccountFormField.FieldId} fieldId - id of the field to set the value of
|
||||||
|
* @param {string} value - field value to set
|
||||||
|
* @param {PaymentAccountForm} form - form to get the field from
|
||||||
|
* @return {string} the form field value
|
||||||
|
*/
|
||||||
|
static setFormValue(fieldId: PaymentAccountFormField.FieldId, value: string, form: PaymentAccountForm): void {
|
||||||
|
for (const field of form.getFieldsList()) {
|
||||||
|
if (field.getId() === fieldId) {
|
||||||
|
field.setValue(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error("PaymentAccountForm does not have field " + fieldId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getKeyByValue(object: any, value: number): string | undefined {
|
||||||
|
for (const key in object) {
|
||||||
|
if (object.hasOwnProperty(key) && object[key] === value) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user