我是靠谱客的博主 活力冬天,最近开发中收集的这篇文章主要介绍Android支付接入(八):Amazon亚马逊支付,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

      下面跟大家一起走一遍Amazon亚马逊的支付,亚马逊目前刚把业务拓展到大陆市场,但这并不代表Amazon支付不成熟,恰恰相反,Amazon的支付流程,支付结果获取及测试另人称赞,支付流程、测试流程简洁可靠。

      源码链接: http://pan.baidu.com/share/link?shareid=2345870563&uk=473193131
      文档链接: http://pan.baidu.com/share/link?shareid=2085409847&uk=473193131

      好了,废话说多了也没用,看看Amazon支付流程:
   申请Amazon开发者帐号->添加新应用程序->添加应用程序内商品(消费类商品,授权类商品,订阅类商品)->导出JSON数据文件->集成Amazon支付->将导出的JSON文件copy到 /mnt/sdcard/amazon.sdktester.json 目录下->测试支付及修改直到通过测试->上架
   Amazon官方提供的支付通信流程图:

一: 导出JSON数据文件如下:(填写了几个应用内商品导出的JSON文件就包含几个计费点)

二:drawable-hdpi文件夹中最好放置高分辨率的桌面图片(建议512*512),因为Kindlefire设备有个旋转菜单,96*96分辨率在上边太模糊。 android支付接入(八):amazon亚马逊支付

三:做本地测试时必须安装AmazonSDKTester.apk,文件位置在: http://pan.baidu.com/share/link?shareid=2085409847&uk=473193131解压Apps-SDK,如下:

四:本地测试支付成功后,订阅商品可以在 AmazonSDKTester.apk中查看:
android支付接入(八):amazon亚马逊支付 android支付接入(八):amazon亚马逊支付 android支付接入(八):amazon亚马逊支付 android支付接入(八):amazon亚马逊支付

下边来看下代码实现:
      1.Manifest.xml中注册Amazon广播接收器:

 

<receiver android:name ="com.amazon.inapp.purchasing.ResponseReceiver" >
<intent-filter>
<action android:name = "com.amazon.inapp.purchasing.NOTIFY"
android:permission ="com.amazon.inapp.purchasing.Permission.NOTIFY"/>
</intent-filter>
</receiver>

     程序中配置:

 

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
PurchasingManager.registerObserver(new MyObserver(MainActivity.this));
}

      2.添加jar包,以下是路径或者直接从源码中copy

      3.将导出的JSON文件copy到 /mnt/sdcard/amazon.sdktester.json 目录下

private void copyDataBase () throws IOException{
InputStream myInput = getAssets ().open("amazon.sdktester.json");
String outFileName = path;
OutputStream myOutput = new FileOutputStream (outFileName);
byte[]buffer = new byte[1024];
intlength;
while((length = myInput.read (buffer)) > 0){
myOutput.write (buffer, 0, length);
}
myOutput.flush ();
myOutput.close ();
myInput.close ();
}

  4.安装AmazonSDKTester.apk

  5.调用支付接口 

PurchasingManager.initiatePurchaseRequest("qht200catcapcoin");

      6.添加支付回调:

class MyObserver extends BasePurchasingObserver{
public MyObserver(Context arg0){
super(arg0);
// TODO Auto-generatedconstructor stub
}
@Override
public void onItemDataResponse(ItemDataResponsearg0) {
// TODO Auto-generated methodstub
super.onItemDataResponse(arg0);
}
@Override
public void onPurchaseResponse(PurchaseResponsearg0) {
// TODO Auto-generated methodstub
super.onPurchaseResponse(arg0);
switch(arg0.getPurchaseRequestStatus()) {
case SUCCESSFUL:
final Receipt receipt =arg0.getReceipt();
switch (receipt.getItemType()) {
//消耗品
case CONSUMABLE:
newAlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功购买"+productName).setPositiveButton("确定", null).show();
break;
//授权内容,支付成功
case ENTITLED:
newAlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功激活正版游戏").setPositiveButton("确定", null).show();
break;
//订阅内容,支付成功
case SUBSCRIPTION:
break;
}
break;
caseALREADY_ENTITLED:
//已经授权过的,属于成功,此时将道具给玩家即可
break;
case FAILED:
Toast.makeText(MainActivity.this, "支付失败", Toast.LENGTH_LONG).show();
break;
case INVALID_SKU:
Toast.makeText(MainActivity.this, "无法识别的sku", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onGetUserIdResponse(GetUserIdResponsearg0) {
// TODO Auto-generated methodstub
super.onGetUserIdResponse(arg0);
}
@Override
public void onSdkAvailable(boolean arg0) {
// TODO Auto-generated methodstub
super.onSdkAvailable(arg0);
}
}

      MainActivity.java完整代码如下:
package com.example.blogforamazonbilling;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
importcom.amazon.inapp.purchasing.BasePurchasingObserver;
import com.amazon.inapp.purchasing.GetUserIdResponse;
import com.amazon.inapp.purchasing.ItemDataResponse;
import com.amazon.inapp.purchasing.PurchaseResponse;
import com.amazon.inapp.purchasing.PurchasingManager;
import com.amazon.inapp.purchasing.Receipt;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String path ="/mnt/sdcard/amazon.sdktester.json";
private String productName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
copyDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//消耗类商品
findViewById(R.id.button1).setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PurchasingManager.initiatePurchaseRequest("qht200catcapcoin");
productName = "200猫币";
}
});
//消耗类商品
findViewById(R.id.button2).setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PurchasingManager.initiatePurchaseRequest("qht340catcapcoin");
productName = "340猫币";
}
});
//订阅类商品
findViewById(R.id.button3).setOnClickListener(newView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PurchasingManager.initiatePurchaseRequest("qhtactive");
productName = "激活正版游戏";
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
PurchasingManager.registerObserver(newMyObserver(MainActivity.this));
}
private void copyDataBase () throws IOException{
InputStream myInput = getAssets ().open("amazon.sdktester.json");
String outFileName = path;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read (buffer))> 0){
myOutput.write (buffer, 0, length);
}
myOutput.flush ();
myOutput.close ();
myInput.close ();
}
class MyObserver extends BasePurchasingObserver{
public MyObserver(Context arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
@Override
public void onItemDataResponse(ItemDataResponse arg0) {
// TODO Auto-generated method stub
super.onItemDataResponse(arg0);
}
@Override
public void onPurchaseResponse(PurchaseResponse arg0) {
// TODO Auto-generated method stub
super.onPurchaseResponse(arg0);
switch (arg0.getPurchaseRequestStatus()) {
caseSUCCESSFUL:
final Receipt receipt =arg0.getReceipt();
switch(receipt.getItemType()) {
//消耗品
case CONSUMABLE:
newAlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功购
买"+productName).setPositiveButton("确定", null).show();
break;
//授权内容,支付成功
case ENTITLED:
newAlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功激
活正版游戏").setPositiveButton("确定", null).show();
break;
//订阅内容,支付成功
case SUBSCRIPTION:
break;
}
break;
caseALREADY_ENTITLED:
//已经授权过的,属于成功,此时将道具给玩家即可
break;
caseFAILED:
Toast.makeText(MainActivity.this, "支付失败",Toast.LENGTH_LONG).show();
break;
caseINVALID_SKU:
Toast.makeText(MainActivity.this, "无法识别的sku",Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void onGetUserIdResponse(GetUserIdResponse arg0){
// TODO Auto-generated method stub
super.onGetUserIdResponse(arg0);
}
@Override
public void onSdkAvailable(boolean arg0) {
// TODO Auto-generated method stub
super.onSdkAvailable(arg0);
}
}
}

        以上就是Amazon亚马逊支付的接入流程,很简洁。等游戏发布到Amazon后才可以看到正式付费,但正式付费跟测试付费显示效果是一样的。如有任何疑问,请留言。

最后

以上就是活力冬天为你收集整理的Android支付接入(八):Amazon亚马逊支付的全部内容,希望文章能够帮你解决Android支付接入(八):Amazon亚马逊支付所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部