概述
一:
导出JSON数据文件如下:(填写了几个应用内商品导出的JSON文件就包含几个计费点)
三:做本地测试时必须安装AmazonSDKTester.apk,文件位置在:
http://pan.baidu.com/share/link?shareid=2085409847&uk=473193131解压Apps-SDK,如下:
四:本地测试支付成功后,订阅商品可以在 AmazonSDKTester.apk中查看:
下边来看下代码实现:
<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));
}
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 ();
}
PurchasingManager.initiatePurchaseRequest("qht200catcapcoin");
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);
}
}
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);
}
}
}
最后
以上就是活力冬天为你收集整理的Android支付接入(八):Amazon亚马逊支付的全部内容,希望文章能够帮你解决Android支付接入(八):Amazon亚马逊支付所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复