Skip to content

Commit

Permalink
Merge pull request #95 from mysteriumnetwork/v2-payment-gateways
Browse files Browse the repository at this point in the history
Replace v1 payment with v2 gateways API; modularize
  • Loading branch information
tadaskay authored Oct 7, 2021
2 parents 012bb79 + 6661750 commit 5302c48
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 418 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,19 @@ export {
export { TEQUILAPI_SSE_URL, SSEResponse, SSEEventType, parseSSEResponse, AppState } from './sse/sse'

export { logger, Logger } from './logger'
export { TEQUILAPI_URL, TequilapiClient, HttpTequilapiClient } from './tequilapi-client'
export { TEQUILAPI_URL, TequilapiClient } from './tequilapi-client'
export { TequilapiClientFactory } from './tequilapi-client-factory'
export { TequilapiError, AxiosError } from './tequilapi-error'

export { MMNReport, MMNApiKeyResponse, MMNReportResponse } from './mmn/mmn'
export { Pageable } from './common/pageable'

export {
PaymentAPI,
Money,
PaymentOrderResponse,
PaymentOrderRequest,
PaymentOrderOptionsResponse,
CreatePaymentOrderRequest,
PaymentOrder,
PaymentGateway,
} from './payment'
export { EntertainmentEstimateQuery, EntertainmentEstimateResponse } from './payment/entertainment'
export { ReferralTokenResponse } from './referral'
Expand Down
80 changes: 59 additions & 21 deletions src/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,72 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { HttpInterface } from '../http/interface'

export interface PaymentOrderRequest {
lightningNetwork: boolean
mystAmount: number
export interface Money {
amount: number
currency: string
}

export interface PaymentGateway {
currencies: string[]
name: string
orderOptions: {
minimum: number
suggested: number[]
}
}

export interface CreatePaymentOrderRequest {
mystAmount: string
payCurrency: string
gatewayCallerData: {
country?: string
lightningNetwork?: boolean
}
}

export interface PaymentOrderResponse {
id: number
export interface PaymentOrder {
id: string
status: string
identity: string
mystAmount: number
payAmount: number
gatewayName: string
receiveMyst: string
payAmount: string
payCurrency: string
paymentAddress: string
paymentUrl: string
priceAmount: number
priceCurrency: string
receiveAmount: number
receiveCurrency: string
status: string
publicGatewayData?: {
secureForm?: string
createdAt?: Date
expireAt?: Date
lightningNetwork?: boolean
paymentAddress?: string
paymentUrl?: string
}
}

export interface PaymentOrderOptionsResponse {
minimum: number
suggested: number[]
}
export class PaymentAPI {
private http: HttpInterface
constructor(http: HttpInterface) {
this.http = http
}

export interface Money {
amount: number
currency: string
public async gateways(): Promise<PaymentGateway[]> {
return await this.http.get('/v2/payment-order-gateways')
}

public async createOrder(
id: string,
gateway: string,
req: CreatePaymentOrderRequest
): Promise<PaymentOrder> {
return await this.http.post(`/v2/identities/${id}/${gateway}/payment-order`, req)
}

public async orders(id: string): Promise<PaymentOrder[]> {
return await this.http.get(`/v2/identities/${id}/payment-order`)
}

public async order(id: string, orderId: string): Promise<PaymentOrder> {
return await this.http.get(`/v2/identities/${id}/payment-order/${orderId}`)
}
}
3 changes: 1 addition & 2 deletions src/tequilapi-client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { HttpInterface } from './http/interface'
import axios, { AxiosInstance } from 'axios'
import { AxiosAdapter } from './http/axios-adapter'
import {
HttpTequilapiClient,
pathConfig,
pathConfigDefault,
pathConfigUser,
Expand All @@ -32,7 +31,7 @@ export class TequilapiClientFactory {
if (!adapter) {
adapter = this.buildAdapter()
}
return new HttpTequilapiClient(adapter)
return new TequilapiClient(adapter)
}

public axiosInstance(): AxiosInstance {
Expand Down
28 changes: 26 additions & 2 deletions src/tequilapi-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import MockAdapter from 'axios-mock-adapter'
import { AxiosAdapter } from './http/axios-adapter'
import { HttpTequilapiClient, TequilapiClient } from './tequilapi-client'
import { TequilapiClient } from './tequilapi-client'
import { parseIdentityRef } from './identity/identity'
import { parseServiceInfo, parseServiceListResponse } from './provider/service-info'
import { TequilapiClientFactory } from './tequilapi-client-factory'
Expand All @@ -21,7 +21,7 @@ describe('HttpTequilapiClient', () => {
beforeEach(() => {
const clientFactory = new TequilapiClientFactory()
const axios = clientFactory.axiosInstance()
api = new HttpTequilapiClient(new AxiosAdapter(axios))
api = new TequilapiClient(new AxiosAdapter(axios))
mock = new MockAdapter(axios)
})

Expand Down Expand Up @@ -905,4 +905,28 @@ describe('HttpTequilapiClient', () => {
expect(res.data['dashes-and_underscores']).toEqual(true)
})
})
describe('paymentGateways()', () => {
it('send payment gateways', async () => {
mock.onGet('v2/payment-order-gateways').reply(200, [
{
currencies: ['BTC', 'BCH', 'DAI', 'ETH', 'LTC', 'USDT', 'MYST', 'DOGE'],
name: 'coingate',
order_options: {
minimum: 10.7,
suggested: [20, 40, 90, 120, 150, 240],
},
},
{
currencies: ['EUR', 'USD', 'GBP'],
name: 'cardinity',
order_options: {
minimum: 2.15,
suggested: [10, 20, 45, 60, 75, 120],
},
},
])
const res = await api.payment.gateways()
expect(res).toHaveLength(2)
})
})
})
43 changes: 7 additions & 36 deletions src/tequilapi-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ import {
} from './transactor/settlement'
import { IdentityCurrentRequest } from './identity/selection'
import { AuthRequest, AuthResponse, ChangePasswordRequest } from './auth/auth'
import {
Money,
PaymentOrderOptionsResponse,
PaymentOrderRequest,
PaymentOrderResponse,
} from './payment'
import { Money, PaymentAPI } from './payment'
import { ReferralTokenResponse, ReferralTokenRewardsResponse } from './referral'
import { CurrentPricesResponse } from './prices'
import { parsePayoutAddressResponse, Payout } from './identity/payout'
Expand All @@ -83,7 +78,7 @@ export const pathConfig = 'config'
export const pathConfigUser = 'config/user'
export const pathConfigDefault = 'config/default'

export interface TequilapiClient {
export interface BaseTequilapiClient {
healthCheck(timeout?: number): Promise<NodeHealthcheck>
natStatus(): Promise<NatStatusResponse>
nodeMonitoringStatus(): Promise<NodeMonitoringStatusResponse>
Expand Down Expand Up @@ -158,11 +153,6 @@ export interface TequilapiClient {
getMMNApiKey(): Promise<MMNApiKeyResponse>
clearMMNApiKey(): Promise<void>

createPaymentOrder(identity: string, request: PaymentOrderRequest): Promise<PaymentOrderResponse>
getPaymentOrders(identity: string): Promise<PaymentOrderResponse[]>
getPaymentOrder(identity: string, orderId: number): Promise<PaymentOrderResponse>
getPaymentOrderOptions(): Promise<PaymentOrderOptionsResponse>
getPaymentOrderCurrencies(): Promise<string[]>
exchangeRate(quoteCurrency?: string): Promise<Money>
estimateEntertainment(query: EntertainmentEstimateQuery): Promise<EntertainmentEstimateResponse>

Expand All @@ -171,11 +161,13 @@ export interface TequilapiClient {
validateEthRPCL2(rpcUrls: string[], timeout?: number): Promise<void>
}

export class HttpTequilapiClient implements TequilapiClient {
class BaseHttpTequilapiClient implements BaseTequilapiClient {
public http: HttpInterface
public readonly payment: PaymentAPI

public constructor(http: HttpInterface) {
this.http = http
this.payment = new PaymentAPI(http)
}

public async healthCheck(timeout?: number): Promise<NodeHealthcheck> {
Expand Down Expand Up @@ -562,29 +554,6 @@ export class HttpTequilapiClient implements TequilapiClient {
return this.http.delete(`mmn/api-key`)
}

public async createPaymentOrder(
identity: string,
request: PaymentOrderRequest
): Promise<PaymentOrderResponse> {
return this.http.post(`/identities/${identity}/payment-order`, request)
}

public async getPaymentOrders(identity: string): Promise<PaymentOrderResponse[]> {
return this.http.get(`/identities/${identity}/payment-order`)
}

public async getPaymentOrder(identity: string, orderId: number): Promise<PaymentOrderResponse> {
return this.http.get(`/identities/${identity}/payment-order/${orderId}`)
}

public async getPaymentOrderOptions(): Promise<PaymentOrderOptionsResponse> {
return this.http.get(`/payment-order-options`)
}

public async getPaymentOrderCurrencies(): Promise<string[]> {
return this.http.get(`/payment-order-currencies`)
}

public async getReferralToken(identity: string): Promise<ReferralTokenResponse> {
return this.http.get(`/identities/${identity}/referral`)
}
Expand All @@ -611,3 +580,5 @@ export class HttpTequilapiClient implements TequilapiClient {
return this.http.get(`/transactor/token/${token}/reward`)
}
}

export class TequilapiClient extends BaseHttpTequilapiClient {}
Loading

0 comments on commit 5302c48

Please sign in to comment.