Skip to content

Commit

Permalink
feat(payment): Ajout d'une méthode pour générer une URL Payline
Browse files Browse the repository at this point in the history
  • Loading branch information
raphckrman committed May 25, 2024
1 parent 4b20f5a commit cd5a8e9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/api/accountCreationPost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountCreationPostResult, AccountCreation, AccountCreationPostBody } from "../interfaces/Account";
import { AccountCreationPostResult, AccountCreation } from "../interfaces/Account";
import { CREATE_ACCOUNT } from "../utils/endpoints";
import { TurboselfFetcher } from "../utils/fetcher";

Expand Down
24 changes: 24 additions & 0 deletions lib/api/generatePaymentURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PaymentInitPOSTResult } from "../interfaces/Payment";
import { POST_INIT_PAYMENT } from "../utils/endpoints";
import { TurboselfFetcher } from "../utils/fetcher";

export const generatePaymentURL = async (token: string, id: number, amount: number): Promise<string> => {
const response = await TurboselfFetcher("https://api-rest-prod.incb.fr" + POST_INIT_PAYMENT(id), {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token
},
body: JSON.stringify([{
paiementPayline: {
hote: {
id: id
}
},
montant: amount
}])
});

const raw = await response.json() as PaymentInitPOSTResult;
return raw.redirectURL;
};
11 changes: 11 additions & 0 deletions lib/client/Turboself.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Establishment } from "../parser/Establishment";
import { History } from "../parser/History";
import { Host } from "../parser/Host";
import { LatestPayment } from "../parser/LatestPayment";
import { generatePaymentURL } from "../api/generatePaymentURL";

export default class Turboself {
tokenExpires: number = 0;
Expand Down Expand Up @@ -153,4 +154,14 @@ export default class Turboself {
dayOfWeek
);
}

/** This method is used to generate an URL to pay.
* @param amount Amount in cents to pay.
*/
public async generatePaymentURL(amount: number): Promise<string> {
if (Date.now() > this.tokenExpires) {
await this.refreshToken();
}
return await generatePaymentURL(this.token, this.loginData.hoteId, amount);
}
}
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export * from "./api/siblingsGet";
export * from "./api/historyGet";
export * from "./api/lastPaymentGet";
export * from "./api/accountCreationPost";
export * from "./api/generatePaymentURL";
export * from "./api/canBookEveningGet";
export * from "./api/bookingGet";
export * from "./api/bookDay";
10 changes: 10 additions & 0 deletions lib/interfaces/Payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface PaymentInitPOSTResult {
result: {
code: string;
shortMessage: string;
longMessage: string;
};
token: string;
redirectURL: string;
_rawResponse: string;
}
2 changes: 2 additions & 0 deletions lib/utils/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const GET_SIBLINGS = (id: number) => `/api/v1/hotes/${id}/freres-soeurs`;

export const GET_BOOKING_WEEK = (id: number, weekNumber: number) => `/api/v1/reservations/hotes/${id}/semaines?num=${weekNumber}`;
export const PUT_BOOK_MEAL = (id: number) => `/api/v2/hotes/${id}/reservations-jours`;

export const POST_INIT_PAYMENT = (id: number) => `/api/v2/hotes/${id}/paiements/init`;

0 comments on commit cd5a8e9

Please sign in to comment.