@@ -17,13 +17,15 @@ import { useGetDocumentFromUser } from "@/firebase/firestore/getDocumentFromUser
17
17
import { useGetFavoriteCode } from "@/firebase/firestore/getFavoriteCode"
18
18
import { useGetIsPrivateCodeFromUser } from "@/firebase/firestore/getIsPrivateCodeFromUser"
19
19
import { useUpdateFormDocument } from "@/firebase/firestore/updateFormDocument"
20
+ import usePaymentInitialization from "@/notchpay/initializePayment.js"
20
21
import copyToClipboard from "@/utils/copyToClipboard.js"
21
22
import embedProject from "@/utils/embedStackblitzProject"
22
23
import indentCode from "@/utils/indentCode.js"
23
24
import linearizeCode from "@/utils/linearizeCode"
24
25
import { yupResolver } from "@hookform/resolvers/yup"
25
26
import sdk , { Project } from "@stackblitz/sdk"
26
27
import algoliasearch from "algoliasearch"
28
+ import axios from "axios"
27
29
import hljs from "highlight.js"
28
30
import {
29
31
Check ,
@@ -127,6 +129,7 @@ export default function FormViewPage() {
127
129
text : yup . string ( ) . required ( "This field is required" ) ,
128
130
} )
129
131
) ,
132
+ paymentStatut : yup . string ( ) ,
130
133
} )
131
134
132
135
const ALGOLIA_INDEX_NAME = "forms"
@@ -158,14 +161,17 @@ export default function FormViewPage() {
158
161
} : any = useUpdateFormDocument ( "forms" )
159
162
160
163
const onSubmit = async ( data ) => {
164
+ setPaymentDone ( false )
161
165
let updatedFormData : {
162
166
responses : any [ ]
167
+ paymentStatut ?: string
163
168
} = {
164
169
responses : [
165
170
...dataForm ?. data ?. responses ,
166
171
{
167
172
idResponse : moment ( ) . valueOf ( ) + uid ( ) ,
168
173
createdAt : moment ( ) . valueOf ( ) ,
174
+ paymentStatut : data . paymentStatut ? data . paymentStatut : "" ,
169
175
responses : [
170
176
...data . responses . map ( ( response : any , index : number ) => {
171
177
return {
@@ -178,7 +184,7 @@ export default function FormViewPage() {
178
184
} ,
179
185
] ,
180
186
}
181
- //console.log("updatedFormData", updatedFormData)
187
+ // console.log("updatedFormData", updatedFormData)
182
188
183
189
const id = searchParams . get ( "form" )
184
190
@@ -204,6 +210,74 @@ export default function FormViewPage() {
204
210
window . location . href = dataForm . data . redirectOnCompletion
205
211
}
206
212
213
+ // payment
214
+
215
+ const [ paymentDone , setPaymentDone ] = useState ( false )
216
+
217
+ const checkPaymentStatus = async ( ) => {
218
+ const transactionReference = localStorage . getItem ( "transaction-reference" )
219
+ const CAPTURE_URL = `https://api.notchpay.co/payments/${ transactionReference } `
220
+
221
+ if ( ! transactionReference ) {
222
+ return
223
+ }
224
+
225
+ try {
226
+ const response = await axios . get ( CAPTURE_URL , {
227
+ headers : {
228
+ Accept : "application/json" ,
229
+ Authorization : process . env . NEXT_PUBLIC_NOTCH_PAY_PUBLIC_KEY ,
230
+ } ,
231
+ } )
232
+ const paymentStatus = response . data . transaction . status
233
+
234
+ if ( paymentStatus === "complete" ) {
235
+ localStorage . removeItem ( "transaction-reference" )
236
+ setValue ( "paymentStatut" , "complete" )
237
+ setPaymentDone ( true )
238
+ }
239
+ } catch ( error ) { }
240
+ }
241
+
242
+ useEffect ( ( ) => {
243
+ if ( window . location . search . includes ( "?reference=" ) ) {
244
+ checkPaymentStatus ( )
245
+ }
246
+ } , [ ] )
247
+
248
+ const {
249
+ initializePayment,
250
+ isLoading : isLoadingPayment ,
251
+ isError : isErrorPayment ,
252
+ } = usePaymentInitialization ( )
253
+
254
+ const schemaPayment = yup . object ( ) . shape ( {
255
+ email : yup . string ( ) . email ( ) . required ( ) ,
256
+ } )
257
+
258
+ const {
259
+ register : registerPayment ,
260
+ handleSubmit : handleSubmitPayment ,
261
+ reset : resetPayment ,
262
+ formState : { errors : errorsPayment } ,
263
+ } = useForm ( {
264
+ resolver : yupResolver ( schemaPayment ) ,
265
+ } )
266
+
267
+ const onSubmitPayment = async ( data ) => {
268
+ const { email } = data
269
+ initializePayment (
270
+ email ,
271
+ dataForm ?. data ?. amountNotchPay ,
272
+ "Sharuco Form Payment" ,
273
+ dataForm ?. data ?. publicNotchPayApiKey ,
274
+ `https://sharuco.lndev.me/form/view/${ searchParams . get ( "form" ) } `
275
+ )
276
+ resetPayment ( {
277
+ email : "" ,
278
+ } )
279
+ }
280
+
207
281
return (
208
282
< Layout >
209
283
< Head >
@@ -251,6 +325,70 @@ export default function FormViewPage() {
251
325
</ div >
252
326
< Separator className = "mx-auto my-8 sm:w-2/3" />
253
327
< div className = "mx-auto w-full space-y-6 lg:w-2/3" >
328
+ { dataForm ?. data ?. acceptPayment && ! paymentDone && (
329
+ < div className = "px-4 py-6 bg-emerald-50/50 dark:bg-emerald-500/5 rounded-xl border border-[#11B981] flex flex-col sm:flex-row items-start gap-4" >
330
+ < a href = "https://notchpay.co/" className = "w-12 shrink-0" >
331
+ < img
332
+ src = "/partner/notchpay-favicon.svg"
333
+ alt = "notchpay"
334
+ />
335
+ </ a >
336
+ < div className = "w-full flex flex-col items-start gap-2" >
337
+ < p >
338
+ This form accepts a payment at the rate of{ " " }
339
+ < span className = "font-bold" >
340
+ { dataForm ?. data ?. amountNotchPay } Euro
341
+ </ span > { " " }
342
+ , If you make it this will be notified to the
343
+ administrator of this form.
344
+ </ p >
345
+ < div className = "w-full flex flex-col sm:flex-row items-start gap-2" >
346
+ < div className = "w-full flex flex-col items-start gap-2" >
347
+ < Input
348
+ className = "w-full"
349
+ placeholder = "Enter your email"
350
+ { ...registerPayment ( "email" ) }
351
+ />
352
+ < p className = "text-sm text-red-500" >
353
+ { errorsPayment . email && (
354
+ < > { errorsPayment . email . message } </ >
355
+ ) }
356
+ </ p >
357
+ </ div >
358
+ < Button
359
+ className = "shrink-0 w-full sm:w-fit"
360
+ disabled = { isLoadingPayment }
361
+ onClick = {
362
+ ! isLoadingPayment
363
+ ? handleSubmitPayment ( onSubmitPayment )
364
+ : undefined
365
+ }
366
+ >
367
+ { isLoadingPayment && (
368
+ < Loader2 className = "mr-2 h-4 w-4 animate-spin" />
369
+ ) }
370
+ Pay { dataForm ?. data ?. amountNotchPay } EURO
371
+ </ Button >
372
+ </ div >
373
+ </ div >
374
+ </ div >
375
+ ) }
376
+ { paymentDone ? (
377
+ < div className = "px-4 py-6 bg-emerald-50/50 dark:bg-emerald-500/5 rounded-xl border border-[#11B981] flex flex-col sm:flex-row items-start gap-4" >
378
+ < a href = "https://notchpay.co/" className = "w-12 shrink-0" >
379
+ < img
380
+ src = "/partner/notchpay-favicon.svg"
381
+ alt = "notchpay"
382
+ />
383
+ </ a >
384
+ < div className = "w-full flex flex-col items-start font-bold gap-2" >
385
+ < p > Your payment has been made successfully !</ p >
386
+ < p >
387
+ Fill out this form now without reloading the page !
388
+ </ p >
389
+ </ div >
390
+ </ div >
391
+ ) : null }
254
392
{ dataForm ?. data ?. questions . map ( ( question , index ) => {
255
393
return (
256
394
< div
0 commit comments