request-payment.uvue 4.7 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 29 30 31 32 33 34 35 36
	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));
					let array = e.provider as string[]
					array.forEach((value : string) => {
						switch (value) {
							case 'alipay':
								this.providerList.push({
									name: '支付宝',
									id: "alipay",
									provider: e.providers.find((item) : boolean => {
M
mahaifeng 已提交
37
										return item.id == 'alipay'
M
mahaifeng 已提交
38 39 40 41 42 43 44 45
									})
								} as PayItem);
								break;
							case 'wxpay':
								this.providerList.push({
									name: '微信',
									id: "wxpay",
									provider: e.providers.find((item) : boolean => {
M
mahaifeng 已提交
46
										return item.id == 'wxpay'
M
mahaifeng 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
									})
								} 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") {
W
WOSHIMAHAIFENG 已提交
66

M
mahaifeng 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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
					if (e.provider != null && e.provider?.isAppExist==false) {
						uni.showToast({
							title: "微信没有安装",
							icon:'error'
						})
					} else {
						this.payWX()
					}

				}
			},
			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: "请求中..."
				})
120 121
				let url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__uniappx/?total=0.01'
				const res = uni.getAppBaseInfo();
雪洛's avatar
雪洛 已提交
122
				let packageName:string | null = ''
123 124 125 126 127 128 129 130 131 132 133

				// #ifdef APP-ANDROID
				packageName = res.packageName
				// #endif

				// #ifdef APP-IOS
				packageName = res.bundleId
				// #endif

				if (packageName == 'io.dcloud.hellouniappx') {//hello uniappx
				  url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__HelloUniAppX/?total=0.01'
M
mahaifeng 已提交
134 135 136 137 138 139 140 141 142 143 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
				}
				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 已提交
191 192 193 194 195
</script>

<style>

</style>