import { Constants } from '../common/Constants';
import * as WxOpenSdk from '@tencent/wechat_open_sdk';
import { WXApi } from './wechat/WXApiEventHandlerImpl';
import { AppUtil } from '@pura/harmony-utils';
import { PayOrderEntity } from '../entity/OrderPayEntity';
import { Pay } from '@cashier_alipay/cashiersdk/src/main/ets/api/Pay';
import { ToastUtils } from './ToastUtils';

export class PayUtils {

  /**
   * 微信支付
   * @param orderEntity
   */
  static async toWXPay(orderEntity: PayOrderEntity) {
    let req = new WxOpenSdk.PayReq
    req.partnerId = orderEntity.partnerId
    req.appId = Constants.WX_APP_ID
    req.packageValue = 'Sign=WXPay'
    req.prepayId = orderEntity.prepayId
    req.nonceStr = orderEntity.nonceStr
    req.timeStamp = orderEntity.timeStamp
    req.sign = orderEntity.sign
    WXApi.sendReq(AppUtil.getContext(), req)
  }

  /**
   * 微信小程序支付
   */
  static async toWXMPPay(outTradeNo: string, mpAppId: string) {
    try {
        let launchMiniProgramReq = new WxOpenSdk.LaunchMiniProgramReq()
        launchMiniProgramReq.userName = mpAppId  //拉起的小程序的原始id
        launchMiniProgramReq.path = `pages/index/index?outTradeNo=${outTradeNo}`
        launchMiniProgramReq.miniprogramType = 0 //拉起小程序的类型 0-正式版 1-开发版 2-体验版
        await WXApi.sendReq(AppUtil.getContext(), launchMiniProgramReq)
    } catch (e) {
      ToastUtils.show('跳转失败，请联系客服')
    }
  }

  /**
   * 支付宝支付
   * @param orderInfo
   * @returns
   */
  static async toAliPay(orderInfo: string): Promise<Map<string, string>> {
    return new Pay().pay(orderInfo, true);
  }
}