Skip to content

Commit

Permalink
fix: update inplayer.js version and types
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovladimitrovski committed May 5, 2023
1 parent 759ed17 commit f62f359
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commit-msg
# yarn commit-msg
17 changes: 11 additions & 6 deletions src/services/inplayer.account.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import InPlayer, { AccountData, Env, GetRegisterField, UpdateAccountData, FavoritesData, WatchHistory } from '@inplayer-org/inplayer.js';
import InPlayer, {
AccountData,
Env,
RegisterField,
UpdateAccountData,
FavoritesData,
WatchHistory,
} from '@inplayer-org/inplayer.js';
import i18next from 'i18next';

import type {
Expand Down Expand Up @@ -136,11 +143,9 @@ export const getPublisherConsents: GetPublisherConsents = async (config) => {
const { jwp } = config.integrations;
const { data } = await InPlayer.Account.getRegisterFields(jwp?.clientId || '');

// @ts-ignore
// wrong data type from InPlayer SDK (will be updated in the SDK)
const result: Consent[] = data?.collection
.filter((field: GetRegisterField) => field.type === 'checkbox')
.map((consent: GetRegisterField) => formatPublisherConsents(consent));
.filter((field) => field.type === 'checkbox')
.map((consent) => formatPublisherConsents(consent));

return {
consents: [getTermsConsent(), ...result],
Expand Down Expand Up @@ -389,7 +394,7 @@ function formatAuth(auth: InPlayerAuthData): AuthData {
};
}

function formatPublisherConsents(consent: Partial<GetRegisterField>) {
function formatPublisherConsents(consent: Partial<RegisterField>) {
return {
broadcasterId: 0,
enabledByDefault: false,
Expand Down
19 changes: 6 additions & 13 deletions src/services/inplayer.checkout.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import InPlayer, { GetAccessFee, MerchantPaymentMethod } from '@inplayer-org/inplayer.js';
import InPlayer, { AccessFee, MerchantPaymentMethod } from '@inplayer-org/inplayer.js';

import type {
CardPaymentData,
Expand Down Expand Up @@ -36,8 +36,7 @@ export const getOffers: GetOffers = async (payload) => {
try {
const { data } = await InPlayer.Asset.getAssetAccessFees(parseInt(`${assetId}`));

// TODO fix this type in the InPlayer SDK, because the actual type of `data` is GetAccessFee[], not GetAccessFee
return (data as unknown as GetAccessFee[])?.map((offer) => formatOffer(offer));
return data?.map((offer) => formatOffer(offer));
} catch {
throw new Error('Failed to get offers');
}
Expand Down Expand Up @@ -84,11 +83,9 @@ export const paymentWithPayPal: PaymentWithPayPal = async (payload) => {
origin: `${window.location.origin}?u=waiting-for-payment`,
accessFeeId: payload.order.id,
paymentMethod: 2,
//@ts-ignore
voucherCode: payload.couponCode,
});
//@ts-ignore
//TODO fix this type in InPlayer SDK

if (response.data?.id) {
return {
errors: ['Already have an active access'],
Expand Down Expand Up @@ -131,8 +128,6 @@ export const updateOrder: UpdateOrder = async ({ order, couponCode }) => {
order.discount = {
applied: true,
type: 'coupon',
//@ts-ignore
// TODO fix this type in InPlayer SDK
periods: response.data.discount_duration,
};

Expand All @@ -155,22 +150,20 @@ export const updateOrder: UpdateOrder = async ({ order, couponCode }) => {

export const directPostCardPayment = async (cardPaymentPayload: CardPaymentData, order: Order) => {
const payload = {
number: cardPaymentPayload.cardNumber,
number: parseInt(`${cardPaymentPayload.cardNumber}`),
cardName: cardPaymentPayload.cardholderName,
expMonth: cardPaymentPayload.cardExpMonth || '',
expYear: cardPaymentPayload.cardExpYear || '',
cvv: parseInt(cardPaymentPayload.cardCVC),
accessFee: order.id,
paymentMethod: '1',
paymentMethod: 1,
voucherCode: cardPaymentPayload.couponCode,
referrer: window.location.href,
returnUrl: `${window.location.href}&u=waiting-for-payment`,
};

try {
if (isSVODOffer(order)) {
//@ts-ignore
//Fix this type in InPlayer SDK
await InPlayer.Subscription.createSubscription(payload);
} else {
await InPlayer.Payment.createPayment(payload);
Expand Down Expand Up @@ -201,7 +194,7 @@ const formatEntitlements = (expiresAt: number = 0, accessGranted: boolean = fals
};
};

const formatOffer = (offer: GetAccessFee): Offer => {
const formatOffer = (offer: AccessFee): Offer => {
const offerId = offer.access_type.name === 'ppv' ? `C${offer.id}` : `S${offer.id}`;
return {
id: offer.id,
Expand Down
15 changes: 6 additions & 9 deletions src/services/inplayer.subscription.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import i18next from 'i18next';
import InPlayer, { Card, GetItemAccessV1, SubscriptionDetails as InplayerSubscription } from '@inplayer-org/inplayer.js';
import InPlayer, { PurchaseDetails, Card, GetItemAccessV1, SubscriptionDetails as InplayerSubscription } from '@inplayer-org/inplayer.js';

import type { PaymentDetail, Subscription, Transaction, UpdateSubscription } from '#types/subscription';
import type { Config } from '#types/Config';
import type { InPlayerError, InPlayerPurchaseDetails } from '#types/inplayer';
import type { InPlayerError } from '#types/inplayer';

interface SubscriptionDetails extends InplayerSubscription {
item_id?: number;
Expand Down Expand Up @@ -47,9 +47,8 @@ export async function getActiveSubscription({ config }: { config: Config }) {
export async function getAllTransactions() {
try {
const { data } = await InPlayer.Payment.getPurchaseHistory('active', 0, 30);
// @ts-ignore
// TODO fix PurchaseHistoryCollection type in InPlayer SDK
return data?.collection?.map((transaction: InPlayerPurchaseDetails) => formatTransaction(transaction));

return data?.collection?.map((transaction) => formatTransaction(transaction));
} catch {
throw new Error('Failed to get transactions');
}
Expand All @@ -60,8 +59,6 @@ export async function getActivePayment() {
const { data } = await InPlayer.Payment.getDefaultCreditCard();
const cards: PaymentDetail[] = [];
for (const currency in data?.cards) {
// @ts-ignore
// TODO fix Card type in InPlayer SDK
cards.push(formatCardDetails(data.cards?.[currency]));
}

Expand Down Expand Up @@ -95,7 +92,7 @@ export const updateSubscription: UpdateSubscription = async ({ offerId, unsubscr
}
};

const formatCardDetails = (card: Card & { card_type: string; account_id: number }): PaymentDetail => {
const formatCardDetails = (card: Card): PaymentDetail => {
const { number, exp_month, exp_year, card_name, card_type, account_id } = card;
const zeroFillExpMonth = `0${exp_month}`.slice(-2);
return {
Expand All @@ -111,7 +108,7 @@ const formatCardDetails = (card: Card & { card_type: string; account_id: number
};

// TODO: fix PurchaseDetails type in InPlayer SDK
const formatTransaction = (transaction: InPlayerPurchaseDetails): Transaction => {
const formatTransaction = (transaction: PurchaseDetails): Transaction => {
const purchasedAmount = transaction?.purchased_amount?.toString() || '0';

return {
Expand Down
5 changes: 1 addition & 4 deletions types/inplayer.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PurchaseDetails, PurchaseHistoryCollection } from '@inplayer-org/inplayer.js';
import type { PurchaseHistoryCollection } from '@inplayer-org/inplayer.js';

export type InPlayerAuthData = {
access_token: string;
Expand All @@ -21,6 +21,3 @@ export type InPlayerResponse<T> = {
config: AxiosRequestConfig;
};

export type InPlayerPurchaseDetails = PurchaseDetails & {
purchased_access_fee_description: Record<string>;
};

0 comments on commit f62f359

Please sign in to comment.