Skip to content

Commit b52e78f

Browse files
authored
Merge pull request #1089 from dproychev-payhawk/add-offer-price-endpoint
feat: Add offer price endpoint
2 parents 9e35a48 + 8208802 commit b52e78f

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

src/booking/Offers/OfferTypes.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,76 @@ export interface UpdateOffer {
707707
family_name: string
708708
age: number
709709
}
710+
711+
export type OfferIntendedPaymentMethod =
712+
| {
713+
/**
714+
* The type of payment method intended to use
715+
*/
716+
type: 'card'
717+
718+
/**
719+
* The ID of the card intended to pay with
720+
*/
721+
card_id: string
722+
}
723+
| {
724+
/**
725+
* The type of payment method intended to use
726+
*/
727+
type: 'airline_credit'
728+
729+
/**
730+
* The ID of the airline credit intended to pay with
731+
*/
732+
airline_credit_id: string
733+
}
734+
735+
export interface OfferIntendedService {
736+
/**
737+
* The ID of the service to pay for
738+
*/
739+
id: string
740+
741+
/**
742+
* The quantity of the service ID to pay for
743+
*/
744+
quantity: number
745+
}
746+
747+
export interface GetOfferPricedParams {
748+
/**
749+
* The ID of the offer
750+
*/
751+
offerId: string
752+
753+
/**
754+
* The payment methods intended to use to pay for the offer
755+
*/
756+
intended_payment_methods: OfferIntendedPaymentMethod[]
757+
758+
/**
759+
* The services intended to book along with the offer
760+
*/
761+
intended_services: OfferIntendedService[]
762+
}
763+
764+
/**
765+
* Represents a priced offer, including the charge and surcharge amounts for the intended payment methods and services
766+
*/
767+
export interface OfferPriced extends Offer {
768+
/**
769+
* The payment methods intended to use to pay for the offer, along with the charge and surcharge amounts
770+
*/
771+
intended_payment_methods: (OfferIntendedPaymentMethod & {
772+
charge_currency: string
773+
charge_amount: string
774+
surcharge_amount: string
775+
surcharge_currency: string
776+
})[]
777+
778+
/**
779+
* The services intended to book along with the offer
780+
*/
781+
intended_services: OfferIntendedService[]
782+
}

src/booking/Offers/Offers.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nock from 'nock'
22
import { Client } from '../../Client'
33
import { Duffel } from '../../index'
4-
import { mockOffer, mockUpdatedOffer } from './mockOffer'
4+
import { mockOffer, mockOfferPriced, mockUpdatedOffer } from './mockOffer'
55
import { Offers } from './Offers'
66

77
const duffel = new Duffel({ token: 'mockToken' })
@@ -109,4 +109,30 @@ describe('offers', () => {
109109
1,
110110
)
111111
})
112+
113+
test('should get offer price', async () => {
114+
nock(/(.*)/)
115+
.post(`/air/offers/${mockOffer.id}/actions/price`)
116+
.reply(200, { data: mockOfferPriced })
117+
118+
const response = await new Offers(
119+
new Client({ token: 'mockToken' }),
120+
).getPriced({
121+
offerId: mockOffer.id,
122+
intended_payment_methods: [
123+
{
124+
type: 'card',
125+
card_id: 'card_00009htYpSCXrwaB9DnUm0',
126+
},
127+
],
128+
intended_services: [
129+
{
130+
id: 'ase_00009UhD4ongolulWd9123',
131+
quantity: 1,
132+
},
133+
],
134+
})
135+
136+
expect(response.data).toStrictEqual(mockOfferPriced)
137+
})
112138
})

src/booking/Offers/Offers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { Client } from '../../Client'
22
import { Resource } from '../../Resource'
33
import {
44
DuffelResponse,
5+
GetOfferPricedParams,
56
ListOffersParams,
67
Offer,
8+
OfferPriced,
79
LoyaltyProgrammeAccounts,
810
UpdateOffer,
911
} from '../../types'
@@ -97,4 +99,19 @@ export class Offers extends Resource {
9799
path: `${this.path}/${offerId}/passengers/${passengerId}`,
98100
...(params && { data: params }),
99101
})
102+
103+
/**
104+
* Price the offer with intended payment methods and intended services. This will return the total amount that will be charged to the customer, including any applicable surcharges.
105+
* @param {Object.<GetOfferPricedParams>} params - The intended payment methods and services of the offer
106+
* @param {string} params.offerId - Duffel's unique identifier for the offer
107+
*/
108+
public getPriced = async ({
109+
offerId,
110+
...params
111+
}: GetOfferPricedParams): Promise<DuffelResponse<OfferPriced>> =>
112+
this.request({
113+
method: 'POST',
114+
path: `${this.path}/${offerId}/actions/price`,
115+
data: params,
116+
})
100117
}

src/booking/Offers/mockOffer.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Offer } from '../../types'
1+
import { Offer, OfferPriced } from '../../types'
22

33
export const mockOffer: Offer = {
44
updated_at: '2020-01-17T10:12:14.545Z',
@@ -275,3 +275,23 @@ export const mockUpdatedOffer = {
275275
family_name: 'Earhart',
276276
age: 14,
277277
}
278+
279+
export const mockOfferPriced: OfferPriced = {
280+
...mockOffer,
281+
intended_payment_methods: [
282+
{
283+
type: 'card',
284+
card_id: 'card_00009htYpSCXrwaB9DnUm0',
285+
surcharge_amount: '2.50',
286+
surcharge_currency: 'GBP',
287+
charge_amount: '41.81',
288+
charge_currency: 'GBP',
289+
},
290+
],
291+
intended_services: [
292+
{
293+
id: 'ase_00009UhD4ongolulWd9123',
294+
quantity: 1,
295+
},
296+
],
297+
}

0 commit comments

Comments
 (0)