티스토리 뷰
Google Play Billing Library Version 3
참고사항
- 테스트결제시 apk 빌드는 develop 인증서 사용가능. (단 프로덕션 이하로 앱게시 필요)
- 결제테스트시 스토어 관리자계정이 아닌 다른 구글계정으로 테스트 필요.
- 최신 빌링 라이브러리 버전.
- AndroidManifest.xml에 BILLING 권한 필요없음.
예제
dependencies {
implementation 'com.android.billingclient:billing:3.0.3'
}
public class GoogleBillingImpl implements PurchasesUpdatedListener {
private static final String TAG = "GoogleBillingImpl";
private final BillingClient mBillingClient;
private List<SkuDetails> skuDetailsList = new ArrayList<>();
public GoogleBillingImpl(@NonNull final Context applicationContext)
{
mBillingClient = BillingClient.newBuilder(applicationContext)
.enablePendingPurchases()
.setListener(this)
.build();
}
public void init()
{
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult)
{
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
{
// The billing client is ready. You can query purchases here.
List<String> strList = new ArrayList<>();
strList.add("test_1000");
strList.add("test_2000");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(strList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult,
@Nullable List<SkuDetails> list)
{
// Process the result.
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& list != null)
{
if(list.isEmpty())
{
Log.d(TAG, "list is zero");
}else{
skuDetailsList = list;
}
}
}
});
}else{
Log.d(TAG, "google purchase error");
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "onBillingServiceDisconnected");
}
});
}
public void purchase(Activity activity, String productId)
{
BillingFlowParams flowParams = null;
BillingResult billingResult;
SkuDetails sku = getSkuDetail(productId);
if( sku != null )
{
flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(sku)
.build();
billingResult = mBillingClient.launchBillingFlow(activity, flowParams);
}
else
{
Log.d(TAG, "sku is null");
}
}
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& list != null) {
for (Purchase purchase : list) {
handlePurchase(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
Log.d(TAG, "user purchase cancel");
} else {
// Handle any other error codes.
Log.d(TAG, "google purchase error");
}
}
private void handlePurchase(Purchase purchase) {
String purchaseToken, payLoad;
purchaseToken = purchase.getPurchaseToken();
payLoad = purchase.getDeveloperPayload();
if(purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)
{
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchaseToken)
.build();
mBillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {
@Override
public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String s)
{
if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
{
Log.d(TAG, "google purchase success");
}else{
Log.d(TAG, "google purchase consume error");
}
}
});
}
}
private SkuDetails getSkuDetail(String productId) {
for(SkuDetails item : skuDetailsList) {
if(item.getSku().equals(productId)) {
return item;
}
}
return null;
}
}
'Development Note > Android' 카테고리의 다른 글
[Android Java] FCM 및 로컬푸쉬 사용법 정리 (0) | 2020.08.11 |
---|---|
[Android Java] 안드로이드 권한 요청 정리 (0) | 2020.08.10 |
[Android Java] Retrofit2 을 AsyncTask 로 사용하기 (0) | 2020.07.31 |
[Android Java] 안드로이드 millisecond to Date, String 로 변환 (0) | 2020.04.12 |
[Android Java] UTC Time 가져오기 (0) | 2020.04.12 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 퍼미션체크
- permissionCheck
- tempdir
- iTunes Store에 연결할 수 없음
- abs
- Error Domain=SKErrorDomain Code=0
- convert nsurl to nsstring
- ioutil
- setCancelable vs setCanceledOnTouchOutside
- java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line
- DeviceModel
- nsurl to nsstring convert
- Golang
- tempfile
- countryName
- Locale.getDefault().getLanguage()
- 절대값함수
- nsurl 에서 nsstring 변환
- countryCode
- LanguageCode
- dialog
- NSUserDefaults 예제
- abs 함수
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함