[java/kotlin]ショップをバインドしたアプリケーションの支払い


注意事項


  • java:
    public class BillingOneStoreImpl implements PurchasesUpdatedListener {
    
        private static String TAG = "BillingOneStoreImpl";
    
        @NonNull
        private final PurchaseClient mPurchaseClient;    
        private List<ProductDetail> skuDetailsList = new ArrayList<>();    
    
        public PubBillingOneStoreImpl(@NonNull final Activity activity) {
            mPurchaseClient = PurchaseClient.newBuilder(activity)
                    .setBase64PublicKey(activity.getResources().getString(R.string.onestore_license_key))
                    .setListener(this)
                    .build();
        }
        
        public void init()
        {
            mPurchaseClient.startConnection(new PurchaseClientStateListener() {
                @Override
                public void onSetupFinished(IapResult iapResult) {
                    if(iapResult.isSuccess()){                    
                        queryProductDetailsAsync();
                    } else {
                        Log.d(TAG, iapResult.toJsonString());
                    }
                }
    
                @Override
                public void onServiceDisconnected() {                
                }
            });
        }
    
        private void queryProductDetailsAsync()
        {
            List<String> strList = new ArrayList<>();
            strList.add("test_1000");
            strList.add("test_2000");
    
            ProductDetailsParams params = ProductDetailsParams.newBuilder()
                    .setProductIdList(strList)
                    .setProductType(PurchaseClient.ProductType.INAPP)
                    .build();
            mPurchaseClient.queryProductDetailsAsync(params, new ProductDetailsListener() {
                @Override
                public void onProductDetailsResponse(IapResult iapResult,
                                                     @Nullable List<ProductDetail> list) {
                    //Log.d(TAG, iapResult.toJsonString());
    
                    if(iapResult.isSuccess() && list != null){
    
                        for(ProductDetail skuDetail : list){                    
                            Log.d(TAG, "ProductDetail = " + skuDetail.toString());
                        }                    
                    }else{
                        Log.d(TAG, iapResult.toJsonString());
                    }
                }
            });
        }
    
        public void purchase(@NonNull Activity activity,
                             @NonNull String pid,
                             @NonNull String devPayload) 
        {        
            ProductDetail sku = getSkuDetail(pid);
            if( sku != null) {
                PurchaseFlowParams params = PurchaseFlowParams.newBuilder()
                        .setProductId(pid)
                        .setProductType(PurchaseClient.ProductType.INAPP)
                        .setDeveloperPayload(devPayload)
                        .setPromotionApplicable(false)
                        .build();
    
                mPurchaseClient.launchPurchaseFlow(activity, params);
            }else{
                Log.d(TAG, "sku is null");
            }
        }
    
        @Override
        public void onPurchasesUpdated(IapResult iapResult, @Nullable List<PurchaseData> list) {
            if(iapResult.isSuccess() && list != null) {
                for(PurchaseData purchase : list){
                    consumeAsync(purchase);
                }
            }else if(iapResult.getResponseCode() == PurchaseClient.ResponseCode.RESULT_USER_CANCELED){
                Log.d(TAG, iapResult.toJsonString());
            }else {
                Log.d(TAG, iapResult.toJsonString());
            }
        }
    
        private void consumeAsync(final PurchaseData purchase) {
    
            ConsumeParams params = ConsumeParams.newBuilder()
                    .setPurchaseData(purchase)
                    .build();
            mPurchaseClient.consumeAsync(params, new ConsumeListener() {
                @Override
                public void onConsumeResponse(IapResult iapResult, @Nullable PurchaseData purchaseData) {
                    if (iapResult.isSuccess() && purchaseData != null) {
                        if (purchaseData.getPurchaseToken().equals(purchase.getPurchaseToken())) {                        
                            Log.d(TAG, purchase.toString());
                        }else{
                            Log.d(TAG, "purchaseToken not equal");
                        }
                    } else {
                        Log.d(TAG, iapResult.toJsonString());
                    }
                }
            });
        }
        
        public void restorePurchases()
        {
            mPurchaseClient.queryPurchasesAsync(PurchaseClient.ProductType.INAPP, new PurchasesListener() {
                @Override
                public void onPurchasesResponse(IapResult iapResult, @Nullable List<PurchaseData> list) {
                    if (iapResult.isSuccess() && list != null) {                    
                        if(list.size() != 0){
                            for(PurchaseData purchaseData : list){
                                if(purchaseData.getPurchaseState() == PurchaseData.PurchaseState.PURCHASED){                                
                                    Log.d(TAG, purchaseData.toString());
                                    unConsumedListExecute(purchaseData);
                                }
                            }
                        }else{
                            Log.d(TAG, "list size 0");                        
                        }
                    } else {
                        Log.d(TAG, iapResult.toJsonString());
                    }
                }
            });
        }
    
        //소비되지 않은 결제리스트 소비
        private void unConsumedListExecute(final PurchaseData purchaseData)
        {
            ConsumeParams params = ConsumeParams.newBuilder()
                    .setPurchaseData(purchaseData)
                    .build();
            mPurchaseClient.consumeAsync(params, new ConsumeListener() {
                @Override
                public void onConsumeResponse(IapResult iapResult, @Nullable PurchaseData purchaseData) {
                    if (iapResult.isSuccess() && purchaseData != null) {
                        if (purchaseData.getPurchaseToken().equals(purchaseData.getPurchaseToken())) {                        
                            Log.d(TAG, purchaseData.toString());
                        }else{
                            Log.d(TAG, "purchaseToken not equal");
                        }
                    } else {
                        Log.d(TAG, iapResult.toJsonString());
                    }
                }
            });
        }
    
        private ProductDetail getSkuDetail(String pid) {
            for(ProductDetail item : skuDetailsList) {
                if(item.getProductId().equals(pid)) {
                    return item;
                }
            }
            return null;
        }
    }
    kotlin:
    ...