Activity.java 부분
googleBilling mGoogle = new googleBilling(this, this);
mGoogle.setOnGoogleListener(this);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (mGoogle.onActivityResult(requestCode, resultCode, data))
super.onActivityResult(requestCode, resultCode, data);
}
public class googleBilling {
IabHelper mHelper;
String payID;
String base64EncodedPublicKey;
int ActivityResultCode = 2799;
String LogTag = "billing";
Context mContext;
Activity mAct;
onGoogleBillingListener mListener;
public interface onGoogleBillingListener {
public void startLoading();
public void endLoading();
public void sendMessage(String Message);
public void complete(String ItemType, String jsonPurchaseInfo, String signature);
}
public googleBilling(Context context, Activity act) {
mContext = context;
mAct = act;
payID = mContext.getResources().getString(R.string.payID);
base64EncodedPublicKey = mContext.getResources().getString(R.string.base64EncodedPublicKey);
mHelper = new IabHelper(mContext, base64EncodedPublicKey);
mHelper.enableDebugLogging(true); // 디버깅 모드시 true
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// 초기화 실패 하면 여기로 들오 옴
Log.d(LogTag, "초기화 실패 " + result.getMessage());
if (mListener != null)
mListener.sendMessage("구글 결재 초기화를 실패 했습니다.\n종료합니다.");
mAct.finish();
return;
}
Log.d(LogTag, "구매 정보를 설정하고 받아 옵니다");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
}
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
// TODO
if (result.isFailure()) {
Log.d(LogTag, "구매 한적 있는지 확인 실패" + result.getMessage());
return; // handle error
}
Purchase checkPurchase = inv.getPurchase(payID);
if (checkPurchase == null)
Log.d(LogTag, "구매 한적이 없습니다.");
else
Log.d(LogTag, "구매 한적이 있습니다.");
// 구매한 적이있으면 일단은 구매 정보를 지운다 어짜피 서버에 기록되어있으니깐
if (checkPurchase != null && verifyDeveloperPayload(checkPurchase)) {
Log.d(LogTag, "We have gas. Consuming it.");
mHelper.consumeAsync(inv.getPurchase(payID), mConsumeFinishedListener);
return;
}
}
};
public void BuyItemClick() {
Log.d(LogTag, "아이템 구매 클릭 시작");
Log.d(LogTag, "mListener " + mListener);
if (mListener != null)
mListener.startLoading();
mHelper.launchPurchaseFlow(mAct, payID, ActivityResultCode, mPurchaseFinishedListener);
}
// 아이템 구입이 되면 진행되는 콜백
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {
if (result.isFailure()) {
if (mListener != null)
mListener.sendMessage("구입에 실패 했습니다.");
return;
}
mHelper.consumeAsync(info, mConsumeFinishedListener);
Log.d(LogTag, "구입 성공.");
// if (info.getSku().equals(payID)) {
//
// } else {
// Log.d(LogTag, "구입 성공 했으나 틀림 " + info.getSku() + " " + payID);
// paymentDataInit();
// }
}
};
// 아이템 과금이 완료 되었을때 호출 되는 부분 입니다.
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
@Override
public void onConsumeFinished(Purchase purchase, IabResult result) {
paymentDataInit();
// TODO
if (result.isSuccess()) {
// 성공
if (mListener != null)
mListener.complete(purchase.getItemType(), purchase.getOriginalJson(), purchase.getSignature());
Log.d(LogTag, "결제 과금에 성공 " + mListener);
Log.d(LogTag, "OrderID : " + purchase.getOrderId());
Log.d(LogTag, "PackageName : " + purchase.getPackageName());
Log.d(LogTag, "PurchaseTime :" + purchase.getPurchaseTime());
Log.d(LogTag, "PurchaseState : " + purchase.getPurchaseState());
Log.d(LogTag, "developerPayload : " + purchase.getDeveloperPayload());
Log.d(LogTag, "PurchaseToken : " + purchase.getToken());
Log.d(LogTag, "ItemType : " + purchase.getItemType());
Log.d(LogTag, "Siginature : " + purchase.getSignature());
Log.d(LogTag, "purchaseInfo : " + purchase.toString());
} else {
Log.d(LogTag, "결제 과금에 실패");
Log.d(LogTag, "OrderID : " + purchase.getOrderId());
Log.d(LogTag, "PackageName : " + purchase.getPackageName());
Log.d(LogTag, "PurchaseTime :" + purchase.getPurchaseTime());
Log.d(LogTag, "PurchaseState : " + purchase.getPurchaseState());
Log.d(LogTag, "developerPayload : " + purchase.getDeveloperPayload());
Log.d(LogTag, "PurchaseToken : " + purchase.getToken());
Log.d(LogTag, "ItemType : " + purchase.getItemType());
Log.d(LogTag, "Siginature : " + purchase.getSignature());
Log.d(LogTag, "purchaseInfo : " + purchase.toString());
}
}
};
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
boolean fGoNextLogic = false;
Log.d(LogTag, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
fGoNextLogic = true;
// super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(LogTag, "onActivityResult handled by IABUtil.");
if (mListener != null)
mListener.endLoading();
fGoNextLogic = false;
}
return fGoNextLogic;
}
public void release() {
Log.d(LogTag, "아이템 구매 종료 (초기화)");
if (mHelper != null)
mHelper.dispose();
mHelper = null;
}
public void paymentDataInit() {
endPay();
}
public void endPay() {
// 로딩 스톱
if (mListener != null)
mListener.startLoading();
}
public void setOnGoogleListener(onGoogleBillingListener listener) {
mListener = listener;
}
/** Verifies the developer payload of a purchase. */
boolean verifyDeveloperPayload(Purchase p) {
if (p == null)
return false;
String payload = p.getDeveloperPayload();
/*
* TODO: verify that the developer payload of the purchase is correct.
* It will be the same one that you sent when initiating the purchase.
*
* WARNING: Locally generating a random string when starting a purchase
* and verifying it here might seem like a good approach, but this will
* fail in the case where the user purchases an item on one device and
* then uses your app on a different device, because on the other device
* you will not have access to the random string you originally
* generated.
*
* So a good developer payload has these characteristics:
*
* 1. If two different users purchase an item, the payload is different
* between them, so that one user's purchase can't be replayed to
* another user.
*
* 2. The payload must be such that you can verify it even when the app
* wasn't the one who initiated the purchase flow (so that items
* purchased by the user on one device work on other devices owned by
* the user).
*
* Using your own server to store and verify developer payloads across
* app installations is recommended.
*/
return true;
}
}