request-payment.uvue 6.2 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
      } as GetProviderSyncOptions)
      console.log(provider)
      provider.providerObjects.forEach((value : UniProvider) => {
        switch (value.id) {
          case 'alipay':
M
mahaifeng 已提交
32 33
            var aliPayProvider = value as UniPaymentAlipayProvider
            console.log('alipay', aliPayProvider)
34
            this.providerList.push({
M
mahaifeng 已提交
35 36 37
              name: aliPayProvider.description,
              id: aliPayProvider.id,
              provider: aliPayProvider
38 39 40
            } as PayItem);
            break;
          case 'wxpay':
M
mahaifeng 已提交
41 42
            var wxPayProvider = value as UniPaymentWxpayProvider
            console.log('wxpay', wxPayProvider)
43
            this.providerList.push({
M
mahaifeng 已提交
44 45 46
              name: wxPayProvider.description,
              id: wxPayProvider.id,
              provider: wxPayProvider
47 48 49 50
            } as PayItem);
            break;
          default:
            break;
M
mahaifeng 已提交
51
        }
52
      })
M
mahaifeng 已提交
53 54 55 56 57
    },
    methods: {
      requestPayment(e : PayItem) {
        const provider = e.id
        if (provider == "alipay") {
M
mahaifeng 已提交
58
          this.payAli(provider)
M
mahaifeng 已提交
59 60 61 62 63 64 65 66
        } 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 {
M
mahaifeng 已提交
67
            this.payWX(provider)
M
mahaifeng 已提交
68 69 70
          }
          // #endif
          // #ifdef APP-IOS
71
          if (e.provider != null && ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled == undefined || ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled != null && (e.provider as UniPaymentWxpayProvider).isWeChatInstalled == false))) {
M
mahaifeng 已提交
72 73 74 75 76
            uni.showToast({
              title: "微信没有安装",
              icon: 'error'
            })
          } else {
M
mahaifeng 已提交
77
            this.payWX(provider)
M
mahaifeng 已提交
78 79
          }
          // #endif
80 81
        }
      },
M
mahaifeng 已提交
82
      payAli(id:string) {
83 84 85 86 87 88 89 90 91 92 93 94
        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({
M
mahaifeng 已提交
95
              provider: id,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
              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()
          },
        });
      },
M
mahaifeng 已提交
120
      payWX(id:string) {
121 122 123 124 125 126
        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 已提交
127

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

132 133 134
        // #ifdef APP-IOS
        packageName = res.bundleId
        // #endif
M
mahaifeng 已提交
135

136 137
        if (packageName == 'io.dcloud.hellouniappx') {//hello uniappx
          url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=0.01'
M
mahaifeng 已提交
138
        }
139 140 141 142 143 144 145 146 147 148 149
        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({
M
mahaifeng 已提交
150
              provider: id,
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
              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 已提交
193 194
    }
  }
M
mahaifeng 已提交
195 196 197 198 199
</script>

<style>

</style>