mts-booking-library 1.1.10 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,7 @@
1
1
  import { MTSConfig, MTSEnvs } from "../config";
2
+ import { ErrorResponse } from "../types/ErrorResponse";
3
+ import { GetPaymentInformationFromGatewayResponse, GetSellerGatewaysResponse, PaymentMethods } from "../types/common/Payment";
4
+ import { AddReductionRequest } from "../types/common/Reduction";
2
5
  export declare abstract class Booking {
3
6
  readonly sellerId?: number | undefined;
4
7
  readonly resellerId?: number | undefined;
@@ -6,7 +9,16 @@ export declare abstract class Booking {
6
9
  readonly config: MTSConfig;
7
10
  language: string;
8
11
  selectedCurrency: Booking.Currencies;
9
- cartStatus: Booking.CartStatus;
12
+ /**
13
+ * This map describes the status of the booking steps. The keys of this dictionary represent the booking steps.
14
+ * If the key is present, then the booking step shall be displayed. See {@link Booking.BookingSteps} for the possible values.
15
+ * The value is a list of 3 booleans: <br/>
16
+ * - The first tells whether it is possible to go to that section. <br/>
17
+ * - The second tells whether the section is completed. <br/>
18
+ * - The third tells whether the section is required. <br/>
19
+ */
20
+ bookingStepsToStatus: Map<string, boolean[]>;
21
+ cartId: number | undefined;
10
22
  bookingDueDate: Date | undefined;
11
23
  onCartExpiration: () => void;
12
24
  /**
@@ -26,12 +38,51 @@ export declare abstract class Booking {
26
38
  * @param {string} access_token The new access token
27
39
  */
28
40
  renewAccessToken(access_token: string): void;
29
- callGetApi(url: string): Promise<any>;
41
+ callGetApi(url: string): Promise<ErrorResponse | any>;
30
42
  callPostApi(url: string, body: any): Promise<any>;
31
- getCartStatus(): Booking.CartStatus;
43
+ getBookingStepsToStatus(): Map<string, boolean[]>;
32
44
  changeCurrency(currency: Booking.Currencies): void;
33
45
  changeLanguage(language: string): void;
34
46
  getCartExpirationTimeInMs(): Promise<number>;
47
+ /**
48
+ * @description This method allows to add a reduction to the cart
49
+ * @param {AddReductionRequest} request The information about the reduction to add
50
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
51
+ */
52
+ addReduction(request: AddReductionRequest): Promise<ErrorResponse | any>;
53
+ /**
54
+ * @description This method allows the user to use the balance in his wallet to pay the cart. If the balance of
55
+ * the wallet is enough to pay the cart, the cart will be paid fully. Otherwise, the user will be asked to pay the
56
+ * remaining amount with another payment method.
57
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
58
+ */
59
+ useWallet(): Promise<ErrorResponse | any>;
60
+ /**
61
+ * @description This method allows the user to use a voucher to pay the cart.
62
+ * If the balance of the value of the voucher is enough to pay the cart, the cart will be paid fully.
63
+ * Otherwise, the user will be asked to pay the remaining amount with another payment method.
64
+ * @param {string} voucherCode The code of the voucher to use
65
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
66
+ */
67
+ useVoucher(voucherCode: string): Promise<ErrorResponse | any>;
68
+ /**
69
+ * @description This method shall be used to get the available payment methods for the cart. They depend on the seller configuration.
70
+ * @returns An {@link ErrorResponse} object in case of error, a {@link GetSellerGatewaysResponse} object otherwise.
71
+ */
72
+ getSellerGateways(): Promise<ErrorResponse | GetSellerGatewaysResponse>;
73
+ /**
74
+ * @description This method shall be used to get all necessary informations to initialize a payment with a certain gateway.
75
+ * @param {number} gatewayId The id of the gateway to use. Its value shall be taken from the response of the {@link Booking.getSellerGateways} API
76
+ * @param {string} [returnUrl=undefined] The url to which the user will be redirected after the payment. This value shall be set only if the
77
+ * chosen gateway is {@link GatewayTypes.PAYPAL}
78
+ * @returns An {@link ErrorResponse} object in case of error, a {@link GetPaymentInformationFromGatewayResponse} object otherwise.
79
+ */
80
+ getPaymentInformationFromGateway(gatewayId: number, returnUrl?: string): Promise<ErrorResponse | GetPaymentInformationFromGatewayResponse>;
81
+ /**
82
+ * @description This method shall be used to issue the tickets. It must be called after the payment was successful.
83
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
84
+ */
85
+ issueTickets(paymentMethod: PaymentMethods): Promise<ErrorResponse | any>;
35
86
  }
36
87
  export declare namespace Booking {
37
88
  enum Currencies {
@@ -46,13 +97,13 @@ export declare namespace Booking {
46
97
  DE = "de",
47
98
  ES = "es"
48
99
  }
49
- enum CartStatus {
50
- EMPTY = "EMPTY",
51
- CREATION = "CREATION",
52
- PASSENGERS = "PASSENGERS",
53
- SEATS = "SEATS",
54
- EXTRAS = "EXTRAS",
55
- PAYMENT = "PAYMENT"
100
+ enum BookingSteps {
101
+ SEARCH = "BOOKING_STEP_SEARCH",
102
+ BUYER_PASSENGERS = "BOOKING_STEP_BUYER_PASSENGERS",
103
+ SEATS_SELECTION = "BOOKING_STEP_SEATS_SELECTION",
104
+ EXTRAS = "BOOKING_STEP_EXTRAS",
105
+ DISCOUNTS = "BOOKING_STEP_DISCOUNTS",
106
+ ISSUE = "BOOKING_STEP_ISSUE"
56
107
  }
57
108
  enum BookingTypes {
58
109
  MLP = "MLP",
@@ -49,6 +49,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.Booking = void 0;
51
51
  var config_1 = require("../config");
52
+ var ErrorResponse_1 = require("../types/ErrorResponse");
53
+ var Payment_1 = require("../types/common/Payment");
52
54
  var apiCall_1 = require("../utils/apiCall");
53
55
  var Booking = /** @class */ (function () {
54
56
  /**
@@ -67,7 +69,15 @@ var Booking = /** @class */ (function () {
67
69
  if (language === void 0) { language = Booking.Languages.EN; }
68
70
  this.language = Booking.Languages.EN;
69
71
  this.selectedCurrency = Booking.Currencies.EUR;
70
- this.cartStatus = Booking.CartStatus.EMPTY;
72
+ /**
73
+ * This map describes the status of the booking steps. The keys of this dictionary represent the booking steps.
74
+ * If the key is present, then the booking step shall be displayed. See {@link Booking.BookingSteps} for the possible values.
75
+ * The value is a list of 3 booleans: <br/>
76
+ * - The first tells whether it is possible to go to that section. <br/>
77
+ * - The second tells whether the section is completed. <br/>
78
+ * - The third tells whether the section is required. <br/>
79
+ */
80
+ this.bookingStepsToStatus = new Map();
71
81
  this.sellerId = sellerId || undefined;
72
82
  this.resellerId = resellerId || undefined;
73
83
  this.config = (0, config_1.setConfig)(env, sub_key, debug);
@@ -94,8 +104,8 @@ var Booking = /** @class */ (function () {
94
104
  var completeBody = __assign(__assign(__assign(__assign({}, body), (this.sellerId && { sellerId: this.sellerId.toString() })), (this.resellerId && { resellerId: this.resellerId.toString() })), { language: this.language });
95
105
  return (0, apiCall_1.makePost)(url, completeBody);
96
106
  };
97
- Booking.prototype.getCartStatus = function () {
98
- return this.cartStatus;
107
+ Booking.prototype.getBookingStepsToStatus = function () {
108
+ return this.bookingStepsToStatus;
99
109
  };
100
110
  Booking.prototype.changeCurrency = function (currency) {
101
111
  this.selectedCurrency = currency;
@@ -123,6 +133,158 @@ var Booking = /** @class */ (function () {
123
133
  });
124
134
  });
125
135
  };
136
+ /**
137
+ * @description This method allows to add a reduction to the cart
138
+ * @param {AddReductionRequest} request The information about the reduction to add
139
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
140
+ */
141
+ Booking.prototype.addReduction = function (request) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ var discountsStep, url;
144
+ return __generator(this, function (_a) {
145
+ // First check that it is possible to call this API
146
+ if (!this.cartId || this.cartId === 0) {
147
+ throw Error("Cart is not initialized yet");
148
+ }
149
+ discountsStep = this.bookingStepsToStatus.get(Booking.BookingSteps.DISCOUNTS);
150
+ if (!discountsStep || !discountsStep[0]) {
151
+ throw Error("The status of the cart does not allow to call this API");
152
+ }
153
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/reduction");
154
+ return [2 /*return*/, this.callPostApi(url, request).then(function (response) {
155
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
156
+ })];
157
+ });
158
+ });
159
+ };
160
+ /**
161
+ * @description This method allows the user to use the balance in his wallet to pay the cart. If the balance of
162
+ * the wallet is enough to pay the cart, the cart will be paid fully. Otherwise, the user will be asked to pay the
163
+ * remaining amount with another payment method.
164
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
165
+ */
166
+ Booking.prototype.useWallet = function () {
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ var discountsStep, url;
169
+ return __generator(this, function (_a) {
170
+ // First check that it is possible to call this API
171
+ if (!this.cartId || this.cartId === 0) {
172
+ throw Error("Cart is not initialized yet");
173
+ }
174
+ discountsStep = this.bookingStepsToStatus.get(Booking.BookingSteps.DISCOUNTS);
175
+ if (!discountsStep || !discountsStep[0]) {
176
+ throw Error("The status of the cart does not allow to call this API");
177
+ }
178
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/wallet");
179
+ return [2 /*return*/, this.callPostApi(url, {}).then(function (response) {
180
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
181
+ })];
182
+ });
183
+ });
184
+ };
185
+ /**
186
+ * @description This method allows the user to use a voucher to pay the cart.
187
+ * If the balance of the value of the voucher is enough to pay the cart, the cart will be paid fully.
188
+ * Otherwise, the user will be asked to pay the remaining amount with another payment method.
189
+ * @param {string} voucherCode The code of the voucher to use
190
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
191
+ */
192
+ Booking.prototype.useVoucher = function (voucherCode) {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var discountsStep, url;
195
+ return __generator(this, function (_a) {
196
+ // First check that it is possible to call this API
197
+ if (!this.cartId || this.cartId === 0) {
198
+ throw Error("Cart is not initialized yet");
199
+ }
200
+ discountsStep = this.bookingStepsToStatus.get(Booking.BookingSteps.DISCOUNTS);
201
+ if (!discountsStep || !discountsStep[0]) {
202
+ throw Error("The status of the cart does not allow to call this API");
203
+ }
204
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/voucher");
205
+ return [2 /*return*/, this.callPostApi(url, { voucherCode: voucherCode }).then(function (response) {
206
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
207
+ })];
208
+ });
209
+ });
210
+ };
211
+ /**
212
+ * @description This method shall be used to get the available payment methods for the cart. They depend on the seller configuration.
213
+ * @returns An {@link ErrorResponse} object in case of error, a {@link GetSellerGatewaysResponse} object otherwise.
214
+ */
215
+ Booking.prototype.getSellerGateways = function () {
216
+ return __awaiter(this, void 0, void 0, function () {
217
+ var issueStep, url;
218
+ return __generator(this, function (_a) {
219
+ // First check that it is possible to call this API
220
+ if (!this.cartId || this.cartId === 0) {
221
+ throw Error("Cart is not initialized yet");
222
+ }
223
+ issueStep = this.bookingStepsToStatus.get(Booking.BookingSteps.ISSUE);
224
+ if (!issueStep || !issueStep[0]) {
225
+ throw Error("The status of the cart does not allow to call this API");
226
+ }
227
+ url = "".concat(this.config.API_ENDPOINT, "/sellers/").concat(this.sellerId, "/gateways");
228
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
229
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
230
+ })];
231
+ });
232
+ });
233
+ };
234
+ /**
235
+ * @description This method shall be used to get all necessary informations to initialize a payment with a certain gateway.
236
+ * @param {number} gatewayId The id of the gateway to use. Its value shall be taken from the response of the {@link Booking.getSellerGateways} API
237
+ * @param {string} [returnUrl=undefined] The url to which the user will be redirected after the payment. This value shall be set only if the
238
+ * chosen gateway is {@link GatewayTypes.PAYPAL}
239
+ * @returns An {@link ErrorResponse} object in case of error, a {@link GetPaymentInformationFromGatewayResponse} object otherwise.
240
+ */
241
+ Booking.prototype.getPaymentInformationFromGateway = function (gatewayId, returnUrl) {
242
+ return __awaiter(this, void 0, void 0, function () {
243
+ var issueStep, url, body;
244
+ return __generator(this, function (_a) {
245
+ // First check that it is possible to call this API
246
+ if (!this.cartId || this.cartId === 0) {
247
+ throw Error("Cart is not initialized yet");
248
+ }
249
+ issueStep = this.bookingStepsToStatus.get(Booking.BookingSteps.ISSUE);
250
+ if (!issueStep || !issueStep[0]) {
251
+ throw Error("The status of the cart does not allow to call this API");
252
+ }
253
+ url = "".concat(this.config.API_ENDPOINT, "/getPaymentInformationFromGateway");
254
+ body = __assign({ cartId: this.cartId, gatewayId: gatewayId }, (returnUrl && { returnUrl: returnUrl }));
255
+ return [2 /*return*/, this.callPostApi(url, body).then(function (response) {
256
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
257
+ })];
258
+ });
259
+ });
260
+ };
261
+ /**
262
+ * @description This method shall be used to issue the tickets. It must be called after the payment was successful.
263
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
264
+ */
265
+ Booking.prototype.issueTickets = function (paymentMethod) {
266
+ return __awaiter(this, void 0, void 0, function () {
267
+ var issueStep, url, body;
268
+ return __generator(this, function (_a) {
269
+ // First check that it is possible to call this API
270
+ if (!this.cartId || this.cartId === 0) {
271
+ throw Error("Cart is not initialized yet");
272
+ }
273
+ issueStep = this.bookingStepsToStatus.get(Booking.BookingSteps.ISSUE);
274
+ if (!issueStep || !issueStep[0]) {
275
+ throw Error("The status of the cart does not allow to call this API");
276
+ }
277
+ url = "".concat(this.config.API_ENDPOINT, "/issueTickets");
278
+ body = {
279
+ cartId: this.cartId,
280
+ paymentMethod: paymentMethod === Payment_1.PaymentMethods.PAY_LATER ? null : paymentMethod,
281
+ };
282
+ return [2 /*return*/, this.callPostApi(url, body).then(function (response) {
283
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
284
+ })];
285
+ });
286
+ });
287
+ };
126
288
  return Booking;
127
289
  }());
128
290
  exports.Booking = Booking;
@@ -141,15 +303,15 @@ exports.Booking = Booking;
141
303
  Languages["DE"] = "de";
142
304
  Languages["ES"] = "es";
143
305
  })(Languages = Booking.Languages || (Booking.Languages = {}));
144
- var CartStatus;
145
- (function (CartStatus) {
146
- CartStatus["EMPTY"] = "EMPTY";
147
- CartStatus["CREATION"] = "CREATION";
148
- CartStatus["PASSENGERS"] = "PASSENGERS";
149
- CartStatus["SEATS"] = "SEATS";
150
- CartStatus["EXTRAS"] = "EXTRAS";
151
- CartStatus["PAYMENT"] = "PAYMENT";
152
- })(CartStatus = Booking.CartStatus || (Booking.CartStatus = {}));
306
+ var BookingSteps;
307
+ (function (BookingSteps) {
308
+ BookingSteps["SEARCH"] = "BOOKING_STEP_SEARCH";
309
+ BookingSteps["BUYER_PASSENGERS"] = "BOOKING_STEP_BUYER_PASSENGERS";
310
+ BookingSteps["SEATS_SELECTION"] = "BOOKING_STEP_SEATS_SELECTION";
311
+ BookingSteps["EXTRAS"] = "BOOKING_STEP_EXTRAS";
312
+ BookingSteps["DISCOUNTS"] = "BOOKING_STEP_DISCOUNTS";
313
+ BookingSteps["ISSUE"] = "BOOKING_STEP_ISSUE";
314
+ })(BookingSteps = Booking.BookingSteps || (Booking.BookingSteps = {}));
153
315
  var BookingTypes;
154
316
  (function (BookingTypes) {
155
317
  BookingTypes["MLP"] = "MLP";
@@ -1,5 +1,7 @@
1
1
  import { MTSEnvs } from "../config";
2
- import { PassengersDetails } from "../types/common/Person";
2
+ import { ErrorResponse } from "../types/ErrorResponse";
3
+ import { EditPassengersDetailsRequest, GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
4
+ import { BusMatrix } from "../types/journeys/BusMatrix";
3
5
  import { CreateJourneyCartRequest, JourneyCart } from "../types/journeys/JourneyCart";
4
6
  import { JourneySearchRequest, JourneySearchResult } from "../types/journeys/JourneySearch";
5
7
  import { Booking } from "./booking";
@@ -25,32 +27,63 @@ export declare class JourneyBooking extends Booking {
25
27
  * This method returns the possible departures for all journeys (MLP) sold by this seller.
26
28
  * @returns {string[]} The list of possible departures
27
29
  */
28
- getJourneysDepartures(): Promise<string[]>;
30
+ getJourneysDepartures(): Promise<ErrorResponse | string[]>;
29
31
  /**
30
32
  * This method returns the possible destination for the journeys sold by this seller that start at the selected departure.
31
33
  * @param {string} departureStopName The departure stop name (as returned by {@link getJourneysDepartures})
32
34
  * @returns {string[]} The list of possible destinations
33
35
  */
34
- getJourneysDestinations(departureStopName: string): Promise<string[]>;
36
+ getJourneysDestinations(departureStopName: string): Promise<ErrorResponse | string[]>;
35
37
  /**
36
38
  * This method returns the journeys that match the given parameters.
37
39
  * Note that it will always return the journeys for the one-way trip.
38
40
  * If you want to get the journeys for the round trip, you have to call this method twice (make sure to swap departureStopName and destinationStopName)
39
- * @param {JourneySearch} params an object of type {@link JourneySearch} containing the parameters of the search
41
+ * @param {JourneySearch} request an object of type {@link JourneySearch} containing the parameters of the search
40
42
  * @returns {JourneySearchResult[]} The returned journeys that match the given parameters
41
43
  */
42
- getJourneys(params: JourneySearchRequest): Promise<JourneySearchResult[]>;
43
- createJourneyCart(tripCart: CreateJourneyCartRequest): Promise<JourneyCart>;
44
- getBusMatrix(tripId: number, departureStopId: number, destinationStopId: number): Promise<any>;
45
- getSeatsStatus(params: {
46
- tripId: number;
47
- departureStopId: number;
48
- destinationStopId: number;
49
- }): Promise<any>;
50
- movePassengers(params: {
51
- tripId: number;
52
- seatsIds: number[];
53
- }): Promise<any>;
54
- getPassengersDetails(): Promise<any>;
55
- updatePassengersDetails(passengersDetails: PassengersDetails): Promise<any>;
44
+ getJourneys(request: JourneySearchRequest): Promise<ErrorResponse | JourneySearchResult[]>;
45
+ createJourneyCart(journeyCart: CreateJourneyCartRequest): Promise<ErrorResponse | JourneyCart>;
46
+ /**
47
+ * @description This method shall be called when the user wants to retrieve information about the buyer and the passengers.
48
+ * @returns An object of type {@link GetBuyerPassengersDetailsResponse} containing the buyer and the passengers information,
49
+ * as well as a list of the available tariffs for each trip.
50
+ */
51
+ getBuyerPassengersDetails(): Promise<ErrorResponse | GetBuyerPassengersDetailsResponse>;
52
+ /**
53
+ * @description This method shall be called when the user wants to retrieve information about an esisting buyer.
54
+ * @param {string} [linkavelCardNumber=undefined] The linkavel card number of the buyer. This parameter is required if linkavelCardPhoneNumber is not set.
55
+ * A linkavelCardNumber is a string of 9 digits.
56
+ * @param {string} [linkavelCardPhoneNumber=undefined] The linkavel card phone number of the buyer. This parameter is required if linkavelCardNumber is not set.
57
+ * @returns An object of type {@link GetBuyerPassengersDetailsResponse} containing the buyer and the passengers information,
58
+ * as well as a list of the available tariffs for each trip.
59
+ */
60
+ getBuyer(linkavelCardNumber?: string, linkavelCardPhoneNumber?: string): Promise<ErrorResponse | Person>;
61
+ /**
62
+ * @description This methosd shall be called when the user wants to update the buyer and the passengers information.
63
+ * @param {EditPassengersDetailsRequest} passengersDetails The object containing the buyer and the passengers information.
64
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
65
+ */
66
+ updateBuyerPassengersDetails(passengersDetails: EditPassengersDetailsRequest): Promise<ErrorResponse | any>;
67
+ /**
68
+ * @description This method shall be used when the user wants to retrieve the bus matrix for a trip. The bus matrix shows
69
+ * the seats that are available and the ones that are already taken, as well as how the bus is made.
70
+ * @param {number} tripId The id of the trip for which the bus matrix should be retrieved
71
+ * @param {number} departureStopId The id of the departure stop
72
+ * @param {number} destinationStopId The id of the destination stop
73
+ * @returns An {@link ErrorResponse} object in case of error, a {@link BusMatrix} otherwise.
74
+ */
75
+ getBusMatrix(tripId: number, departureStopId: number, destinationStopId: number): Promise<ErrorResponse | BusMatrix>;
76
+ /**
77
+ * @description This method shall be called when the user wants to retrieve which seats were assigned for this booking.
78
+ * @param {number} tripId The id of the trip for which the seats should be retrieved.
79
+ * @returns An {@link ErrorResponse} object in case of error, a list of {@link BusLayoutCell} ids otherwise, representing the assigned seats.
80
+ */
81
+ getAssignedSeats(tripId: number): Promise<ErrorResponse | number[]>;
82
+ /**
83
+ * @description This method shall be called when the user wants to change the assigned seats of a trip.
84
+ * @param {number} tripId The id of the trip for which the seats should be changed
85
+ * @param {number[]} newSeatsIds The ids of the new seats, as found the {@link BusMatrix}
86
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
87
+ */
88
+ changeSeatsOfTrip(tripId: number, newSeatsIds: number[]): Promise<ErrorResponse | any>;
56
89
  }