Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
manjav committed Jul 29, 2021
2 parents f8e5b80 + f9da9a0 commit a4ce56f
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 46 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="https://github.com/manjav/PoolakeyUnitySdk/blob/main/images/Poolakey-unity.jpg?raw=true"/><br/>
<img src="https://github.com/cafebazaar/PoolakeyUnitySdk/blob/main/images/Poolakey-unity.jpg?raw=true"/><br/>

[........... راهنمــــــای فـــــــارسی ..........](https://github.com/manjav/PoolakeyUnitySdk/blob/main/README_FA.md)
[........... راهنمــــــای فـــــــارسی ..........](https://github.com/cafebazaar/PoolakeyUnitySdk/blob/main/README_FA.md)


### Unity SDK for [Poolakey](https://github.com/cafebazaar/Poolakey) ( New Cafebazaar IAP ).<br/><br/>
Expand All @@ -14,4 +14,4 @@ var consumeResult = await payment.Consume(purchaseResult.data.purchaseToken);
```
<br/><br/>
## How to use?
For more information regarding the usage of Poolakey, please check out the [wiki](https://github.com/manjav/PoolakeyUnitySdk/wiki) page.
For more information regarding the usage of Poolakey, please check out the [wiki](https://github.com/cafebazaar/PoolakeyUnitySdk/wiki) page.
6 changes: 3 additions & 3 deletions README_FA.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="https://github.com/manjav/PoolakeyUnitySdk/blob/main/images/Poolakey-unity.jpg?raw=true"/><br/>
<img src="https://github.com/cafebazaar/PoolakeyUnitySdk/blob/main/images/Poolakey-unity.jpg?raw=true"/><br/>

### [<center>........... English Readme ..........</center>](https://github.com/manjav/PoolakeyUnitySdk/blob/main/README.md)
### [<center>........... English Readme ..........</center>](https://github.com/cafebazaar/PoolakeyUnitySdk/blob/main/README.md)
<br/>

## <div dir="rtl">پولکی (کتابخانه جدید و ساده پرداخت درون‌برنامه کافه بازار)</div><br/>
Expand All @@ -13,4 +13,4 @@ var consumeResult = await payment.Consume(purchaseResult.data.purchaseToken);
```
<br/><br/>

## [<center>پیاده سازی گام به گام کتابخانه</center>](https://github.com/manjav/PoolakeyUnitySdk/wiki/%DB%B1.-%D9%86%D8%B5%D8%A8-SDK)
## [<center>پیاده سازی گام به گام کتابخانه</center>](https://github.com/cafebazaar/PoolakeyUnitySdk/wiki/وارد-کردن-پکیج-یونیتی)
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {


dependencies {
implementation "com.github.cafebazaar.Poolakey:poolakey:1.1.0-beta02"
implementation "com.github.cafebazaar.Poolakey:poolakey:1.2.2"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Expand All @@ -61,7 +61,7 @@ task createJar(type: Copy) {
task updateAAR(type: Copy) {
from('build/outputs/aar')
into('../unity/Assets/Poolakey/Plugins/')
include(project.name + '.aar')
include(project.name + '-release.aar')
}

project.tasks.whenTaskAdded { Task theTask ->
Expand Down
43 changes: 31 additions & 12 deletions android/src/main/java/com/farsitel/bazaar/PaymentActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import com.farsitel.bazaar.callback.PaymentCallback
import ir.cafebazaar.poolakey.exception.DynamicPriceNotSupportedException
import ir.cafebazaar.poolakey.request.PurchaseRequest
import java.security.InvalidParameterException

class PaymentActivity : FragmentActivity() {
private var productId: String? = null
private var payload: String? = null
private var dynamicPriceToken: String? = null
private var command: Command? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -23,18 +25,30 @@ class PaymentActivity : FragmentActivity() {
}
}

private fun initArgs() {
productId = intent.extras?.getString(KEY_PRODUCT_ID)
payload = intent.extras?.getString(KEY_PAYLOAD)
dynamicPriceToken = intent.extras?.getString(KEY_DYNAMIC_PRICE_TOKEN)
command = Command.valueOf(requireNotNull(intent.extras?.getString(KEY_COMMAND)))
}

private fun purchaseProduct() {
PoolakeyKotlinBridge.payment.purchaseProduct(
this@PaymentActivity,
PurchaseRequest(productId!!, REQUEST_CODE, payload)
PurchaseRequest(productId!!, REQUEST_CODE, payload, dynamicPriceToken)
) {
purchaseFlowBegan {
// Bazaar's billing screen has opened successfully
paymentCallback?.onStart()
}
failedToBeginFlow { throwable ->
// Failed to open Bazaar's billing screen
paymentCallback?.onFailure(throwable.message, throwable.stackTrace.joinToString("\n"))
if (throwable is DynamicPriceNotSupportedException) {
dynamicPriceToken = null
purchaseProduct()
} else {
paymentCallback?.onFailure(throwable.message, throwable.stackTrace.joinToString("\n"))
}
finish()
}
}
Expand All @@ -43,26 +57,28 @@ class PaymentActivity : FragmentActivity() {
private fun subscribeProduct() {
PoolakeyKotlinBridge.payment.subscribeProduct(
this@PaymentActivity,
PurchaseRequest(productId!!, REQUEST_CODE, payload)
PurchaseRequest(productId!!, REQUEST_CODE, payload, dynamicPriceToken)
) {
purchaseFlowBegan {
// Bazaar's billing screen has opened successfully
paymentCallback?.onStart()
}
failedToBeginFlow { throwable ->
// Failed to open Bazaar's billing screen
paymentCallback?.onFailure(throwable.message, throwable.stackTrace.joinToString("\n"))
finish()
if (throwable is DynamicPriceNotSupportedException) {
dynamicPriceToken = null
subscribeProduct()
} else {
paymentCallback?.onFailure(
throwable.message,
throwable.stackTrace.joinToString("\n")
)
finish()
}
}
}
}

private fun initArgs() {
productId = intent.extras?.getString(KEY_PRODUCT_ID)
payload = intent.extras?.getString(KEY_PAYLOAD)
command = Command.valueOf(requireNotNull(intent.extras?.getString(KEY_COMMAND)))
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
PoolakeyKotlinBridge.payment.onActivityResult(requestCode, resultCode, data) {
Expand Down Expand Up @@ -103,6 +119,7 @@ class PaymentActivity : FragmentActivity() {
private const val REQUEST_CODE: Int = 1000
private const val KEY_PRODUCT_ID = "productId"
private const val KEY_PAYLOAD = "payload"
private const val KEY_DYNAMIC_PRICE_TOKEN = "dynamicPriceToken"
private const val KEY_COMMAND = "command"
var paymentCallback: PaymentCallback? = null

Expand All @@ -112,12 +129,14 @@ class PaymentActivity : FragmentActivity() {
command: Command,
productId: String,
callback: PaymentCallback,
payload: String?
payload: String?,
dynamicPriceToken: String?
) {
paymentCallback = callback
val intent = Intent(activity, PaymentActivity::class.java)
intent.putExtra(KEY_PRODUCT_ID, productId)
intent.putExtra(KEY_PAYLOAD, payload)
intent.putExtra(KEY_DYNAMIC_PRICE_TOKEN, dynamicPriceToken)
intent.putExtra(KEY_COMMAND, command.name)
activity.startActivity(intent)
}
Expand Down
14 changes: 10 additions & 4 deletions android/src/main/java/com/farsitel/bazaar/PoolakeyBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import com.farsitel.bazaar.callback.ConnectionCallback;
import com.farsitel.bazaar.callback.ConsumeCallback;
import com.farsitel.bazaar.callback.OwnedProductsCallback;
import com.farsitel.bazaar.callback.PaymentCallback;
import com.farsitel.bazaar.callback.SKUDetailsCallback;

import java.lang.reflect.Field;
import java.util.Arrays;

public class PoolakeyBridge {
public static final String TAG = "PoolakeyBridge";
Expand Down Expand Up @@ -59,15 +61,19 @@ public void disconnect() {
PoolakeyKotlinBridge.INSTANCE.disconnect();
}

public void getSkuDetails(String type, String productId, SKUDetailsCallback callback) {
PoolakeyKotlinBridge.INSTANCE.getSkuDetails(type, productId, callback);
public void getSkuDetails(String type, String productIds, SKUDetailsCallback callback) {
PoolakeyKotlinBridge.INSTANCE.getSkuDetails(type, Arrays.asList(productIds.split(",")), callback);
}

public void purchase(String type, String productId, String payload, PaymentCallback callback) {
public void getOwnedProducts(String type, OwnedProductsCallback callback) {
PoolakeyKotlinBridge.INSTANCE.getOwnedProducts(type, callback);
}

public void purchase(String type, String productId, String payload, String dynamicPriceToken, PaymentCallback callback) {
PaymentActivity.Command cmd = PaymentActivity.Command.PurchaseProduct;
if (!type.equalsIgnoreCase("inApp"))
cmd = PaymentActivity.Command.Subscribe;
PoolakeyKotlinBridge.INSTANCE.startActivity(getCurrentActivity(), cmd, callback, productId, payload);
PoolakeyKotlinBridge.INSTANCE.startActivity(getCurrentActivity(), cmd, callback, productId, payload, dynamicPriceToken);
}

public void consume(String token, ConsumeCallback callback) {
Expand Down
43 changes: 34 additions & 9 deletions android/src/main/java/com/farsitel/bazaar/PoolakeyKotlinBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.farsitel.bazaar

import android.app.Activity
import android.content.Context
import com.farsitel.bazaar.callback.ConnectionCallback
import com.farsitel.bazaar.callback.ConsumeCallback
import com.farsitel.bazaar.callback.PaymentCallback
import com.farsitel.bazaar.callback.SKUDetailsCallback
import com.farsitel.bazaar.callback.*
import ir.cafebazaar.poolakey.Connection
import ir.cafebazaar.poolakey.ConnectionState
import ir.cafebazaar.poolakey.Payment
Expand Down Expand Up @@ -40,31 +37,58 @@ object PoolakeyKotlinBridge {
}


fun getSkuDetails(type: String, productId: String, callback: SKUDetailsCallback) {
fun getSkuDetails(type: String, productIds: List<String>, callback: SKUDetailsCallback) {
if (connection.getState() != ConnectionState.Connected) {
callback.onFailure("Connection not found.", "In order to getting ske details, connect to Poolakey!")
return
}
when (type) {
"inApp" ->
payment.getInAppSkuDetails(skuIds = listOf(productId)) {
payment.getInAppSkuDetails(skuIds = productIds) {
getSkuDetailsSucceed(callback::onSuccess)
getSkuDetailsFailed{ throwable -> callback.onFailure(throwable.message, throwable.stackTrace.joinToString { "\n" }) }
}
else ->
payment.getSubscriptionSkuDetails(skuIds = listOf(productId)) {
payment.getSubscriptionSkuDetails(skuIds = productIds) {
getSkuDetailsSucceed(callback::onSuccess)
getSkuDetailsFailed{ throwable -> callback.onFailure(throwable.message, throwable.stackTrace.joinToString { "\n" }) }
}
}
}

fun getOwnedProducts(type:String, callback: OwnedProductsCallback) {
if (connection.getState() != ConnectionState.Connected) {
callback.onFailure("Connection not found.", "In order to getting purchases, connect to Poolakey!")
return
}
when (type) {
"inApp" -> payment.getPurchasedProducts {
querySucceed(callback::onSuccess)
queryFailed { throwable ->
callback.onFailure(
throwable.message,
throwable.stackTrace.joinToString { "\n" })
}
}
else ->
payment.getSubscribedProducts {
querySucceed(callback::onSuccess)
queryFailed { throwable ->
callback.onFailure(
throwable.message,
throwable.stackTrace.joinToString { "\n" })
}
}
}
}

fun startActivity(
activity: Activity,
command: PaymentActivity.Command,
callback: PaymentCallback,
productId: String,
payload: String
payload: String?,
dynamicPriceToken: String?
) {
if (connection.getState() != ConnectionState.Connected) {
callback.onFailure("Connection not found.", "In order to purchasing, connect to Poolakey!")
Expand All @@ -75,7 +99,8 @@ object PoolakeyKotlinBridge {
command,
productId,
callback,
payload
payload,
dynamicPriceToken
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.farsitel.bazaar.callback;

import java.util.List;

import ir.cafebazaar.poolakey.entity.PurchaseInfo;
import ir.cafebazaar.poolakey.entity.SkuDetails;

public interface OwnedProductsCallback {
void onSuccess(List<PurchaseInfo> data);
void onFailure(String message, String stackTrace);
}
Loading

0 comments on commit a4ce56f

Please sign in to comment.