You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cordova Purchase插件Android端finish()事件无法触发求助

Android端Cordova Purchase插件finished事件未触发问题排查与修复

问题描述

使用j3k0开发的最新版Cordova Purchase插件,仅在Android端出现finished()事件从未触发的异常:

  • 支付弹窗可正常唤起,支付操作完成后返回“Ok”,确认购买已成功
  • approved()回调正常执行,但finished()事件始终未触发

相关代码

this.CdvPurchase.store
            .when()
            .productUpdated(product => {
                this.logObjet('when productUpdated:product', product);
            })
            .approved(transaction => {
                this.logObjet('when approved:transaction', transaction);

         
                transaction.finish();
                const produitVerification = this.CdvPurchase.store.get(this.valeurEnvoi.app, plateformeStore);
                const transactionVerification = this.CdvPurchase.store.findInLocalReceipts(produitVerification);
                this.logObjet('when approved:produitVerification', produitVerification);
                this.logObjet('when approved:transactionVerification', transactionVerification);

                // transaction.verify();
            })
            .verified(receipt => {
                 this.logObjet('when verified:receipt', receipt);
                 receipt.finish();
                 // this.enregistrerPaiementMobile(informationsTransaction);
             }).finished(receipt => {
                this.logObjet('when finished:receipt', receipt);
                const produitVerification = this.CdvPurchase.store.get(this.valeurEnvoi.app, plateformeStore);
                const transactionVerification = this.CdvPurchase.store.findInLocalReceipts(produitVerification);
                this.logObjet('when finished:produitVerification', produitVerification);
                this.logObjet('when finished:transactionVerification', transactionVerification);
                // this.enregistrerPaiementMobile(informationsTransaction);
            });

        this.CdvPurchase.store.register([
            {
                id: this.valeurEnvoi.app,
                platform: plateformeStore,
                type: typeProduit,
            },
        ]);

        this.CdvPurchase.store.ready(() => {
            const produit = this.CdvPurchase.store.get(this.valeurEnvoi.app, plateformeStore);

            this.logObjet('when ready:store.get:produit', produit, 'offers');

            if (produit.canPurchase) {
                this.CdvPurchase.store.order(produit.getOffer());
            }

        });

        this.CdvPurchase.store.initialize([
            plateformeStore,
        ]);

排查与修复方案

1. 调整事务处理流程(核心修复)

Android平台要求购买事务需先验证收据,再完成事务。你当前在approved回调中直接调用transaction.finish(),跳过了验证步骤,可能导致finished事件不触发。建议恢复transaction.verify()调用,在verified回调中执行receipt.finish():

.approved(transaction => {
    this.logObjet('when approved:transaction', transaction);
    // 先触发收据验证,而非直接finish
    transaction.verify();
})
.verified(receipt => {
     this.logObjet('when verified:receipt', receipt);
     // 验证通过后再完成事务,触发finished事件
     receipt.finish();
 })

2. 开启调试日志排查细节

开启插件调试模式,查看Android Logcat中PurchasePlugin相关日志,确认是否有事务处理异常:

// 在initialize前添加调试日志配置
this.CdvPurchase.store.verbosity = this.CdvPurchase.store.DEBUG;

3. 检查插件版本与配置

  • 确认使用的是最新稳定版插件,若为开发版可重新安装稳定版:
    cordova plugin remove cordova-plugin-purchase
    cordova plugin add cordova-plugin-purchase
    
  • 检查AndroidManifest.xml是否包含必要权限与服务(插件通常自动配置,可手动确认):
    <uses-permission android:name="com.android.vending.BILLING" />
    <service android:name="com.j3k0.cordova.purchase.BillingService" />
    

4. 确认事件监听顺序

当前代码中store.when()事件监听在register和initialize之前,顺序正确。若存在其他地方覆盖事件监听,需排查冲突。

内容的提问来源于stack exchange,提问作者n.Dr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 20:55:57