首页 > 解决方案 > DOMException:支付应用程序返回了 RESULT_CANCELED 代码。这就是支付应用程序以编程方式关闭其活动的方式

问题描述

问题

我正在尝试使用此Google Tutorial在 TWA 上实施 Google Play Billing ,但是当我调用const paymentResponse = await request.show();时会发生以下错误:

DOMException: Payment app returned RESULT_CANCELED code. This is how payment apps can close their activity programmatically.



代码

这是我从 JS 端在 TWA 应用程序上运行的代码:

const service =
     await window.getDigitalGoodsService('https://play.google.com/billing');

const skuDetails = await service.getDetails(['gem']);

let paymentMethods = [{
       supportedMethods: "https://play.google.com/billing",
       data: {
           sku: skuDetails[0],
       }
   }];
let paymentDetails = {
    total: {
        label: `Total`,
        amount: {currency: `USD`, value: `10`}
    }
};

const request = new PaymentRequest(paymentMethods, paymentDetails);

const paymentResponse = await request.show();

我没有使用Bubblewrap,而是使用 Android Studio 来完全控制应用程序。

这是我的安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mwar.maliciouswar">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MaliciousWar">

        <meta-data
            android:name="asset_statements"
            android:resource="@string/asset_statements" />

        <activity
            android:name="com.google.androidbrowserhelper.trusted.LauncherActivity">

            <!-- Edit android:value to change the url opened by the Trusted Web Activity -->
            <meta-data
                android:name="android.support.customtabs.trusted.DEFAULT_URL"
                android:value="https://mal-war.com?utm_source=twa" />

            <meta-data
                android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR"
                android:resource="@color/brown"/>

            <meta-data
                android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR_DARK"
                android:resource="@color/brown" />

            <meta-data
                android:name="android.support.customtabs.trusted.NAVIGATION_BAR_COLOR"
                android:resource="@color/brown" />

            <meta-data
                android:name="android.support.customtabs.trusted.NAVIGATION_BAR_COLOR_DARK"
                android:resource="@color/brown" />

            <meta-data android:name="android.support.customtabs.trusted.DISPLAY_MODE"
                android:value="immersive"/>

            <meta-data android:name="android.support.customtabs.trusted.SCREEN_ORIENTATION"
                android:value="landscape"/>

<!--            <meta-data android:name="android.support.customtabs.trusted.SPLASH_IMAGE_DRAWABLE"-->
<!--                android:resource="@drawable/splash"/>-->

<!--            <meta-data android:name="android.support.customtabs.trusted.SPLASH_SCREEN_BACKGROUND_COLOR"-->
<!--                android:resource="@color/backgroundColor"/>-->

<!--            <meta-data android:name="android.support.customtabs.trusted.SPLASH_SCREEN_FADE_OUT_DURATION"-->
<!--                android:value="300"/>-->

<!--            <meta-data android:name="android.support.customtabs.trusted.FILE_PROVIDER_AUTHORITY"-->
<!--                android:value="@string/provider_authority"/>-->

            <!-- This intent-filter adds the Trusted Web Activity to the Android Launcher -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!--
              This intent-filter allows the Trusted Web Activity to handle Intents to open
              airhorner.com.
            -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE"/>

                <!-- Edit android:host to handle links to the target URL-->
                <data
                    android:scheme="https"
                    android:host="mal-war.com"/>
            </intent-filter>
        </activity>

        <activity android:name="com.google.androidbrowserhelper.trusted.FocusActivity" />


<!--        <provider-->
<!--            android:name="androidx.core.content.FileProvider"-->
<!--            android:authorities="com.example.twa.myapplication.fileprovider"-->
<!--            android:grantUriPermissions="true"-->
<!--            android:exported="false">-->
<!--            <meta-data-->
<!--                android:name="android.support.FILE_PROVIDER_PATHS"-->
<!--                android:resource="@xml/filepaths" />-->
<!--        </provider>-->

        <service
            android:name=".ExtraFeaturesService"
            android:exported="true">

            <intent-filter>
                <action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

        <activity
            android:name="com.google.androidbrowserhelper.playbilling.provider.PaymentActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|keyboard|orientation|screenLayout|screenSize"
            android:exported="true">
            <intent-filter>
                <action android:name="org.chromium.intent.action.PAY" />
            </intent-filter>
            <meta-data
                android:name="org.chromium.default_payment_method_name"
                android:value="https://play.google.com/billing" />
        </activity>
        <!-- This service checks who calls it at runtime. -->
        <service
            android:name="com.google.androidbrowserhelper.playbilling.provider.PaymentService"
            android:exported="true" >
            <intent-filter>
                <action android:name="org.chromium.intent.action.IS_READY_TO_PAY" />
            </intent-filter>
        </service>

    </application>
</manifest>

和我的 build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.mwar.maliciouswar"
        minSdkVersion 23 // android 6.0
        targetSdkVersion 30
        versionCode 9
        versionName "1.06"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.2'
//    implementation "com.android.billingclient:billing:4.0.0"
    implementation 'com.google.androidbrowserhelper:billing:1.0.0-alpha08'

}

标签: androidprogressive-web-appsandroid-billingplay-billing-librarytrusted-web-activity

解决方案


推荐阅读