Skip to content

Commit f179fb8

Browse files
authored
Release 2.0.0 (#64)
* Update poolakey version to 2.0.0 * refactor runOnCommunicator fun (#60) * Refactor getting payment result (#63) * Update fragment and appcompat version * Increase compileSdkVersion to 31
1 parent c5fef1e commit f179fb8

16 files changed

Lines changed: 251 additions & 510 deletions

File tree

app/src/main/java/ir/cafebazaar/poolakeysample/MainActivity.kt

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ir.cafebazaar.poolakeysample
22

3-
import android.content.Intent
43
import android.os.Bundle
54
import android.widget.Toast
65
import androidx.annotation.StringRes
@@ -49,8 +48,7 @@ class MainActivity : AppCompatActivity() {
4948
if (paymentConnection.getState() == ConnectionState.Connected) {
5049
purchaseProduct(
5150
productId = skuValueInput.text.toString(),
52-
requestCode = PURCHASE_REQUEST_CODE,
53-
payload = "payload",
51+
payload = "purchasePayload",
5452
dynamicPriceToken = dynamicPriceToken.text.toString()
5553
)
5654
}
@@ -59,8 +57,7 @@ class MainActivity : AppCompatActivity() {
5957
if (paymentConnection.getState() == ConnectionState.Connected) {
6058
subscribeProduct(
6159
productId = skuValueInput.text.toString(),
62-
requestCode = SUBSCRIBE_REQUEST_CODE,
63-
payload = "",
60+
payload = "subscribePayload",
6461
dynamicPriceToken = dynamicPriceToken.text.toString()
6562
)
6663
}
@@ -83,18 +80,17 @@ class MainActivity : AppCompatActivity() {
8380

8481
private fun subscribeProduct(
8582
productId: String,
86-
requestCode: Int,
8783
payload: String,
8884
dynamicPriceToken: String?
8985
) {
86+
val purchaseRequest = PurchaseRequest(
87+
productId = skuValueInput.text.toString(),
88+
payload = payload,
89+
dynamicPriceToken = dynamicPriceToken
90+
)
9091
payment.subscribeProduct(
91-
activity = this@MainActivity,
92-
request = PurchaseRequest(
93-
productId = skuValueInput.text.toString(),
94-
requestCode = SUBSCRIBE_REQUEST_CODE,
95-
payload = "",
96-
dynamicPriceToken = dynamicPriceToken
97-
)
92+
registry = activityResultRegistry,
93+
request = purchaseRequest
9894
) {
9995
purchaseFlowBegan {
10096
toast(R.string.general_purchase_flow_began_message)
@@ -103,25 +99,35 @@ class MainActivity : AppCompatActivity() {
10399
// bazaar need to update, in this case we only launch purchase without discount
104100
if (it is DynamicPriceNotSupportedException) {
105101
toast(R.string.general_purchase_failed_dynamic_price_token_message)
106-
subscribeProduct(productId, requestCode, payload, null)
102+
subscribeProduct(productId, payload, null)
107103
} else {
108104
toast(R.string.general_purchase_failed_message)
109105
}
110106
}
107+
purchaseSucceed {
108+
toast(R.string.general_purchase_succeed_message)
109+
if (consumeSwitch.isChecked) {
110+
consumePurchasedItem(it.purchaseToken)
111+
}
112+
}
113+
purchaseCanceled {
114+
toast(R.string.general_purchase_cancelled_message)
115+
}
116+
purchaseFailed {
117+
toast(R.string.general_purchase_failed_message)
118+
}
111119
}
112120
}
113121

114122
private fun purchaseProduct(
115123
productId: String,
116-
requestCode: Int,
117124
payload: String,
118125
dynamicPriceToken: String?
119126
) {
120127
payment.purchaseProduct(
121-
activity = this,
128+
registry = activityResultRegistry,
122129
request = PurchaseRequest(
123130
productId = productId,
124-
requestCode = requestCode,
125131
payload = payload,
126132
dynamicPriceToken = dynamicPriceToken
127133
)
@@ -133,11 +139,23 @@ class MainActivity : AppCompatActivity() {
133139
// bazaar need to update, in this case we only launch purchase without discount
134140
if (it is DynamicPriceNotSupportedException) {
135141
toast(R.string.general_purchase_failed_dynamic_price_token_message)
136-
purchaseProduct(productId, requestCode, payload, null)
142+
purchaseProduct(productId, payload, null)
137143
} else {
138144
toast(R.string.general_purchase_failed_message)
139145
}
140146
}
147+
purchaseSucceed {
148+
toast(R.string.general_purchase_succeed_message)
149+
if (consumeSwitch.isChecked) {
150+
consumePurchasedItem(it.purchaseToken)
151+
}
152+
}
153+
purchaseCanceled {
154+
toast(R.string.general_purchase_cancelled_message)
155+
}
156+
purchaseFailed {
157+
toast(R.string.general_purchase_failed_message)
158+
}
141159
}
142160
}
143161

@@ -221,24 +239,6 @@ class MainActivity : AppCompatActivity() {
221239
}
222240
}
223241

224-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
225-
super.onActivityResult(requestCode, resultCode, data)
226-
payment.onActivityResult(requestCode, resultCode, data) {
227-
purchaseSucceed {
228-
toast(R.string.general_purchase_succeed_message)
229-
if (consumeSwitch.isChecked) {
230-
consumePurchasedItem(it.purchaseToken)
231-
}
232-
}
233-
purchaseCanceled {
234-
toast(R.string.general_purchase_cancelled_message)
235-
}
236-
purchaseFailed {
237-
toast(R.string.general_purchase_failed_message)
238-
}
239-
}
240-
}
241-
242242
private fun consumePurchasedItem(purchaseToken: String) {
243243
payment.consumeProduct(purchaseToken) {
244244
consumeSucceed {
@@ -262,10 +262,4 @@ class MainActivity : AppCompatActivity() {
262262
paymentConnection.disconnect()
263263
super.onDestroy()
264264
}
265-
266-
companion object {
267-
268-
private const val PURCHASE_REQUEST_CODE = 1000
269-
private const val SUBSCRIBE_REQUEST_CODE = 1001
270-
}
271265
}

poolakey-rx/src/main/java/ir/cafebazaar/poolakey/rx/PaymentRx.kt

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package 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
64
import io.reactivex.Completable
75
import io.reactivex.Observable
86
import io.reactivex.Single
@@ -12,7 +10,6 @@ import ir.cafebazaar.poolakey.entity.PurchaseInfo
1210
import ir.cafebazaar.poolakey.entity.SkuDetails
1311
import ir.cafebazaar.poolakey.entity.TrialSubscriptionInfo
1412
import 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

Comments
 (0)