Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple payments #2

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/interfaces/Payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export interface Payment {
method_id: UUID // PaymentMethod.id
status: PaymentStatus
amount: number
date: string
orders: {
id: UUID
amount: number
}[]
}

export interface PaymentCreateDto {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './PackagesTemplate'
export * from './Page'
export * from './Payments'
export * from './PaymentMethods'
export * from './Payments'
export { Permission, PermissionEntry, PermissionsTree } from './Permissions'
export * from './Product'
export * from './ProductSet'
Expand Down
15 changes: 7 additions & 8 deletions src/services/api/modules/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { createEntityAuditsService, EntityAuditsService } from './audits'
import { createOrderDocumentsService, OrderDocumentsService } from './ordersDocuments'
import { stringifyQueryParams } from '../../../utils'
import { FieldSort } from '../../../interfaces/Sort'
import { Payment } from '../../../interfaces/Payments'

export interface OrdersListParams extends SearchParam, PaginationParams, MetadataParams {
/**
Expand All @@ -60,7 +61,7 @@ export interface OrdersService extends EntityMetadataService, EntityAuditsServic
* Creates new payment for the given order
* @returns The payment URL to redirect the user to
*/
pay(orderCode: string, paymentMethodId: UUID, continueUrl: string): Promise<string>
pay(orderIds: UUID[], paymentMethodId: string, continueUrl: string): Promise<string>

/**
* Creates payment that is paid for the given order
Expand Down Expand Up @@ -126,15 +127,13 @@ export const createOrdersService: ServiceFactory<OrdersService> = (axios) => {
const paymentMethodsService = createPaymentMethodsService(axios)

return {
async pay(code, paymentMethodId, continueUrl) {
async pay(orderIds, paymentMethodId, continueUrl) {
const {
data: { data },
} = await axios.post<HeseyaResponse<OrderPayment>>(
`${route}/${code}/pay/id:${paymentMethodId}`,
{
continue_url: continueUrl,
},
)
} = await axios.post<HeseyaResponse<OrderPayment>>(`${route}/pay/id:${paymentMethodId}`, {
continue_url: continueUrl,
orders: orderIds.map((id) => ({ id })),
})

return data.redirect_url
},
Expand Down
6 changes: 3 additions & 3 deletions src/services/api/modules/tests/orders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { createOrdersService } from '../orders'
const dummyOrdersResponse: HeseyaResponse<OrderPayment> = {
data: {
id: '1',
external_id: '1',
method: 'payu',
method_id: null,
status: PaymentStatus.Successful,
amount: 2137,
redirect_url: '/redirect',
continue_url: '/continue',
date: '2020-01-01',
external_id: 'XN-2137',
},
meta: {
currency: { name: 'pln', symbol: 'pln', decimals: 2 },
Expand Down Expand Up @@ -129,11 +129,11 @@ afterEach(() => {
describe('orders service test', () => {
it('should make a request to create new payment for the given order', async () => {
const service = createOrdersService(axios)
const expectedUrl = `orders/2137/pay/id:payment-id`
const expectedUrl = `orders/pay/id:payment-id`

mock.onPost(expectedUrl).reply(200, dummyOrdersResponse)

const result = await service.pay('2137', 'payment-id', dummyOrdersResponse.data.continue_url)
const result = await service.pay(['2137'], 'payment-id', dummyOrdersResponse.data.continue_url)
expect(mock.history.post[0]?.url).toEqual(expectedUrl)
expect(result).toEqual(dummyOrdersResponse.data.redirect_url)
})
Expand Down