@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-client.ts
ADDED
@@ -0,0 +1,1650 @@
|
|
1
|
+
import PKCustomerProfile, {
|
2
|
+
ProcessorId,
|
3
|
+
} from "../typed-model/customer-profile";
|
4
|
+
import PhoneUser from "../typed-model/phone-user";
|
5
|
+
|
6
|
+
import {
|
7
|
+
APIContracts,
|
8
|
+
APIControllers,
|
9
|
+
Constants as PKConstants,
|
10
|
+
} from "authorizenet";
|
11
|
+
|
12
|
+
import { TransactionData, CreditCard, Address, PkSubStatus } from "./types";
|
13
|
+
|
14
|
+
import moment = require("moment");
|
15
|
+
import {
|
16
|
+
CustomerProfile,
|
17
|
+
Subscription,
|
18
|
+
SubscriptionPlan,
|
19
|
+
} from "../typed-model";
|
20
|
+
|
21
|
+
const mode = APIContracts.ValidationModeEnum.LIVEMODE;
|
22
|
+
const DEFAULT_PROCESSOR = ProcessorId.KAJA;
|
23
|
+
|
24
|
+
export type TransactionStatus =
|
25
|
+
| "authorizedPendingCapture"
|
26
|
+
| "capturedPendingSettlement"
|
27
|
+
| "communicationError"
|
28
|
+
| "refundSettledSuccessfully"
|
29
|
+
| "refundPendingSettlement"
|
30
|
+
| "approvedReview"
|
31
|
+
| "declined"
|
32
|
+
| "couldNotVoid"
|
33
|
+
| "expired"
|
34
|
+
| "generalError"
|
35
|
+
| "failedReview"
|
36
|
+
| "settledSuccessfully"
|
37
|
+
| "settlementError"
|
38
|
+
| "underReview"
|
39
|
+
| "voided"
|
40
|
+
| "FDSPendingReview"
|
41
|
+
| "FDSAuthorizedPendingReview"
|
42
|
+
| "returnedItem";
|
43
|
+
|
44
|
+
const getCredentialsForProcessorId = (processorId: string) => {
|
45
|
+
let apiKey = process.env.PK_API_LOGIN_KEY!;
|
46
|
+
let transactionKey = process.env.PK_TRANSACTION_KEY!;
|
47
|
+
|
48
|
+
switch (processorId) {
|
49
|
+
case ProcessorId.HUMBOLDT:
|
50
|
+
apiKey = process.env.PK_API_LOGIN_KEY!;
|
51
|
+
transactionKey = process.env.PK_TRANSACTION_KEY!;
|
52
|
+
break;
|
53
|
+
case ProcessorId.KAJA:
|
54
|
+
apiKey = process.env.KAJA_API_LOGIN_KEY!;
|
55
|
+
transactionKey = process.env.KAJA_TRANSACTION_KEY!;
|
56
|
+
break;
|
57
|
+
case ProcessorId.KAJACOACHING:
|
58
|
+
apiKey = process.env.KAJACOACHING_API_LOGIN_KEY!;
|
59
|
+
transactionKey = process.env.KAJACOACHING_TRANSACTION_KEY!;
|
60
|
+
break;
|
61
|
+
}
|
62
|
+
|
63
|
+
return {
|
64
|
+
apiKey,
|
65
|
+
transactionKey,
|
66
|
+
};
|
67
|
+
};
|
68
|
+
|
69
|
+
const getMerchantAuthForCustomer = async (customerProfileId?: string) => {
|
70
|
+
let processorId = ProcessorId.HUMBOLDT;
|
71
|
+
let customerProfile = await CustomerProfile.findOne({
|
72
|
+
where: { customerProfileId },
|
73
|
+
});
|
74
|
+
|
75
|
+
if (customerProfile) {
|
76
|
+
processorId = customerProfile.processorId;
|
77
|
+
}
|
78
|
+
return getMerchantAuthForProcessor(processorId);
|
79
|
+
};
|
80
|
+
|
81
|
+
const getMerchantAuthForProcessor = (processorId: ProcessorId) => {
|
82
|
+
const response = getCredentialsForProcessorId(processorId);
|
83
|
+
const apiKey = response.apiKey;
|
84
|
+
const transactionKey = response.transactionKey;
|
85
|
+
|
86
|
+
const merchantAuthenticationType =
|
87
|
+
new APIContracts.MerchantAuthenticationType();
|
88
|
+
merchantAuthenticationType.setName(apiKey);
|
89
|
+
merchantAuthenticationType.setTransactionKey(transactionKey);
|
90
|
+
return {
|
91
|
+
merchantAuthenticationType,
|
92
|
+
processorId,
|
93
|
+
};
|
94
|
+
};
|
95
|
+
|
96
|
+
const buildCreditCard = (card: CreditCard) => {
|
97
|
+
let creditCard = new APIContracts.CreditCardType();
|
98
|
+
creditCard.setCardNumber(card.cardNumber);
|
99
|
+
creditCard.setExpirationDate(card.expirationDate);
|
100
|
+
creditCard.setCardCode(card.cardCode);
|
101
|
+
return creditCard;
|
102
|
+
};
|
103
|
+
|
104
|
+
const buildAddress = (address: Address) => {
|
105
|
+
let customerAddress = new APIContracts.CustomerAddressType();
|
106
|
+
customerAddress.setFirstName(address.firstName);
|
107
|
+
customerAddress.setLastName(address.lastName);
|
108
|
+
customerAddress.setAddress(address.address);
|
109
|
+
customerAddress.setCity(address.city);
|
110
|
+
customerAddress.setState(address.state);
|
111
|
+
customerAddress.setZip(address.zip);
|
112
|
+
customerAddress.setCountry(address.country);
|
113
|
+
return customerAddress;
|
114
|
+
};
|
115
|
+
|
116
|
+
export const createCustomerProfile = async (
|
117
|
+
user: PhoneUser,
|
118
|
+
data: TransactionData
|
119
|
+
) => {
|
120
|
+
console.log("createCustomerProfile");
|
121
|
+
let { merchantAuthenticationType, processorId } =
|
122
|
+
await getMerchantAuthForProcessor(DEFAULT_PROCESSOR);
|
123
|
+
|
124
|
+
let creditCard = buildCreditCard(data.payment.creditCard);
|
125
|
+
let paymentType = new APIContracts.PaymentType();
|
126
|
+
paymentType.setCreditCard(creditCard);
|
127
|
+
|
128
|
+
let customerPaymentProfileType =
|
129
|
+
new APIContracts.CustomerPaymentProfileType();
|
130
|
+
customerPaymentProfileType.setCustomerType(
|
131
|
+
APIContracts.CustomerTypeEnum.INDIVIDUAL
|
132
|
+
);
|
133
|
+
customerPaymentProfileType.setPayment(paymentType);
|
134
|
+
customerPaymentProfileType.setDefaultPaymentProfile(true);
|
135
|
+
|
136
|
+
if (data.billTo) {
|
137
|
+
let customerAddress = buildAddress(data.billTo);
|
138
|
+
customerPaymentProfileType.setBillTo(customerAddress);
|
139
|
+
}
|
140
|
+
|
141
|
+
let paymentProfilesList = [customerPaymentProfileType];
|
142
|
+
|
143
|
+
let customerProfileType = new APIContracts.CustomerProfileType();
|
144
|
+
customerProfileType.setMerchantCustomerId(user.id);
|
145
|
+
customerProfileType.setEmail(user.email);
|
146
|
+
customerProfileType.setPaymentProfiles(paymentProfilesList);
|
147
|
+
|
148
|
+
let createRequest = new APIContracts.CreateCustomerProfileRequest();
|
149
|
+
createRequest.setProfile(customerProfileType);
|
150
|
+
createRequest.setValidationMode(mode);
|
151
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
152
|
+
|
153
|
+
//pretty print request
|
154
|
+
console.log(JSON.stringify(createRequest.getJSON(), null, 2));
|
155
|
+
|
156
|
+
let ctrl = new APIControllers.CreateCustomerProfileController(
|
157
|
+
createRequest.getJSON()
|
158
|
+
);
|
159
|
+
if (process.env.ENVIRONMENT === "production") {
|
160
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
161
|
+
}
|
162
|
+
|
163
|
+
return new Promise((resolve, reject) => {
|
164
|
+
ctrl.execute(function () {
|
165
|
+
let apiResponse = ctrl.getResponse();
|
166
|
+
|
167
|
+
let response = new APIContracts.CreateCustomerProfileResponse(
|
168
|
+
apiResponse
|
169
|
+
);
|
170
|
+
|
171
|
+
//pretty print response
|
172
|
+
console.log(JSON.stringify(response, null, 2));
|
173
|
+
|
174
|
+
if (response != null) {
|
175
|
+
if (
|
176
|
+
response.getMessages().getResultCode() ==
|
177
|
+
APIContracts.MessageTypeEnum.OK
|
178
|
+
) {
|
179
|
+
PKCustomerProfile.create({
|
180
|
+
userId: user.id,
|
181
|
+
customerProfileId: response.getCustomerProfileId(),
|
182
|
+
processorId,
|
183
|
+
}).then(() =>
|
184
|
+
resolve([
|
185
|
+
response.getCustomerProfileId(),
|
186
|
+
response.getCustomerPaymentProfileIdList()[0],
|
187
|
+
])
|
188
|
+
);
|
189
|
+
} else {
|
190
|
+
reject(response.getMessages().getMessage()[0].getText());
|
191
|
+
}
|
192
|
+
} else {
|
193
|
+
reject("Null response received");
|
194
|
+
}
|
195
|
+
});
|
196
|
+
});
|
197
|
+
};
|
198
|
+
|
199
|
+
export const getCustomerProfile = async (
|
200
|
+
customerProfileId?: string,
|
201
|
+
userId?: string
|
202
|
+
): Promise<any> => {
|
203
|
+
let { merchantAuthenticationType, processorId } =
|
204
|
+
await getMerchantAuthForCustomer(customerProfileId);
|
205
|
+
|
206
|
+
let getRequest = new APIContracts.GetCustomerProfileRequest();
|
207
|
+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
|
208
|
+
if (userId) {
|
209
|
+
getRequest.setMerchantCustomerId(userId);
|
210
|
+
} else {
|
211
|
+
getRequest.setCustomerProfileId(customerProfileId);
|
212
|
+
}
|
213
|
+
|
214
|
+
//pretty print request
|
215
|
+
// console.log(JSON.stringify(getRequest.getJSON(), null, 2));
|
216
|
+
|
217
|
+
let ctrl = new APIControllers.GetCustomerProfileController(
|
218
|
+
getRequest.getJSON()
|
219
|
+
);
|
220
|
+
if (process.env.ENVIRONMENT === "production") {
|
221
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
222
|
+
}
|
223
|
+
|
224
|
+
return new Promise((resolve, reject) => {
|
225
|
+
ctrl.execute(function () {
|
226
|
+
let apiResponse = ctrl.getResponse();
|
227
|
+
|
228
|
+
let response = new APIContracts.GetCustomerProfileResponse(apiResponse);
|
229
|
+
|
230
|
+
//pretty print response
|
231
|
+
|
232
|
+
if (response != null) {
|
233
|
+
if (
|
234
|
+
response.getMessages().getResultCode() ==
|
235
|
+
APIContracts.MessageTypeEnum.OK
|
236
|
+
) {
|
237
|
+
resolve(response.getProfile());
|
238
|
+
} else {
|
239
|
+
reject(response.getMessages().getMessage()[0].getText());
|
240
|
+
}
|
241
|
+
} else {
|
242
|
+
reject("Null response received");
|
243
|
+
}
|
244
|
+
});
|
245
|
+
});
|
246
|
+
};
|
247
|
+
|
248
|
+
export const getCustomerPaymentProfile = async (
|
249
|
+
customerProfileId: string,
|
250
|
+
customerPaymentProfileId: string
|
251
|
+
): Promise<TransactionData> => {
|
252
|
+
let { merchantAuthenticationType, processorId } =
|
253
|
+
await getMerchantAuthForCustomer(customerProfileId);
|
254
|
+
|
255
|
+
let getRequest = new APIContracts.GetCustomerPaymentProfileRequest();
|
256
|
+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
|
257
|
+
getRequest.setCustomerProfileId(customerProfileId);
|
258
|
+
getRequest.setCustomerPaymentProfileId(customerPaymentProfileId);
|
259
|
+
|
260
|
+
//pretty print request
|
261
|
+
//console.log(JSON.stringify(createRequest.getJSON(), null, 2));
|
262
|
+
|
263
|
+
let ctrl = new APIControllers.GetCustomerProfileController(
|
264
|
+
getRequest.getJSON()
|
265
|
+
);
|
266
|
+
if (process.env.ENVIRONMENT === "production") {
|
267
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
268
|
+
}
|
269
|
+
return new Promise((resolve, reject) => {
|
270
|
+
ctrl.execute(function () {
|
271
|
+
let apiResponse = ctrl.getResponse();
|
272
|
+
|
273
|
+
let response = new APIContracts.GetCustomerPaymentProfileResponse(
|
274
|
+
apiResponse
|
275
|
+
);
|
276
|
+
|
277
|
+
//pretty print response
|
278
|
+
//console.log(JSON.stringify(response, null, 2));
|
279
|
+
|
280
|
+
if (response != null) {
|
281
|
+
if (
|
282
|
+
response.getMessages().getResultCode() ==
|
283
|
+
APIContracts.MessageTypeEnum.OK
|
284
|
+
) {
|
285
|
+
resolve(response.getPaymentProfile());
|
286
|
+
} else {
|
287
|
+
reject(response.getMessages().getMessage()[0].getText());
|
288
|
+
}
|
289
|
+
} else {
|
290
|
+
reject("Get payment profile Null response received");
|
291
|
+
}
|
292
|
+
});
|
293
|
+
});
|
294
|
+
};
|
295
|
+
|
296
|
+
export const addPaymentProfile = async (
|
297
|
+
customerProfileId: string,
|
298
|
+
data: TransactionData
|
299
|
+
): Promise<string> => {
|
300
|
+
let { merchantAuthenticationType, processorId } =
|
301
|
+
await getMerchantAuthForCustomer(customerProfileId);
|
302
|
+
|
303
|
+
let creditCard = buildCreditCard(data.payment.creditCard);
|
304
|
+
|
305
|
+
let paymentType = new APIContracts.PaymentType();
|
306
|
+
paymentType.setCreditCard(creditCard);
|
307
|
+
|
308
|
+
let customerAddress = buildAddress(data.billTo);
|
309
|
+
|
310
|
+
let profile = new APIContracts.CustomerPaymentProfileType();
|
311
|
+
profile.setBillTo(customerAddress);
|
312
|
+
profile.setPayment(paymentType);
|
313
|
+
profile.setDefaultPaymentProfile(data.defaultPaymentProfile || false);
|
314
|
+
|
315
|
+
let createRequest = new APIContracts.CreateCustomerPaymentProfileRequest();
|
316
|
+
|
317
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
318
|
+
createRequest.setCustomerProfileId(customerProfileId);
|
319
|
+
createRequest.setPaymentProfile(profile);
|
320
|
+
|
321
|
+
//pretty print request
|
322
|
+
// console.log(JSON.stringify(createRequest.getJSON(), null, 2));
|
323
|
+
|
324
|
+
let ctrl = new APIControllers.CreateCustomerPaymentProfileController(
|
325
|
+
createRequest.getJSON()
|
326
|
+
);
|
327
|
+
|
328
|
+
if (process.env.ENVIRONMENT === "production") {
|
329
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
330
|
+
}
|
331
|
+
|
332
|
+
return new Promise((resolve, reject) => {
|
333
|
+
ctrl.execute(function () {
|
334
|
+
let apiResponse = ctrl.getResponse();
|
335
|
+
|
336
|
+
let response = new APIContracts.CreateCustomerPaymentProfileResponse(
|
337
|
+
apiResponse
|
338
|
+
);
|
339
|
+
|
340
|
+
//pretty print response
|
341
|
+
console.log(JSON.stringify(response, null, 2));
|
342
|
+
|
343
|
+
if (response != null) {
|
344
|
+
if (
|
345
|
+
response.getMessages().getResultCode() ==
|
346
|
+
APIContracts.MessageTypeEnum.OK
|
347
|
+
) {
|
348
|
+
resolve(response.getCustomerPaymentProfileId());
|
349
|
+
} else {
|
350
|
+
reject(response.getMessages().getMessage()[0].getText());
|
351
|
+
}
|
352
|
+
} else {
|
353
|
+
reject("Null response received");
|
354
|
+
}
|
355
|
+
});
|
356
|
+
});
|
357
|
+
};
|
358
|
+
|
359
|
+
export const deleteCustomerProfile = async (
|
360
|
+
customerProfileId: string,
|
361
|
+
customerPaymentProfileId: string
|
362
|
+
) => {
|
363
|
+
let { merchantAuthenticationType, processorId } =
|
364
|
+
await getMerchantAuthForCustomer(customerProfileId);
|
365
|
+
|
366
|
+
let deleteRequest = new APIContracts.DeleteCustomerPaymentProfileRequest();
|
367
|
+
deleteRequest.setMerchantAuthentication(merchantAuthenticationType);
|
368
|
+
deleteRequest.setCustomerProfileId(customerProfileId);
|
369
|
+
deleteRequest.setCustomerPaymentProfileId(customerPaymentProfileId);
|
370
|
+
|
371
|
+
//pretty print request
|
372
|
+
//console.log(JSON.stringify(createRequest.getJSON(), null, 2));
|
373
|
+
|
374
|
+
let ctrl = new APIControllers.DeleteCustomerPaymentProfileController(
|
375
|
+
deleteRequest.getJSON()
|
376
|
+
);
|
377
|
+
if (process.env.ENVIRONMENT === "production") {
|
378
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
379
|
+
}
|
380
|
+
|
381
|
+
return new Promise((resolve, reject) => {
|
382
|
+
ctrl.execute(function () {
|
383
|
+
let apiResponse = ctrl.getResponse();
|
384
|
+
|
385
|
+
let response = new APIContracts.DeleteCustomerPaymentProfileResponse(
|
386
|
+
apiResponse
|
387
|
+
);
|
388
|
+
if (response != null) {
|
389
|
+
if (
|
390
|
+
response.getMessages().getResultCode() ==
|
391
|
+
APIContracts.MessageTypeEnum.OK
|
392
|
+
) {
|
393
|
+
resolve(customerPaymentProfileId);
|
394
|
+
} else {
|
395
|
+
reject(response.getMessages().getMessage()[0].getText());
|
396
|
+
}
|
397
|
+
} else {
|
398
|
+
reject("Null response received");
|
399
|
+
}
|
400
|
+
});
|
401
|
+
});
|
402
|
+
};
|
403
|
+
|
404
|
+
export const updateCustomerProfile = async (
|
405
|
+
customerProfileId: string,
|
406
|
+
customerPaymentProfileId: string,
|
407
|
+
data: TransactionData
|
408
|
+
): Promise<{ customerPaymentProfileId: string; processorId: ProcessorId }> => {
|
409
|
+
let { merchantAuthenticationType, processorId } =
|
410
|
+
await getMerchantAuthForCustomer(customerProfileId);
|
411
|
+
|
412
|
+
let oldProfile: TransactionData = await getCustomerPaymentProfile(
|
413
|
+
customerProfileId,
|
414
|
+
customerPaymentProfileId
|
415
|
+
);
|
416
|
+
console.log(oldProfile);
|
417
|
+
if (!oldProfile) {
|
418
|
+
throw Error("No payment profile found");
|
419
|
+
}
|
420
|
+
|
421
|
+
let creditCardForUpdate = buildCreditCard(
|
422
|
+
(data.payment && data.payment.creditCard) || oldProfile.payment.creditCard
|
423
|
+
);
|
424
|
+
|
425
|
+
let paymentType = new APIContracts.PaymentType();
|
426
|
+
paymentType.setCreditCard(creditCardForUpdate);
|
427
|
+
|
428
|
+
let customerAddressType = buildAddress(data.billTo || oldProfile.billTo);
|
429
|
+
|
430
|
+
let customerForUpdate = new APIContracts.CustomerPaymentProfileExType();
|
431
|
+
customerForUpdate.setPayment(paymentType);
|
432
|
+
//customerForUpdate.setDefaultPaymentProfile(true);
|
433
|
+
customerForUpdate.setDefaultPaymentProfile(
|
434
|
+
data.defaultPaymentProfile || false
|
435
|
+
);
|
436
|
+
|
437
|
+
customerForUpdate.setCustomerPaymentProfileId(customerPaymentProfileId);
|
438
|
+
customerForUpdate.setBillTo(customerAddressType);
|
439
|
+
|
440
|
+
let updateRequest = new APIContracts.UpdateCustomerPaymentProfileRequest();
|
441
|
+
updateRequest.setMerchantAuthentication(merchantAuthenticationType);
|
442
|
+
updateRequest.setCustomerProfileId(customerProfileId);
|
443
|
+
updateRequest.setPaymentProfile(customerForUpdate);
|
444
|
+
updateRequest.setValidationMode(mode);
|
445
|
+
|
446
|
+
let ctrl = new APIControllers.UpdateCustomerPaymentProfileController(
|
447
|
+
updateRequest.getJSON()
|
448
|
+
);
|
449
|
+
if (process.env.ENVIRONMENT === "production") {
|
450
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
451
|
+
}
|
452
|
+
|
453
|
+
return new Promise((resolve, reject) => {
|
454
|
+
ctrl.execute(function () {
|
455
|
+
let apiResponse = ctrl.getResponse();
|
456
|
+
|
457
|
+
let response = new APIContracts.UpdateCustomerPaymentProfileResponse(
|
458
|
+
apiResponse
|
459
|
+
);
|
460
|
+
|
461
|
+
if (response != null) {
|
462
|
+
if (
|
463
|
+
response.getMessages().getResultCode() ==
|
464
|
+
APIContracts.MessageTypeEnum.OK
|
465
|
+
) {
|
466
|
+
console.log(
|
467
|
+
"Successfully updated a customer payment profile with id: " +
|
468
|
+
customerPaymentProfileId
|
469
|
+
);
|
470
|
+
resolve({ customerPaymentProfileId, processorId });
|
471
|
+
} else {
|
472
|
+
reject(response.getMessages().getMessage()[0].getText());
|
473
|
+
}
|
474
|
+
} else {
|
475
|
+
reject("Null response received");
|
476
|
+
}
|
477
|
+
});
|
478
|
+
});
|
479
|
+
};
|
480
|
+
|
481
|
+
export const chargeCreditCardAndCreateProfile = async (
|
482
|
+
user: PhoneUser,
|
483
|
+
data: TransactionData,
|
484
|
+
amountInCents: number,
|
485
|
+
description: string,
|
486
|
+
skipProfileCreation?: boolean,
|
487
|
+
useCoachingMid?: boolean
|
488
|
+
): Promise<{
|
489
|
+
amountInCents: number;
|
490
|
+
transactionId: string;
|
491
|
+
pkCustomerProfileId?: string;
|
492
|
+
pkCustomerPaymentProfileId?: string;
|
493
|
+
processorId: ProcessorId;
|
494
|
+
}> => {
|
495
|
+
let { merchantAuthenticationType, processorId } =
|
496
|
+
await getMerchantAuthForProcessor(
|
497
|
+
useCoachingMid ? DEFAULT_PROCESSOR : DEFAULT_PROCESSOR
|
498
|
+
);
|
499
|
+
|
500
|
+
let creditCard = buildCreditCard(data.payment.creditCard);
|
501
|
+
let paymentType = new APIContracts.PaymentType();
|
502
|
+
paymentType.setCreditCard(creditCard);
|
503
|
+
|
504
|
+
let customerAddress = buildAddress(data.billTo);
|
505
|
+
|
506
|
+
var userField = new APIContracts.UserField();
|
507
|
+
userField.setName("userId");
|
508
|
+
userField.setValue(user.id);
|
509
|
+
|
510
|
+
var userFields = new APIContracts.TransactionRequestType.UserFields();
|
511
|
+
var ufArray: any = [userField];
|
512
|
+
userFields.setUserField(ufArray);
|
513
|
+
|
514
|
+
var transactionSetting1 = new APIContracts.SettingType();
|
515
|
+
transactionSetting1.setSettingName("duplicateWindow");
|
516
|
+
transactionSetting1.setSettingValue("5");
|
517
|
+
|
518
|
+
var transactionSetting2 = new APIContracts.SettingType();
|
519
|
+
transactionSetting2.setSettingName("recurringBilling");
|
520
|
+
transactionSetting2.setSettingValue("false");
|
521
|
+
|
522
|
+
var transactionSettingList = [transactionSetting1, transactionSetting2];
|
523
|
+
|
524
|
+
var transactionSettings = new APIContracts.ArrayOfSetting();
|
525
|
+
transactionSettings.setSetting(transactionSettingList);
|
526
|
+
|
527
|
+
var profile = new APIContracts.CustomerProfilePaymentType();
|
528
|
+
profile.setCreateProfile(skipProfileCreation ? false : true);
|
529
|
+
|
530
|
+
let customerProfileType = new APIContracts.CustomerType();
|
531
|
+
customerProfileType.setId(user.id);
|
532
|
+
customerProfileType.setEmail(user.email);
|
533
|
+
customerProfileType.setType(APIContracts.CustomerTypeEnum.INDIVIDUAL);
|
534
|
+
|
535
|
+
// let customerBaseType = new APIContracts.CustomerPaymentProfileBaseType();
|
536
|
+
// customerBaseType.setCustomerType(customerProfileType);
|
537
|
+
// customerBaseType.setBillTo(customerAddress);
|
538
|
+
|
539
|
+
var orderDetails = new APIContracts.OrderType();
|
540
|
+
orderDetails.setDescription(description);
|
541
|
+
|
542
|
+
var transactionRequestType = new APIContracts.TransactionRequestType();
|
543
|
+
transactionRequestType.setTransactionType(
|
544
|
+
APIContracts.TransactionTypeEnum.AUTHCAPTURETRANSACTION
|
545
|
+
);
|
546
|
+
transactionRequestType.setPayment(paymentType);
|
547
|
+
transactionRequestType.setAmount(amountInCents / 100.0);
|
548
|
+
transactionRequestType.setUserFields(userFields);
|
549
|
+
transactionRequestType.setOrder(orderDetails);
|
550
|
+
transactionRequestType.setBillTo(customerAddress);
|
551
|
+
transactionRequestType.setTransactionSettings(transactionSettings);
|
552
|
+
transactionRequestType.setProfile(profile);
|
553
|
+
transactionRequestType.setCustomer(customerProfileType);
|
554
|
+
|
555
|
+
var createRequest = new APIContracts.CreateTransactionRequest();
|
556
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
557
|
+
createRequest.setTransactionRequest(transactionRequestType);
|
558
|
+
|
559
|
+
var ctrl = new APIControllers.CreateTransactionController(
|
560
|
+
createRequest.getJSON()
|
561
|
+
);
|
562
|
+
|
563
|
+
if (process.env.ENVIRONMENT === "production") {
|
564
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
565
|
+
}
|
566
|
+
|
567
|
+
return new Promise((resolve, reject) => {
|
568
|
+
ctrl.execute(function () {
|
569
|
+
let apiResponse = ctrl.getResponse();
|
570
|
+
|
571
|
+
let response = new APIContracts.CreateTransactionResponse(apiResponse);
|
572
|
+
|
573
|
+
//pretty print response
|
574
|
+
console.log(JSON.stringify(response, null, 2));
|
575
|
+
|
576
|
+
if (response != null) {
|
577
|
+
let transactionResponse = response.getTransactionResponse();
|
578
|
+
let profileResponse = response.getProfileResponse();
|
579
|
+
if (
|
580
|
+
response.getMessages().getResultCode() ==
|
581
|
+
APIContracts.MessageTypeEnum.OK
|
582
|
+
) {
|
583
|
+
if (transactionResponse.getMessages() != null) {
|
584
|
+
return resolve({
|
585
|
+
amountInCents,
|
586
|
+
transactionId: transactionResponse.getTransId(),
|
587
|
+
pkCustomerProfileId:
|
588
|
+
profileResponse && profileResponse.getCustomerProfileId(),
|
589
|
+
pkCustomerPaymentProfileId:
|
590
|
+
profileResponse &&
|
591
|
+
profileResponse.getCustomerPaymentProfileIdList() &&
|
592
|
+
profileResponse
|
593
|
+
.getCustomerPaymentProfileIdList()
|
594
|
+
.getNumericString()[0]
|
595
|
+
.toString(),
|
596
|
+
processorId,
|
597
|
+
});
|
598
|
+
} else {
|
599
|
+
if (transactionResponse.getErrors() != null) {
|
600
|
+
return reject(
|
601
|
+
transactionResponse.getErrors().getError()[0].getErrorText()
|
602
|
+
);
|
603
|
+
}
|
604
|
+
}
|
605
|
+
} else {
|
606
|
+
console.log("Failed Transaction. ");
|
607
|
+
if (
|
608
|
+
transactionResponse != null &&
|
609
|
+
transactionResponse.getErrors() != null
|
610
|
+
) {
|
611
|
+
return reject(
|
612
|
+
transactionResponse.getErrors().getError()[0].getErrorText()
|
613
|
+
);
|
614
|
+
} else {
|
615
|
+
return reject(response.getMessages().getMessage()[0].getText());
|
616
|
+
}
|
617
|
+
}
|
618
|
+
} else {
|
619
|
+
reject("Null response received");
|
620
|
+
}
|
621
|
+
});
|
622
|
+
});
|
623
|
+
};
|
624
|
+
|
625
|
+
export const purchaseCreditsWithCreditCardAndCreatorProfile = async (
|
626
|
+
user: PhoneUser,
|
627
|
+
data: TransactionData,
|
628
|
+
amountInCents: number
|
629
|
+
): Promise<{
|
630
|
+
amountInCents: number;
|
631
|
+
transactionId: string;
|
632
|
+
processorId: ProcessorId;
|
633
|
+
}> => {
|
634
|
+
let chargeResponse = await chargeCreditCardAndCreateProfile(
|
635
|
+
user,
|
636
|
+
data,
|
637
|
+
amountInCents,
|
638
|
+
"CLIQ Credits"
|
639
|
+
);
|
640
|
+
|
641
|
+
if (
|
642
|
+
chargeResponse.pkCustomerProfileId &&
|
643
|
+
chargeResponse.pkCustomerPaymentProfileId
|
644
|
+
) {
|
645
|
+
await updateUserPaymentProfile(
|
646
|
+
user.id,
|
647
|
+
chargeResponse.pkCustomerProfileId,
|
648
|
+
chargeResponse.processorId,
|
649
|
+
chargeResponse.pkCustomerPaymentProfileId
|
650
|
+
);
|
651
|
+
}
|
652
|
+
return {
|
653
|
+
amountInCents: chargeResponse.amountInCents,
|
654
|
+
transactionId: chargeResponse.transactionId,
|
655
|
+
processorId: chargeResponse.processorId,
|
656
|
+
};
|
657
|
+
};
|
658
|
+
|
659
|
+
export const purchaseCreditsWithCustomerProfile = async (
|
660
|
+
customerProfileId: string,
|
661
|
+
customerPaymentProfileId: string,
|
662
|
+
amountInCents: number
|
663
|
+
) => {
|
664
|
+
return await chargeCustomerByPaymentProfile(
|
665
|
+
customerProfileId,
|
666
|
+
customerPaymentProfileId,
|
667
|
+
amountInCents,
|
668
|
+
"CLIQ Credits"
|
669
|
+
);
|
670
|
+
};
|
671
|
+
|
672
|
+
export const chargeCustomerByPaymentProfile = async (
|
673
|
+
customerProfileId: string,
|
674
|
+
customerPaymentProfileId: string,
|
675
|
+
amountInCents: number,
|
676
|
+
description: string
|
677
|
+
): Promise<{
|
678
|
+
amountInCents: number;
|
679
|
+
transactionId: string;
|
680
|
+
processorId: ProcessorId;
|
681
|
+
}> => {
|
682
|
+
let { merchantAuthenticationType, processorId } =
|
683
|
+
await getMerchantAuthForCustomer(customerProfileId);
|
684
|
+
|
685
|
+
var profileToCharge = new APIContracts.CustomerProfilePaymentType();
|
686
|
+
profileToCharge.setCustomerProfileId(customerProfileId);
|
687
|
+
|
688
|
+
var paymentProfile = new APIContracts.PaymentProfile();
|
689
|
+
paymentProfile.setPaymentProfileId(customerPaymentProfileId);
|
690
|
+
profileToCharge.setPaymentProfile(paymentProfile);
|
691
|
+
|
692
|
+
var orderDetails = new APIContracts.OrderType();
|
693
|
+
orderDetails.setDescription(description);
|
694
|
+
|
695
|
+
var duplicateWindow: any = new APIContracts.SettingType();
|
696
|
+
duplicateWindow.setSettingName("duplicateWindow");
|
697
|
+
duplicateWindow.setSettingValue("5");
|
698
|
+
|
699
|
+
var transactionSettingList: any[] = [];
|
700
|
+
transactionSettingList.push(duplicateWindow);
|
701
|
+
|
702
|
+
var transactionSettings = new APIContracts.ArrayOfSetting();
|
703
|
+
transactionSettings.setSetting(transactionSettingList);
|
704
|
+
|
705
|
+
var transactionRequestType = new APIContracts.TransactionRequestType();
|
706
|
+
transactionRequestType.setTransactionType(
|
707
|
+
APIContracts.TransactionTypeEnum.AUTHCAPTURETRANSACTION
|
708
|
+
);
|
709
|
+
transactionRequestType.setProfile(profileToCharge);
|
710
|
+
transactionRequestType.setAmount(amountInCents / 100.0);
|
711
|
+
transactionRequestType.setTransactionSettings(transactionSettings);
|
712
|
+
transactionRequestType.setOrder(orderDetails);
|
713
|
+
|
714
|
+
var createRequest = new APIContracts.CreateTransactionRequest();
|
715
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
716
|
+
createRequest.setTransactionRequest(transactionRequestType);
|
717
|
+
|
718
|
+
var ctrl = new APIControllers.CreateTransactionController(
|
719
|
+
createRequest.getJSON()
|
720
|
+
);
|
721
|
+
if (process.env.ENVIRONMENT === "production") {
|
722
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
723
|
+
}
|
724
|
+
|
725
|
+
return new Promise((resolve, reject) => {
|
726
|
+
ctrl.execute(function () {
|
727
|
+
var apiResponse = ctrl.getResponse();
|
728
|
+
|
729
|
+
var response = new APIContracts.CreateTransactionResponse(apiResponse);
|
730
|
+
|
731
|
+
//pretty print response
|
732
|
+
console.log(JSON.stringify(response, null, 2));
|
733
|
+
|
734
|
+
if (response != null) {
|
735
|
+
if (
|
736
|
+
response.getMessages().getResultCode() ==
|
737
|
+
APIContracts.MessageTypeEnum.OK
|
738
|
+
) {
|
739
|
+
if (response.getTransactionResponse().getMessages() != null) {
|
740
|
+
// Success
|
741
|
+
console.log("Successful transaction");
|
742
|
+
resolve({
|
743
|
+
transactionId: response.getTransactionResponse().getTransId(),
|
744
|
+
amountInCents,
|
745
|
+
processorId,
|
746
|
+
});
|
747
|
+
} else {
|
748
|
+
console.log("Failed Transaction.");
|
749
|
+
if (response.getTransactionResponse().getErrors() != null) {
|
750
|
+
reject(
|
751
|
+
response
|
752
|
+
.getTransactionResponse()
|
753
|
+
.getErrors()
|
754
|
+
.getError()[0]
|
755
|
+
.getErrorText()
|
756
|
+
);
|
757
|
+
} else {
|
758
|
+
reject("Failed Transaction.");
|
759
|
+
}
|
760
|
+
}
|
761
|
+
} else {
|
762
|
+
if (
|
763
|
+
response.getTransactionResponse() != null &&
|
764
|
+
response.getTransactionResponse().getErrors() != null
|
765
|
+
) {
|
766
|
+
reject(
|
767
|
+
response
|
768
|
+
.getTransactionResponse()
|
769
|
+
.getErrors()
|
770
|
+
.getError()[0]
|
771
|
+
.getErrorText()
|
772
|
+
);
|
773
|
+
} else {
|
774
|
+
reject(response.getMessages().getMessage()[0].getText());
|
775
|
+
}
|
776
|
+
}
|
777
|
+
} else {
|
778
|
+
reject("Null Response.");
|
779
|
+
}
|
780
|
+
});
|
781
|
+
});
|
782
|
+
};
|
783
|
+
|
784
|
+
export async function createSubscription({
|
785
|
+
intervalCount,
|
786
|
+
intervalUnit,
|
787
|
+
trialLength,
|
788
|
+
amountInCents,
|
789
|
+
trialAmountInCents,
|
790
|
+
customerProfileId,
|
791
|
+
customerPaymentProfileId,
|
792
|
+
creatorName,
|
793
|
+
processorId,
|
794
|
+
startDate,
|
795
|
+
}: {
|
796
|
+
intervalCount: number;
|
797
|
+
intervalUnit: "months" | "days";
|
798
|
+
trialAmountInCents: number;
|
799
|
+
trialLength: number;
|
800
|
+
amountInCents: number;
|
801
|
+
customerProfileId: string;
|
802
|
+
customerPaymentProfileId: string;
|
803
|
+
creatorName: string;
|
804
|
+
processorId: ProcessorId;
|
805
|
+
startDate?: Date;
|
806
|
+
}): Promise<{ pkSubId: string }> {
|
807
|
+
try {
|
808
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
809
|
+
processorId
|
810
|
+
);
|
811
|
+
|
812
|
+
return new Promise(async (resolve, reject) => {
|
813
|
+
var interval = new APIContracts.PaymentScheduleType.Interval();
|
814
|
+
interval.setLength(intervalCount);
|
815
|
+
interval.setUnit(intervalUnit);
|
816
|
+
|
817
|
+
var paymentScheduleType = new APIContracts.PaymentScheduleType();
|
818
|
+
paymentScheduleType.setInterval(interval);
|
819
|
+
|
820
|
+
paymentScheduleType.setStartDate(
|
821
|
+
moment(startDate)
|
822
|
+
.add(intervalCount, intervalUnit)
|
823
|
+
.add(1, "day")
|
824
|
+
.toISOString()
|
825
|
+
.substring(0, 10)
|
826
|
+
);
|
827
|
+
paymentScheduleType.setTotalOccurrences(9999);
|
828
|
+
paymentScheduleType.setTrialOccurrences(trialLength);
|
829
|
+
|
830
|
+
var customerProfileIdType = new APIContracts.CustomerProfileIdType();
|
831
|
+
customerProfileIdType.setCustomerProfileId(customerProfileId);
|
832
|
+
customerProfileIdType.setCustomerPaymentProfileId(
|
833
|
+
customerPaymentProfileId
|
834
|
+
);
|
835
|
+
|
836
|
+
var arbSubscription = new APIContracts.ARBSubscriptionType();
|
837
|
+
arbSubscription.setName(
|
838
|
+
`${creatorName} ${
|
839
|
+
intervalUnit == "days" ? "weekly" : "monthly"
|
840
|
+
} subscription`
|
841
|
+
);
|
842
|
+
arbSubscription.setPaymentSchedule(paymentScheduleType);
|
843
|
+
arbSubscription.setAmount(amountInCents / 100.0);
|
844
|
+
arbSubscription.setTrialAmount((trialAmountInCents || 0) / 100.0);
|
845
|
+
arbSubscription.setProfile(customerProfileIdType);
|
846
|
+
|
847
|
+
var createRequest = new APIContracts.ARBCreateSubscriptionRequest();
|
848
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
849
|
+
createRequest.setSubscription(arbSubscription);
|
850
|
+
|
851
|
+
var ctrl = new APIControllers.ARBCreateSubscriptionController(
|
852
|
+
createRequest.getJSON()
|
853
|
+
);
|
854
|
+
if (process.env.ENVIRONMENT === "production") {
|
855
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
856
|
+
}
|
857
|
+
|
858
|
+
ctrl.execute(function () {
|
859
|
+
var apiResponse = ctrl.getResponse();
|
860
|
+
|
861
|
+
var response = new APIContracts.ARBCreateSubscriptionResponse(
|
862
|
+
apiResponse
|
863
|
+
);
|
864
|
+
|
865
|
+
console.log(JSON.stringify(response, null, 2));
|
866
|
+
|
867
|
+
if (response != null) {
|
868
|
+
if (
|
869
|
+
response.getMessages().getResultCode() ==
|
870
|
+
APIContracts.MessageTypeEnum.OK
|
871
|
+
) {
|
872
|
+
resolve({ pkSubId: response.getSubscriptionId() });
|
873
|
+
} else {
|
874
|
+
reject(response.getMessages().getMessage()[0].getText());
|
875
|
+
}
|
876
|
+
} else {
|
877
|
+
reject("Null Response.");
|
878
|
+
}
|
879
|
+
});
|
880
|
+
});
|
881
|
+
} catch (err) {
|
882
|
+
console.log(err);
|
883
|
+
throw new Error("Subscription failed to create");
|
884
|
+
}
|
885
|
+
}
|
886
|
+
|
887
|
+
export async function createSubscriptionWithCreditCard({
|
888
|
+
intervalCount,
|
889
|
+
intervalUnit,
|
890
|
+
trialLength,
|
891
|
+
amountInCents,
|
892
|
+
trialAmountInCents,
|
893
|
+
data,
|
894
|
+
creatorName,
|
895
|
+
processorId: processorId,
|
896
|
+
}: {
|
897
|
+
intervalCount: number;
|
898
|
+
intervalUnit: "months" | "days";
|
899
|
+
trialAmountInCents: number;
|
900
|
+
trialLength: number;
|
901
|
+
amountInCents: number;
|
902
|
+
data: TransactionData;
|
903
|
+
creatorName: string;
|
904
|
+
processorId: ProcessorId;
|
905
|
+
}): Promise<{
|
906
|
+
pkSubId: string;
|
907
|
+
paymentProfile: {
|
908
|
+
pkCustomerProfileId?: string;
|
909
|
+
pkCustomerPaymentProfileId?: string;
|
910
|
+
processorId: ProcessorId;
|
911
|
+
};
|
912
|
+
}> {
|
913
|
+
try {
|
914
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
915
|
+
processorId
|
916
|
+
);
|
917
|
+
|
918
|
+
return new Promise(async (resolve, reject) => {
|
919
|
+
var interval = new APIContracts.PaymentScheduleType.Interval();
|
920
|
+
interval.setLength(intervalCount);
|
921
|
+
interval.setUnit(intervalUnit);
|
922
|
+
|
923
|
+
var paymentScheduleType = new APIContracts.PaymentScheduleType();
|
924
|
+
paymentScheduleType.setInterval(interval);
|
925
|
+
|
926
|
+
paymentScheduleType.setStartDate(
|
927
|
+
moment()
|
928
|
+
.add(intervalCount, intervalUnit)
|
929
|
+
.add(1, "day")
|
930
|
+
.toISOString()
|
931
|
+
.substring(0, 10)
|
932
|
+
);
|
933
|
+
paymentScheduleType.setTotalOccurrences(9999);
|
934
|
+
paymentScheduleType.setTrialOccurrences(trialLength);
|
935
|
+
|
936
|
+
let creditCard = buildCreditCard(data.payment.creditCard);
|
937
|
+
let paymentType = new APIContracts.PaymentType();
|
938
|
+
paymentType.setCreditCard(creditCard);
|
939
|
+
|
940
|
+
let customerAddress = buildAddress(data.billTo);
|
941
|
+
|
942
|
+
var arbSubscription = new APIContracts.ARBSubscriptionType();
|
943
|
+
arbSubscription.setName(
|
944
|
+
`${creatorName} ${
|
945
|
+
intervalUnit == "days" ? "weekly" : "monthly"
|
946
|
+
} subscription`
|
947
|
+
);
|
948
|
+
arbSubscription.setPaymentSchedule(paymentScheduleType);
|
949
|
+
arbSubscription.setAmount(amountInCents / 100.0);
|
950
|
+
arbSubscription.setTrialAmount((trialAmountInCents || 0) / 100.0);
|
951
|
+
arbSubscription.setBillTo(customerAddress);
|
952
|
+
arbSubscription.setPayment(paymentType);
|
953
|
+
|
954
|
+
var createRequest = new APIContracts.ARBCreateSubscriptionRequest();
|
955
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
956
|
+
createRequest.setSubscription(arbSubscription);
|
957
|
+
|
958
|
+
var ctrl = new APIControllers.ARBCreateSubscriptionController(
|
959
|
+
createRequest.getJSON()
|
960
|
+
);
|
961
|
+
if (process.env.ENVIRONMENT === "production") {
|
962
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
963
|
+
}
|
964
|
+
|
965
|
+
ctrl.execute(function () {
|
966
|
+
var apiResponse = ctrl.getResponse();
|
967
|
+
|
968
|
+
var response = new APIContracts.ARBCreateSubscriptionResponse(
|
969
|
+
apiResponse
|
970
|
+
);
|
971
|
+
|
972
|
+
console.log(JSON.stringify(response, null, 2));
|
973
|
+
|
974
|
+
if (response != null) {
|
975
|
+
if (
|
976
|
+
response.getMessages().getResultCode() ==
|
977
|
+
APIContracts.MessageTypeEnum.OK
|
978
|
+
) {
|
979
|
+
let profileResponse = response.getProfile();
|
980
|
+
let pkCustomerProfileId =
|
981
|
+
profileResponse && profileResponse.getCustomerProfileId();
|
982
|
+
let pkCustomerPaymentProfileId =
|
983
|
+
profileResponse &&
|
984
|
+
profileResponse.getCustomerPaymentProfileId &&
|
985
|
+
profileResponse.getCustomerPaymentProfileId() &&
|
986
|
+
profileResponse.getCustomerPaymentProfileId().getNumericString &&
|
987
|
+
profileResponse
|
988
|
+
.getCustomerPaymentProfileId()
|
989
|
+
.getNumericString()
|
990
|
+
.toString();
|
991
|
+
resolve({
|
992
|
+
pkSubId: response.getSubscriptionId(),
|
993
|
+
paymentProfile: {
|
994
|
+
pkCustomerProfileId,
|
995
|
+
pkCustomerPaymentProfileId,
|
996
|
+
processorId,
|
997
|
+
},
|
998
|
+
});
|
999
|
+
} else {
|
1000
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1001
|
+
}
|
1002
|
+
} else {
|
1003
|
+
reject("Null Response.");
|
1004
|
+
}
|
1005
|
+
});
|
1006
|
+
});
|
1007
|
+
} catch (err) {
|
1008
|
+
console.log(err);
|
1009
|
+
throw new Error("Subscription failed to create");
|
1010
|
+
}
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
export async function cancelSubscription(
|
1014
|
+
subscription: Subscription,
|
1015
|
+
processorId: ProcessorId
|
1016
|
+
) {
|
1017
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1018
|
+
processorId
|
1019
|
+
);
|
1020
|
+
|
1021
|
+
var cancelRequest = new APIContracts.ARBCancelSubscriptionRequest();
|
1022
|
+
cancelRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1023
|
+
cancelRequest.setSubscriptionId(subscription.pkSubscriptionId);
|
1024
|
+
|
1025
|
+
var ctrl = new APIControllers.ARBCancelSubscriptionController(
|
1026
|
+
cancelRequest.getJSON()
|
1027
|
+
);
|
1028
|
+
if (process.env.ENVIRONMENT === "production") {
|
1029
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1030
|
+
}
|
1031
|
+
|
1032
|
+
return new Promise((resolve, reject) => {
|
1033
|
+
ctrl.execute(function () {
|
1034
|
+
var apiResponse = ctrl.getResponse();
|
1035
|
+
|
1036
|
+
var response = new APIContracts.ARBCancelSubscriptionResponse(
|
1037
|
+
apiResponse
|
1038
|
+
);
|
1039
|
+
|
1040
|
+
console.log(JSON.stringify(response, null, 2));
|
1041
|
+
|
1042
|
+
if (response != null) {
|
1043
|
+
if (
|
1044
|
+
response.getMessages().getResultCode() ==
|
1045
|
+
APIContracts.MessageTypeEnum.OK
|
1046
|
+
) {
|
1047
|
+
resolve(true);
|
1048
|
+
} else {
|
1049
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1050
|
+
}
|
1051
|
+
} else {
|
1052
|
+
reject("Null Response.");
|
1053
|
+
}
|
1054
|
+
});
|
1055
|
+
});
|
1056
|
+
}
|
1057
|
+
|
1058
|
+
export async function updateSubscription({
|
1059
|
+
authSubscriptionId,
|
1060
|
+
intervalCount,
|
1061
|
+
intervalUnit,
|
1062
|
+
processorId: processorId,
|
1063
|
+
}: {
|
1064
|
+
authSubscriptionId: number;
|
1065
|
+
intervalCount: number;
|
1066
|
+
intervalUnit: "months" | "days";
|
1067
|
+
processorId: ProcessorId;
|
1068
|
+
}) {
|
1069
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1070
|
+
processorId
|
1071
|
+
);
|
1072
|
+
|
1073
|
+
var paymentScheduleType = new APIContracts.PaymentScheduleType();
|
1074
|
+
|
1075
|
+
paymentScheduleType.setStartDate(
|
1076
|
+
moment()
|
1077
|
+
.add(intervalCount, intervalUnit)
|
1078
|
+
.add(1, "day")
|
1079
|
+
.toISOString()
|
1080
|
+
.substring(0, 10)
|
1081
|
+
);
|
1082
|
+
|
1083
|
+
var arbSubscription = new APIContracts.ARBSubscriptionType();
|
1084
|
+
arbSubscription.setPaymentSchedule(paymentScheduleType);
|
1085
|
+
|
1086
|
+
var updateRequest = new APIContracts.ARBUpdateSubscriptionRequest();
|
1087
|
+
updateRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1088
|
+
updateRequest.setSubscriptionId(authSubscriptionId);
|
1089
|
+
updateRequest.setSubscription(arbSubscription);
|
1090
|
+
|
1091
|
+
console.log(JSON.stringify(updateRequest.getJSON(), null, 2));
|
1092
|
+
|
1093
|
+
var ctrl = new APIControllers.ARBUpdateSubscriptionController(
|
1094
|
+
updateRequest.getJSON()
|
1095
|
+
);
|
1096
|
+
if (process.env.ENVIRONMENT === "production") {
|
1097
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1098
|
+
}
|
1099
|
+
|
1100
|
+
return new Promise((resolve, reject) => {
|
1101
|
+
ctrl.execute(function () {
|
1102
|
+
var apiResponse = ctrl.getResponse();
|
1103
|
+
|
1104
|
+
var response = new APIContracts.ARBUpdateSubscriptionResponse(
|
1105
|
+
apiResponse
|
1106
|
+
);
|
1107
|
+
|
1108
|
+
console.log(JSON.stringify(response, null, 2));
|
1109
|
+
|
1110
|
+
if (response != null) {
|
1111
|
+
if (
|
1112
|
+
response.getMessages().getResultCode() ==
|
1113
|
+
APIContracts.MessageTypeEnum.OK
|
1114
|
+
) {
|
1115
|
+
resolve(true);
|
1116
|
+
} else {
|
1117
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1118
|
+
}
|
1119
|
+
} else {
|
1120
|
+
reject("Null Response.");
|
1121
|
+
}
|
1122
|
+
});
|
1123
|
+
});
|
1124
|
+
}
|
1125
|
+
|
1126
|
+
export async function getSubscription(
|
1127
|
+
subscriptionId: string,
|
1128
|
+
processorId: ProcessorId
|
1129
|
+
) {
|
1130
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1131
|
+
processorId
|
1132
|
+
);
|
1133
|
+
|
1134
|
+
var getRequest = new APIContracts.ARBGetSubscriptionRequest();
|
1135
|
+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1136
|
+
getRequest.setSubscriptionId(subscriptionId);
|
1137
|
+
getRequest.setIncludeTransactions(true);
|
1138
|
+
|
1139
|
+
var ctrl = new APIControllers.ARBGetSubscriptionController(
|
1140
|
+
getRequest.getJSON()
|
1141
|
+
);
|
1142
|
+
|
1143
|
+
if (process.env.ENVIRONMENT === "production") {
|
1144
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1145
|
+
}
|
1146
|
+
|
1147
|
+
return new Promise((resolve, reject) => {
|
1148
|
+
ctrl.execute(function () {
|
1149
|
+
var apiResponse = ctrl.getResponse();
|
1150
|
+
|
1151
|
+
var response = new APIContracts.ARBGetSubscriptionResponse(apiResponse);
|
1152
|
+
|
1153
|
+
if (response != null) {
|
1154
|
+
if (
|
1155
|
+
response.getMessages().getResultCode() ==
|
1156
|
+
APIContracts.MessageTypeEnum.OK
|
1157
|
+
) {
|
1158
|
+
resolve(response.getSubscription());
|
1159
|
+
} else {
|
1160
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1161
|
+
}
|
1162
|
+
} else {
|
1163
|
+
reject("Null Response.");
|
1164
|
+
}
|
1165
|
+
});
|
1166
|
+
});
|
1167
|
+
}
|
1168
|
+
|
1169
|
+
export async function getSubscriptionStatus(
|
1170
|
+
subscriptionId: string,
|
1171
|
+
processorId: ProcessorId
|
1172
|
+
): Promise<PkSubStatus> {
|
1173
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1174
|
+
processorId
|
1175
|
+
);
|
1176
|
+
|
1177
|
+
var getRequest = new APIContracts.ARBGetSubscriptionStatusRequest();
|
1178
|
+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1179
|
+
getRequest.setSubscriptionId(subscriptionId);
|
1180
|
+
|
1181
|
+
var ctrl = new APIControllers.ARBGetSubscriptionStatusController(
|
1182
|
+
getRequest.getJSON()
|
1183
|
+
);
|
1184
|
+
|
1185
|
+
if (process.env.ENVIRONMENT === "production") {
|
1186
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1187
|
+
}
|
1188
|
+
|
1189
|
+
return new Promise((resolve, reject) => {
|
1190
|
+
ctrl.execute(function () {
|
1191
|
+
var apiResponse = ctrl.getResponse();
|
1192
|
+
|
1193
|
+
var response = new APIContracts.ARBGetSubscriptionStatusResponse(
|
1194
|
+
apiResponse
|
1195
|
+
);
|
1196
|
+
|
1197
|
+
if (response != null) {
|
1198
|
+
if (
|
1199
|
+
response.getMessages().getResultCode() ==
|
1200
|
+
APIContracts.MessageTypeEnum.OK
|
1201
|
+
) {
|
1202
|
+
resolve(response.getStatus());
|
1203
|
+
} else {
|
1204
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1205
|
+
}
|
1206
|
+
} else {
|
1207
|
+
reject("Null Response.");
|
1208
|
+
}
|
1209
|
+
});
|
1210
|
+
});
|
1211
|
+
}
|
1212
|
+
|
1213
|
+
export async function getSubscriptionList(
|
1214
|
+
processorId: ProcessorId,
|
1215
|
+
inactive?: boolean
|
1216
|
+
): Promise<APIContracts.SubscriptionDetail[]> {
|
1217
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1218
|
+
processorId
|
1219
|
+
);
|
1220
|
+
|
1221
|
+
var sorting = new APIContracts.ARBGetSubscriptionListSorting();
|
1222
|
+
sorting.setOrderDescending(true);
|
1223
|
+
sorting.setOrderBy(
|
1224
|
+
APIContracts.ARBGetSubscriptionListOrderFieldEnum.CREATETIMESTAMPUTC
|
1225
|
+
);
|
1226
|
+
|
1227
|
+
var listRequest = new APIContracts.ARBGetSubscriptionListRequest();
|
1228
|
+
|
1229
|
+
listRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1230
|
+
|
1231
|
+
listRequest.setSearchType(
|
1232
|
+
inactive
|
1233
|
+
? APIContracts.ARBGetSubscriptionListSearchTypeEnum.SUBSCRIPTIONINACTIVE
|
1234
|
+
: APIContracts.ARBGetSubscriptionListSearchTypeEnum.SUBSCRIPTIONACTIVE
|
1235
|
+
);
|
1236
|
+
|
1237
|
+
listRequest.setSorting(sorting);
|
1238
|
+
|
1239
|
+
console.log(JSON.stringify(listRequest.getJSON(), null, 2));
|
1240
|
+
|
1241
|
+
var ctrl = new APIControllers.ARBGetSubscriptionListController(
|
1242
|
+
listRequest.getJSON()
|
1243
|
+
);
|
1244
|
+
|
1245
|
+
if (process.env.ENVIRONMENT === "production") {
|
1246
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1247
|
+
}
|
1248
|
+
|
1249
|
+
return new Promise((resolve, reject) => {
|
1250
|
+
ctrl.execute(function () {
|
1251
|
+
var apiResponse = ctrl.getResponse();
|
1252
|
+
|
1253
|
+
var response = new APIContracts.ARBGetSubscriptionListResponse(
|
1254
|
+
apiResponse
|
1255
|
+
);
|
1256
|
+
|
1257
|
+
if (response != null) {
|
1258
|
+
if (
|
1259
|
+
response.getMessages().getResultCode() ==
|
1260
|
+
APIContracts.MessageTypeEnum.OK
|
1261
|
+
) {
|
1262
|
+
console.log("Total Results: " + response.getTotalNumInResultSet());
|
1263
|
+
var subscriptions = response
|
1264
|
+
.getSubscriptionDetails()
|
1265
|
+
.getSubscriptionDetail();
|
1266
|
+
resolve(subscriptions);
|
1267
|
+
} else {
|
1268
|
+
console.log("Result Code: " + response.getMessages().getResultCode());
|
1269
|
+
console.log(
|
1270
|
+
"Error Code: " + response.getMessages().getMessage()[0].getCode()
|
1271
|
+
);
|
1272
|
+
console.log(
|
1273
|
+
"Error message: " + response.getMessages().getMessage()[0].getText()
|
1274
|
+
);
|
1275
|
+
reject();
|
1276
|
+
}
|
1277
|
+
} else {
|
1278
|
+
reject();
|
1279
|
+
}
|
1280
|
+
});
|
1281
|
+
});
|
1282
|
+
}
|
1283
|
+
|
1284
|
+
export async function voidTransaction(
|
1285
|
+
transactionId: string,
|
1286
|
+
processorId: ProcessorId
|
1287
|
+
) {
|
1288
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1289
|
+
processorId
|
1290
|
+
);
|
1291
|
+
|
1292
|
+
var transactionRequestType = new APIContracts.TransactionRequestType();
|
1293
|
+
transactionRequestType.setTransactionType(
|
1294
|
+
APIContracts.TransactionTypeEnum.VOIDTRANSACTION
|
1295
|
+
);
|
1296
|
+
transactionRequestType.setRefTransId(transactionId);
|
1297
|
+
|
1298
|
+
var createRequest = new APIContracts.CreateTransactionRequest();
|
1299
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1300
|
+
createRequest.setTransactionRequest(transactionRequestType);
|
1301
|
+
|
1302
|
+
var ctrl = new APIControllers.CreateTransactionController(
|
1303
|
+
createRequest.getJSON()
|
1304
|
+
);
|
1305
|
+
|
1306
|
+
if (process.env.ENVIRONMENT === "production") {
|
1307
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1308
|
+
}
|
1309
|
+
|
1310
|
+
return new Promise((resolve, reject) => {
|
1311
|
+
ctrl.execute(function () {
|
1312
|
+
var apiResponse = ctrl.getResponse();
|
1313
|
+
|
1314
|
+
var response = new APIContracts.CreateTransactionResponse(apiResponse);
|
1315
|
+
|
1316
|
+
if (response != null) {
|
1317
|
+
if (
|
1318
|
+
response.getMessages().getResultCode() ==
|
1319
|
+
APIContracts.MessageTypeEnum.OK
|
1320
|
+
) {
|
1321
|
+
if (response.getTransactionResponse().getMessages() != null) {
|
1322
|
+
resolve(true);
|
1323
|
+
} else {
|
1324
|
+
if (response.getTransactionResponse().getErrors() != null) {
|
1325
|
+
reject(
|
1326
|
+
response
|
1327
|
+
.getTransactionResponse()
|
1328
|
+
.getErrors()
|
1329
|
+
.getError()[0]
|
1330
|
+
.getErrorText()
|
1331
|
+
);
|
1332
|
+
}
|
1333
|
+
}
|
1334
|
+
} else {
|
1335
|
+
console.log("Failed Transaction. ");
|
1336
|
+
if (
|
1337
|
+
response.getTransactionResponse() != null &&
|
1338
|
+
response.getTransactionResponse().getErrors() != null
|
1339
|
+
) {
|
1340
|
+
reject(
|
1341
|
+
response
|
1342
|
+
.getTransactionResponse()
|
1343
|
+
.getErrors()
|
1344
|
+
.getError()[0]
|
1345
|
+
.getErrorText()
|
1346
|
+
);
|
1347
|
+
} else {
|
1348
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1349
|
+
}
|
1350
|
+
}
|
1351
|
+
} else {
|
1352
|
+
console.log("Null Response.");
|
1353
|
+
}
|
1354
|
+
});
|
1355
|
+
});
|
1356
|
+
}
|
1357
|
+
|
1358
|
+
export const updateUserPaymentProfile = async (
|
1359
|
+
userId: string,
|
1360
|
+
customerProfileId: string,
|
1361
|
+
processorId: ProcessorId,
|
1362
|
+
pkCustomerPaymentProfileId?: string
|
1363
|
+
): Promise<PKCustomerProfile> => {
|
1364
|
+
console.log("Update payment profile");
|
1365
|
+
let profile = await PKCustomerProfile.findOne({
|
1366
|
+
where: { userId: userId },
|
1367
|
+
});
|
1368
|
+
if (!profile) {
|
1369
|
+
console.log("Create new payment profile");
|
1370
|
+
profile = await PKCustomerProfile.create({
|
1371
|
+
userId: userId,
|
1372
|
+
customerProfileId: customerProfileId,
|
1373
|
+
defaultPaymentProfileId: pkCustomerPaymentProfileId,
|
1374
|
+
processorId,
|
1375
|
+
});
|
1376
|
+
} else {
|
1377
|
+
profile.customerProfileId = customerProfileId;
|
1378
|
+
profile.defaultPaymentProfileId = pkCustomerPaymentProfileId;
|
1379
|
+
profile.processorId = processorId;
|
1380
|
+
await profile.save();
|
1381
|
+
}
|
1382
|
+
return profile;
|
1383
|
+
};
|
1384
|
+
|
1385
|
+
export async function getTransactionsForCustomerProfile({
|
1386
|
+
customerProfileId,
|
1387
|
+
processorId,
|
1388
|
+
}: {
|
1389
|
+
customerProfileId: string;
|
1390
|
+
processorId: ProcessorId;
|
1391
|
+
}): Promise<
|
1392
|
+
{
|
1393
|
+
transId: string;
|
1394
|
+
amountInCents: number;
|
1395
|
+
status: string;
|
1396
|
+
}[]
|
1397
|
+
> {
|
1398
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1399
|
+
processorId
|
1400
|
+
);
|
1401
|
+
|
1402
|
+
var paging = new APIContracts.Paging();
|
1403
|
+
paging.setLimit(10);
|
1404
|
+
paging.setOffset(1);
|
1405
|
+
|
1406
|
+
var sorting = new APIContracts.TransactionListSorting();
|
1407
|
+
sorting.setOrderBy(APIContracts.TransactionListOrderFieldEnum.ID);
|
1408
|
+
sorting.setOrderDescending(true);
|
1409
|
+
|
1410
|
+
var getRequest = new APIContracts.GetTransactionListForCustomerRequest();
|
1411
|
+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1412
|
+
getRequest.setCustomerProfileId(customerProfileId);
|
1413
|
+
getRequest.setPaging(paging);
|
1414
|
+
getRequest.setSorting(sorting);
|
1415
|
+
|
1416
|
+
console.log(JSON.stringify(getRequest.getJSON(), null, 2));
|
1417
|
+
|
1418
|
+
var ctrl = new APIControllers.GetTransactionListForCustomerController(
|
1419
|
+
getRequest.getJSON()
|
1420
|
+
);
|
1421
|
+
|
1422
|
+
if (process.env.ENVIRONMENT === "production") {
|
1423
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1424
|
+
}
|
1425
|
+
|
1426
|
+
return new Promise((resolve, reject) => {
|
1427
|
+
ctrl.execute(function () {
|
1428
|
+
var apiResponse = ctrl.getResponse();
|
1429
|
+
|
1430
|
+
console.log(apiResponse);
|
1431
|
+
|
1432
|
+
var response = new APIContracts.GetTransactionListResponse(apiResponse);
|
1433
|
+
|
1434
|
+
console.log(response.getTransactions());
|
1435
|
+
|
1436
|
+
if (response != null) {
|
1437
|
+
if (
|
1438
|
+
response.getMessages().getResultCode() ==
|
1439
|
+
APIContracts.MessageTypeEnum.OK
|
1440
|
+
) {
|
1441
|
+
if (response.getTransactions() != null) {
|
1442
|
+
var transactions = response.getTransactions().getTransaction();
|
1443
|
+
resolve(
|
1444
|
+
transactions.map((t) => {
|
1445
|
+
return {
|
1446
|
+
transId: t.getTransId(),
|
1447
|
+
amountInCents: t.getSettleAmount() * 100,
|
1448
|
+
status: t.getTransactionStatus(),
|
1449
|
+
};
|
1450
|
+
})
|
1451
|
+
);
|
1452
|
+
} else {
|
1453
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1454
|
+
}
|
1455
|
+
} else {
|
1456
|
+
console.log("Failed Transaction. ");
|
1457
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1458
|
+
}
|
1459
|
+
} else {
|
1460
|
+
console.log("Null Response.");
|
1461
|
+
}
|
1462
|
+
});
|
1463
|
+
});
|
1464
|
+
}
|
1465
|
+
|
1466
|
+
export async function refundTransaction({
|
1467
|
+
transId,
|
1468
|
+
creditCard,
|
1469
|
+
customerProfile,
|
1470
|
+
amountInCents,
|
1471
|
+
processorId,
|
1472
|
+
}: {
|
1473
|
+
transId: number;
|
1474
|
+
creditCard?: APIContracts.CreditCardMaskedType;
|
1475
|
+
customerProfile?: APIContracts.CustomerProfileIdType;
|
1476
|
+
amountInCents: number;
|
1477
|
+
processorId: ProcessorId;
|
1478
|
+
}): Promise<{ refundTransactionId: string }> {
|
1479
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1480
|
+
processorId
|
1481
|
+
);
|
1482
|
+
|
1483
|
+
let cc = new APIContracts.CreditCardType();
|
1484
|
+
cc.cardNumber = creditCard?.cardNumber;
|
1485
|
+
cc.expirationDate = creditCard?.expirationDate;
|
1486
|
+
|
1487
|
+
var paymentType = new APIContracts.PaymentType();
|
1488
|
+
paymentType.setCreditCard(cc);
|
1489
|
+
|
1490
|
+
var transactionRequestType = new APIContracts.TransactionRequestType();
|
1491
|
+
transactionRequestType.setTransactionType(
|
1492
|
+
APIContracts.TransactionTypeEnum.REFUNDTRANSACTION
|
1493
|
+
);
|
1494
|
+
transactionRequestType.setAmount(amountInCents / 100.0);
|
1495
|
+
transactionRequestType.setRefTransId(transId);
|
1496
|
+
// transactionRequestType.setCustomer(customerProfile);
|
1497
|
+
transactionRequestType.setPayment(paymentType);
|
1498
|
+
|
1499
|
+
var createRequest = new APIContracts.CreateTransactionRequest();
|
1500
|
+
createRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1501
|
+
createRequest.setTransactionRequest(transactionRequestType);
|
1502
|
+
|
1503
|
+
//pretty print request
|
1504
|
+
console.log(JSON.stringify(createRequest.getJSON(), null, 2));
|
1505
|
+
|
1506
|
+
var ctrl = new APIControllers.CreateTransactionController(
|
1507
|
+
createRequest.getJSON()
|
1508
|
+
);
|
1509
|
+
|
1510
|
+
if (process.env.ENVIRONMENT === "production") {
|
1511
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1512
|
+
}
|
1513
|
+
|
1514
|
+
return new Promise((resolve, reject) => {
|
1515
|
+
ctrl.execute(function () {
|
1516
|
+
var apiResponse = ctrl.getResponse();
|
1517
|
+
|
1518
|
+
var response = new APIContracts.CreateTransactionResponse(apiResponse);
|
1519
|
+
|
1520
|
+
if (response != null) {
|
1521
|
+
if (
|
1522
|
+
response.getMessages().getResultCode() ==
|
1523
|
+
APIContracts.MessageTypeEnum.OK
|
1524
|
+
) {
|
1525
|
+
if (response.getTransactionResponse().getMessages() != null) {
|
1526
|
+
resolve({
|
1527
|
+
refundTransactionId: response
|
1528
|
+
.getTransactionResponse()
|
1529
|
+
.getTransId(),
|
1530
|
+
});
|
1531
|
+
} else {
|
1532
|
+
reject(
|
1533
|
+
response
|
1534
|
+
.getTransactionResponse()
|
1535
|
+
.getErrors()
|
1536
|
+
.getError()[0]
|
1537
|
+
.getErrorText()
|
1538
|
+
);
|
1539
|
+
}
|
1540
|
+
} else {
|
1541
|
+
if (
|
1542
|
+
response.getTransactionResponse() != null &&
|
1543
|
+
response.getTransactionResponse().getErrors() != null
|
1544
|
+
) {
|
1545
|
+
reject(
|
1546
|
+
response
|
1547
|
+
.getTransactionResponse()
|
1548
|
+
.getErrors()
|
1549
|
+
.getError()[0]
|
1550
|
+
.getErrorText()
|
1551
|
+
);
|
1552
|
+
} else {
|
1553
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1554
|
+
}
|
1555
|
+
}
|
1556
|
+
} else {
|
1557
|
+
reject("Null Response.");
|
1558
|
+
}
|
1559
|
+
});
|
1560
|
+
});
|
1561
|
+
}
|
1562
|
+
|
1563
|
+
export async function getTransactionStatus({
|
1564
|
+
transId,
|
1565
|
+
processorId,
|
1566
|
+
}: {
|
1567
|
+
transId: number;
|
1568
|
+
processorId: ProcessorId;
|
1569
|
+
}): Promise<{
|
1570
|
+
status: TransactionStatus;
|
1571
|
+
transId: number;
|
1572
|
+
code: "1" | "2" | "3" | "4";
|
1573
|
+
settleAmountInCents: number;
|
1574
|
+
creditCard?: APIContracts.CreditCardMaskedType;
|
1575
|
+
customerProfile?: APIContracts.CustomerProfileIdType;
|
1576
|
+
}> {
|
1577
|
+
let { merchantAuthenticationType } = await getMerchantAuthForProcessor(
|
1578
|
+
processorId
|
1579
|
+
);
|
1580
|
+
|
1581
|
+
var getRequest = new APIContracts.GetTransactionDetailsRequest();
|
1582
|
+
getRequest.setMerchantAuthentication(merchantAuthenticationType);
|
1583
|
+
getRequest.setTransId(transId);
|
1584
|
+
|
1585
|
+
console.log(JSON.stringify(getRequest.getJSON(), null, 2));
|
1586
|
+
|
1587
|
+
var ctrl = new APIControllers.GetTransactionDetailsController(
|
1588
|
+
getRequest.getJSON()
|
1589
|
+
);
|
1590
|
+
|
1591
|
+
if (process.env.ENVIRONMENT === "production") {
|
1592
|
+
ctrl.setEnvironment(PKConstants.endpoint.production);
|
1593
|
+
}
|
1594
|
+
|
1595
|
+
return new Promise((resolve, reject) => {
|
1596
|
+
ctrl.execute(function () {
|
1597
|
+
var apiResponse = ctrl.getResponse();
|
1598
|
+
|
1599
|
+
var response = new APIContracts.GetTransactionDetailsResponse(
|
1600
|
+
apiResponse
|
1601
|
+
);
|
1602
|
+
|
1603
|
+
if (response != null) {
|
1604
|
+
if (
|
1605
|
+
response.getMessages().getResultCode() ==
|
1606
|
+
APIContracts.MessageTypeEnum.OK
|
1607
|
+
) {
|
1608
|
+
let payment = response.getTransaction().getPayment();
|
1609
|
+
let customer = response.getTransaction().getCustomer();
|
1610
|
+
|
1611
|
+
resolve({
|
1612
|
+
status: response.getTransaction().getTransactionStatus(),
|
1613
|
+
transId: response.getTransaction().getTransId(),
|
1614
|
+
code: response.getTransaction().getResponseCode(),
|
1615
|
+
settleAmountInCents:
|
1616
|
+
response.getTransaction().getSettleAmount() * 100,
|
1617
|
+
creditCard: payment && payment.getCreditCard(),
|
1618
|
+
customerProfile: customer,
|
1619
|
+
});
|
1620
|
+
} else {
|
1621
|
+
reject(response.getMessages().getMessage()[0].getText());
|
1622
|
+
}
|
1623
|
+
} else {
|
1624
|
+
reject("Null Response.");
|
1625
|
+
}
|
1626
|
+
});
|
1627
|
+
});
|
1628
|
+
}
|
1629
|
+
|
1630
|
+
export default {
|
1631
|
+
createCustomerProfile,
|
1632
|
+
getCustomerProfile,
|
1633
|
+
addPaymentProfile,
|
1634
|
+
deleteCustomerProfile,
|
1635
|
+
updateCustomerProfile,
|
1636
|
+
purchaseCreditsWithCustomerProfile,
|
1637
|
+
purchaseCreditsWithCreditCardAndCreatorProfile,
|
1638
|
+
createSubscription,
|
1639
|
+
cancelSubscription,
|
1640
|
+
getSubscription,
|
1641
|
+
getSubscriptionStatus,
|
1642
|
+
chargeCustomerByPaymentProfile,
|
1643
|
+
getSubscriptionList,
|
1644
|
+
updateUserPaymentProfile,
|
1645
|
+
voidTransaction,
|
1646
|
+
chargeCreditCardAndCreateProfile,
|
1647
|
+
getTransactionsForCustomerProfile,
|
1648
|
+
getTransactionStatus,
|
1649
|
+
refundTransaction,
|
1650
|
+
};
|