@winible/winible-typed 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/migrations/20220601151258-custom_name_describing_your_migration.js +19 -0
- package/dist/migrations/20220601151258-custom_name_describing_your_migration.js.map +1 -0
- package/dist/migrations/20220605225655-create-dashboard-user-permissions.js +27 -0
- package/dist/migrations/20220605225655-create-dashboard-user-permissions.js.map +1 -0
- package/dist/migrations/20220606001300-update-dashboard-user-permissions.js +14 -0
- package/dist/migrations/20220606001300-update-dashboard-user-permissions.js.map +1 -0
- package/dist/migrations/20220607091109-add-read-date-for-message.js +19 -0
- package/dist/migrations/20220607091109-add-read-date-for-message.js.map +1 -0
- package/dist/migrations/20220616225456-add-delete-columns.js +21 -0
- package/dist/migrations/20220616225456-add-delete-columns.js.map +1 -0
- package/dist/migrations/20220709202211-disable-feed.js +24 -0
- package/dist/migrations/20220709202211-disable-feed.js.map +1 -0
- package/dist/migrations/20220723215213-limit-promotion-claims.js +18 -0
- package/dist/migrations/20220723215213-limit-promotion-claims.js.map +1 -0
- package/dist/migrations/20220723220233-num_claims.js +12 -0
- package/dist/migrations/20220723220233-num_claims.js.map +1 -0
- package/dist/migrations/20220723221907-default_false_promos.js +17 -0
- package/dist/migrations/20220723221907-default_false_promos.js.map +1 -0
- package/dist/migrations/20220724172559-default_zero_claims.js +13 -0
- package/dist/migrations/20220724172559-default_zero_claims.js.map +1 -0
- package/dist/migrations/20220724201419-pinned_post.js +13 -0
- package/dist/migrations/20220724201419-pinned_post.js.map +1 -0
- package/dist/migrations/20220729164625-unsend_mass.js +12 -0
- package/dist/migrations/20220729164625-unsend_mass.js.map +1 -0
- package/dist/migrations/20220823033002-interval-unit.js +14 -0
- package/dist/migrations/20220823033002-interval-unit.js.map +1 -0
- package/dist/migrations/20220830193530-processorId.js +15 -0
- package/dist/migrations/20220830193530-processorId.js.map +1 -0
- package/dist/migrations/20220907121932-add-processor-to-sub.js +15 -0
- package/dist/migrations/20220907121932-add-processor-to-sub.js.map +1 -0
- package/dist/migrations/20220907212232-add-trans-id.js +12 -0
- package/dist/migrations/20220907212232-add-trans-id.js.map +1 -0
- package/dist/migrations/20220907220423-big-int-trans.js +12 -0
- package/dist/migrations/20220907220423-big-int-trans.js.map +1 -0
- package/dist/migrations/20220909161902-add-stale.js +13 -0
- package/dist/migrations/20220909161902-add-stale.js.map +1 -0
- package/dist/migrations/20220922000225-verified-spender.js +13 -0
- package/dist/migrations/20220922000225-verified-spender.js.map +1 -0
- package/dist/migrations/20220922212108-verified-spender-2.js +16 -0
- package/dist/migrations/20220922212108-verified-spender-2.js.map +1 -0
- package/dist/migrations/20221013223648-product-subscription-list.js +12 -0
- package/dist/migrations/20221013223648-product-subscription-list.js.map +1 -0
- package/dist/migrations/20221014001037-product-subscription-list.js +75 -0
- package/dist/migrations/20221014001037-product-subscription-list.js.map +1 -0
- package/dist/migrations/20221026055245-update-type.js +15 -0
- package/dist/migrations/20221026055245-update-type.js.map +1 -0
- package/dist/scripts/authorizeTransfer.js +188 -0
- package/dist/scripts/authorizeTransfer.js.map +1 -0
- package/dist/scripts/createDashboardLink.js +30 -0
- package/dist/scripts/createDashboardLink.js.map +1 -0
- package/dist/scripts/deleteMalformedImages.js +84 -0
- package/dist/scripts/deleteMalformedImages.js.map +1 -0
- package/dist/scripts/importMixpanelEvents.js +44 -0
- package/dist/scripts/importMixpanelEvents.js.map +1 -0
- package/dist/scripts/payoutSubscriptions.js +185 -0
- package/dist/scripts/payoutSubscriptions.js.map +1 -0
- package/dist/scripts/removeUnreferenceSubs.js +39 -0
- package/dist/scripts/removeUnreferenceSubs.js.map +1 -0
- package/dist/scripts/test.js +33 -0
- package/dist/scripts/test.js.map +1 -0
- package/dist/server/index.js +30 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/middlewares/slack-auth.js +40 -0
- package/dist/server/middlewares/slack-auth.js.map +1 -0
- package/dist/server/v1/bypassCreator.js +81 -0
- package/dist/server/v1/bypassCreator.js.map +1 -0
- package/dist/server/v1/cancelSubscription.js +35 -0
- package/dist/server/v1/cancelSubscription.js.map +1 -0
- package/dist/server/v1/changeName.js +41 -0
- package/dist/server/v1/changeName.js.map +1 -0
- package/dist/server/v1/checkAuthSub.js +58 -0
- package/dist/server/v1/checkAuthSub.js.map +1 -0
- package/dist/server/v1/dashboardLink.js +30 -0
- package/dist/server/v1/dashboardLink.js.map +1 -0
- package/dist/server/v1/getSuspendedSubs.js +76 -0
- package/dist/server/v1/getSuspendedSubs.js.map +1 -0
- package/dist/server/v1/index.js +28 -0
- package/dist/server/v1/index.js.map +1 -0
- package/dist/server/v1/manualCreateSub.js +159 -0
- package/dist/server/v1/manualCreateSub.js.map +1 -0
- package/dist/server/v1/refund.js +78 -0
- package/dist/server/v1/refund.js.map +1 -0
- package/dist/server/v1/retrySubscription.js +81 -0
- package/dist/server/v1/retrySubscription.js.map +1 -0
- package/dist/server/v1/searchUser.js +169 -0
- package/dist/server/v1/searchUser.js.map +1 -0
- package/dist/server/v1/slack.js +14 -0
- package/dist/server/v1/slack.js.map +1 -0
- package/dist/src/index.js +24 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/pk-client.js +1103 -0
- package/dist/src/pk-client.js.map +1 -0
- package/dist/src/pk-sub.js +310 -0
- package/dist/src/pk-sub.js.map +1 -0
- package/dist/src/slack-client.js +48 -0
- package/dist/src/slack-client.js.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/support/index.js +19 -0
- package/dist/support/index.js.map +1 -0
- package/dist/support/metrics.js +21 -0
- package/dist/support/metrics.js.map +1 -0
- package/dist/support/mixpanel.js +7 -0
- package/dist/support/mixpanel.js.map +1 -0
- package/dist/support/slack-client.js +51 -0
- package/dist/support/slack-client.js.map +1 -0
- package/dist/support/uploadcare.js +140 -0
- package/dist/support/uploadcare.js.map +1 -0
- package/dist/support/utils.js +69 -0
- package/dist/support/utils.js.map +1 -0
- package/dist/typed-model/asset-category.js +38 -0
- package/dist/typed-model/asset-category.js.map +1 -0
- package/dist/typed-model/asset-constent-person.js +56 -0
- package/dist/typed-model/asset-constent-person.js.map +1 -0
- package/dist/typed-model/blocked-ip.js +34 -0
- package/dist/typed-model/blocked-ip.js.map +1 -0
- package/dist/typed-model/category-tag.js +46 -0
- package/dist/typed-model/category-tag.js.map +1 -0
- package/dist/typed-model/consent-person.js +55 -0
- package/dist/typed-model/consent-person.js.map +1 -0
- package/dist/typed-model/content-category-tag.js +58 -0
- package/dist/typed-model/content-category-tag.js.map +1 -0
- package/dist/typed-model/content-like.js +58 -0
- package/dist/typed-model/content-like.js.map +1 -0
- package/dist/typed-model/credit-payment.js +389 -0
- package/dist/typed-model/credit-payment.js.map +1 -0
- package/dist/typed-model/credit-payout.js +42 -0
- package/dist/typed-model/credit-payout.js.map +1 -0
- package/dist/typed-model/credit-purchase.js +46 -0
- package/dist/typed-model/credit-purchase.js.map +1 -0
- package/dist/typed-model/customer-profile.js +51 -0
- package/dist/typed-model/customer-profile.js.map +1 -0
- package/dist/typed-model/dashboard-user-permissions.js +34 -0
- package/dist/typed-model/dashboard-user-permissions.js.map +1 -0
- package/dist/typed-model/dashboard-user.js +109 -0
- package/dist/typed-model/dashboard-user.js.map +1 -0
- package/dist/typed-model/fan-list.js +46 -0
- package/dist/typed-model/fan-list.js.map +1 -0
- package/dist/typed-model/fraud.js +41 -0
- package/dist/typed-model/fraud.js.map +1 -0
- package/dist/typed-model/index.js +101 -0
- package/dist/typed-model/index.js.map +1 -0
- package/dist/typed-model/list-member.js +47 -0
- package/dist/typed-model/list-member.js.map +1 -0
- package/dist/typed-model/mass-message.js +43 -0
- package/dist/typed-model/mass-message.js.map +1 -0
- package/dist/typed-model/media-asset.js +73 -0
- package/dist/typed-model/media-asset.js.map +1 -0
- package/dist/typed-model/media-category.js +49 -0
- package/dist/typed-model/media-category.js.map +1 -0
- package/dist/typed-model/media.js +59 -0
- package/dist/typed-model/media.js.map +1 -0
- package/dist/typed-model/message-media.js +53 -0
- package/dist/typed-model/message-media.js.map +1 -0
- package/dist/typed-model/message.js +191 -0
- package/dist/typed-model/message.js.map +1 -0
- package/dist/typed-model/notification.js +84 -0
- package/dist/typed-model/notification.js.map +1 -0
- package/dist/typed-model/pb-sequelize.js +25 -0
- package/dist/typed-model/pb-sequelize.js.map +1 -0
- package/dist/typed-model/phone-chat-participation.js +127 -0
- package/dist/typed-model/phone-chat-participation.js.map +1 -0
- package/dist/typed-model/phone-chat.js +118 -0
- package/dist/typed-model/phone-chat.js.map +1 -0
- package/dist/typed-model/phone-owner.js +266 -0
- package/dist/typed-model/phone-owner.js.map +1 -0
- package/dist/typed-model/phone-user.js +109 -0
- package/dist/typed-model/phone-user.js.map +1 -0
- package/dist/typed-model/premium-content.js +123 -0
- package/dist/typed-model/premium-content.js.map +1 -0
- package/dist/typed-model/purchased-content.js +69 -0
- package/dist/typed-model/purchased-content.js.map +1 -0
- package/dist/typed-model/short-url.js +88 -0
- package/dist/typed-model/short-url.js.map +1 -0
- package/dist/typed-model/subscription-bundle.js +53 -0
- package/dist/typed-model/subscription-bundle.js.map +1 -0
- package/dist/typed-model/subscription-category-tags.js +58 -0
- package/dist/typed-model/subscription-category-tags.js.map +1 -0
- package/dist/typed-model/subscription-plan.js +126 -0
- package/dist/typed-model/subscription-plan.js.map +1 -0
- package/dist/typed-model/subscription-promotion.js +93 -0
- package/dist/typed-model/subscription-promotion.js.map +1 -0
- package/dist/typed-model/subscription.js +123 -0
- package/dist/typed-model/subscription.js.map +1 -0
- package/dist/typed-model/twilio-phone.js +92 -0
- package/dist/typed-model/twilio-phone.js.map +1 -0
- package/dist/typed-model/user-follow.js +103 -0
- package/dist/typed-model/user-follow.js.map +1 -0
- package/dist/typed-model/verification-code.js +52 -0
- package/dist/typed-model/verification-code.js.map +1 -0
- package/index.ts +3 -0
- package/package.json +72 -0
- package/src/index.ts +2 -0
- package/src/pk-client.ts +1650 -0
- package/src/pk-sub.ts +455 -0
- package/src/types.ts +40 -0
- package/support/index.ts +2 -0
- package/support/slack-client.ts +43 -0
- package/support/uploadcare.ts +191 -0
- package/support/utils.ts +72 -0
- package/typed-model/asset-category.ts +57 -0
- package/typed-model/asset-constent-person.ts +82 -0
- package/typed-model/blocked-ip.ts +52 -0
- package/typed-model/category-tag.ts +66 -0
- package/typed-model/consent-person.ts +78 -0
- package/typed-model/content-category-tag.ts +79 -0
- package/typed-model/content-like.ts +79 -0
- package/typed-model/credit-payment.ts +678 -0
- package/typed-model/credit-payout.ts +62 -0
- package/typed-model/credit-purchase.ts +67 -0
- package/typed-model/customer-profile.ts +70 -0
- package/typed-model/dashboard-user-permissions.ts +51 -0
- package/typed-model/dashboard-user.ts +146 -0
- package/typed-model/fan-list.ts +68 -0
- package/typed-model/fraud.ts +62 -0
- package/typed-model/index.ts +58 -0
- package/typed-model/list-member.ts +67 -0
- package/typed-model/mass-message.ts +64 -0
- package/typed-model/media-asset.ts +99 -0
- package/typed-model/media-category.ts +70 -0
- package/typed-model/media.ts +85 -0
- package/typed-model/message-media.ts +74 -0
- package/typed-model/message.ts +258 -0
- package/typed-model/notification.ts +130 -0
- package/typed-model/pb-sequelize.ts +25 -0
- package/typed-model/phone-chat-participation.ts +166 -0
- package/typed-model/phone-chat.ts +185 -0
- package/typed-model/phone-owner.ts +341 -0
- package/typed-model/phone-user.ts +146 -0
- package/typed-model/premium-content.ts +164 -0
- package/typed-model/purchased-content.ts +100 -0
- package/typed-model/short-url.ts +120 -0
- package/typed-model/subscription-bundle.ts +75 -0
- package/typed-model/subscription-category-tags.ts +79 -0
- package/typed-model/subscription-plan.ts +171 -0
- package/typed-model/subscription-promotion.ts +125 -0
- package/typed-model/subscription.ts +170 -0
- package/typed-model/twilio-phone.ts +149 -0
- package/typed-model/user-follow.ts +141 -0
- package/typed-model/verification-code.ts +74 -0
package/src/pk-sub.ts
ADDED
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import PhoneUser from "../typed-model/phone-user";
|
|
2
|
+
import PhoneOwner from "../typed-model/phone-owner";
|
|
3
|
+
import Subscription from "../typed-model/subscription";
|
|
4
|
+
import SubscriptionPlan from "../typed-model/subscription-plan";
|
|
5
|
+
import SubscriptionBundle from "../typed-model/subscription-bundle";
|
|
6
|
+
import { TransactionData } from "./types";
|
|
7
|
+
|
|
8
|
+
import sequelize from "../typed-model/pb-sequelize";
|
|
9
|
+
import CreditPayments from "../typed-model/credit-payment";
|
|
10
|
+
import { ProcessorId } from "../typed-model/customer-profile";
|
|
11
|
+
|
|
12
|
+
export class SubscriptionInitException extends Error {
|
|
13
|
+
constructor(message: string) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "SubscriptionInitException";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class SubscriptionChargeException extends Error {
|
|
20
|
+
constructor(message: string) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "SubscriptionChargeException";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class SubscriptionChargeCreditCardException extends SubscriptionChargeException {
|
|
27
|
+
constructor(message: string) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "SubscriptionChargeCreditCardException";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class SubscriptionChargeProfileException extends SubscriptionChargeException {
|
|
34
|
+
constructor(message: string) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "SubscriptionChargeProfileException";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class CreatePaymentProfileException extends Error {
|
|
41
|
+
constructor(message: string) {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "CreatePaymentProfileException";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class InvalidSubscriptionRequestException extends Error {
|
|
48
|
+
constructor(message: string) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "InvalidSubscriptionRequestException";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class CliqSubscriptionCreationException extends Error {
|
|
55
|
+
constructor(message: string) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.name = "CliqSubscriptionCreationException";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class CliqPaymentCreationException extends Error {
|
|
62
|
+
constructor() {
|
|
63
|
+
super("Failed to create Cliq credit payment");
|
|
64
|
+
this.name = "CliqSubscriptionCreationException";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class CreatePKSubscriptionException extends Error {
|
|
69
|
+
constructor(name: string, message: string) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.name = name;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
import {
|
|
76
|
+
chargeCustomerByPaymentProfile,
|
|
77
|
+
chargeCreditCardAndCreateProfile,
|
|
78
|
+
updateUserPaymentProfile,
|
|
79
|
+
createSubscription,
|
|
80
|
+
voidTransaction,
|
|
81
|
+
createSubscriptionWithCreditCard,
|
|
82
|
+
} from "./pk-client";
|
|
83
|
+
import { rejects } from "assert";
|
|
84
|
+
|
|
85
|
+
export class PKSub {
|
|
86
|
+
user: PhoneUser;
|
|
87
|
+
creator: PhoneOwner;
|
|
88
|
+
subscriptionPlan: SubscriptionPlan;
|
|
89
|
+
trialAmount: number;
|
|
90
|
+
trialLength: number;
|
|
91
|
+
amountInCents: number;
|
|
92
|
+
intervalCount: number;
|
|
93
|
+
intervalUnit: "months" | "days";
|
|
94
|
+
transactionData?: TransactionData;
|
|
95
|
+
useCoachingMid: boolean;
|
|
96
|
+
paymentProfile?: {
|
|
97
|
+
customerProfileId: string;
|
|
98
|
+
customerPaymentProfileId: string;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
constructor(
|
|
102
|
+
user: PhoneUser,
|
|
103
|
+
creator: PhoneOwner,
|
|
104
|
+
subscriptionPlan: SubscriptionPlan,
|
|
105
|
+
subscriptionBundle?: SubscriptionBundle,
|
|
106
|
+
isPreviouslySubscribed?: boolean,
|
|
107
|
+
useCoachingMid?: boolean
|
|
108
|
+
) {
|
|
109
|
+
this.user = user;
|
|
110
|
+
this.creator = creator;
|
|
111
|
+
this.subscriptionPlan = subscriptionPlan;
|
|
112
|
+
this.useCoachingMid = !!useCoachingMid;
|
|
113
|
+
|
|
114
|
+
if (!user) {
|
|
115
|
+
throw new SubscriptionInitException("User cannot be null");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!creator || !creator.id) {
|
|
119
|
+
console.log(creator);
|
|
120
|
+
throw new SubscriptionInitException("Creator cannot be null");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
var promotion =
|
|
124
|
+
subscriptionPlan.SubscriptionPromotions &&
|
|
125
|
+
subscriptionPlan.SubscriptionPromotions[0];
|
|
126
|
+
|
|
127
|
+
this.trialAmount = 0;
|
|
128
|
+
this.trialLength = 0;
|
|
129
|
+
this.amountInCents = subscriptionPlan.amountInCents!;
|
|
130
|
+
this.intervalCount = subscriptionPlan.intervalCount || 1;
|
|
131
|
+
this.intervalUnit = subscriptionPlan.intervalUnit || "months";
|
|
132
|
+
|
|
133
|
+
if (subscriptionBundle) {
|
|
134
|
+
promotion = null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!isPreviouslySubscribed && promotion) {
|
|
138
|
+
this.trialAmount = Math.round(
|
|
139
|
+
(1 - promotion.trialDiscountPercentage) *
|
|
140
|
+
subscriptionPlan.amountInCents!
|
|
141
|
+
);
|
|
142
|
+
this.trialLength = promotion.trialLength;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (subscriptionBundle) {
|
|
146
|
+
this.amountInCents = Math.round(
|
|
147
|
+
(1 - subscriptionBundle.bundleDiscountPercentage!) *
|
|
148
|
+
this.amountInCents *
|
|
149
|
+
subscriptionBundle.bundleIntervalCount!
|
|
150
|
+
);
|
|
151
|
+
this.intervalCount = subscriptionBundle.bundleIntervalCount!;
|
|
152
|
+
this.trialAmount = 0;
|
|
153
|
+
this.trialLength = 0;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async _charge(
|
|
158
|
+
amount: number,
|
|
159
|
+
description: string
|
|
160
|
+
): Promise<
|
|
161
|
+
| {
|
|
162
|
+
amountInCents: number;
|
|
163
|
+
transactionId: string;
|
|
164
|
+
pkCustomerProfileId?: string;
|
|
165
|
+
pkCustomerPaymentProfileId?: string;
|
|
166
|
+
processorId: ProcessorId;
|
|
167
|
+
}
|
|
168
|
+
| undefined
|
|
169
|
+
> {
|
|
170
|
+
console.log("Start Charge...");
|
|
171
|
+
if (this.paymentProfile) {
|
|
172
|
+
console.log("Charge by payment profile...");
|
|
173
|
+
let response = await chargeCustomerByPaymentProfile(
|
|
174
|
+
this.paymentProfile.customerProfileId,
|
|
175
|
+
this.paymentProfile.customerPaymentProfileId,
|
|
176
|
+
amount,
|
|
177
|
+
description
|
|
178
|
+
).catch((err) => {
|
|
179
|
+
throw new SubscriptionChargeProfileException(err);
|
|
180
|
+
});
|
|
181
|
+
return {
|
|
182
|
+
...response,
|
|
183
|
+
pkCustomerProfileId: this.paymentProfile.customerProfileId,
|
|
184
|
+
pkCustomerPaymentProfileId:
|
|
185
|
+
this.paymentProfile.customerPaymentProfileId,
|
|
186
|
+
};
|
|
187
|
+
} else if (this.transactionData) {
|
|
188
|
+
console.log("Charge by credit card...");
|
|
189
|
+
let chargeResponse = await chargeCreditCardAndCreateProfile(
|
|
190
|
+
this.user,
|
|
191
|
+
this.transactionData,
|
|
192
|
+
amount,
|
|
193
|
+
description,
|
|
194
|
+
true,
|
|
195
|
+
this.useCoachingMid
|
|
196
|
+
).catch((err) => {
|
|
197
|
+
throw new SubscriptionChargeCreditCardException(err);
|
|
198
|
+
});
|
|
199
|
+
try {
|
|
200
|
+
// if (chargeResponse.pkCustomerProfileId) {
|
|
201
|
+
// await updateUserPaymentProfile(
|
|
202
|
+
// this.user.id,
|
|
203
|
+
// chargeResponse.pkCustomerProfileId,
|
|
204
|
+
// chargeResponse.pkCustomerPaymentProfileId!
|
|
205
|
+
// );
|
|
206
|
+
// this.paymentProfile = {
|
|
207
|
+
// customerProfileId: chargeResponse.pkCustomerProfileId,
|
|
208
|
+
// customerPaymentProfileId:
|
|
209
|
+
// chargeResponse.pkCustomerPaymentProfileId!,
|
|
210
|
+
// };
|
|
211
|
+
// }
|
|
212
|
+
return {
|
|
213
|
+
amountInCents: chargeResponse.amountInCents,
|
|
214
|
+
transactionId: chargeResponse.transactionId,
|
|
215
|
+
processorId: chargeResponse.processorId,
|
|
216
|
+
// pkCustomerProfileId: chargeResponse.pkCustomerProfileId,
|
|
217
|
+
// pkCustomerPaymentProfileId: chargeResponse.pkCustomerPaymentProfileId,
|
|
218
|
+
};
|
|
219
|
+
} catch (err) {
|
|
220
|
+
console.log(err);
|
|
221
|
+
if (chargeResponse.transactionId) {
|
|
222
|
+
// Void transaction
|
|
223
|
+
await voidTransaction(
|
|
224
|
+
chargeResponse.transactionId,
|
|
225
|
+
chargeResponse.processorId
|
|
226
|
+
);
|
|
227
|
+
console.log("Failed to create customer id");
|
|
228
|
+
}
|
|
229
|
+
throw new CreatePaymentProfileException(
|
|
230
|
+
"Failed to create customer profile"
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
throw new InvalidSubscriptionRequestException(
|
|
235
|
+
"Must include transaction data or customer profile"
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async _chargeFirstPayment(): Promise<{
|
|
240
|
+
amountInCents: number;
|
|
241
|
+
transactionId: string;
|
|
242
|
+
pkCustomerProfileId?: string;
|
|
243
|
+
pkCustomerPaymentProfileId?: string;
|
|
244
|
+
processorId: ProcessorId;
|
|
245
|
+
}> {
|
|
246
|
+
console.log("Charge first payment");
|
|
247
|
+
if (this.trialLength > 0 && this.trialAmount > 0) {
|
|
248
|
+
const charge = await this._charge(
|
|
249
|
+
this.trialAmount,
|
|
250
|
+
`${this.creator.name} ${
|
|
251
|
+
this.intervalUnit == "days" ? "weekly" : "monthly"
|
|
252
|
+
} subscriptions`
|
|
253
|
+
);
|
|
254
|
+
this.trialLength = this.trialLength - 1;
|
|
255
|
+
if (this.trialLength == 0) {
|
|
256
|
+
this.trialAmount = 0;
|
|
257
|
+
}
|
|
258
|
+
return charge!;
|
|
259
|
+
} else {
|
|
260
|
+
const charge = await this._charge(
|
|
261
|
+
this.amountInCents,
|
|
262
|
+
`${this.creator.name} ${
|
|
263
|
+
this.intervalUnit == "days" ? "weekly" : "monthly"
|
|
264
|
+
} subscriptions`
|
|
265
|
+
);
|
|
266
|
+
return charge!;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async _initializePkSubscription(
|
|
271
|
+
processorId: ProcessorId
|
|
272
|
+
): Promise<{ pkSubId: string }> {
|
|
273
|
+
if (this.paymentProfile) {
|
|
274
|
+
console.log("Creating sub with payment profile");
|
|
275
|
+
return await createSubscription({
|
|
276
|
+
intervalCount: this.intervalCount,
|
|
277
|
+
intervalUnit: this.intervalUnit,
|
|
278
|
+
trialLength: this.trialLength,
|
|
279
|
+
trialAmountInCents: this.trialAmount,
|
|
280
|
+
customerProfileId: this.paymentProfile!.customerProfileId,
|
|
281
|
+
customerPaymentProfileId: this.paymentProfile!.customerPaymentProfileId,
|
|
282
|
+
creatorName: this.creator.name!,
|
|
283
|
+
amountInCents: this.amountInCents,
|
|
284
|
+
processorId: processorId,
|
|
285
|
+
}).catch((err) => {
|
|
286
|
+
throw new CreatePKSubscriptionException(
|
|
287
|
+
"CreatePKSubscriptionWithProfileException",
|
|
288
|
+
"Failed to create authorize subscription with payment profile"
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
} else if (this.transactionData) {
|
|
292
|
+
console.log("Creating sub credit card");
|
|
293
|
+
let subscriptionResponse = await createSubscriptionWithCreditCard({
|
|
294
|
+
intervalCount: this.intervalCount,
|
|
295
|
+
intervalUnit: this.intervalUnit,
|
|
296
|
+
trialLength: this.trialLength,
|
|
297
|
+
trialAmountInCents: this.trialAmount,
|
|
298
|
+
data: this.transactionData,
|
|
299
|
+
creatorName: this.creator.name!,
|
|
300
|
+
amountInCents: this.amountInCents,
|
|
301
|
+
processorId: processorId,
|
|
302
|
+
}).catch((err) => {
|
|
303
|
+
throw new CreatePKSubscriptionException(
|
|
304
|
+
"CreatePKSubscriptionWithCardException",
|
|
305
|
+
"Failed to create authorize subscription with credit card"
|
|
306
|
+
);
|
|
307
|
+
});
|
|
308
|
+
if (subscriptionResponse.paymentProfile.pkCustomerProfileId) {
|
|
309
|
+
console.log("Updating customer profile from sub response");
|
|
310
|
+
await updateUserPaymentProfile(
|
|
311
|
+
this.user.id,
|
|
312
|
+
subscriptionResponse.paymentProfile.pkCustomerProfileId,
|
|
313
|
+
processorId,
|
|
314
|
+
subscriptionResponse.paymentProfile.pkCustomerPaymentProfileId
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
return subscriptionResponse;
|
|
318
|
+
}
|
|
319
|
+
throw new InvalidSubscriptionRequestException(
|
|
320
|
+
"No payment method for subscription"
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
setTransactionData(transactionData: TransactionData) {
|
|
325
|
+
this.transactionData = transactionData;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
setPaymentProfile(paymentProfile: {
|
|
329
|
+
customerProfileId: string;
|
|
330
|
+
customerPaymentProfileId: string;
|
|
331
|
+
}) {
|
|
332
|
+
this.paymentProfile = paymentProfile;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/*
|
|
336
|
+
1) Start SQL transaction
|
|
337
|
+
2) Create sequelize updates
|
|
338
|
+
- Create first payment
|
|
339
|
+
- Create subscription
|
|
340
|
+
3) Create first charge
|
|
341
|
+
- If charge fails, rollback transaction
|
|
342
|
+
4) Create subscription
|
|
343
|
+
- If subscription fails
|
|
344
|
+
- Refund transaction
|
|
345
|
+
- Rollback transaction
|
|
346
|
+
5) Commit transaction
|
|
347
|
+
*/
|
|
348
|
+
|
|
349
|
+
async buildSubscription(): Promise<{
|
|
350
|
+
subscription: any;
|
|
351
|
+
creditPayment: any;
|
|
352
|
+
pkSubId: string;
|
|
353
|
+
}> {
|
|
354
|
+
let firstPayment;
|
|
355
|
+
try {
|
|
356
|
+
if (this.trialLength == 0 || this.trialAmount > 0) {
|
|
357
|
+
firstPayment = await this._chargeFirstPayment();
|
|
358
|
+
}
|
|
359
|
+
} catch (err) {
|
|
360
|
+
if (err instanceof Error) {
|
|
361
|
+
throw err;
|
|
362
|
+
} else {
|
|
363
|
+
throw new Error("Failed to charge: " + err);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (!firstPayment) {
|
|
368
|
+
throw new Error("Failed to charge credit card");
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
let transaction = await sequelize.transaction();
|
|
372
|
+
let subscription;
|
|
373
|
+
try {
|
|
374
|
+
subscription = await Subscription.create(
|
|
375
|
+
{
|
|
376
|
+
userId: this.user.id,
|
|
377
|
+
subscriptionPlanId: this.subscriptionPlan.id,
|
|
378
|
+
lastPaymentDate: new Date(),
|
|
379
|
+
cancelDate: this.subscriptionPlan.accessEndDate || undefined,
|
|
380
|
+
processorId: firstPayment.processorId,
|
|
381
|
+
},
|
|
382
|
+
{ transaction }
|
|
383
|
+
);
|
|
384
|
+
} catch (err) {
|
|
385
|
+
console.log(err);
|
|
386
|
+
await transaction.rollback();
|
|
387
|
+
// Void first transaction
|
|
388
|
+
await voidTransaction(
|
|
389
|
+
firstPayment.transactionId,
|
|
390
|
+
firstPayment.processorId
|
|
391
|
+
);
|
|
392
|
+
throw new CliqSubscriptionCreationException(
|
|
393
|
+
"Failed to create subscription in CLIQ"
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
let creditPayment;
|
|
398
|
+
try {
|
|
399
|
+
if (subscription) {
|
|
400
|
+
creditPayment = await CreditPayments.logSubscriptionPayment(
|
|
401
|
+
this.user.id,
|
|
402
|
+
this.creator,
|
|
403
|
+
firstPayment.amountInCents,
|
|
404
|
+
subscription,
|
|
405
|
+
transaction,
|
|
406
|
+
firstPayment.transactionId
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
} catch (err) {
|
|
410
|
+
console.log(err);
|
|
411
|
+
await transaction.rollback();
|
|
412
|
+
// Void first transaction
|
|
413
|
+
if (firstPayment) {
|
|
414
|
+
await voidTransaction(
|
|
415
|
+
firstPayment.transactionId,
|
|
416
|
+
firstPayment.processorId
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
throw new CliqPaymentCreationException();
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
let pkSub;
|
|
423
|
+
let setRecurring =
|
|
424
|
+
!this.subscriptionPlan.accessEndDate &&
|
|
425
|
+
!this.subscriptionPlan.oneTimePurchase;
|
|
426
|
+
try {
|
|
427
|
+
if (setRecurring) {
|
|
428
|
+
pkSub = await this._initializePkSubscription(firstPayment.processorId);
|
|
429
|
+
}
|
|
430
|
+
} catch (err) {
|
|
431
|
+
console.log(err);
|
|
432
|
+
await transaction.rollback();
|
|
433
|
+
// Void first transaction
|
|
434
|
+
await voidTransaction(
|
|
435
|
+
firstPayment.transactionId,
|
|
436
|
+
firstPayment.processorId
|
|
437
|
+
);
|
|
438
|
+
throw new CreatePKSubscriptionException(
|
|
439
|
+
"CreatePKSubscriptionException",
|
|
440
|
+
"Failed to create subscription: " + err
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
await transaction.commit();
|
|
445
|
+
if (subscription && setRecurring) {
|
|
446
|
+
subscription.pkSubscriptionId = pkSub.pkSubId;
|
|
447
|
+
await subscription.save();
|
|
448
|
+
}
|
|
449
|
+
return {
|
|
450
|
+
subscription,
|
|
451
|
+
creditPayment,
|
|
452
|
+
pkSubId: pkSub && pkSub.pkSubId,
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type CreditCard = {
|
|
2
|
+
cardNumber: string;
|
|
3
|
+
expirationDate: string;
|
|
4
|
+
cardCode: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type Payment = {
|
|
8
|
+
creditCard: CreditCard;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type Address = {
|
|
12
|
+
firstName: string;
|
|
13
|
+
lastName: string;
|
|
14
|
+
address: string;
|
|
15
|
+
city: string;
|
|
16
|
+
zip: string;
|
|
17
|
+
country: string;
|
|
18
|
+
state: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type TransactionData = {
|
|
22
|
+
payment: Payment;
|
|
23
|
+
billTo: Address;
|
|
24
|
+
defaultPaymentProfile: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type PkSubStatus =
|
|
28
|
+
| "active"
|
|
29
|
+
| "expired"
|
|
30
|
+
| "suspended"
|
|
31
|
+
| "canceled"
|
|
32
|
+
| "terminated";
|
|
33
|
+
|
|
34
|
+
export type ArbTransaction = {
|
|
35
|
+
getTransId: () => string;
|
|
36
|
+
getResponse: () => string;
|
|
37
|
+
getSubmitTimeUTC: () => string;
|
|
38
|
+
getPayNum: () => number;
|
|
39
|
+
getAttemptNum: () => string;
|
|
40
|
+
};
|
package/support/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
const WEBHOOK_URL =
|
|
3
|
+
"https://hooks.slack.com/services/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";
|
|
4
|
+
|
|
5
|
+
export const postSlackNotification = async (body: any) => {
|
|
6
|
+
try {
|
|
7
|
+
await axios.post(WEBHOOK_URL, body);
|
|
8
|
+
} catch (err) {
|
|
9
|
+
console.log(err);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const postSlackText = async (text: string) => {
|
|
14
|
+
await postSlackNotification({ text });
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const postSubscriptionAlarm = async (pkIds: string[]) => {
|
|
18
|
+
try {
|
|
19
|
+
await postSlackNotification({
|
|
20
|
+
text: `Unreferenced subscriptions created`,
|
|
21
|
+
blocks: [
|
|
22
|
+
{
|
|
23
|
+
type: "header",
|
|
24
|
+
text: {
|
|
25
|
+
type: "plain_text",
|
|
26
|
+
text: `Unreferenced subscriptions created`,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: "section",
|
|
31
|
+
fields: [
|
|
32
|
+
{
|
|
33
|
+
type: "mrkdwn",
|
|
34
|
+
text: `*pk_subscription_ids:*\n${pkIds.join(", ")}`,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
} catch (err) {
|
|
41
|
+
console.log(err);
|
|
42
|
+
}
|
|
43
|
+
};
|