安卓接入Google支付

目录

1.注册 Google Play 开发者帐号

2.在 Google Play 管理中心启用结算相关功能

3.创建和配置产品

4.代码

可能遇到的问题


1.注册 Google Play 开发者帐号

        注册需要准备Gmail邮箱、手机号、Visa卡(谷歌开发者账号注册需要支付25美元)。注册地址,请科学上网

2.在 Google Play 管理中心启用结算相关功能

        设置开发者帐户后,必须发布包含 Google Play 结算库的应用版本。

        在应用程序 build.gradle文件添加依赖项

dependencies {
    ...
    ...
    implementation "com.android.billingclient:billing:4.0.0"
    ...
    ...
}

        在AndroidManifest.xml中添加权限。

<uses-permission android:name="com.android.vending.BILLING" />

3.创建和配置产品

        后台创建商品的话需要先在后台上传一个带有支付功能的草稿包,确保包名、签名、支付库就可构建APK上传。

        对于每个产品,您需要提供唯一的产品 ID、标题、描述和定价信息。后台的商品分为一次性消耗和订阅2中类型。

         注意产品ID一旦创建之后就不要删除,删除之后就无法再次创建相同的产品ID

4.代码

        以下代码是一个完整的Google支付类。代码是基于4.0.0版本库。

大致流程:

        初始化一个BillingClient实例

        链接GooglePlay

        获取可购买的产品列表

        购买产品

        消耗购买的产品

package com.game.billing;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

// Google pay
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;

import java.util.ArrayList;
import java.util.List;

public class BillingWrapper implements BillingClientStateListener, SkuDetailsResponseListener, PurchasesUpdatedListener, ConsumeResponseListener {

    private static final String LOG_TAG = "BillingWrapper";

    private static final int STATE_NULL     = 0;
    private static final int STATE_QUERY    = 1;
    private static final int STATE_BUY      = 2;

    private int state = STATE_NULL;

    private BillingClient billingClient;
    private boolean isBillingClientConnection;
    private List<String> skuList = new ArrayList<>();

    private List<SkuDetails> skuDetailsList = null;
    private String toPurchaseSkuId = "";

    private Activity mActivity = null;

    private static BillingWrapper _instance = null;
    public static BillingWrapper getInstance() {
        if (_instance == null) {
            _instance = new BillingWrapper();
        }
        return  _instance;
    }

    private void log(String msg) {
        Log.d(LOG_TAG, msg);
    }

    public void InitBilling(@NonNull Activity activity) {
        mActivity = activity;
        billingClient = BillingClient.newBuilder(activity)
                .setListener(this)
                .enablePendingPurchases()
                .build();

        state = STATE_NULL;
        startConnection();
    }

    ///START链接///
    public void startConnection() {
        billingClient.startConnection(this);
    }
    @Override
    public void onBillingSetupFinished(BillingResult billingResult) {
        if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
            // The BillingClient is ready. You can query purchases here.
            isBillingClientConnection = true;
            log("The BillingClient is ready.");
            if ( state == STATE_QUERY ) {
                querySkuDetailsAsync();
            } else if ( state == STATE_BUY ) {
                launchBillingFlow();
            } else {
            }
            state = STATE_NULL;
        } else {
            isBillingClientConnection = false;
            log(billingResult.getDebugMessage() + " responseCode = " + billingResult.getResponseCode());
        }
    }
    @Override
    public void onBillingServiceDisconnected() {
        // Try to restart the connection on the next request to
        // Google Play by calling the startConnection() method.
        isBillingClientConnection = false;
    }
    ///END链接///

    ///START查询///
    public void getProductList(String keys) {
        skuList.clear();
        for (String k : keys.split("_")) {
            skuList.add(k);
        }
        if (!isBillingClientConnection) {
            state = STATE_QUERY;
            startConnection();
        } else {
            querySkuDetailsAsync();
        }
    }

    private void querySkuDetailsAsync() {
        billingClient.querySkuDetailsAsync(SkuDetailsParams.newBuilder()
                .setSkusList(skuList)
                .setType(BillingClient.SkuType.INAPP)
                .build(), this);
    }

    @Override
    public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {
        int responseCode = billingResult.getResponseCode();
        if (responseCode == BillingClient.BillingResponseCode.OK) {
            skuDetailsList = list;
            log("Google billingClient.querySkuDetailsAsync succeed.");
        } else {
            log("Google billingClient.querySkuDetailsAsync failed. responseCode = " + responseCode);
        }
    }
    ///END查询///

    ///START支付///
    public void buyStoreItem(String skuId) {
        toPurchaseSkuId = skuId;
        if (!isBillingClientConnection) {
            state = STATE_BUY;
            startConnection();
        } else {
            launchBillingFlow();
        }
        log("billingClient.buyStoreItem");
    }

    private void launchBillingFlow() {
        if (toPurchaseSkuId.isEmpty()) {
            return;
        }

        SkuDetails skuDetails = null;
        if (skuDetailsList != null) {
            for (SkuDetails sku : skuDetailsList) {
                if (sku.getSku().equals(toPurchaseSkuId)) {
                    skuDetails = sku;
                    break;
                }
            }
        }
        if (skuDetails == null) {
            return;
        }

        BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                .setSkuDetails(skuDetails)
                .build();

        log("try buy: " + skuDetails.toString());
        // Launch the billing flow
        BillingResult billingResult = billingClient.launchBillingFlow(mActivity, billingFlowParams);
        int responseCode = billingResult.getResponseCode();
        if (responseCode == BillingClient.BillingResponseCode.OK) {
            log("launchBillingFlow successful.");
        } else {
            log("launchBillingFlow failed. " + billingResult.getDebugMessage());
        }
    }
    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> purchases) {
        // To be implemented in a later section.
        int responseCode = billingResult.getResponseCode();
        if (responseCode == BillingClient.BillingResponseCode.OK) {
            log("onPurchasesUpdated successful.");
            if (!purchases.isEmpty()) {
                Purchase one = purchases.get(0);
                consumeAsync(one);
            }
        } else {
            log("onPurchasesUpdated failed. " + billingResult.getDebugMessage());
        }
    }
    ///END支付///

    ///START消耗///
    private void consumeAsync(Purchase purchase) {
        ConsumeParams consumeParams = ConsumeParams.newBuilder()
                .setPurchaseToken(purchase.getPurchaseToken())
                .build();
        billingClient.consumeAsync(consumeParams, this);
    }

    @Override
    public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
        int responseCode = billingResult.getResponseCode();
        if (responseCode == BillingClient.BillingResponseCode.OK) {
            log("onConsumeResponse successful. ");
        } else {
            log("onConsumeResponse failed. responseCode = " + responseCode + " " + billingResult.getDebugMessage());
        }
    }
    ///END消耗///
}

可能遇到的问题

1.startConnection时返回错误 Google Play In-app Billing API version is less than 3.

        确保自己的手机VPN是正确的,再Google play中查看所在国家地区是否正确。

2.支付时提示"无法购买您要买的商品"

        确保安装版本与后台的版本一致,包含AppVersion,bundleVersion,签名。确保支付的账号在测试人员列表中。同时需要测试人员接受的测试邀请

您好!要在安卓应用中接入微信支付,您可以按照以下步骤进行操作: 1. 在微信开放平台上注册并创建一个应用,获取到对应的AppID和AppSecret。 2. 在安卓应用的项目中,添加微信支付的SDK文件,可以通过在项目的build.gradle文件中添加相应的依赖来引入。 3. 在安卓应用的manifest文件中,添加以下权限和activity声明: ```xml <uses-permission android:name="android.permission.INTERNET" /> <activity android:name="com.tencent.mm.opensdk.openapi.WXPayEntryActivity" android:exported="true" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> ``` 4. 在安卓应用的代码中,使用微信支付SDK提供的API进行支付操作。具体步骤如下: - 在您的支付页面或订单确认页面,用户点击支付按钮后,调用以下代码创建一个支付请求: ```java PayReq req = new PayReq(); req.appId = "您的AppID"; req.partnerId = "商户号"; req.prepayId = "预支付交易会话ID"; req.packageValue = "扩展字段(暂填写固定值:Sign=WXPay)"; req.nonceStr = "随机字符串"; req.timeStamp = "时间戳"; req.sign = "签名"; // 发送支付请求 IWXAPI api = WXAPIFactory.createWXAPI(context, req.appId); api.registerApp(req.appId); api.sendReq(req); ``` - 在安卓应用的代码中,实现一个WXPayEntryActivity来接收支付结果的回调。在该Activity的`onCreate`方法中,调用以下代码处理支付结果: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wxpay_entry); // 处理支付结果 handlePayResult(getIntent()); } private void handlePayResult(Intent intent) { // 获取支付结果信息 PayResp resp = new PayResp(); resp.fromIntent(intent); // 根据支付结果进行相应处理 if (resp.errCode == BaseResp.ErrCode.ERR_OK) { // 支付成功 // TODO: 进行支付成功后的逻辑处理 } else { // 支付失败 // TODO: 进行支付失败后的逻辑处理 } finish(); } ``` 以上是接入微信支付的基本步骤,您需要根据具体的业务需求和微信支付提供的文档进行相应的配置和开发。希望对您有所帮助!如果还有其他问题,请随时提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coding·life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值