流程:
申请 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>
<action
android:name="com.amazon.inapp.purchasing.NOTIFY" />
</intent-filter>
</receiver>
步骤三:实现并注册PurchasingListener (onCreate 生命周期方法中调用)
purchasingListener = new PurchasingListener() {
/**
* 确定当前登录用户的UserId和marketplace。
* @param response
*/
@Override
public 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
*/
@Override
public 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
*/
@Override
public 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.getProductData
for (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
*/
@Override
public 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 设置为沙盒模式
//检查应用是否处于测试模式文章来源:https://www.toymoban.com/news/detail-676043.html
Log.d(TAG, "Appstore SDK Mode: " + LicensingService.getAppstoreSDKMode());
以上就是 Amazon 支付的接入流程,发布到 Amazon 后才可以正式付费,但正式付费跟测试付费显示效果是一样的.文章来源地址https://www.toymoban.com/news/detail-676043.html
到了这里,关于android amazon 支付接入的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!