@unify-payment/node 0.0.4 → 0.0.5

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.
@@ -0,0 +1,72 @@
1
+ import {
2
+ ISSLCommerzCheckoutResponse,
3
+ ISSLCommerzCreateCheckoutPayload,
4
+ ISSLCommerzOptions,
5
+ } from "../types/sslcommerz";
6
+ import { UnifyFetch } from "../utils/fetch";
7
+
8
+ export class SSLCommerz extends UnifyFetch {
9
+ constructor(private options: ISSLCommerzOptions) {
10
+ super();
11
+ }
12
+
13
+ private getApiBaseUrl() {
14
+ return this.options.apiUrl;
15
+ }
16
+
17
+ private getApiCheckoutUrl() {
18
+ return `${this.getApiBaseUrl()}/gwprocess/v4/api.php`;
19
+ }
20
+
21
+ private getApiValidationUrl() {
22
+ return `${this.getApiBaseUrl()}/validator/api/validationserverAPI.php`;
23
+ }
24
+
25
+ private getApiRefundUrl() {
26
+ return `${this.getApiBaseUrl()}/validator/api/merchantTransIDvalidationAPI.php`;
27
+ }
28
+
29
+ private getApiRefundQueryUrl() {
30
+ return `${this.getApiBaseUrl()}/validator/api/merchantTransIDvalidationAPI.php`;
31
+ }
32
+
33
+ private getApiTransactionQueryBySessionIdUrl() {
34
+ return `${this.getApiBaseUrl()}/validator/api/merchantTransIDvalidationAPI.php`;
35
+ }
36
+
37
+ private getApiTransactionQueryByTransactionIdUrl() {
38
+ return `${this.getApiBaseUrl()}/validator/api/merchantTransIDvalidationAPI.php`;
39
+ }
40
+
41
+ private getApiHeaders() {
42
+ return {
43
+ "Content-Type": "application/x-www-form-urlencoded",
44
+ };
45
+ }
46
+
47
+ private urlFormEncode(payload: Record<string, string | number>) {
48
+ return Object.entries(payload)
49
+ .map(
50
+ ([key, value]) =>
51
+ `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
52
+ )
53
+ .join("&");
54
+ }
55
+
56
+ async getCheckoutUrl(payload: ISSLCommerzCreateCheckoutPayload) {
57
+ const [res] = await this.jsonFetch<ISSLCommerzCheckoutResponse>(
58
+ this.getApiCheckoutUrl(),
59
+ {
60
+ method: "POST",
61
+ body: this.urlFormEncode(payload),
62
+ headers: this.getApiHeaders(),
63
+ }
64
+ );
65
+
66
+ if (res.status === "FAILED") {
67
+ throw new Error(res.failedreason);
68
+ }
69
+
70
+ return res.redirectGatewayURL;
71
+ }
72
+ }
@@ -0,0 +1,43 @@
1
+ import { Stripe as StripeSDK } from "stripe";
2
+ import { TStripeWebhookEventResponse } from "../types/stripe";
3
+
4
+ export class Stripe {
5
+ private stripe: StripeSDK;
6
+
7
+ constructor(
8
+ private apiKey: string,
9
+ config?: StripeSDK.StripeConfig
10
+ ) {
11
+ this.stripe = new StripeSDK(apiKey, config);
12
+ }
13
+
14
+ async getCheckoutUrl(params: StripeSDK.Checkout.SessionCreateParams) {
15
+ const session = await this.stripe.checkout.sessions.create(params);
16
+ if (!session.url) {
17
+ throw new Error("Failed to get checkout url");
18
+ }
19
+ return session.url;
20
+ }
21
+
22
+ async verifySignature(payload: {
23
+ signature: string;
24
+ secret: string;
25
+ body: string;
26
+ }): Promise<TStripeWebhookEventResponse> {
27
+ try {
28
+ const event = await this.stripe.webhooks.constructEventAsync(
29
+ payload.body,
30
+ payload.signature,
31
+ payload.secret
32
+ );
33
+
34
+ return {
35
+ event,
36
+ };
37
+ } catch (err) {
38
+ return {
39
+ error: err as Error,
40
+ };
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,49 @@
1
+ export interface IBkashAccessTokenResponse {
2
+ expires_in: string;
3
+ id_token: string;
4
+ refresh_token: string;
5
+ token_type: "Bearer" | string;
6
+ statusCode: string;
7
+ statusMessage: string;
8
+ }
9
+
10
+ export interface IBkashErrorResponse {
11
+ errorCode: string;
12
+ errorMessage: string;
13
+ }
14
+
15
+ export interface IBkashCheckoutOptions {
16
+ code: "0011";
17
+ payerReference: string;
18
+ callbackURL: string;
19
+ amount: string;
20
+ currency: "BDT";
21
+ intent: "sale";
22
+ merchantInvoiceNumber: string;
23
+ merchantAssociationInfo?: string;
24
+ }
25
+
26
+ export interface IBkashCheckoutResponse {
27
+ paymentID: string;
28
+ paymentCreateTime: string;
29
+ transactionStatus: string;
30
+ amount: string;
31
+ currency: "BDT";
32
+ intent: "sale";
33
+ merchantInvoiceNumber: string;
34
+ bkashURL: string;
35
+ callbackURL: string;
36
+ successCallbackURL: string;
37
+ failureCallbackURL: string;
38
+ cancelledCallbackURL: string;
39
+ statusCode: string;
40
+ statusMessage: string;
41
+ }
42
+
43
+ export interface IBkashPayloadProps {
44
+ apiUrl: string;
45
+ username: string;
46
+ password: string;
47
+ app_key: string;
48
+ app_secret: string;
49
+ }
@@ -0,0 +1,163 @@
1
+ export type TLemonSqueezyWebhookEvents =
2
+ | "order_created"
3
+ | "order_refunded"
4
+ | "subscription_created"
5
+ | "subscription_updated"
6
+ | "subscription_cancelled"
7
+ | "subscription_resumed"
8
+ | "subscription_expired"
9
+ | "subscription_paused"
10
+ | "subscription_unpaused"
11
+ | "subscription_payment_success"
12
+ | "subscription_payment_failed"
13
+ | "subscription_payment_recovered"
14
+ | "subscription_payment_refunded"
15
+ | "license_key_created"
16
+ | "license_key_updated";
17
+
18
+ export type TGetCheckoutUrl =
19
+ | { data: { attributes: { url: string } } }
20
+ | { errors: [{ detail: string }] };
21
+
22
+ export type TWebhookEventResponse =
23
+ | { error: Error }
24
+ | {
25
+ event: ILemonSqueezyWebhookEeventResponse;
26
+ type: TLemonSqueezyWebhookEvents;
27
+ };
28
+
29
+ export interface ILemonSqueezyWebhookEeventResponse {
30
+ meta: {
31
+ event_name: string;
32
+ custom_data: {
33
+ customer_id: number;
34
+ };
35
+ };
36
+ data: {
37
+ type: "orders" | "subscriptions";
38
+ id: string;
39
+ attributes: {
40
+ store_id: number;
41
+ customer_id: number;
42
+ identifier: string;
43
+ order_number: number;
44
+ user_name: string;
45
+ user_email: string;
46
+ currency: string;
47
+ currency_rate: string;
48
+ subtotal: number;
49
+ discount_total: number;
50
+ tax: number;
51
+ total: number;
52
+ subtotal_usd: number;
53
+ discount_total_usd: number;
54
+ tax_usd: number;
55
+ total_usd: number;
56
+ tax_name: string;
57
+ tax_rate: string;
58
+ status: string;
59
+ status_formatted: string;
60
+ refunded: boolean;
61
+ refunded_at: any;
62
+ subtotal_formatted: string;
63
+ discount_total_formatted: string;
64
+ tax_formatted: string;
65
+ total_formatted: string;
66
+ first_order_item: {
67
+ id: number;
68
+ order_id: number;
69
+ product_id: number;
70
+ variant_id: number;
71
+ product_name: string;
72
+ variant_name: string;
73
+ price: number;
74
+ created_at: string;
75
+ updated_at: string;
76
+ deleted_at: any;
77
+ test_mode: boolean;
78
+ };
79
+ urls: {
80
+ receipt: string;
81
+ };
82
+ created_at: string;
83
+ updated_at: string;
84
+ };
85
+ relationships: {
86
+ store: {
87
+ links: {
88
+ related: string;
89
+ self: string;
90
+ };
91
+ };
92
+ customer: {
93
+ links: {
94
+ related: string;
95
+ self: string;
96
+ };
97
+ };
98
+ "order-items": {
99
+ links: {
100
+ related: string;
101
+ self: string;
102
+ };
103
+ };
104
+ subscriptions: {
105
+ links: {
106
+ related: string;
107
+ self: string;
108
+ };
109
+ };
110
+ "license-keys": {
111
+ links: {
112
+ related: string;
113
+ self: string;
114
+ };
115
+ };
116
+ "discount-redemptions": {
117
+ links: {
118
+ related: string;
119
+ self: string;
120
+ };
121
+ };
122
+ };
123
+ links: {
124
+ self: string;
125
+ };
126
+ };
127
+ }
128
+
129
+ export interface ILemonSqueezyCheckoutOptions {
130
+ type: "checkouts";
131
+ attributes?: {
132
+ custom_price?: number;
133
+ product_options?: {
134
+ redirect_url?: string;
135
+ enabled_variants?: number[];
136
+ };
137
+ checkout_options?: {
138
+ button_color: string;
139
+ };
140
+ checkout_data?: {
141
+ discount_code?: string;
142
+ custom?: {
143
+ user_id: number;
144
+ };
145
+ };
146
+ expires_at?: string;
147
+ preview?: boolean;
148
+ };
149
+ relationships: {
150
+ store: {
151
+ data: {
152
+ type: string;
153
+ id: string;
154
+ };
155
+ };
156
+ variant: {
157
+ data: {
158
+ type: string;
159
+ id: string;
160
+ };
161
+ };
162
+ };
163
+ }
@@ -0,0 +1,57 @@
1
+ export interface IPaypalOptions {
2
+ clientId: string;
3
+ clientSecret: string;
4
+ sandbox?: boolean;
5
+ }
6
+
7
+ export interface IPaypalPayload {
8
+ intent: "CAPTURE";
9
+ purchase_units: {
10
+ items: {
11
+ name: string;
12
+ description: string;
13
+ quantity: number;
14
+ unit_amount: {
15
+ currency_code: "USD" | "EUR";
16
+ value: string;
17
+ };
18
+ }[];
19
+
20
+ amount: {
21
+ currency_code: "USD" | "EUR";
22
+ value: string;
23
+ breakdown: {
24
+ item_total: {
25
+ currency_code: "USD" | "EUR";
26
+ value: string;
27
+ };
28
+ };
29
+ };
30
+ }[];
31
+ application_context: {
32
+ return_url: string;
33
+ cancel_url: string;
34
+ shipping_preference?: "NO_SHIPPING";
35
+ user_action?: "PAY_NOW";
36
+ brand_name?: string;
37
+ };
38
+ }
39
+
40
+ export interface IPaypalAuthResponse {
41
+ access_token: string;
42
+ token_type: string;
43
+ app_id: string;
44
+ expires_in: number;
45
+ nonce: string;
46
+ scope: string;
47
+ }
48
+
49
+ export interface IPayPalOrderResponse {
50
+ id: string;
51
+ status: string;
52
+ links: {
53
+ href: string;
54
+ rel: string;
55
+ method: string;
56
+ }[];
57
+ }
@@ -0,0 +1,116 @@
1
+ export type ISSLCommerzCreateCheckoutPayload =
2
+ | {
3
+ tran_id: string;
4
+ store_id: string;
5
+ store_passwd: string;
6
+
7
+ total_amount: number;
8
+ currency: "USD" | "EUR";
9
+
10
+ success_url?: string;
11
+ cancel_url?: string;
12
+
13
+ cus_name: string;
14
+ cus_email: string;
15
+ cus_add1: string;
16
+ cus_add2?: string;
17
+ cus_city: string;
18
+ cus_state: string;
19
+ cus_postcode: string;
20
+ cus_country: string;
21
+ cus_phone: string;
22
+ cus_fax?: string;
23
+
24
+ shipping_method: "NO";
25
+
26
+ product_name: string;
27
+ product_category: string;
28
+ product_profile:
29
+ | "general"
30
+ | "physical-goods"
31
+ | "non-physical-goods"
32
+ | "airline-tickets"
33
+ | "travel-vertical"
34
+ | "telecom-vertical";
35
+ }
36
+ | {
37
+ tran_id: string;
38
+ store_id: string;
39
+ store_passwd: string;
40
+
41
+ total_amount: number;
42
+ currency: "USD" | "EUR";
43
+
44
+ success_url?: string;
45
+ cancel_url?: string;
46
+
47
+ cus_name: string;
48
+ cus_email: string;
49
+ cus_add1: string;
50
+ cus_add2?: string;
51
+ cus_city: string;
52
+ cus_state: string;
53
+ cus_postcode: string;
54
+ cus_country: string;
55
+ cus_phone: string;
56
+ cus_fax?: string;
57
+
58
+ shipping_method: "YES";
59
+
60
+ ship_name: string;
61
+ ship_add1: string;
62
+ ship_add2?: string;
63
+ ship_city: string;
64
+ ship_state: string;
65
+ ship_postcode: string;
66
+ ship_country: string;
67
+
68
+ product_name: string;
69
+ product_category: string;
70
+ product_profile:
71
+ | "general"
72
+ | "physical-goods"
73
+ | "non-physical-goods"
74
+ | "airline-tickets"
75
+ | "travel-vertical"
76
+ | "telecom-vertical";
77
+ };
78
+
79
+ export interface ISSLCommerzCheckoutResponse {
80
+ status: "SUCCESS" | "FAILED";
81
+ failedreason: string;
82
+ sessionkey: string;
83
+ gw: {
84
+ visa: string;
85
+ master: string;
86
+ amex: string;
87
+ othercards: string;
88
+ internetbanking: string;
89
+ mobilebanking: string;
90
+ };
91
+ redirectGatewayURL: string;
92
+ directPaymentURLBank: string;
93
+ directPaymentURLCard: string;
94
+ directPaymentURL: string;
95
+ redirectGatewayURLFailed: string;
96
+ GatewayPageURL: string;
97
+ storeBanner: string;
98
+ storeLogo: string;
99
+ store_name: string;
100
+ desc: {
101
+ name: string;
102
+ type: string;
103
+ logo: string;
104
+ gw: string;
105
+ r_flag?: string;
106
+ redirectGatewayURL?: string;
107
+ }[];
108
+ is_direct_pay_enable: string;
109
+ }
110
+
111
+ export type ISSLCommerzOptions = {
112
+ apiUrl: string;
113
+ store_id: string;
114
+ store_url?: string;
115
+ store_passwd: string;
116
+ };
@@ -0,0 +1,5 @@
1
+ import Stripe from "stripe";
2
+
3
+ export type TStripeWebhookEventResponse =
4
+ | { error: Error }
5
+ | { event: Stripe.Event };
@@ -0,0 +1,19 @@
1
+ type TFetchOptions = {
2
+ method?: string;
3
+ headers?: HeadersInit;
4
+ body?: BodyInit;
5
+ };
6
+
7
+ export class UnifyFetch {
8
+ async jsonFetch<T>(
9
+ url: string,
10
+ options?: TFetchOptions
11
+ ): Promise<[T, Response]> {
12
+ const req = await fetch(url, options);
13
+ const res = (await req.json()) as T;
14
+
15
+ return [res, req];
16
+ }
17
+
18
+ async axios<T>(url: string, options?: TFetchOptions) {}
19
+ }