request-payment.uvue 2.0 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4 5 6 7 8 9 10 11
<template>
  <page-head title="支付宝支付"></page-head>
  <button :type="btnType" style="margin: 10px;" @click="pay()">{{btnText}}</button>
</template>

<script>
  export default {
    data() {
      return {
        btnText: "支付宝支付",
        btnType: "primary",
W
WOSHIMAHAIFENG 已提交
12
        orderInfo: "",
M
mahaifeng 已提交
13
        errorCode: 0,
W
WOSHIMAHAIFENG 已提交
14 15
        errorMsg: "",
        complete: false
M
mahaifeng 已提交
16 17 18 19
      }
    },
    methods: {
      pay() {
W
WOSHIMAHAIFENG 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32
        uni.showLoading({
          title: "请求中..."
        })
        uni.request({
          url: 'https://demo.dcloud.net.cn/payment/alipay/?total=0.01',
          method: 'GET',
          timeout: 6000,
          success: (res) => {
            uni.hideLoading()
            uni.requestPayment({
              provider: "alipay",
              orderInfo: res.data as string,
              fail: (res : RequestPaymentFail) => {
W
WOSHIMAHAIFENG 已提交
33
                console.log(JSON.stringify(res))
W
WOSHIMAHAIFENG 已提交
34
                this.errorCode = res.errCode
W
WOSHIMAHAIFENG 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
                uni.showToast({
                  icon: 'error',
                  title: 'errorCode:' + this.errorCode
                });
              },
              success: (res : RequestPaymentSuccess) => {
                console.log(JSON.stringify(res))
                uni.showToast({
                  icon: 'success',
                  title: '支付成功'
                });
              }
            } as RequestPaymentOptions)
M
mahaifeng 已提交
48
          },
W
WOSHIMAHAIFENG 已提交
49 50 51 52
          fail: () => {
            uni.hideLoading()
          },
        });
W
WOSHIMAHAIFENG 已提交
53
      },
W
WOSHIMAHAIFENG 已提交
54 55 56 57 58 59
      //自动化测试使用
      jest_pay() {
        uni.requestPayment({
          provider: "alipay",
          orderInfo: this.orderInfo,
          fail: (res : RequestPaymentFail) => {
W
WOSHIMAHAIFENG 已提交
60 61
            this.errorCode = res.errCode
            this.complete = true
W
WOSHIMAHAIFENG 已提交
62 63
          },
          success: (res : RequestPaymentSuccess) => {
W
WOSHIMAHAIFENG 已提交
64 65
            console.log(JSON.stringify(res))
             this.complete = true
W
WOSHIMAHAIFENG 已提交
66 67
          }
        } as RequestPaymentOptions)
M
mahaifeng 已提交
68 69 70 71 72 73 74 75
      }
    }
  }
</script>

<style>

</style>