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

  <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>
W
WOSHIMAHAIFENG 已提交
11
  export type PayItem = { id : string, name : string }
M
mahaifeng 已提交
12 13 14 15 16
  export default {
    data() {
      return {
        btnText: "支付宝支付",
        btnType: "primary",
W
WOSHIMAHAIFENG 已提交
17
        orderInfo: "",
M
mahaifeng 已提交
18
        errorCode: 0,
W
WOSHIMAHAIFENG 已提交
19
        errorMsg: "",
W
WOSHIMAHAIFENG 已提交
20 21
        complete: false,
        providerList: [] as PayItem[]
M
mahaifeng 已提交
22 23
      }
    },
W
WOSHIMAHAIFENG 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    onLoad: function () {
      uni.getProvider({
        service: "payment",
        success: (e) => {
          console.log("payment success:" + JSON.stringify(e));
          let array = e.provider as string[]
          array.forEach((value : string) => {
            switch (value) {
              case 'alipay':
                this.providerList.push({
                  name: '支付宝',
                  id: "alipay",
                } as PayItem);
                break;
              case 'wxpay':
                this.providerList.push({
                  name: '微信',
                  id: "wxpay",
                } as PayItem);
                break;
              default:
                break;
            }
          })
        },
        fail: (e) => {
          console.log("获取支付通道失败:", e);
        }
      });
    },
M
mahaifeng 已提交
54
    methods: {
W
WOSHIMAHAIFENG 已提交
55 56 57 58 59 60 61 62 63
      requestPayment(e : PayItem) {
        const provider = e.id
        if (provider == "alipay") {
          this.payAli()
        } else if (provider == "wxpay") {
          this.payWX()
        }
      },
      payAli() {
W
WOSHIMAHAIFENG 已提交
64 65 66 67 68 69 70 71
        uni.showLoading({
          title: "请求中..."
        })
        uni.request({
          url: 'https://demo.dcloud.net.cn/payment/alipay/?total=0.01',
          method: 'GET',
          timeout: 6000,
          success: (res) => {
W
WOSHIMAHAIFENG 已提交
72 73
            this.orderInfo = JSON.stringify(res.data);
            console.log("====" + this.orderInfo)
W
WOSHIMAHAIFENG 已提交
74 75 76 77
            uni.hideLoading()
            uni.requestPayment({
              provider: "alipay",
              orderInfo: res.data as string,
W
WOSHIMAHAIFENG 已提交
78
              fail: (res) => {
W
WOSHIMAHAIFENG 已提交
79
                console.log(JSON.stringify(res))
W
WOSHIMAHAIFENG 已提交
80
                this.errorCode = res.errCode
W
WOSHIMAHAIFENG 已提交
81 82 83 84 85
                uni.showToast({
                  icon: 'error',
                  title: 'errorCode:' + this.errorCode
                });
              },
W
WOSHIMAHAIFENG 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
              success: (res) => {
                console.log(JSON.stringify(res))
                uni.showToast({
                  icon: 'success',
                  title: '支付成功'
                });
              }
            })
          },
          fail: (e) => {
            console.log(e)
            uni.hideLoading()
          },
        });
      },
      payWX() {
        uni.showLoading({
          title: "请求中..."
        })
W
WOSHIMAHAIFENG 已提交
105 106 107 108 109
        let url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__uniappx/?total=1'
        const res = uni.getAppBaseInfo();
        if (res.packageName == 'io.dcloud.hellouniappx') {//hello uniappx
          url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=1'
        }
W
WOSHIMAHAIFENG 已提交
110
        uni.request({
W
WOSHIMAHAIFENG 已提交
111
          url: url,
W
WOSHIMAHAIFENG 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
          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) => {
W
WOSHIMAHAIFENG 已提交
133 134
                console.log(JSON.stringify(res))
                uni.showToast({
W
WOSHIMAHAIFENG 已提交
135
                  duration: 5000,
W
WOSHIMAHAIFENG 已提交
136 137 138 139
                  icon: 'success',
                  title: '支付成功'
                });
              }
W
WOSHIMAHAIFENG 已提交
140
            })
M
mahaifeng 已提交
141
          },
W
WOSHIMAHAIFENG 已提交
142
          fail: (res) => {
W
WOSHIMAHAIFENG 已提交
143
            uni.hideLoading()
W
WOSHIMAHAIFENG 已提交
144
            console.log(res)
W
WOSHIMAHAIFENG 已提交
145 146
          },
        });
W
WOSHIMAHAIFENG 已提交
147
      },
W
WOSHIMAHAIFENG 已提交
148

W
WOSHIMAHAIFENG 已提交
149 150 151 152 153 154
      //自动化测试使用
      jest_pay() {
        uni.requestPayment({
          provider: "alipay",
          orderInfo: this.orderInfo,
          fail: (res : RequestPaymentFail) => {
W
WOSHIMAHAIFENG 已提交
155
            this.errorCode = res.errCode
W
WOSHIMAHAIFENG 已提交
156
            this.complete = true
W
WOSHIMAHAIFENG 已提交
157 158
          },
          success: (res : RequestPaymentSuccess) => {
W
WOSHIMAHAIFENG 已提交
159 160
            console.log(JSON.stringify(res))
            this.complete = true
W
WOSHIMAHAIFENG 已提交
161 162
          }
        } as RequestPaymentOptions)
M
mahaifeng 已提交
163 164 165 166 167 168 169 170
      }
    }
  }
</script>

<style>

</style>