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 14 15 16 17 18
        errorCode: 0,
        errorMsg: ""
      }
    },
    methods: {
      pay() {
W
WOSHIMAHAIFENG 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
        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 已提交
32
                console.log(JSON.stringify(res))
W
WOSHIMAHAIFENG 已提交
33
                this.errorCode = res.errCode
W
WOSHIMAHAIFENG 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
                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 已提交
47
          },
W
WOSHIMAHAIFENG 已提交
48 49 50 51
          fail: () => {
            uni.hideLoading()
          },
        });
W
WOSHIMAHAIFENG 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
      },
      //自动化测试使用
      jest_pay() {
        uni.requestPayment({
          provider: "alipay",
          orderInfo: this.orderInfo,
          fail: (res : RequestPaymentFail) => {
            console.log(JSON.stringify(res))
            this.errorCode = res.errCode
          },
          success: (res : RequestPaymentSuccess) => {
            console.log(JSON.stringify(res))
          }
        } as RequestPaymentOptions)
M
mahaifeng 已提交
66 67 68 69 70 71 72 73
      }
    }
  }
</script>

<style>

</style>