当前位置: 首页 > news >正文

android amazon 支付接入

流程:

申请 Amazon 开发者帐号 ---> 在 amazon 控制台添加应用 ---> 添加应用内商品(消费类商品,授权类商品,订阅类商品)---> 导出 JSON 文件 --->集成 Amazon 支付 ---> 将导出的 JSON 文件 copy 到 /mnt/sdcard/目录下---> 沙河模式下测试支付 ---> 上传发布

注意:亚马逊目前使用 V1 签名;2.targetSdkVersion 版本必须小于等于29

SDK运行模式一共三种

①:SANDBOX 沙河模式,用于测试,与 Amazon App Tester 测试工具交互

②:PRODUCTION 提交到亚马逊应用商店才可测试

③:UNKNOWN 尚未通过调用verifyLicense()初始化SDK。
 

步骤一:集成亚马逊内购SDK

    implementation 'com.amazon.device:amazon-appstore-sdk:3.0.4'

步骤二:在 AndroidManifest.xml 文件中添加 ResponseReceiver 配置,如果应用 targetSdkVersion 设置的是 31 或更高版本,必须显式地将 android:exported 设置 ResponseReceiver 中android:exported 设置为true

<receiver android:name="com.amazon.device.iap.ResponseReceiver" android:exported="true"android:permission="com.amazon.inapp.purchasing.Permission.NOTIFY" ><intent-filter><actionandroid:name="com.amazon.inapp.purchasing.NOTIFY" /></intent-filter></receiver>

步骤三:实现并注册PurchasingListener (onCreate 生命周期方法中调用)

 purchasingListener = new PurchasingListener() {/*** 确定当前登录用户的UserId和marketplace。* @param response*/@Overridepublic void onUserDataResponse(final UserDataResponse response) {Log.d(LOG_TAG, "onUserDataResponse: requestId (" + response.getRequestId()+ ") userIdRequestStatus: "+ response.getRequestStatus()+ ")");//当前用户相关信息(用户ID和市场final UserDataResponse.RequestStatus status = response.getRequestStatus();switch (status) {case SUCCESSFUL:Log.d(LOG_TAG, "onUserDataResponse: get user id " + response.getUserData().getUserId()+ ", marketplace "+ response.getUserData().getMarketplace());currentUserId = response.getUserData().getUserId();currentMarketplace = response.getUserData().getMarketplace();break;case FAILED:Log.d(LOG_TAG, "onUserDataResponse FAILED failed, status code is " + status);case NOT_SUPPORTED:Log.d(LOG_TAG, "onUserDataResponse NOT_SUPPORTED failed, status code is " + status);break;}}/*** 检索应用中销售的SKU的信息* 使用来自ProductDataResponse对象的有效SKU。** @param response*/@Overridepublic void onProductDataResponse(final ProductDataResponse response) {final ProductDataResponse.RequestStatus status = response.getRequestStatus();Log.d(LOG_TAG, "onProductDataResponse: RequestStatus (" + status + ")");switch (status) {case SUCCESSFUL:Log.d(LOG_TAG, "onProductDataResponse: successful.  The item data map in this response includes the valid SKUs");final Set<String> unavailableSkus = response.getUnavailableSkus();for (final String s : response.getUnavailableSkus()) {Log.v(LOG_TAG, unavailableSkus.size() + " 不可用SKU:" + s);}final Map<String, Product> products = response.getProductData();for (final String key : products.keySet()) {Product product = products.get(key);Log.v(LOG_TAG, String.format("可购买的产品:%s\n 类型:%s\n SKU:%s\n 价格:%s\n 描述:%s\n", product.getTitle(), product.getProductType(), product.getSku(), product.getPrice(), product.getDescription()));// 处理产品}break;case FAILED:case NOT_SUPPORTED:Log.d(LOG_TAG, "onProductDataResponse: failed, should retry request");break;}}/*** 检索自上次调用该方法之后用户完成的所有购买交易* 仅可检索未履行和已取消的消费品购买* @param response*/@Overridepublic void onPurchaseUpdatesResponse(final PurchaseUpdatesResponse response) {Log.d(LOG_TAG, "PurchaseUpdatesResponse: requestId (" + response.getRequestId()+ ") Status ("+ response.getRequestStatus()+ ") userId ("+ response.getUserData().getUserId()+ ")");//获取请求状态final PurchaseUpdatesResponse.RequestStatus status = response.getRequestStatus();switch (status) {case SUCCESSFUL:Log.e(LOG_TAG, "========no consumeProduct==========" + response.getReceipts());//PurchasingService.getProductDatafor (final Receipt receipt : response.getReceipts()) {LogD("购买收据 + receipt " + receipt.isCanceled() + "\n" + "收据:" + receipt.getReceiptId());if (!receipt.isCanceled()) {String localReceipt = SharedPreferencesUtil.getString(mContext, receipt.getReceiptId(), "");Gson gson = new Gson();Type type = new TypeToken<Hashtable<String, String>>(){}.getType();Hashtable<String, String> localPurchase= gson.fromJson(localReceipt, type);if(null!=localPurchase){myProductInfo = localPurchase;}receiptID = receipt.getReceiptId();Log.e(LOG_TAG, "----------myProductInfo------------: " + myProductInfo);consumeProduct(receipt,"2");}}//                        if (response.hasMore()) {
//                            //如果不启用“待定购买”,则在每次getPurchaseUpdates(false)调用中只返回未履行的消费品收据
//                            PurchasingService.getPurchaseUpdates(false);
//                            return;
//                        }break;case NOT_SUPPORTED:Log.d(LOG_TAG, "onProductDataResponse: failed, should retry request");break;}}/*** purchase()用户拉起支付后,用于确定购买状态。** @param response*/@Overridepublic void onPurchaseResponse(final PurchaseResponse response) {final String requestId = response.getRequestId().toString();final String userId = response.getUserData().getUserId();final PurchaseResponse.RequestStatus status = response.getRequestStatus();Log.d(LOG_TAG, "支付成功回调 onPurchaseResponse: requestId (" + requestId+ ") userId ("+ userId+ ") purchaseRequestStatus ("+ status+ ")");switch (status) {case SUCCESSFUL://交易成功final Receipt receipt = response.getReceipt();Log.d(LOG_TAG, "onPurchaseResponse: receipt json:" + receipt.toJSON());receiptID = receipt.getReceiptId();Log.d(LOG_TAG, "onPurchaseResponse: receipt receiptID:" + receipt.getReceiptId());//请求服务端跟amazon校验//校验成功去消耗PurchasingService.notifyFulfillment(receiptID, FulfillmentResult.FULFILLED);break;
//                    case PENDING:
//                        Log.d(LOG_TAG, "onPurchaseResponse: 等待远程批准——如果获得批准,购买将在 getPurchaseUpdates 中返回");
//                        break;case ALREADY_PURCHASED://已经授权过的,属于成功,此时将道具给玩家即可Log.d(LOG_TAG, "onPurchaseResponse: 已经授权过的,属于成功,此时将道具给玩家即可");break;case INVALID_SKU:Log.d(LOG_TAG, "onPurchaseResponse: invalid SKU!无效SKU,无法识别");break;case FAILED:case NOT_SUPPORTED:Log.e(LOG_TAG, "支付失败 用户在完成之前取消了购买");break;}}};

步骤四:注册监听(onCreate 生命周期方法中调用)

PurchasingService.registerListener(mContext, purchasingListener);

步骤五:通过 getUserData() 当前用户相关(用户ID和市场),我这边是放在了onCreate 生命周期方法中 

PurchasingService.getUserData();(onCreate 生命周期方法中调用)

步骤六:检索自上次调用该方法之后用户完成的所有购买交易,以确保获取最新的更新

PurchasingService.getPurchaseUpdates(true);(onCreate 生命周期方法中调用)
  • false - 返回自上次调用getPurchaseUpdates()之后的购买记录的分页响应。检索用户的未履行消费品、权利和订阅购买的收据。亚马逊应用商店建议在大部分情况下使用此方法。

     注意: 如果您的应用启用了待定购买,则亚马逊应用商店会在每次getPurchaseUpdates(false)调用中为用户返回所有未履行的消费品和权利收据,直到您的应用调用notifyFulfillment()为止。如果不启用“待定购买”,则在每次getPurchaseUpdates(false)调用中只返回未履行的消费品收据。

  • true - 检索用户的完整购买记录。需要将数据存储在某个位置(例如服务器端数据缓存),或将所有数据保留在内存中。

步骤七:验证SKU,购买中不会因 SKU 无效而意外失败。

 Set<String> productSkus = new HashSet<>();productSkus.add("skus1");productSkus.add("skus12");productSkus.add("skus3");....PurchasingService.getProductData(productSkus);// 触发PurchasingListener.onProductDataResponse()

步骤八:拉起支付弹框支付

            final RequestId requestId =  PurchasingService.purchase("Product_Id");

注意:在消耗商品前,先完成后端服务器通过亚马逊的Receipt Verification Service (RVS)验证receiptId,从而验证购买所产生的收据。验证后再去消耗。

测试流程

1:从亚马逊应用商店将Amazon App Tester下载到手机上。App Tester模拟生产环境,下载链接

2:下载后台的amazon.sdktester.json 文件复制到手机的/sdcard/文件夹中,在Amazon App Tester应用中找到 IAP items in JSON File,看下是否有数据

       adb push F:/download/amazon.sdktester.json /mnt/sdcard/

3:adb shell setprop debug.amazon.sandboxmode debug 设置为沙盒模式

//检查应用是否处于测试模式

  Log.d(TAG, "Appstore SDK Mode: " + LicensingService.getAppstoreSDKMode()); 

  以上就是 Amazon 支付的接入流程,发布到 Amazon 后才可以正式付费,但正式付费跟测试付费显示效果是一样的.

http://www.lryc.cn/news/142359.html

相关文章:

  • Vue2-快速搭建pc端后台管理系统
  • 【产品文档】团队介绍PPT模板
  • 组件库的使用和自定义组件
  • 网站和API支持HTTPS,最好在Nginx上配置
  • UnitTest笔记: 拓展库DDT的使用
  • 裂缝检测,只依赖OPENCV,基于YOLO8S
  • python编程环境使用技巧3-程序打包pyinstaller
  • Go 自学:defer关键字
  • 【云计算】Docker特别版——前端一篇学会
  • .net使用RabbitMQ小记
  • layUI 中 穿梭框无法获取值的细节问题
  • Kaggle回归问题Mercedes——Benz Greener Manufacturing
  • 天润融通「微藤大语言模型平台2.0」以知识驱动企业高速增长
  • 【BUG】解决安装oracle11g或12C中无法访问临时位置的问题
  • 2. 使用IDEA创建Spring Boot Hello项目并管理依赖——Maven入门指南
  • Python在电路课程中的应用
  • Spark SQL join的三种实现方式
  • wazuh环境配置和漏洞复现
  • 九五从零开始的运维之路(其三十六)
  • 同步和异步有什么区别,使用场景?
  • webassembly009 transformers.js 网页端侧推理
  • Android动态添加和删除控件/布局
  • maven下载不了仓库地址为https的依赖jar,配置参数忽略ssl安全检查
  • 3.Redis 单线程模型
  • 0基础学习VR全景平台篇 第90篇:智慧眼-数据统计
  • 【Go】Goland项目配置运行教程
  • Docker容器与虚拟化技术:Docker consul 实现服务注册与发现
  • 【大模型AIGC系列课程 2-2】大语言模型的“第二大脑”
  • Java基础数据结构
  • PP-TS基于启发式搜索和集成方法的时序预测模型,使预测更加准确