우선 IabHelper 와 Product ID 를 선언해줍니다.
// The helper object
IabHelper mHelper;
// SKUs for our products: the premium upgrade (non-consumable) and gas (consumable)
// Google Billing Product ID
//Product ID 는 "관리되지 않는 제품" 으로 등록 되어있습니다.
static final String SKU_1 = "gas";
그리고 onCreate 함수 안에
public void onCreate(Bundle savedInstanceState) {
아래와 같이 구현해줍니다.
//구글 APK 등록할때 생성된 GCM Key를 넣어주고
String base64EncodedPublicKey = "CONSTRUCT_YOUR_KEY_AND_PLACE_IT_HERE";
// Create the helper, passing it our context and the public key to verify signatures with
Log.d("TEST", "Creating IAB helper.");'
// IabHelper 초기화
mHelper = new IabHelper(this, base64EncodedPublicKey);
// enable debug logging (for a production application, you should set this to false).
// true 로하면 IabHelper Tag 로 진행과정을 로그로 볼수있습니다.
mHelper.enableDebugLogging(true);
// Start setup. This is asynchronous and the specified listener
// will be called once setup completes.
Log.d("TEST", "Starting setup.");
// Start setup 은 기본
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d("TEST", "Setup finished.");
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d("TEST", "Starting Error.");
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d("TEST", "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);
}
});
} // onCreate 끝
그리고 리스너들을 구현합니다.
// Listener that's called when we finish querying the items and subscriptions we own
// 이부분은 구매한 내역등을 확인 하는 것인것 같은데. 빌링 예제를 참조 하면 될겁니다. 여기선 기본 기능만 구현
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d("TEST", "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// Is it a failure?
if (result.isFailure()) {
Log.d("TEST", "Query inventory isFailure.");
return;
}
}
};
// User clicked the "Buy" button
public void onBuyButtonClicked(View arg0) {
Log.d("TEST", "Buy gas button clicked.");
/* TODO: for security, generate your payload here for verification. See the comments on
* verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
* an empty string, but on a production app you should carefully generate this. */
String payload = "";
// 구매 할때 호출하는 함수 입니다. productID 를 넣어주고 12345 는 결과 리턴시 onActivityResult 로 오는 resultCode 인데 받기 원하는 숫자를 넣으면 되지만 현재 V3 에서는 사용안하는 듯합니다.
// launchPurchaseFlow ( activity, product ID , return int , Listener, extradata )
mHelper.launchPurchaseFlow(this, SKU_1 , 12345,
mPurchaseFinishedListener, payload);
}
// 결제후 결과 값이 오는 곳 입니다.
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d("TEST", "Purchase finished: " + result + ", purchase: " + purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null) return;
// 결제 후 처리들은 이 아래 쪽에서 하면됩니다.
// 결제 실패 처리는 여기서.
if (result.isFailure()) {
Log.d("TEST", "Purchase isFailure: " + result + ", purchase: " + purchase);
return;
}
Log.d("TEST", "Purchase successful.");
// 결제 성공 후 처리는 여기서.
if (purchase.getSku().equals("SKU_1")) {
// bought 1/4 tank of gas. So consume it.
Log.d("TEST", "Purchase is gas. Starting gas consumption.");
// 이게 제일 중요한 것인데. 구글 보안 정책상 결제자가 구매한 아이템을 다 소모 후 다시 구매할 수 있어서, 결제 후 바로 구매가 되질 않습니다.
// 이를 강제로 소모했다고 알려 주는 것이 이 함수입니다.
// 그리고 Product ID 도 "관리되지 않는 제품" 으로 등록 되어야합니다.
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
}
}
};
추가 설명
인앱 제품 Product ID 생성할 때 제품 유형을 선택하게 되는데
관리되는 제품 - 한번 구매후 영원히 보유될 아이템
관리되지 않는 제품 - 소모성아이템
구독 - 월정액
이런 식으로 되어 있습니다.
그래서 주로 "관리되지 않는 제품" 으로 등록 합니다.
소스에서 설명했듯이 반드시 소모 했다는 함수 (consumeAsync) 를 호출 해줘야 다시 재구매가 가능합니다.
댓글 없음 :
댓글 쓰기