request-payment.uvue 5.8 KB
Newer Older
M
mahaifeng 已提交
1
<template>
M
mahaifeng 已提交
2
  <page-head title="发起支付"></page-head>
W
WOSHIMAHAIFENG 已提交
3

M
mahaifeng 已提交
4 5 6 7
  <template v-if="providerList.length > 0">
    <button style="margin-top: 20px;" type="primary" v-for="(item,index) in providerList" :key="index"
      @click="requestPayment(item)">{{item.name}}支付</button>
  </template>
M
mahaifeng 已提交
8 9 10
</template>

<script>
M
mahaifeng 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24
  export type PayItem = { id : string, name : string, provider ?: UniProvider }
  export default {
    data() {
      return {
        btnText: "支付宝支付",
        btnType: "primary",
        orderInfo: "",
        errorCode: 0,
        errorMsg: "",
        complete: false,
        providerList: [] as PayItem[]
      }
    },
    onLoad: function () {
25
      let provider = uni.getProviderSync({
M
mahaifeng 已提交
26
        service: "payment",
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      } as GetProviderSyncOptions)
      console.log(provider)
      provider.providerObjects.forEach((value : UniProvider) => {
        switch (value.id) {
          case 'alipay':
            this.providerList.push({
              name: '支付宝',
              id: "alipay",
              provider: value
            } as PayItem);
            break;
          case 'wxpay':
            this.providerList.push({
              name: '微信',
              id: "wxpay",
              provider: value
            } as PayItem);
            break;
          default:
            break;
M
mahaifeng 已提交
47
        }
48
      })
M
mahaifeng 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    },
    methods: {
      requestPayment(e : PayItem) {
        const provider = e.id
        if (provider == "alipay") {
          this.payAli()
        } else if (provider == "wxpay") {
          // #ifdef APP-ANDROID
          if (e.provider != null && e.provider instanceof UniPaymentWxpayProvider && !((e.provider as UniPaymentWxpayProvider).isWeChatInstalled)) {
            uni.showToast({
              title: "微信没有安装",
              icon: 'error'
            })
          } else {
            this.payWX()
          }
          // #endif
          // #ifdef APP-IOS
67
          if (e.provider != null && ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled == undefined || ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled != null && (e.provider as UniPaymentWxpayProvider).isWeChatInstalled == false))) {
M
mahaifeng 已提交
68 69 70 71 72
            uni.showToast({
              title: "微信没有安装",
              icon: 'error'
            })
          } else {
73
            this.payWX()
M
mahaifeng 已提交
74 75
          }
          // #endif
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
      },
      payAli() {
        uni.showLoading({
          title: "请求中..."
        })
        uni.request({
          url: 'https://demo.dcloud.net.cn/payment/alipay/?total=0.01',
          method: 'GET',
          timeout: 6000,
          success: (res) => {
            this.orderInfo = JSON.stringify(res.data);
            console.log("====" + this.orderInfo)
            uni.hideLoading()
            uni.requestPayment({
              provider: "alipay",
              orderInfo: res.data as string,
              fail: (res) => {
                console.log(JSON.stringify(res))
                this.errorCode = res.errCode
                uni.showToast({
                  icon: 'error',
                  title: 'errorCode:' + this.errorCode
                });
              },
              success: (res) => {
                console.log(JSON.stringify(res))
                uni.showToast({
                  icon: 'success',
                  title: '支付成功'
                });
              }
            })
          },
          fail: (e) => {
            console.log(e)
            uni.hideLoading()
          },
        });
      },
      payWX() {
        uni.showLoading({
          title: "请求中..."
        })
        let url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__uniappx/?total=0.01'
        const res = uni.getAppBaseInfo();
        let packageName : string | null
M
mahaifeng 已提交
123

124 125 126
        // #ifdef APP-ANDROID
        packageName = res.packageName
        // #endif
M
mahaifeng 已提交
127

128 129 130
        // #ifdef APP-IOS
        packageName = res.bundleId
        // #endif
M
mahaifeng 已提交
131

132 133
        if (packageName == 'io.dcloud.hellouniappx') {//hello uniappx
          url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=0.01'
M
mahaifeng 已提交
134
        }
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        uni.request({
          url: url,
          method: 'GET',
          timeout: 6000,
          header: {
            "Content-Type": "application/json"
          } as UTSJSONObject,
          success: (res) => {
            console.log(res.data)
            uni.hideLoading()
            uni.requestPayment({
              provider: "wxpay",
              orderInfo: JSON.stringify(res.data),
              fail: (res) => {
                console.log(JSON.stringify(res))
                this.errorCode = res.errCode
                uni.showToast({
                  duration: 5000,
                  icon: 'error',
                  title: 'errorCode:' + this.errorCode,
                });
              },
              success: (res) => {
                console.log(JSON.stringify(res))
                uni.showToast({
                  duration: 5000,
                  icon: 'success',
                  title: '支付成功'
                });
              }
            })
          },
          fail: (res) => {
            uni.hideLoading()
            console.log(res)
          },
        });
      },

      //自动化测试使用
      jest_pay() {
        uni.requestPayment({
          provider: "alipay",
          orderInfo: this.orderInfo,
          fail: (res : RequestPaymentFail) => {
            this.errorCode = res.errCode
            this.complete = true
          },
          success: (res : RequestPaymentSuccess) => {
            console.log(JSON.stringify(res))
            this.complete = true
          }
        } as RequestPaymentOptions)
      }
M
mahaifeng 已提交
189 190
    }
  }
M
mahaifeng 已提交
191 192 193 194 195
</script>

<style>

</style>