import './stringPay.scss';
import Taro, { Component, useState, useCallback } from '@tarojs/taro';
import { View, Button, Textarea } from '@tarojs/components';

class stringPay extends Component {
  render() {
    const [value, setValue] = useState('');
    const inputHandle = useCallback(
      e => {
        setValue(e.detail.value);
      },
      [setValue],
    );
    const payHandle = () => {
      console.log(value);
      if (value) {
        const payData = JSON.parse(value);
        Taro.requestPayment({
          timeStamp: payData.timeStamp.toString(),
          nonceStr: payData.nonceStr,
          package: payData.package,
          signType: payData.signType,
          paySign: payData.paySign,
        }).then(res => {
          console.log(res);
          Taro.showToast({
            title: '支付成功！',
            icon: 'success',
            mask: true,
          });
          setValue('');
        });
      }
    };
    return (
      <View className='stringPay'>
        <Textarea
          className='stringPay-input'
          value={value}
          maxlength={-1}
          onInput={inputHandle}></Textarea>
        <Button onClick={payHandle}>支付</Button>
      </View>
    );
  }
}
export default stringPay;
