/
index.js
139 lines (134 loc) · 5.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React, { useState } from "react";
import { func, object } from "prop-types";
import "./checkout-modal.m.css";
const currencyLabels = {
INR: "Rs.",
};
export const CheckoutModal = ({ member, setActiveTab, initRazorPayPayment, selectedPlan, validateCoupon }) => {
const [couponCode, setCouponCode] = useState("");
const [showCouponCode, setShowCouponCode] = useState(false);
const [isCouponApplied, setIsCouponApplied] = useState("");
const [plan, setPlan] = useState(selectedPlan.plan);
return (
<div styleName="modal">
<div styleName="checkout-label">Checkout</div>
<div styleName="secure">100% secure</div>
<div styleName="current-step">Step 1 of 2</div>
{member && (
<div styleName="change-account">
<div styleName="label">Account</div>
<span>{member.name}</span>
<span styleName="not-you-btn" onClick={() => setActiveTab("login")}>
Not you?
</span>
</div>
)}
<div styleName="checkout-container">
<div>
<div styleName="label">Plan Details</div>
<div styleName="plan-preview">
<div styleName="plan-name-and-price">
<div styleName="group-plan-name">{`${plan.title} - ${
plan.recurring ? "Recurring Plan" : "One Time Plan"
}`}</div>
<div styleName="price">{`${currencyLabels[plan.price_currency]} ${plan.price_cents / 100}/-`}</div>
</div>
</div>
<div styleName="validity">{`Valid for ${plan.duration_length} ${
plan.duration_length === 1
? plan.duration_unit.substring(0, plan.duration_unit.length - 1)
: plan.duration_unit
}`}</div>
<div styleName="plan-and-coupon-btns">
<div styleName="change-plan-btn" onClick={() => setActiveTab("subscription")}>
Change Plan
</div>
{!showCouponCode && (
<div styleName="add-coupon-code-btn" onClick={() => setShowCouponCode(true)}>
Add Coupon Code
</div>
)}
</div>
{showCouponCode && (
<div>
<div styleName="coupon-code-label">Coupon code</div>
<div styleName="coupon-apply">
<input
styleName="coupon-code-input"
value={couponCode}
type="text"
onChange={(e) => setCouponCode(e.target.value)}
/>
<div
onClick={() => {
validateCoupon(plan.id, couponCode)
.then((res) => {
if (res.valid) {
const updatedPlan = { ...plan };
updatedPlan.coupon_code = couponCode;
updatedPlan.discounted_price_cents = res.discount_details.discounted_price_cents;
updatedPlan.coupon_discount = res.discount_details.value;
setIsCouponApplied("applied");
setPlan(updatedPlan);
} else {
setIsCouponApplied("failed");
}
})
.catch((err) => console.error(`Coupon code error: ${err}`));
}}
styleName="apply-coupon-btn"
>
Apply
</div>
</div>
</div>
)}
{isCouponApplied === "applied" && (
<div styleName="amount-saved">{`You saved ${currencyLabels[plan.price_currency]} ${
(plan.price_cents - plan.discounted_price_cents) / 100
}/-`}</div>
)}
{isCouponApplied === "failed" && <div styleName="coupon-error">This coupon code is invalid</div>}
</div>
<div styleName="total-payment">
<div styleName="label">To Pay</div>
<div styleName="price">{`${currencyLabels[plan.price_currency]} ${
plan.discounted_price_cents !== undefined ? plan.discounted_price_cents / 100 : plan.price_cents / 100
}/-`}</div>
</div>
<div>
<div styleName="label">Payment Method</div>
<input type={"radio"} id={"credit-debit-cards"} value={"razorpay"} checked />
<label styleName="radio-label" htmlFor="credit-debit-cards">
Credit/Debit Cards, UPI
</label>
<button
styleName="proceed-to-payment-btn"
onClick={async () => {
const updatedPlan = JSON.parse(JSON.stringify(plan));
updatedPlan.coupon_code = couponCode;
const paymentResponse =
updatedPlan.discounted_price_cents === 0 || updatedPlan.price_cents === 0
? await initRazorPayPayment(updatedPlan, "standard", "", "", "", "skip_payment_gateway")
: await initRazorPayPayment(updatedPlan, "standard");
if (paymentResponse.subscription) {
window.location.href = "/profile";
}
}}
>
{`Proceed to Pay ${currencyLabels[plan.price_currency]} ${
plan.discounted_price_cents !== undefined ? plan.discounted_price_cents / 100 : plan.price_cents / 100
}/-`}
</button>
</div>
</div>
</div>
);
};
CheckoutModal.propTypes = {
member: object,
setActiveTab: func,
initRazorPayPayment: func,
selectedPlan: object,
validateCoupon: func,
};