写不完
写不完
  • 发布:2021-03-03 18:09
  • 更新:2023-01-11 10:04
  • 阅读:6175

uuniapp做过IOS的内购支付,uni.requestPayment方法没有任何回调支付也不知道成功失败

分类:uni-app

根据官方的代码做IOS内购支付,支付环境与支付商品信息都能获取到,但是最后点击支付的时候没有任何回调信息。
<u-button v-if="!is_an" type="warning" @click="requestPayment('234')" ripple shape="circle">确认支付</u-button>

<script>  
    let iapChannel = null,  
        productId = '234',  
        productIds = ['234', '1234'];  
    export default {  
        data() {  
            return {  
                // 支付环境  
                zfhj: false,  
                //  
                info: {},  
                is_an: '',  
                payType: 0,  
                price: "",  
                //ios内购测试数据  
                loading: false,  
                disabled: true,  
                priceList: [{  
                    value: '234',  
                    text: '购买视频',  
                    checked: true  
                }, {  
                    value: '1234',  
                    text: '购买会员VIP ',  
                    checked: false  
                }]  
            };  
        },  
        computed: {  
            userInfo() {  
                return this.$store.state.userInfo  
            }  
        },  
        onLoad: function(options) {  
            this.info = options || {}  
            this.price = options.price || {}  
            this.id = options.id  
            this.chapter_id = options.chapter_id  
            if (!this.$store.state.hasLogin) {  
                uni.redirectTo({  
                    url: '../login/login'  
                })  
            }  
            //检测环境  
            switch (uni.getSystemInfoSync().platform) {  
                case 'android':  
                    console.log('运行Android上')  
                    this.is_an = true  
                    break;  
                case 'ios':  
                    console.log('运行iOS上')  
                    this.is_an = false  
                    //拉取支付环境  
                    plus.payment.getChannels((channels) => {  
                        console.log("获取到channel" + JSON.stringify(channels))  
                        this.zfhj = JSON.stringify(channels)  
                        for (var i in channels) {  
                            var channel = channels[i];  
                            if (channel.id === 'appleiap') {  
                                iapChannel = channel;  
                                this.requestOrder();  
                                this.zfhj = iapChannel  
                            }  
                        }  
                        if (!iapChannel) {  
                            this.errorMsg()  
                        }  
                    }, (error) => {  
                        this.errorMsg()  
                    });  
                    break;  
                default:  
                    console.log('运行在开发者工具上')  
                    break;  
            }  

        },  
        methods: {  
            /*********************************************************************************************************/  
            //ios内购支付  
            requestOrder() {  
                uni.showLoading({  
                    title: '检测支付环境...'  
                })  
                iapChannel.requestOrder(productIds, (orderList) => { //必须调用此方法才能进行 iap 支付  
                    this.disabled = false;  
                    console.log('requestOrder success666: ' + JSON.stringify(orderList));  
                    uni.hideLoading();  
                    this.zfhj = 'requestOrder success666: ' + JSON.stringify(orderList)  
                }, (e) => {  
                    this.zfhj = 'requestOrder failed: ' + JSON.stringify(orderList)  
                    console.log('requestOrder failed: ' + JSON.stringify(e));  
                    uni.hideLoading();  
                    this.errorMsg()  
                });  
            },  
            requestPayment(e) {  
                this.loading = true;  
                uni.requestPayment({  
                    provider: 'appleiap',  
                    orderInfo: {  
                        productid: this.productId,  
                        username: "购买视频"  
                    },  
                    success: (e) => {  
                        uni.showModal({  
                            content: "感谢您的赞助",  
                            showCancel: false  
                        })  
                        this.zfhj = "感谢您的赞助?" + e  
                    },  
                    fail: (e) => {  
                        this.zfhj = "支付失败?" + e  
                        uni.showModal({  
                            content: "支付失败,原因为: " + e.errMsg,  
                            showCancel: false  
                        })  
                    },  
                    complete: () => {  
                        console.log("payment结束")  
                        this.loading = false;  
                    }  
                })  
            },  
            applePriceChange(e) {  
                productId = e.detail.value;  
            },  
            errorMsg() {  
                uni.showModal({  
                    content: "暂不支持苹果 iap 支付",  
                    showCancel: false  
                })  
            },  
            /*********************************************************************************************************/  

    }  
</script>
2021-03-03 18:09 负责人:DCloud_iOS_XHY 分享
已邀请:
y***@163.com

y***@163.com

我们今天也碰到这个问题,不过后来发现是错把 productid 参数写成了 productId 导致的,这个接口有点坑,参数不对不报错,居然就一直卡在那没有任何回调。。

  • c***@163.com

    正常参数后回调正常嘛?我这里支付成功回调很慢

    2022-09-01 11:48

  • y***@163.com

    回复 c***@163.com: 过程确实是很慢,这个可能走苹果国外服务器了,非常慢。

    2022-09-01 17:52

  • 1***@qq.com

    APP卸载了重新编译安装一遍就能拿到了,坑我了起码半小时。。。

    2022-09-03 23:11

  • Gyys148

    遇到了一样的问题,感谢

    2022-10-17 15:16

  • d***@qq.com

    果然是这个问题,从文档里拷贝的代码,改成productid就可以了,感谢感谢。

    2022-11-15 23:47

DCloud_iOS_XHY

DCloud_iOS_XHY

直接调用5+api试一下看看,按照下面的方式

1.获取appleiap支付通道

var iap = null;  
plus.payment.getChannels(function(channels) {  
    for (var i in channels) {  
        var channel = channels[i];  
            // 获取 id 为 'appleiap' 的 channel  
            if (channel.id === 'appleiap') {  
                iap = channel;  
            }  
        }  
}, function(e) {  
    console.log("获取支付通道失败:" + e.message);  
});

2.获取订单信息

// ids 为在苹果开发后台配置的应用内购项目的标识集合  
var ids = ['donation', 'donation6'];   
// iap 为刚刚获取的`appleiap`支付通道  
iap.requestOrder(ids, function(e) {  
    // 获取订单信息成功回调方法  
    console.log('requestOrder success: ' + JSON.stringify(e));  
}, function(e) {  
    // 获取订单信息失败回调方法  
    console.log('requestOrder failed: ' + JSON.stringify(e));  
});

3.进行支付

plus.payment.request(iap, {  
    productid: id,  
    username: "appusername",  
    optimize: true  // 支付时设置 optimize: true  
}, function(result) {  
    // 支付成功,result 为 IAP商品交易信息对象 IAPTransaction 需将返回的支付凭证传给后端进行二次认证  
}, function(e) {  
    // 支付失败,需要调用一下 restoreComplateRequest 方法  
    restoreComplateRequest()  
});

4.恢复购买方法(获取已购的非消耗性商品和订阅商品)

注意:如果用户在支付过程中退出了App,或其他情况造成的丢单的消耗类型商品在支付完成后,首次调用该接口可返回支付凭证

function restoreComplateRequest() {  
    iap.restoreComplateRequest({}, function(results) {  
        // results 格式为数组存放恢复的IAP商品交易信息对象 IAPTransaction,通用需将返回的支付凭证传给后端进行二次认证  
    });  
}

5.在支付页面每次显示的时候调用 restoreComplateRequest()恢复购买方法确认是否有未完成或可恢复的订单

document.addEventListener('resume',function(){  
    restoreComplateRequest() // 在支付页面每次显示时调用 `restoreComplateRequest()`方法  
},false);
  • 2***@qq.com

    一直 获取订单信息失败

    2021-06-18 10:09

  • DCloud_iOS_XHY

    回复 2***@qq.com: 获取订单失败的话检查一下,Apple 开发者后台商品配置是否正确吧

    2021-06-18 12:15

  • 2***@qq.com

    回复 DCloud_iOS_XHY: 检查了看着都没问题 实在不知道从哪看出啥错

    2021-06-18 13:25

  • AnsonLu

    回复 DCloud_iOS_XHY: 我也是同样的问题,总是订单信息获取失败,税务也配好了,管理里面也添加了内购项目,处于准备提交状态了

    2021-07-09 11:12

  • 张小皮

    返回订单信息失败 怎么解决?

    2021-08-12 11:54

  • zuosonglin

    修改为5+api,还是没有支付回调

    2021-09-26 11:58

  • 白月

    回复 AnsonLu: 我遇到这个问题了,解决办法是将plus.payment.request放再requestOrder的success下面,如果再同一个层级,下面的同时会执行,所以导致还没有获得requestOrder的信息就执行payment.request

    2022-02-14 14:10

  • 8***@qq.com

    回复 DCloud_iOS_XHY: 麻烦问一下,我们是消耗型项目,如果ios支付调起起很慢,本地只存了这个订单号,还未收到ios的任何回调,这时候杀掉了app,,在外面弹起了支付,并且继续付款成功,然后打开app回到页面发现充值并未到账,这里后续需要怎么处理丢单问题呢,restoreComplateRequest 可以拿到这比订单凭证吗?

    2022-05-14 11:45

  • s***@163.com

    回复 DCloud_iOS_XHY: 你好,问下新App测试内购必须提交审核之后才能测试吗?目前是apple后台配置内购商品,状态为待提交,然后打自定义包测试时提示【Payment_appleiap:返回订单信息失败】

    2022-06-20 01:13

  • Planets

    回复 s***@163.com: 你解决了么,目前我也是这个问题

    2022-07-07 14:04

9***@qq.com

9***@qq.com

同上,无法获取支付成功回调,楼主解决了吗

  • 1***@qq.com

    您好 你这边的问题解决了吗

    2021-07-15 19:33

张小皮

张小皮 - 小小皮

返回订单信息失败 怎么解决?

2***@qq.com

2***@qq.com

我也是一样,uni.requestPayment没有任何反应,完全不知道成功或失败回调,也试了上面的plus.payment.request方法,完全没有任何反应啊,实在不知道是哪里出错了

  • 周谦

    请问你解决了吗?我也遇见这个问题

    2022-05-12 16:11

9***@qq.com

9***@qq.com

我支付渠道拿到了,苹果返回的订单信息也拿到。苹果支付无法发起 uni.requestPayment 是什么情况呢

7***@qq.com

7***@qq.com

沙盒测试支付成功了,回调函数调用很慢,杀死App重启,进入支付页面,重新拉起支付,回调函数调用了

  • 白月

    我直接是回调函数没有

    2022-02-14 14:10

  • c***@163.com

    请问解决了吗?同样的问题

    2022-09-01 11:48

  • 7***@qq.com

    回复 c***@163.com: 当时考虑可能是因为是沙盒测试环境才会慢,想着上架之后再看看,没等到这一天,项目死了~~~~

    2022-09-02 11:16

x***@126.com

x***@126.com

uni.requestPayment({
provider: 'appleiap',
orderInfo: {
productid: entity.productid, // 商品id
quantity: 1, // 数量,默认1
username: ${entity.baseId}, // 用户标识
optimize: true,
manualFinishTransaction: false
},
success: (res) => {
console.log('requestPayment------------', res)
resolve(res)
// this.$emit('cancel')
// uni.hideLoading()
uni.showModal({
content: "成功支付!",
showCancel: false
})
},
fail: (err) => {
console.log('fail------------err', err)
// uni.hideLoading()
uni.hideLoading()
reject(err)
},
complete: () => {
console.log("payment结束")
}
});

success
fail
complete

三个回调函数都不执行

原生提示支付成功, 沙盒测试

c***@ztag.vip

c***@ztag.vip

前面所有的流程都没有问题,就是卡在uni.requestPayment 始终没有回调,点击购买输入密码后,苹果那边已经提示购买成功了,但是uni.requestPayment就是没有任何反应,也不走 fail ,也不走complete,如何破?

  • DCloud_iOS_XHY

    如果使用的是沙盒测试,会有这种情况出现,测试一次后需要换一个测试账号在测试,如果还是不行就重启一下手机试试

    2023-01-11 15:38

要回复问题请先登录注册