11package ir.cafebazaar.poolakey.rx
22
3- import android.app.Activity
4- import android.content.Intent
5- import androidx.fragment.app.Fragment
3+ import androidx.activity.result.ActivityResultRegistry
64import io.reactivex.Completable
75import io.reactivex.Observable
86import io.reactivex.Single
@@ -12,7 +10,6 @@ import ir.cafebazaar.poolakey.entity.PurchaseInfo
1210import ir.cafebazaar.poolakey.entity.SkuDetails
1311import ir.cafebazaar.poolakey.entity.TrialSubscriptionInfo
1412import ir.cafebazaar.poolakey.request.PurchaseRequest
15- import ir.cafebazaar.poolakey.rxbase.exception.PurchaseCanceledException
1613
1714/* *
1815 * You have to use this function to connect to the In-App Billing service. Note that you have to
@@ -39,30 +36,16 @@ fun Payment.connect(): Observable<Connection> {
3936 * You can use this function to navigate user to Bazaar's payment activity to purchase a product.
4037 * Note that for subscribing a product you have to use the 'subscribeProduct' function.
4138 * @see subscribeProduct
42- * @param activity We use this activity instance to actually start Bazaar's payment activity.
39+ * @param registry We use this activityResultRegistry instance to actually start Bazaar's payment activity.
4340 * @param request This contains some information about the product that we are going to purchase.
4441 * @return Completable that you can subscribe to it and it gets completed when purchase flow begins.
4542 */
46- fun Payment.purchaseProduct (activity : Activity , request : PurchaseRequest ): Completable {
43+ fun Payment.purchaseProduct (
44+ registry : ActivityResultRegistry ,
45+ request : PurchaseRequest
46+ ): Completable {
4747 return Completable .create { emitter ->
48- purchaseProduct(activity, request) {
49- purchaseFlowBegan { emitter.onComplete() }
50- failedToBeginFlow { emitter.onError(it) }
51- }
52- }
53- }
54-
55- /* *
56- * You can use this function to navigate user to Bazaar's payment activity to purchase a product.
57- * Note that for subscribing a product you have to use the 'subscribeProduct' function.
58- * @see subscribeProduct
59- * @param fragment We use this fragment instance to actually start Bazaar's payment activity.
60- * @param request This contains some information about the product that we are going to purchase.
61- * @return Completable that you can subscribe to it and it gets completed when purchase flow begins.
62- */
63- fun Payment.purchaseProduct (fragment : Fragment , request : PurchaseRequest ): Completable {
64- return Completable .create { emitter ->
65- purchaseProduct(fragment, request) {
48+ purchaseProduct(registry, request) {
6649 purchaseFlowBegan { emitter.onComplete() }
6750 failedToBeginFlow { emitter.onError(it) }
6851 }
@@ -92,30 +75,16 @@ fun Payment.consumeProduct(purchaseToken: String): Completable {
9275 * You can use this function to navigate user to Bazaar's payment activity to subscribe a product.
9376 * Note that for purchasing a product you have to use the 'purchaseProduct' function.
9477 * @see purchaseProduct
95- * @param activity We use this activity instance to actually start Bazaar's payment activity.
96- * @param request This contains some information about the product that we are going to subscribe.
97- * @return Completable that you can subscribe to it and it gets completed when purchase flow begins.
98- */
99- fun Payment.subscribeProduct (activity : Activity , request : PurchaseRequest ): Completable {
100- return Completable .create { emitter ->
101- subscribeProduct(activity, request) {
102- purchaseFlowBegan { emitter.onComplete() }
103- failedToBeginFlow { emitter.onError(it) }
104- }
105- }
106- }
107-
108- /* *
109- * You can use this function to navigate user to Bazaar's payment activity to subscribe a product.
110- * Note that for purchasing a product you have to use the 'purchaseProduct' function.
111- * @see purchaseProduct
112- * @param fragment We use this fragment instance to actually start Bazaar's payment activity.
78+ * @param registry We use this activityResultRegistry instance to actually start Bazaar's payment activity.
11379 * @param request This contains some information about the product that we are going to subscribe.
11480 * @return Completable that you can subscribe to it and it gets completed when purchase flow begins.
11581 */
116- fun Payment.subscribeProduct (fragment : Fragment , request : PurchaseRequest ): Completable {
82+ fun Payment.subscribeProduct (
83+ registry : ActivityResultRegistry ,
84+ request : PurchaseRequest
85+ ): Completable {
11786 return Completable .create { emitter ->
118- subscribeProduct(fragment , request) {
87+ subscribeProduct(registry , request) {
11988 purchaseFlowBegan { emitter.onComplete() }
12089 failedToBeginFlow { emitter.onError(it) }
12190 }
@@ -199,30 +168,4 @@ fun Payment.checkTrialSubscription(): Single<TrialSubscriptionInfo> {
199168 checkTrialSubscriptionFailed { emitter.onError(it) }
200169 }
201170 }
202- }
203-
204- /* *
205- * You have to use this function in order to check if user purchased or subscribed the product.
206- * Note that even if the purchase was successful, it's highly recommended to double check the
207- * purchase via Bazaar's REST API: http://developers.cafebazaar.ir/fa/docs/developer-api-v2-introduction/
208- * @param requestCode This is the request code that you've used when constructing PurchaseRequest.
209- * @see PurchaseRequest
210- * @param resultCode When you override 'onActivityResult' function in your activity or fragment
211- * you have access to this argument and it indicates if user canceled the purchase or not.
212- * @param data When you override 'onActivityResult' function in your activity or fragment
213- * you have access to this argument and it contains purchase data.
214- * @return Single that you can subscribe to it and get notified about the purchase state.
215- */
216- fun Payment.onActivityResult (
217- requestCode : Int ,
218- resultCode : Int ,
219- data : Intent ?
220- ): Single <PurchaseInfo > {
221- return Single .create { emitter ->
222- onActivityResult(requestCode, resultCode, data) {
223- purchaseSucceed { emitter.onSuccess(it) }
224- purchaseCanceled { emitter.onError(PurchaseCanceledException ()) }
225- purchaseFailed { emitter.onError(it) }
226- }
227- }
228- }
171+ }
0 commit comments