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 } = window.location;
        let baseUrl = `${protocol}//${hostname}`
        if (hostname === 'localhost') {
            baseUrl = `${protocol}//${hostname}:${port}`
        }
        const cbUrl = `${baseUrl}`
        const response = await sdk.payTemplateCode(templateCode, imageUrl, cbUrl)
        if (response.url) {
            window.location.href = response.url;
            return;
        }
        throw new Error(`payment error: ${response}`)
    }
}
