// Copyright 2022 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from "react"; import {Button, Descriptions, Modal, Spin} from "antd"; import {CheckCircleTwoTone} from "@ant-design/icons"; import i18next from "i18next"; import * as ProductBackend from "./backend/ProductBackend"; import * as Setting from "./Setting"; class ProductBuyPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, productName: props.match?.params.productName, product: null, isPlacingOrder: false, qrCodeModalProvider: null, }; } UNSAFE_componentWillMount() { this.getProduct(); } getProduct() { ProductBackend.getProduct("admin", this.state.productName) .then((product) => { this.setState({ product: product, }); }); } getProductObj() { if (this.props.product !== undefined) { return this.props.product; } else { return this.state.product; } } getCurrencySymbol(product) { if (product?.currency === "USD") { return "$"; } else if (product?.currency === "CNY") { return "¥"; } else { return "(Unknown currency)"; } } getCurrencyText(product) { if (product?.currency === "USD") { return i18next.t("product:USD"); } else if (product?.currency === "CNY") { return i18next.t("product:CNY"); } else { return "(Unknown currency)"; } } getPrice(product) { return `${this.getCurrencySymbol(product)}${product?.price} (${this.getCurrencyText(product)})`; } buyProduct(product, provider) { if (provider.clientId.startsWith("http")) { this.setState({ qrCodeModalProvider: provider, }); return; } this.setState({ isPlacingOrder: true, }); ProductBackend.buyProduct(this.state.product.owner, this.state.productName, provider.name) .then((res) => { if (res.status === "ok") { const payUrl = res.data; Setting.goToLink(payUrl); } else { Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`); this.setState({ isPlacingOrder: false, }); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } renderQrCodeModal() { return ( {" " + i18next.t("product:Please scan the QR code to pay")} } open={this.state.qrCodeModalProvider !== null} onOk={() => { Setting.goToLink(this.state.product.returnUrl); }} onCancel={() => { this.setState({ qrCodeModalProvider: null, }); }} okText={i18next.t("product:I have completed the payment")} cancelText={i18next.t("general:Cancel")}>

{ i18next.t("product:Please provide your username in the remark") } :   { Setting.getTag("default", this.props.account.name) }

{this.state.qrCodeModalProvider?.name}

); } getPayButton(provider) { let text = provider.type; if (provider.type === "Alipay") { text = i18next.t("product:Alipay"); } else if (provider.type === "WeChat Pay") { text = i18next.t("product:WeChat Pay"); } else if (provider.type === "Paypal") { text = i18next.t("product:Paypal"); } return ( ); } renderProviderButton(provider, product) { return ( this.buyProduct(product, provider)}> { this.getPayButton(provider) } ); } renderPay(product) { if (product === undefined || product === null) { return null; } if (product.state !== "Published") { return i18next.t("product:This product is currently not in sale."); } if (product.providerObjs.length === 0) { return i18next.t("product:There is no payment channel for this product."); } return product.providerObjs.map(provider => { return this.renderProviderButton(provider, product); }); } render() { const product = this.getProductObj(); if (product === null) { return null; } return (
{Setting.getLanguageText(product?.displayName)} {Setting.getLanguageText(product?.detail)} {product?.tag} {product?.name} {product?.name} { this.getPrice(product) } {product?.quantity} {product?.sold} { this.renderPay(product) } { this.renderQrCodeModal() }
); } } export default ProductBuyPage;