import { useMemo } from 'react';
import { useRequest } from 'ahooks';
import NavBar from "../../../../../../NavBar";
import StripeSDK from "../../../../../../StripeSDK";
import Content from "./Content";
import { isWxApp } from "../../../../../../../utils";
import { getStripeKey } from "./serve";
import styles from "./index.less";
var StripePayment = function StripePayment() {
  var _getStripeKey$data;
  var history = useHistory();
  //@ts-ignore
  var query = history.location.query;

  /**
   * @title: 获取 stripe key
   * @description:
   * @param {*}
   * @return {*}
   * @Author: Wzw
   * @Date: 2022-04-01 11:05
   */
  var _getStripeKey = useRequest(getStripeKey, {
    formatResult: function formatResult(res) {
      return res.data;
    },
    onError: function onError() {
      goFinish('error');
    }
  });

  /**
   * @title: 返回订单详情
   * @description:
   * @param {*}
   * @return {*}
   * @Author: Wzw
   * @Date: 2022-04-01 11:06
   */
  var goBack = function goBack() {
    history.replace("/order/details?id=".concat(query === null || query === void 0 ? void 0 : query.order_id));
  };

  /**
   * @title: 跳转至支付成功 | 失败页面
   * @description:
   * @param {*} status
   * @return {*}
   * @Author: Wzw
   * @Date: 2022-04-01 11:06
   */
  var goFinish = function goFinish(status) {
    if (query !== null && query !== void 0 && query.order_id) {
      var _pathname = '';
      // 微信环境走微信的页面
      if ((query === null || query === void 0 ? void 0 : query.scene) === 'wxapp') {
        if (status === 'success') {
          _pathname = '/miniapp-payment/success';
        } else {
          _pathname = '/miniapp-payment/fail';
        }
      } else {
        if (status === 'success') {
          _pathname = '/payment-finish/success';
        } else {
          _pathname = '/payment-finish/fail';
        }
      }
      history.replace("".concat(_pathname, "?order_id=").concat(query === null || query === void 0 ? void 0 : query.order_id, "&amount=").concat(query.amount, "&status=").concat(status));
    } else {
      history.replace('/');
    }
  };

  /**
   * @title: 支付回调
   * @description:
   * @param {*}
   * @return {*}
   * @Author: Wzw
   * @Date: 2022-04-01 11:06
   */
  var onPaymentChange = function onPaymentChange(status) {
    // 成功
    if (status === 'succeeded') {
      goFinish('success');
    }

    // 处理中
    if (status === 'processing') {
      goFinish('success');
    }

    // 失败
    if (status === 'requires_payment_method') {
      goFinish('error');
    }

    // 错误
    if (status === 'error') {
      goFinish('error');
    }
  };
  var _title = useMemo(function () {
    return decodeURIComponent(query.payment_method || '');
  }, [query]);
  return /*#__PURE__*/React.createElement("div", {
    className: styles['stripe-payment-page']
  }, /*#__PURE__*/React.createElement(NavBar, {
    name: _title,
    onChange: goBack,
    bgColor: "#ffffff",
    nameColor: "rgba(0,0,0,.9)",
    backColor: "rgba(0,0,0,.9)"
  }), isWxApp() ? null : /*#__PURE__*/React.createElement(StripeSDK, {
    public_key: (_getStripeKey$data = _getStripeKey.data) === null || _getStripeKey$data === void 0 ? void 0 : _getStripeKey$data.publishable_key
  }, /*#__PURE__*/React.createElement(Content, {
    onChange: onPaymentChange
  })));
};
export default StripePayment;