Slide 1

Slide 1 text

Deep dive into subscriptions DroidKaigi2019, Room2 - 2019/02/07 16:00-16:50

Slide 2

Slide 2 text

whoami twitter:@ymnd, github:@ymnder Application Engineer Android Android https://s.nikkei.com/s_android Day2: Room 4 16:50-17:20 WebView+ViewGroup AOSP by ogapants 2

Slide 3

Slide 3 text

Today s menu 3

Slide 4

Slide 4 text

RealTime Developer Noti cation iOS IAP 4

Slide 5

Slide 5 text

Caution 


Slide 6

Slide 6 text

Caution https://developer.android.com/google/play/billing/ billing_best_practices

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9 αϒεΫϦϓγϣϯ͸ʮߪೖ͍ͯ͠Δظؒ಺͚ͩʯ ౰֘αʔϏεΛ࢖͑ΔΑ͏ʹͳΔ࢓૊Έ

Slide 10

Slide 10 text

10 ՝ۚ ՝ۚ ՝ۚ … ղ໿ ࢧ෷͍ෆঝೝ

Slide 11

Slide 11 text

or 11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

13

Slide 14

Slide 14 text

PlayStore 14

Slide 15

Slide 15 text

15

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

ID ID ID 18

Slide 19

Slide 19 text

GooglePlay ID ID ID 19

Slide 20

Slide 20 text

GooglePlay 30% 15% ref: https://support.google.com/googleplay/android-developer/ answer/112622?hl=ja KPI KPI 20

Slide 21

Slide 21 text

GooglePlay 21

Slide 22

Slide 22 text

PlayStore Web PlayStore 22

Slide 23

Slide 23 text

Google Play 23

Slide 24

Slide 24 text

20 30 ID Play 24

Slide 25

Slide 25 text

PlayStore 25

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Google Play Billing Library GooglePlayStoreApp StoreApp Android 署 API wrap 28

Slide 29

Slide 29 text

Google Play Billing Library https://developer.android.com/reference/com/android/ billingclient/classes v1.1 v1.2 released (2018-10-18) 29

Slide 30

Slide 30 text

app/build.gradle 30 dependencies { implementation “com.android.billingclient:billing:1.1” }

Slide 31

Slide 31 text

BillingClient BillingClient 31 var client = BillingClient.newBuilder(context) .setListener(object : PurchasesUpdatedListener { override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList?) { // call if purchase has done } }) .build()

Slide 32

Slide 32 text

Service startConnection 32 client.startConnection(object : BillingClientStateListener { override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) { if (billingResponseCode == BillingClient.BillingResponse.OK) { // The billing client is ready. You can query purchases here. } } override fun onBillingServiceDisconnected() { // Try to restart the connection on the next request to // Google Play by calling the startConnection() method. } })

Slide 33

Slide 33 text

launchBillingFlow PurchasesUpdatedListener 踏 33 val flowParams = BillingFlowParams.newBuilder() .setSku("skuId") .setType(BillingClient.SkuType.SUBS) // SkuType.SUB for subscription .build() val responseCode = client.launchBillingFlow(activity, flowParams)

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

BillingClient / BillingClientImpl BillingClient PlayStore 35 public static final class Builder { private final Context mContext; private PurchasesUpdatedListener mListener; private Builder(Context context) { mContext = context; } @UiThread public Builder setListener(PurchasesUpdatedListener listener) { mListener = listener; return this; } @UiThread public BillingClient build() { if (mContext == null) { throw new IllegalArgumentException("Please provide a valid Context."); } if (mListener == null) { throw new IllegalArgumentException(/…/); } return new BillingClientImpl(mContext, mListener); } }

Slide 36

Slide 36 text

BillingClient / BillingClientImpl PurchasesUpdatedListener 踏 Client 36 @UiThread public BillingClient build() { if (mContext == null) { throw new IllegalArgumentException("Please provide a valid Context."); } if (mListener == null) { throw new IllegalArgumentException( "Please provide a valid listener for" + " purchases updates."); } return new BillingClientImpl(mContext, mListener); }

Slide 37

Slide 37 text

PurchasesUpdatedListener purchases null 37 public interface PurchasesUpdatedListener { void onPurchasesUpdated(@BillingResponse int responseCode, @Nullable List purchases); }

Slide 38

Slide 38 text

BillingClient Service start / ready / end start InAppBillingService isReady start end Service unbind null end Client 38

Slide 39

Slide 39 text

39 public @interface ClientState { /** This client was not yet connected to billing service or was already disconnected from it. */ int DISCONNECTED = 0; /** This client is currently in process of connecting to billing service. */ int CONNECTING = 1; /** This client is currently connected to billing service. */ int CONNECTED = 2; /** This client was already closed and shouldn't be used again. */ int CLOSED = 3; }

Slide 40

Slide 40 text

isReady startConnection endConnection 40 @UiThread public abstract boolean isReady(); @UiThread public abstract void startConnection(@NonNull final BillingClientStateListener listener); @UiThread public abstract void endConnection();

Slide 41

Slide 41 text

isReady 41 public boolean isReady() { return mClientState == ClientState.CONNECTED && mService != null && mServiceConnection != null; }

Slide 42

Slide 42 text

startConnection 42 public void startConnection(@NonNull BillingClientStateListener listener) { if (isReady()) { listener.onBillingSetupFinished(BillingResponse.OK); return; } if (mClientState == ClientState.CONNECTING) { listener.onBillingSetupFinished(BillingResponse.DEVELOPER_ERROR); return; } if (mClientState == ClientState.CLOSED) { listener.onBillingSetupFinished(BillingResponse.DEVELOPER_ERROR); return; } mClientState = ClientState.CONNECTING; //… if connected, set ClientState.CONNECTED in BillingServiceConnection mClientState = ClientState.DISCONNECTED; BillingHelper.logVerbose(TAG, "Billing service unavailable on device."); listener.onBillingSetupFinished(BillingResponse.BILLING_UNAVAILABLE); }

Slide 43

Slide 43 text

startConnection / onServiceConnected IABv6 43

Slide 44

Slide 44 text

endConnection 44 public void endConnection() { try { LocalBroadcastManager.getInstance(mApplicationContext) .unregisterReceiver(onPurchaseFinishedReceiver); mBroadcastManager.destroy(); if (mServiceConnection != null && mService != null) { BillingHelper.logVerbose(TAG, "Unbinding from service."); mApplicationContext.unbindService(mServiceConnection); mServiceConnection = null; } mService = null; if (mExecutorService != null) { mExecutorService.shutdownNow(); mExecutorService = null; } } catch (Exception ex) { BillingHelper.logWarn(TAG, "There was an exception while ending connection: " + ex); } finally { mClientState = ClientState.CLOSED; } }

Slide 45

Slide 45 text

endConnection 45 public void endConnection() { try { LocalBroadcastManager.getInstance(mApplicationContext) .unregisterReceiver(onPurchaseFinishedReceiver); mBroadcastManager.destroy(); if (mServiceConnection != null && mService != null) { BillingHelper.logVerbose(TAG, "Unbinding from service."); mApplicationContext.unbindService(mServiceConnection); mServiceConnection = null; } mService = null; if (mExecutorService != null) { mExecutorService.shutdownNow(); mExecutorService = null; } } catch (Exception ex) { BillingHelper.logWarn(TAG, "There was an exception while ending connection: " + ex); } finally { mClientState = ClientState.CLOSED; } }

Slide 46

Slide 46 text

endConnection Activity Client onDestroy Application I/O 2018 ref: https://github.com/googlesamples/android-play-billing/ blob/master/TrivialDriveKotlin/app/src/main/java/com/ kotlin/trivialdrive/billingrepo/BillingRepository.kt#L210 46

Slide 47

Slide 47 text

isFeatureSupported PlayStore 47 public @BillingResponse int isFeatureSupported(@FeatureType String feature) { if (!isReady()) { return BillingResponse.SERVICE_DISCONNECTED; } switch (feature) { case FeatureType.SUBSCRIPTIONS: return mSubscriptionsSupported ? BillingResponse.OK : BillingResponse.FEATURE_NOT_SUPPORTED; case FeatureType.SUBSCRIPTIONS_UPDATE: return mSubscriptionUpdateSupported ? BillingResponse.OK: BillingResponse.FEATURE_NOT_SUPPORTED; case FeatureType.IN_APP_ITEMS_ON_VR: return isBillingSupportedOnVr(SkuType.INAPP); case FeatureType.SUBSCRIPTIONS_ON_VR: return isBillingSupportedOnVr(SkuType.SUBS); default: return BillingResponse.DEVELOPER_ERROR; } }

Slide 48

Slide 48 text

launchBillingFlow 48 public int launchBillingFlow(Activity activity, BillingFlowParams params) { if (!isReady()) { return broadcastFailureAndReturnBillingResponse(BillingResponse.SERVICE_DISCONNECTED); } @SkuType String skuType = params.getSkuType(); String newSku = params.getSku(); if (newSku == null) { BillingHelper.logWarn(TAG, "Please fix the input params. SKU can't be null."); return broadcastFailureAndReturnBillingResponse(BillingResponse.DEVELOPER_ERROR); } try { //… Intent intent = new Intent(activity, ProxyBillingActivity.class); PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT); intent.putExtra(RESPONSE_BUY_INTENT, pendingIntent); activity.startActivity(intent); } catch (RemoteException e) { //… } return BillingResponse.OK; }

Slide 49

Slide 49 text

launchBillingFlow 49 public int launchBillingFlow(Activity activity, BillingFlowParams params) { if (!isReady()) { return broadcastFailureAndReturnBillingResponse(BillingResponse.SERVICE_DISCONNECTED); } @SkuType String skuType = params.getSkuType(); String newSku = params.getSku(); if (newSku == null) { BillingHelper.logWarn(TAG, "Please fix the input params. SKU can't be null."); return broadcastFailureAndReturnBillingResponse(BillingResponse.DEVELOPER_ERROR); } try { //… Intent intent = new Intent(activity, ProxyBillingActivity.class); PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT); intent.putExtra(RESPONSE_BUY_INTENT, pendingIntent); activity.startActivity(intent); } catch (RemoteException e) { //… } return BillingResponse.OK; }

Slide 50

Slide 50 text

BillingFlowParams ID Type 50 BillingFlowParams.newBuilder() .setSku("skuId") .setType(BillingClient.SkuType.SUBS) .build()

Slide 51

Slide 51 text

launchBillingFlow Activity BillingFlowParams Activity Client 踏 51

Slide 52

Slide 52 text

launchBillingFlow 52 public int launchBillingFlow(Activity activity, BillingFlowParams params) { if (!isReady()) { return broadcastFailureAndReturnBillingResponse(BillingResponse.SERVICE_DISCONNECTED); } @SkuType String skuType = params.getSkuType(); String newSku = params.getSku(); if (newSku == null) { BillingHelper.logWarn(TAG, "Please fix the input params. SKU can't be null."); return broadcastFailureAndReturnBillingResponse(BillingResponse.DEVELOPER_ERROR); } try { //… Intent intent = new Intent(activity, ProxyBillingActivity.class); PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT); intent.putExtra(RESPONSE_BUY_INTENT, pendingIntent); activity.startActivity(intent); } catch (RemoteException e) { //… } return BillingResponse.OK; }

Slide 53

Slide 53 text

launchBillingFlow Activity BillingFlowParams Activity Client 踏 53

Slide 54

Slide 54 text

launchBillingFlow Activity Params 54

Slide 55

Slide 55 text

queryPurchaseHistoryAsync skuId 55

Slide 56

Slide 56 text

queryPurchaseHistoryAsync 56 public void queryPurchaseHistoryAsync( final @SkuType String skuType, final PurchaseHistoryResponseListener listener) { if (!isReady()) { listener.onPurchaseHistoryResponse( BillingResponse.SERVICE_DISCONNECTED, /* purchasesList */ null); return; } executeAsync( new Runnable() { @Override public void run() { final PurchasesResult result = queryPurchasesInternal(skuType, /* queryHistory */ true); // Post the result to main thread postToUiThread( new Runnable() { @Override public void run() { listener.onPurchaseHistoryResponse( result.getResponseCode(), result.getPurchasesList()); } }); } }); }

Slide 57

Slide 57 text

queryPurchaseHistoryAsync 57 public void queryPurchaseHistoryAsync( final @SkuType String skuType, final PurchaseHistoryResponseListener listener) { if (!isReady()) { listener.onPurchaseHistoryResponse( BillingResponse.SERVICE_DISCONNECTED, /* purchasesList */ null); return; } executeAsync( new Runnable() { @Override public void run() { final PurchasesResult result = queryPurchasesInternal(skuType, /* queryHistory */ true); // Post the result to main thread postToUiThread( new Runnable() { @Override public void run() { listener.onPurchaseHistoryResponse( result.getResponseCode(), result.getPurchasesList()); } }); } }); }

Slide 58

Slide 58 text

queryPurchaseHistoryAsync API API 58

Slide 59

Slide 59 text

queryPurchases Play Store App Cache 59

Slide 60

Slide 60 text

queryPurchases 60 public PurchasesResult queryPurchases(@SkuType String skuType) { if (!isReady()) { return new PurchasesResult(BillingResponse.SERVICE_DISCONNECTED, /* purchasesList */ null); } // Checking for the mandatory argument if (TextUtils.isEmpty(skuType)) { BillingHelper.logWarn(TAG, "Please provide a valid SKU type."); return new PurchasesResult(BillingResponse.DEVELOPER_ERROR, /* purchasesList */ null); } return queryPurchasesInternal(skuType, false /* queryHistory */); }

Slide 61

Slide 61 text

PlayStore 61

Slide 62

Slide 62 text

launchBillingFlow onPurchasesUpdated(int responseCode, List purchases) queryPurchaseHistoryAsync onPurchaseHistoryResponse(int responseCode, List purchasesList) queryPurchases Purchase.PurchasesResult 62

Slide 63

Slide 63 text

63

Slide 64

Slide 64 text

autoRenewing orderId ID packageName productId ID purchaseState purchaseTime purchaseToken https://developer.android.com/google/play/billing/billing_reference? hl=ja#getBuyIntent 64

Slide 65

Slide 65 text

autoRenewing orderId ID packageName productId ID purchaseState purchaseTime purchaseToken https://developer.android.com/google/play/billing/billing_reference? hl=ja#getBuyIntent 65

Slide 66

Slide 66 text

launchBillingFlow 66 { "autoRenewing": true, "orderId": "GPA.0000-1111-2222-33444", "packageName": "com.nikkei.newspaper", "productId": "skuId", "purchaseState": 0, "purchaseTime": 1549417790186, "purchaseToken": “…” }

Slide 67

Slide 67 text

queryPurchaseHistoryAsync 67 { “productId":"skuId", “purchaseToken”:”…", “purchaseTime":1549417790186, “developerPayload”:null }

Slide 68

Slide 68 text

queryPurchases 68 { "autoRenewing": true, "orderId": "GPA.0000-1111-2222-33444", "packageName": "com.nikkei.newspaper", "productId": "skuId", "purchaseState": 0, "purchaseTime": 1549417790186, "purchaseToken": “…” }

Slide 69

Slide 69 text

queryPurchases 69 { "autoRenewing": false, "orderId": "GPA.0000-1111-2222-33444", "packageName": "com.nikkei.newspaper", "productId": “skuId", "purchaseState": 0, "purchaseTime": 1549417790186, "purchaseToken": “…” }

Slide 70

Slide 70 text

queryPurchaseHistoryAsync launchBillingFlow queryPurchases 70

Slide 71

Slide 71 text

queryPurchaseHistoryAsync queryPurchases autoRenewing 71

Slide 72

Slide 72 text

BillingResponse 72 public @interface BillingResponse { int FEATURE_NOT_SUPPORTED = -2; int SERVICE_DISCONNECTED = -1; /** Success */ int OK = 0; /** User pressed back or canceled a dialog */ int USER_CANCELED = 1; /** Network connection is down */ int SERVICE_UNAVAILABLE = 2; /** Billing API version is not supported for the type requested */ int BILLING_UNAVAILABLE = 3; /** Requested product is not available for purchase */ int ITEM_UNAVAILABLE = 4; int DEVELOPER_ERROR = 5; /** Fatal error during the API action */ int ERROR = 6; /** Failure to purchase since item is already owned */ int ITEM_ALREADY_OWNED = 7; /** Failure to consume since item is not owned */ int ITEM_NOT_OWNED = 8; }

Slide 73

Slide 73 text

BillingResponse startConnection 73 return when (resultCode) { BillingClient.BillingResponse.FEATURE_NOT_SUPPORTED -> "FEATURE_NOT_SUPPORTED" BillingClient.BillingResponse.SERVICE_DISCONNECTED -> "SERVICE_DISCONNECTED" BillingClient.BillingResponse.OK -> "OK" BillingClient.BillingResponse.USER_CANCELED -> "USER_CANCELED" BillingClient.BillingResponse.SERVICE_UNAVAILABLE -> "SERVICE_UNAVAILABLE" BillingClient.BillingResponse.BILLING_UNAVAILABLE -> "BILLING_UNAVAILABLE" BillingClient.BillingResponse.ITEM_UNAVAILABLE -> "ITEM_UNAVAILABLE" BillingClient.BillingResponse.DEVELOPER_ERROR -> "DEVELOPER_ERROR" BillingClient.BillingResponse.ERROR -> "ERROR" BillingClient.BillingResponse.ITEM_ALREADY_OWNED -> "ITEM_ALREADY_OWNED" BillingClient.BillingResponse.ITEM_NOT_OWNED -> "ITEM_NOT_OWNED" else -> "UNDEFINED:$resultCode" }

Slide 74

Slide 74 text

querySkuDetailsAsync v1.2 launchPriceChangeCon rmationFlow loadRewardedSku setChildDirected 74

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

UI https://note.mu/telq/n/n836e139e0a6b 76

Slide 77

Slide 77 text

77

Slide 78

Slide 78 text

ID 78

Slide 79

Slide 79 text

79

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

ref: https://developer.android.com/google/play/billing/ billing_admin.html?hl=ja 81

Slide 82

Slide 82 text

GooglePlayConsole > 82

Slide 83

Slide 83 text

83

Slide 84

Slide 84 text

ID skuId productId, ID 84

Slide 85

Slide 85 text

> 85

Slide 86

Slide 86 text

com.android.vending.BILLING implementation com.android.billingclient:billing:1.1' 86

Slide 87

Slide 87 text

87

Slide 88

Slide 88 text

TestCard TestCard Google Play https://support.google.com/googleplay/android-developer/answer/6062777? hl=ja 88

Slide 89

Slide 89 text

> 
 or URL 89

Slide 90

Slide 90 text

> > Gmail 90

Slide 91

Slide 91 text

4,200/5min GPay TestCard 91

Slide 92

Slide 92 text

/ 1 / 5min, 1 / 5min 
 https://developer.android.com/google/play/billing/ billing_testing#testing-renewals 92

Slide 93

Slide 93 text

PlayStore PlayStore https://github.com/googlesamples/android-play-billing/issues/2 PlayStore 93

Slide 94

Slide 94 text

PlayStore 94

Slide 95

Slide 95 text

ID ID PlayStoreApp App Console 95

Slide 96

Slide 96 text

applicationID ID avor 96

Slide 97

Slide 97 text

tips ref: https://techbookfest.org/event/tbf04/circle/14710011 store ID 97

Slide 98

Slide 98 text

tips 98

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

skuId PlayStore API skuId querySkuDetailsAsync 100

Slide 101

Slide 101 text

BillingActivity Activity 踏 踏 Activity BillingClient Activity Activity issue ref: https://github.com/googlesamples/android-play-billing/ issues/95 101

Slide 102

Slide 102 text

RxJava Rx Kotlin ref: https://github.com/googlesamples/android-play-billing/ blob/master/TrivialDriveKotlin/app/src/main/java/com/ kotlin/trivialdrive/billingrepo/BillingRepository.kt#L210 102

Slide 103

Slide 103 text

103 SFNPUF MPDBM SFQPTJUPSZ VTFDBTF QSFTFOUFS WJFX

Slide 104

Slide 104 text

Remote Local Repository interface RxJava Single Completable Remote BillingClient BillingLibrary Manager Listener 104

Slide 105

Slide 105 text

launchBilling ow launchBilling ow BillingClient Listener Callback 105

Slide 106

Slide 106 text

launchBilling ow launchBilling ow PurchasesUpdatedListener 踏 RxJava PublishSubject 106

Slide 107

Slide 107 text

launchBilling ow 107

Slide 108

Slide 108 text

launchBilling ow PublishSubject Subject Subscriber Observable Listener subscribe onNext instance callback PublishSubject callback onNext 踏 Rx Remote Single Wrap Repository 108

Slide 109

Slide 109 text

BillingManager 109 interface PlayBillingManager { fun observePurchasesUpdated(): PublishSubject fun isReady(): Boolean fun startConnection(listener: BillingClientStateListener) fun launchBillingFlow(activity: Activity, params: BillingFlowParams): Int fun queryPurchases(): Purchase.PurchasesResult fun endConnection() fun queryPurchaseHistoryAsync(skuId: String): Single }

Slide 110

Slide 110 text

BillingApiManager 110 @Singleton class PlayBillingManagerImpl @Inject constructor( private val context: Context ) : PlayBillingManager, PurchasesUpdatedListener { private val publishSubject = PublishSubject.create() override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList?) { if (responseCode == BillingClient.BillingResponse.OK) { publishSubject.onNext(PurchasesUpdatedResponse(responseCode, purchases)) } else { publishSubject.onNext(PurchasesUpdatedResponse(responseCode)) } } override fun observePurchasesUpdated(): PublishSubject { return publishSubject } override fun launchBillingFlow(activity: Activity, params: BillingFlowParams): Int { return billingClient.launchBillingFlow(activity, params) } }

Slide 111

Slide 111 text

BillingApiManager 111 @Singleton class PlayBillingManagerImpl @Inject constructor( private val context: Context ) : PlayBillingManager, PurchasesUpdatedListener { private val publishSubject = PublishSubject.create() override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList?) { if (responseCode == BillingClient.BillingResponse.OK) { publishSubject.onNext(PurchasesUpdatedResponse(responseCode, purchases)) } else { publishSubject.onNext(PurchasesUpdatedResponse(responseCode)) } } override fun observePurchasesUpdated(): PublishSubject { return publishSubject } override fun launchBillingFlow(activity: Activity, params: BillingFlowParams): Int { return billingClient.launchBillingFlow(activity, params) } }

Slide 112

Slide 112 text

Remote 112 @Singleton class RemotePlayBillingImpl @Inject constructor( private val billingClient: PlayBillingManager ) : RemotePlayBilling { override fun launchBillingFlow(activity: Activity, skuId: String): Single { return Single.create { val flowParams = BillingFlowParams.newBuilder() .setSku(skuId) .setType(TYPE_SUBSCRIPTION) .build() val responseCode = billingClient.launchBillingFlow(activity, flowParams) if (responseCode == BillingClient.BillingResponse.OK) { it.onSuccess(PurchasePaperResponse.PurchaseSuccess(responseCode)) } else { it.onError(PurchaseException(responseCode, "ߪಡΤϥʔ")) } } } override fun observePurchasesUpdated(): Observable { return billingClient.observePurchasesUpdated() } }

Slide 113

Slide 113 text

Remote 113 @Singleton class RemotePlayBillingImpl @Inject constructor( private val billingClient: PlayBillingManager ) : RemotePlayBilling { override fun launchBillingFlow(activity: Activity, skuId: String): Single { return Single.create { val flowParams = BillingFlowParams.newBuilder() .setSku(skuId) .setType(TYPE_SUBSCRIPTION) .build() val responseCode = billingClient.launchBillingFlow(activity, flowParams) if (responseCode == BillingClient.BillingResponse.OK) { it.onSuccess(PurchasePaperResponse.PurchaseSuccess(responseCode)) } else { it.onError(PurchaseException(responseCode, "ߪಡΤϥʔ")) } } } override fun observePurchasesUpdated(): Observable { return billingClient.observePurchasesUpdated() } }

Slide 114

Slide 114 text

startConnection launchBilling ow PurchasesUpdatedListener 114

Slide 115

Slide 115 text

Repository 115 class PlayBillingRepositoryImpl @Inject constructor( private val remote: RemotePlayBilling ) : PlayBillingRepository { override fun startSubscription(activity: Activity, skuId: String): Observable { return remote.connect() .flatMapObservable { remote.launchBillingFlow(activity, skuId) .flatMapObservable { remote.observePurchasesUpdated() .flatMap { if (it.result != BillingClient.BillingResponse.OK) { return@flatMap Observable.error(…) } return@flatMap Observable.just(it) } //… } } } }

Slide 116

Slide 116 text

Repository 116 class PlayBillingRepositoryImpl @Inject constructor( private val remote: RemotePlayBilling ) : PlayBillingRepository { override fun startSubscription(activity: Activity, skuId: String): Observable { return remote.connect() .flatMapObservable { remote.launchBillingFlow(activity, skuId) .flatMapObservable { remote.observePurchasesUpdated() .flatMap { if (it.result != BillingClient.BillingResponse.OK) { return@flatMap Observable.error(…) } return@flatMap Observable.just(it) } //… } } } }

Slide 117

Slide 117 text

RemoteTest 117 @Before fun setUp() { manager = mock(PlayBillingManager::class.java) remote = RemotePlayBillingImpl(manager) } 
 @Test fun connect_isReady() { `when`(manager.isReady()).thenReturn(true) val response = remote.connect() .test() .await() .values()[0] assertThat(response).isNotNull() assertThat(response.result).isEqualTo(BillingClient.BillingResponse.OK) }

Slide 118

Slide 118 text

RemoteTest 118 @Test fun observePurchasesUpdated_error() { val subject = spy(PublishSubject.create()) `when`(remote.observePurchasesUpdated()).thenReturn(subject) val latch = CountDownLatch(2) var responseCode = 0 remote.observePurchasesUpdated() .subscribe({ responseCode += 1 latch.countDown() }, { latch.countDown() }) subject.onNext(PurchasesUpdatedResponse(0, null)) subject.onError(PurchaseException(BillingClient.BillingResponse.ERROR, "ΤϥʔͰ͢ʂʂ")) subject.onNext(PurchasesUpdatedResponse(1, null)) subject.onError(PurchaseException(BillingClient.BillingResponse.ERROR, "ΤϥʔͰ͢ʂʂ")) latch.await() assertThat(responseCode).isEqualTo(1) verify(subject, times(2)).onNext(any()) verify(subject, times(2)).onError(any()) }

Slide 119

Slide 119 text

踏 119

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

PlayStoreConsole 121

Slide 122

Slide 122 text

122

Slide 123

Slide 123 text

> 123

Slide 124

Slide 124 text

> 124

Slide 125

Slide 125 text

> > 125

Slide 126

Slide 126 text

126

Slide 127

Slide 127 text

127

Slide 128

Slide 128 text

30 1 2 9:51 2 2 9:51 3 2 9:51 128

Slide 129

Slide 129 text

3 129

Slide 130

Slide 130 text

v1.1 > : ID ID v1.2 launchPriceChangeCon rmationFlow 130

Slide 131

Slide 131 text

https://github.com/googlesamples/android-play-billing https://developer.android.com/google/play/billing/billing_overview Library PlayConsole 131

Slide 132

Slide 132 text

Hello Subscription :)

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

PlayStore https://play.google.com/store/account/subscriptions? sku=XXX&package=YYY 134

Slide 135

Slide 135 text

PlayStore app music video 135

Slide 136

Slide 136 text

Google I/O https://www.youtube.com/watch?v=x1AYelepG6o https://www.youtube.com/watch?v=oib_gHJA_- https://www.youtube.com/watch?v=6IT689K3kOo https://developer.android.com/google/play/billing/ https://support.google.com/googleplay/android-developer/answer/140504?hl=ja https://github.com/googlesamples/android-play-billing RxJava https://techlife.cookpad.com/entry/2018/03/14/090000 https://github.com/bu erapp/ReactivePlayBilling https://github.com/vberezkin/billing-android 136

Slide 137

Slide 137 text

Account Hold https://techbookfest.org/event/tbf04/circle/16610003 v1.0 https://medium.com/exploring-android/exploring-the-play- billing-library-for-android-55321f282929 v1.1 https://speakerdeck.com/ymnder/whats-new-in-google-play- billing 137