request-payment.uvue 6.3 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 25 26 27 28
  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 () {
      uni.getProvider({
        service: "payment",
        success: (e) => {
          console.log("payment success:" + JSON.stringify(e));
29
          let array = e.providerIds as string[]
M
mahaifeng 已提交
30 31 32 33 34 35
          array.forEach((value : string) => {
            switch (value) {
              case 'alipay':
                this.providerList.push({
                  name: '支付宝',
                  id: "alipay",
36
                  provider: e.providerObjects.find((item) : boolean => {
M
mahaifeng 已提交
37 38 39 40 41 42 43 44
                    return item.id == 'alipay'
                  })
                } as PayItem);
                break;
              case 'wxpay':
                this.providerList.push({
                  name: '微信',
                  id: "wxpay",
45
                  provider: e.providerObjects.find((item) : boolean => {
M
mahaifeng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
                    return item.id == 'wxpay'
                  })
                } as PayItem);
                break;
              default:
                break;
            }
          })
        },
        fail: (e) => {
          console.log("获取支付通道失败:", e);
        }
      });
    },
    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
77
          if (e.provider != null && ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled == undefined || ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled != null && (e.provider as UniPaymentWxpayProvider).isWeChatInstalled == false))) {
M
mahaifeng 已提交
78 79 80 81 82
            uni.showToast({
              title: "微信没有安装",
              icon: 'error'
            })
          } else {
83
            this.payWX()
M
mahaifeng 已提交
84 85
          }
          // #endif
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 123 124 125 126 127 128 129 130 131 132
        }
      },
      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 已提交
133

134 135 136
        // #ifdef APP-ANDROID
        packageName = res.packageName
        // #endif
M
mahaifeng 已提交
137

138 139 140
        // #ifdef APP-IOS
        packageName = res.bundleId
        // #endif
M
mahaifeng 已提交
141

142 143
        if (packageName == 'io.dcloud.hellouniappx') {//hello uniappx
          url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=0.01'
M
mahaifeng 已提交
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 189 190 191 192 193 194 195 196 197 198
        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 已提交
199 200
    }
  }
M
mahaifeng 已提交
201 202 203 204 205
</script>

<style>

</style>