uniapp 小程序支付功能


項目需求

項目生成訂單,需要用戶通過小程序進行支付。

解決方案

使用uni.requestPayment(),調用小程序中的付款功能。

<script>
	export default {
		data() {
			return {
				request:{
          id:xxxxx,
          info:'xxxx'
        }
			}
		},
		onLoad() {

		},
		methods: {
			// 提交訂單
			subOrder() {
        this.post("api/order/createOrder", this.request).then(res => {
					if (res.code == 0) {
						// 得到訂單的反參調用微信支付
						let pay = JSON.parse(res.data.prepayId);
						uni.requestPayment({
							timeStamp: pay.timeStamp,
							nonceStr: pay.nonceStr,
							package: pay.package,
							signType: pay.signType,
							paySign: pay.paySign,
							// 支付成功的回調
							success(result) {
								console.log(result)
								if (result.errMsg == "requestPayment:ok") {
									uni.showToast({
										icon: "success",
										title: "購買成功!"
									})
							 }

							},
							// 支付失敗回調
							fail(err) {
								uni.showToast({
									icon: "none",
									title: "支付失敗"
								})
							}
						})
					}
				});
      }
		}
	}
</script>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM