Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
p29hieu committed May 29, 2023
1 parent ecfca43 commit 0666f4c
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 134 deletions.
2 changes: 2 additions & 0 deletions example/client-server/app/src/app/api/order/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export async function POST(request: Request, other: { params: any }) {

try {
const order = await FincodeService.i.createOrder({ amount, job_code: "AUTH", pay_type: "Card", tds2_type: "2", tds_type: "2" })
console.log("=== order created", order);

return NextResponse.json(order);
} catch (error: any) {
throw error?.response?.data ?? error;
Expand Down
33 changes: 18 additions & 15 deletions example/client-server/app/src/app/api/purchase/route.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import { FincodeClientService, FincodeNs, FincodeService } from 'fincode';
import { NextResponse } from 'next/server';
import { getTdsRetUrl } from "@/utils";
import { FincodeClientService, FincodeNs, FincodeService } from "fincode";
import { NextResponse } from "next/server";

export async function GET(request: Request, other: { params: any }) {
console.log("fincode", FincodeService.i.configData);

FincodeService.i.config({
secretKey: process.env.FINCODE_SK,
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK,
});
FincodeClientService.i.config({
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK,
});

const { searchParams } = new URL(request.url);
const order_id = searchParams.get("order_id") ?? '';
const access_id = searchParams.get("access_id") ?? '';
const card_id = searchParams.get("card_id") ?? '';
const customer_id = searchParams.get("customer_id") ?? '';
const order_id = searchParams.get("order_id") ?? "";
const access_id = searchParams.get("access_id") ?? "";
const card_id = searchParams.get("card_id") ?? "";
const customer_id = searchParams.get("customer_id") ?? "";
const data: FincodeNs.PaymentExecution = {
access_id,
pay_type: "Card",
card_id,
customer_id,
method: 1,
tds2_ret_url: `${process.env.NEXT_PUBLIC_APP_URL}/api/validate/${order_id}`,
}
tds2_ret_url: getTdsRetUrl(order_id),
};
try {
console.log('paymentExecution', order_id, data);

const res = await FincodeService.i.paymentExecution(order_id, data);
console.log("paymentExecution res", res);

if (res.acs_url) {
const urlSearch = new URLSearchParams();
urlSearch.set('url', res.acs_url)
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_APP_URL}/redirect?${urlSearch.toString()}`)
urlSearch.set("url", res.acs_url);
return NextResponse.redirect(
`${process.env.NEXT_PUBLIC_APP_URL}/redirect?${urlSearch.toString()}`
);
}
return NextResponse.json(res);
} catch (error: any) {
console.error(error?.response?.data ?? error)
console.error(error?.response?.data ?? error);
throw error?.response?.data ?? error;
}
}
122 changes: 84 additions & 38 deletions example/client-server/app/src/app/api/validate/[orderId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,105 @@
import { FincodeClientService, FincodeService } from 'fincode';
import { NextResponse } from 'next/server';
import { getTdsRetUrl } from "@/utils";
import { FincodeClientService, FincodeService } from "fincode";
import { NextResponse } from "next/server";

export async function POST(request: Request, { params }: { params: { orderId: string } }) {
const acquire3DS2 = async (access_id: string) => {
try {
const acquire3DS2Result = await FincodeClientService.i.acquire3DS2Result(
access_id
);
console.log("acquire3DS2Result", acquire3DS2Result);
} catch (error: any) {
console.error("acquire3DS2Result error", error?.response?.data);
}
};

const paymentAfterAuthentication = async (
order_id: string,
access_id: string
) => {
try {
const res = await FincodeService.i.paymentAfterAuthentication(order_id, {
access_id,
pay_type: "Card",
});
console.log("paymentAfterAuthentication res", res);
} catch (error: any) {
console.error(
"paymentAfterAuthentication error",
error?.response?.data ?? error
);

return NextResponse.json({
message: "paymentAfterAuthentication error",
error: error?.response?.data ?? error,
});
}
};

const getOrderDetail = async (orderId: string) => {
const res = await FincodeService.i.getPaymentsId(orderId);
console.log("orderDetail", res);
};

export async function POST(
request: Request,
{ params }: { params: { orderId: string } }
) {
FincodeService.i.config({
secretKey: process.env.FINCODE_SK,
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK,
});
FincodeClientService.i.config({
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK
publicKey: process.env.NEXT_PUBLIC_FINCODE_PK,
});

const bodyText = await request.text();

const bodyParam = new URLSearchParams(bodyText)
const param = bodyParam.get('param') ?? ''
const event = bodyParam.get('event') ?? ''
const bodyParam = new URLSearchParams(bodyText);
const event = bodyParam.get("event") ?? "";
const param = bodyParam.get("param") ?? "";

const { searchParams } = new URL(request.url);
const access_id = searchParams.get("MD") ?? '';
const { orderId } = params
const access_id = searchParams.get("MD") ?? "";
const { orderId } = params;
console.log("validate", { bodyText, searchParams, params });


try {
const get3DS2Result = await FincodeClientService.i.get3DS2Result(access_id)
console.log("get3DS2Result", get3DS2Result);
} catch (error: any) {
console.error("get3DS2Result error", error?.response?.data)
}

if (['3DSMethodFinished', '3DSMethodSkipped'].includes(event)) {
if (["3DSMethodFinished", "3DSMethodSkipped"].includes(event)) {
try {
const run3DS2Authentication = await FincodeService.i.run3DS2Authentication(access_id, { param: `${process.env.NEXT_PUBLIC_APP_URL}/api/validate/${orderId}` })
console.log('run3DS2Authentication', access_id, run3DS2Authentication);
const run3DS2Authentication =
await FincodeService.i.perform3DS2Authentication(access_id, { param });
console.log("run3DS2Authentication", access_id, run3DS2Authentication);

const { challenge_url } = run3DS2Authentication;
if (challenge_url) {
const urlSearch = new URLSearchParams()
urlSearch.set('url', challenge_url)
return NextResponse.redirect(`${process.env.NEXT_PUBLIC_APP_URL}/redirect?${urlSearch.toString()}`)
const urlSearch = new URLSearchParams();
urlSearch.set("url", challenge_url);
return NextResponse.redirect(
`${process.env.NEXT_PUBLIC_APP_URL}/redirect?${urlSearch.toString()}`
);
} else {
const res = await paymentAfterAuthentication(orderId, access_id);
if (res) {
return res;
}
}
}
catch (error: any) {
console.error("run3DS2Authentication error", error?.response?.data ?? error)
return NextResponse.json({ message: "run3DS2Authentication error, Please wait webhook", error: error?.response?.data ?? error })
} catch (error: any) {
console.error(
"run3DS2Authentication error",
error?.response?.data ?? error
);
return NextResponse.json({
message: "run3DS2Authentication error",
error: error?.response?.data ?? error,
});
}
}
if (['AuthResultReady'].includes(event)) {
try {
await FincodeService.i.paymentAfterAuthentication(orderId, {
access_id,
pay_type: "Card",
});
} catch (error: any) {
return NextResponse.json({ message: "AuthResultReady error", error: error?.response?.data ?? error })
if (["AuthResultReady"].includes(event)) {
await acquire3DS2(access_id);
const res = await paymentAfterAuthentication(orderId, access_id);
if (res) {
return res;
}
}
return NextResponse.json({ message: "Please wait webhook" })
}
return NextResponse.json({ message: "Purchase success!" });
}
52 changes: 0 additions & 52 deletions example/client-server/app/src/app/api/webhook/route.ts

This file was deleted.

8 changes: 7 additions & 1 deletion example/client-server/app/src/pages/payment/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import { FincodeClientService, FincodeNs, FincodeService } from "fincode";
import React, { useCallback, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";

type PurchaseDataResponse = {
access_id: string;
Expand Down Expand Up @@ -56,6 +56,10 @@ const PaymentPage = () => {
}
}, [customerId]);

useEffect(() => {
fetchCardList();
}, [fetchCardList]);

return (
<div>
<div>
Expand Down Expand Up @@ -128,6 +132,7 @@ const PaymentPage = () => {
<th>Brand</th>
<th>CardNumber</th>
<th>Expirer</th>
<th>Card ID</th>
<th>Default</th>
<th>Actions</th>
</tr>
Expand All @@ -144,6 +149,7 @@ const PaymentPage = () => {
<td>{card.brand}</td>
<td>{card.card_no}</td>
<td>{card.expire}</td>
<td>{card.id}</td>
<td>{card.default_flag}</td>
<td>
<button
Expand Down
30 changes: 18 additions & 12 deletions example/client-server/app/src/pages/redirect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
"use client"
"use client";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

const ValidatePaymentPage = () => {
const router = useRouter()
const [redirectUrl, setRedirectUrl] = useState<string>()
const router = useRouter();
const [redirectUrl, setRedirectUrl] = useState<string>();

useEffect(() => {
const { url } = router.query;
if (url) {
setRedirectUrl(`${url}`)
setRedirectUrl(`${url}`);
// router.replace(`${url}`)
window.open(`${url}`, '_blank')
// setTimeout(() => {
// window.open(`${url}`, "_blank");
// }, 300);
}
}, [router])
}, [router]);

return <div>
Redirecting...
return (
<div>
<a href={redirectUrl} target="_blank">{redirectUrl}</a>
Redirecting...
<div>
<a href={redirectUrl} target="_blank">
{redirectUrl}
</a>
</div>
</div>
</div>
}
);
};

export default ValidatePaymentPage;
export default ValidatePaymentPage;
2 changes: 2 additions & 0 deletions example/client-server/app/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getTdsRetUrl = (orderId: string) =>
`${process.env.NEXT_PUBLIC_APP_URL}/api/validate/${orderId}`;
10 changes: 2 additions & 8 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,9 @@ const getPaymentsId = async (orderId: string) => {
return res;
}

const run3DS2AuthenticationByClient = async (access_id: string, param: string) => {
console.log("run3DS2AuthenticationByClient");
const res = await FincodeClientService.i.run3DS2Authentication(access_id, { param })
console.log(res);
return res;
}
const run3DS2AuthenticationByServer = async (access_id: string, param: string) => {
console.log("run3DS2AuthenticationByServer");
const res = await FincodeService.i.run3DS2Authentication(access_id, { param })
const res = await FincodeService.i.perform3DS2Authentication(access_id, { param })
console.log(res);
return res;
}
Expand All @@ -150,7 +144,7 @@ const confirmSales = async (orderId: string, data: FincodeNs.ConfirmSales) => {

const get3DS2Result = async (access_id: string) => {
console.log("get3DS2Result");
const res = await FincodeClientService.i.get3DS2Result(access_id)
const res = await FincodeClientService.i.acquire3DS2Result(access_id)
console.log(res);
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fincode",
"version": "0.1.0",
"version": "0.1.1",
"description": "Type scripts for Fincode",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading

0 comments on commit 0666f4c

Please sign in to comment.