123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <template>
- <page-head title="发起支付"></page-head>
- <view class="uni-common-mt" style="padding: 0 10px;">
- <text>如对当前页面的支付示例功能有任何疑问,通过电子邮件:service@dcloud.io 联系我们</text>
- </view>
- <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>
- </template>
- <script>
- export type PayItem = { id : string, name : string, provider ?: UniProvider }
- export default {
- data() {
- return {
- btnText: "支付宝支付",
- btnType: "primary",
- orderInfo: "",
- errorCode: 0,
- errorMsg: "",
- complete: false,
- fail: false,
- providerList: [] as PayItem[],
- outTradeNo: "",
- openid: ""
- }
- },
- onLoad: function () {
- // #ifdef APP
- let provider = uni.getProviderSync({
- service: "payment",
- } as GetProviderSyncOptions)
- console.log(provider)
- provider.providerObjects.forEach((value : UniProvider) => {
- switch (value.id) {
- case 'alipay':
- var aliPayProvider = value as UniPaymentAlipayProvider
- console.log('alipay', aliPayProvider)
- this.providerList.push({
- name: aliPayProvider.description,
- id: aliPayProvider.id,
- provider: aliPayProvider
- } as PayItem);
- break;
- case 'wxpay':
- var wxPayProvider = value as UniPaymentWxpayProvider
- console.log('wxpay', wxPayProvider)
- this.providerList.push({
- name: wxPayProvider.description,
- id: wxPayProvider.id,
- provider: wxPayProvider
- } as PayItem);
- break;
- default:
- break;
- }
- })
- // #endif
- // #ifdef MP-WEIXIN
- this.getOpenId()
- this.providerList.push({
- name: '微信支付',
- id: 'wxpay'
- } as PayItem);
- // #endif
- },
- methods: {
- requestPayment(e : PayItem) {
- const provider = e.id
- // #ifdef APP
- if (provider == "alipay") {
- this.payAli(provider)
- } else if (provider == "wxpay") {
- // #ifdef APP-ANDROID
- if (e.provider != null && e.provider instanceof UniPaymentWxpayProvider && !((e.provider as UniPaymentWxpayProvider).isWeChatInstalled)) {
- uni.showToast({
- title: "微信没有安装",
- icon: 'error'
- })
- } else {
- this.payWX(provider)
- }
- // #endif
- // #ifdef APP-IOS || APP-HARMONY
- if (e.provider != null && ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled == undefined || ((e.provider as UniPaymentWxpayProvider).isWeChatInstalled != null && (e.provider as UniPaymentWxpayProvider).isWeChatInstalled == false))) {
- uni.showToast({
- title: "微信没有安装",
- icon: 'error'
- })
- } else {
- this.payWX(provider)
- }
- // #endif
- }
- // #endif
- // #ifdef MP-WEIXIN
- this.payMpWexin()
- // #endif
- },
- payAli(id : string) {
- 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: id,
- 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(id : string) {
- uni.showLoading({
- title: "请求中..."
- })
- let url = 'https://demo.dcloud.net.cn/payment/wxpayv3.__UNI__uniappx/?total=0.01'
- const res = uni.getAppBaseInfo();
- let packageName : string | null
- // #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'
- }
- 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: id,
- 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)
- },
- });
- },
- // #ifdef MP-WEIXIN
- getOpenId() {
- uni.showLoading({
- title: "请稍等...",
- mask: true
- });
- let bundleId = uni.getAccountInfoSync().miniProgram.appId;
- uni.login({
- provider: 'weixin',
- success: (res) => {
- uni.request({
- url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/getOpenId",
- method: "GET",
- data: {
- code: res.code,
- bundleId
- },
- success: (res) => {
- uni.hideLoading();
- this.openid = res.data.openid;
- console.log('openid: ', this.openid);
- },
- fail: (err) => {
- uni.hideLoading();
- console.error("request-err", err);
- }
- });
- }
- })
- },
- payMpWexin() {
- let bundleId = uni.getAccountInfoSync().miniProgram.appId;
- let random = Math.floor(Math.random() * 9000) + 1000;
- this.outTradeNo = `test${Date.now()}${random}`;
- console.log('outTradeNo: ', this.outTradeNo)
- uni.showLoading({
- title: "请求中..."
- });
- uni.request({
- url: "https://env-00jxt67zj8kj.dev-hz.cloudbasefunction.cn/uni-pay-api/getOrderInfo",
- method: "GET",
- data: {
- outTradeNo: this.outTradeNo,
- bundleId,
- openid: this.openid,
- totalFee: 1
- },
- success: (res) => {
- uni.hideLoading();
- let data = res.data as UTSJSONObject;
- let errCode = data['errCode'] as number;
- if (errCode != 0) {
- uni.showModal({
- title: "提示",
- content: data['errMsg'] as string,
- showCancel: false
- });
- return;
- }
- let orderInfo = data["orderInfo"] as string;
- console.log('orderInfo: ', orderInfo)
- uni.requestPayment({
- provider: "wxpay",
- orderInfo: orderInfo,
- ...JSON.parse(orderInfo),
- success: (res) => {
- console.log('res: ', res)
- uni.showToast({
- title: "支付成功",
- icon: 'success'
- });
- },
- fail: (err) => {
- console.error("err", err);
- uni.hideLoading();
- uni.showToast({
- title: "支付失败",
- icon: 'error'
- });
- }
- });
- },
- fail: (err) => {
- uni.hideLoading();
- console.error("request-err", err);
- }
- });
- },
- // #endif
- //自动化测试使用
- jest_pay() {
- uni.requestPayment({
- provider: "alipay",
- orderInfo: this.orderInfo,
- fail: (res : RequestPaymentFail) => {
- this.errorCode = res.errCode
- this.complete = true
- this.fail = true
- },
- success: (res : RequestPaymentSuccess) => {
- console.log(JSON.stringify(res))
- this.complete = true
- this.fail = false
- }
- } as RequestPaymentOptions)
- }
- }
- }
- </script>
- <style>
- </style>
|