Android支付接入(八):Amazon亚马逊支付

时间:2020-12-23 05:02:02
下面跟大家一起走一遍Amazon亚马逊的支付,亚马逊目前刚把业务拓展到大陆市场,但这并不代表Amazon支付不成熟,恰恰相反,Amazon的支付流程,支付结果获取及测试另人称赞,支付流程、测试流程简洁可靠。不想国内三个运营商,典型的国企,支付成功与否的判断标准仅仅是看短信是否发送出去,连个二次握手都没有,更没有安全可靠之说。在此并无他意,只是在对比运营商支付跟其他支付后的感触。但不可否认的是运营商支付在大陆移动付费端堪称龙头,尽管它并不安全,这就是现状。。。

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

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

三:做本地测试时必须安装AmazonSDKTester.apk,文件位置在:http://pan.baidu.com/share/link?shareid=2085409847&uk=473193131解压Apps-SDK,如下:
Android支付接入(八):Amazon亚马逊支付
四:本地测试支付成功后,订阅商品可以在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

Android支付接入(八):Amazon亚马逊支付

     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];

int length;

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-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()) {

case SUCCESSFUL:

final Receipt receipt = arg0.getReceipt();

switch (receipt.getItemType()) {

//消耗品

case CONSUMABLE:

new AlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功购                                      买"+productName).setPositiveButton("确定", null).show();

break;

//授权内容,支付成功

case ENTITLED:

new AlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功激活正                                 版游戏").setPositiveButton("确定", null).show();

break;

//订阅内容,支付成功

case SUBSCRIPTION:

break;

}

break;

case ALREADY_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(GetUserIdResponse arg0) {

// TODO Auto-generated method stub

super.onGetUserIdResponse(arg0);

}

@Override

public void onSdkAvailable(boolean arg0) {

// TODO Auto-generated method stub

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;
import com.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(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PurchasingManager.initiatePurchaseRequest("qht200catcapcoin");
productName = "200猫币";
}
});
//消耗类商品
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PurchasingManager.initiatePurchaseRequest("qht340catcapcoin");
productName = "340猫币";
}
});
//订阅类商品
findViewById(R.id.button3).setOnClickListener(new View.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(new MyObserver(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()) {
            case SUCCESSFUL:
               
                final Receipt receipt = arg0.getReceipt();
                switch (receipt.getItemType()) {
                //消耗品
                case CONSUMABLE:
                new AlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功购                        买"+productName).setPositiveButton("确定", null).show();
                    break;
                //授权内容,支付成功
                case ENTITLED:
                new AlertDialog.Builder(MainActivity.this).setTitle("支付成功").setMessage("成功激                        活正版游戏").setPositiveButton("确定", null).show();
                    break;
                //订阅内容,支付成功
                case SUBSCRIPTION:
               
                    break;
                }
                
                break;
            case ALREADY_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(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后才可以看到正式付费,但正式付费跟测试付费显示效果是一样的。如有任何疑问,请留言。