-
Notifications
You must be signed in to change notification settings - Fork 60
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
Refactor / align payment (upgrade) integration flows #565
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import InPlayer, { type AccessFee, type MerchantPaymentMethod } from '@inplayer-org/inplayer.js'; | ||
import { injectable } from 'inversify'; | ||
import { inject, injectable, named } from 'inversify'; | ||
|
||
import { isSVODOffer } from '../../../utils/offers'; | ||
import type { | ||
|
@@ -10,18 +10,22 @@ import type { | |
GetEntitlementsResponse, | ||
GetOffers, | ||
GetPaymentMethods, | ||
GetSubscriptionSwitch, | ||
GetSubscriptionSwitches, | ||
Offer, | ||
Order, | ||
Payment, | ||
PaymentMethod, | ||
PaymentWithAdyen, | ||
PaymentWithoutDetails, | ||
PaymentWithPayPal, | ||
SwitchSubscription, | ||
UpdateOrder, | ||
} from '../../../../types/checkout'; | ||
import CheckoutService from '../CheckoutService'; | ||
import type { ServiceResponse } from '../../../../types/service'; | ||
import { isCommonError } from '../../../utils/api'; | ||
import AccountService from '../AccountService'; | ||
|
||
@injectable() | ||
export default class JWPCheckoutService extends CheckoutService { | ||
|
@@ -85,7 +89,6 @@ export default class JWPCheckoutService extends CheckoutService { | |
active: true, | ||
period: offer.access_type.period === 'month' && offer.access_type.quantity === 12 ? 'year' : offer.access_type.period, | ||
freePeriods: offer.trial_period ? 1 : 0, | ||
planSwitchEnabled: offer.item.plan_switch_enabled ?? false, | ||
} as Offer; | ||
}; | ||
|
||
|
@@ -109,6 +112,10 @@ export default class JWPCheckoutService extends CheckoutService { | |
} as Order; | ||
}; | ||
|
||
constructor(@inject(AccountService) @named('JWP') private readonly accountService: AccountService) { | ||
super(); | ||
} | ||
|
||
createOrder: CreateOrder = async (payload) => { | ||
return { | ||
errors: [], | ||
|
@@ -276,13 +283,50 @@ export default class JWPCheckoutService extends CheckoutService { | |
} | ||
}; | ||
|
||
getSubscriptionSwitches = undefined; | ||
getSubscriptionSwitches: GetSubscriptionSwitches = async (payload) => { | ||
const { data } = await InPlayer.Asset.getAssetAccessFees(this.parseOfferId(payload.offerId)); | ||
|
||
const subscriptionSwitches = data?.filter((accessFee) => accessFee.item.plan_switch_enabled).map((accessFee) => this.formatOffer(accessFee)) || []; | ||
|
||
return { responseData: subscriptionSwitches, errors: [] }; | ||
}; | ||
|
||
getOrder = undefined; | ||
|
||
switchSubscription = undefined; | ||
switchSubscription: SwitchSubscription = async (payload) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! |
||
const { subscription, toOfferId } = payload; | ||
const accessFeeId = parseInt(toOfferId.split('_')[1]); | ||
|
||
try { | ||
const response = await InPlayer.Subscription.changeSubscriptionPlan({ | ||
access_fee_id: accessFeeId, | ||
inplayer_token: String(subscription.subscriptionId), | ||
}); | ||
|
||
await this.accountService.updateCustomer({ | ||
metadata: { | ||
[`${subscription.subscriptionId}_pending_downgrade`]: toOfferId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh...this part was in the hook previously. Makes sense to have it here. |
||
}, | ||
}); | ||
|
||
return { | ||
errors: [], | ||
responseData: response.data.message, | ||
}; | ||
} catch { | ||
throw new Error('Failed to change subscription'); | ||
} | ||
}; | ||
|
||
getSubscriptionSwitch: GetSubscriptionSwitch = async ({ subscription }) => { | ||
if (subscription.pendingSwitchId) { | ||
const offers = await this.getOffers({ offerIds: [subscription.offerId] }); | ||
|
||
getSubscriptionSwitch = undefined; | ||
return { responseData: offers.find((offer) => offer.offerId === subscription.pendingSwitchId) || null, errors: [] }; | ||
} | ||
|
||
return { responseData: null, errors: [] }; | ||
}; | ||
|
||
createAdyenPaymentSession = undefined; | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it can't be done for JWP now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should be reverted and solved with a
canSwitchSubscription
flag instead.