30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import { useServerSdk } from "../../hooks";
|
|
import { Payment } from "../types/payment";
|
|
export class H5Payment extends Payment {
|
|
async pay(templateCode: string, imageUrl: string) {
|
|
const sdk = useServerSdk()
|
|
const { hostname, protocol, port, hash } = window.location;
|
|
let baseUrl = `${protocol}//${hostname}`
|
|
if (hostname === 'localhost') {
|
|
baseUrl = `${protocol}//${hostname}:${port}`
|
|
}
|
|
const response = await sdk.payTemplateCode(templateCode, imageUrl, `${baseUrl}#/${hash}`)
|
|
if (response.url) {
|
|
window.location.href = response.url;
|
|
return;
|
|
}
|
|
throw new Error(`payment error: ${response}`)
|
|
}
|
|
|
|
async checkPaymentResult(): Promise<{ paymentId?: string; templateCode?: string }> {
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const paymentId = urlParams.get('paymentId') || undefined;
|
|
const templateCode = urlParams.get('templateCode') || undefined;
|
|
|
|
return {
|
|
paymentId,
|
|
templateCode
|
|
};
|
|
}
|
|
}
|