Skip to content

Commit

Permalink
Merge pull request #36 from cafebazaar/dev
Browse files Browse the repository at this point in the history
Release 4.0.1
  • Loading branch information
hamid97m authored Apr 30, 2023
2 parents 887d651 + 780af57 commit 3d28018
Show file tree
Hide file tree
Showing 38 changed files with 523 additions and 121 deletions.
5 changes: 3 additions & 2 deletions BazaarPay/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'org.jetbrains.kotlin.android'
id 'kotlin-parcelize'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id 'com.google.devtools.ksp'
id 'maven-publish'
}

Expand Down Expand Up @@ -43,6 +43,7 @@ android {
buildFeatures {
viewBinding true
dataBinding true
buildConfig true
}
}

Expand All @@ -69,7 +70,7 @@ dependencies {
implementation libs.squareup.retrofit.core
implementation libs.squareup.retrofit.core
implementation libs.glide.core
kapt libs.glide.compiler
ksp libs.glide.compiler
implementation libs.dotsIndicator
implementation(libs.loggingInterceptor) {
exclude group: 'org.json', module: 'json'
Expand Down
1 change: 1 addition & 0 deletions BazaarPay/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class ir.cafebazaar.bazaarpay.** { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.os.PersistableBundle
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.navigation.findNavController
import androidx.navigation.navOptions
import ir.cafebazaar.bazaarpay.arg.BazaarPayActivityArgs
import ir.cafebazaar.bazaarpay.databinding.ActivityBazaarPayBinding
import ir.cafebazaar.bazaarpay.utils.bindWithRTLSupport
import java.util.Locale

class BazaarPayActivity : AppCompatActivity(), FinishCallbacks {
Expand All @@ -25,7 +25,7 @@ class BazaarPayActivity : AppCompatActivity(), FinishCallbacks {
override fun onCreate(savedInstanceState: Bundle?) {
initNightMode()
super.onCreate(savedInstanceState)
binding = ActivityBazaarPayBinding.inflate(layoutInflater)
binding = layoutInflater.bindWithRTLSupport(ActivityBazaarPayBinding::inflate)
setContentView(binding.root)

args = intent.getParcelableExtra(BAZAARPAY_ACTIVITY_ARGS)
Expand Down Expand Up @@ -99,6 +99,7 @@ class BazaarPayActivity : AppCompatActivity(), FinishCallbacks {
R.id.open_paymentThankYouPageFragment
)
}

isDirectDebitActivationIntent(intent) -> {
findNavController(R.id.nav_host_fragment_bazaar_pay).navigate(
resId = R.id.open_payment_methods,
Expand Down Expand Up @@ -143,6 +144,7 @@ class BazaarPayActivity : AppCompatActivity(), FinishCallbacks {

contextWithCorrectTheme.createConfigurationContext(config)
}

else -> {
config.setLocale(newLocale)
contextWithCorrectTheme.createConfigurationContext(config)
Expand All @@ -158,9 +160,11 @@ class BazaarPayActivity : AppCompatActivity(), FinishCallbacks {
uiMode == Configuration.UI_MODE_NIGHT_UNDEFINED -> {
contextResource.configuration.uiMode
}

isDarkMode() -> {
Configuration.UI_MODE_NIGHT_YES
}

else -> {
Configuration.UI_MODE_NIGHT_NO
}
Expand Down Expand Up @@ -224,6 +228,7 @@ class BazaarPayActivity : AppCompatActivity(), FinishCallbacks {
}

companion object {

const val BAZAARPAY_ACTIVITY_ARGS = "bazaarpayActivityArgs"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ir.cafebazaar.bazaarpay
import android.content.Context
import ir.cafebazaar.bazaarpay.data.bazaar.models.ErrorModel
import ir.cafebazaar.bazaarpay.data.payment.PaymentRepository
import ir.cafebazaar.bazaarpay.data.payment.models.pay.InitCheckoutResult
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PurchaseStatus
import ir.cafebazaar.bazaarpay.extensions.fold

Expand Down Expand Up @@ -48,3 +49,34 @@ suspend fun trace(
ifFailure = onFailure
)
}

/**
* Init the [checkoutToken] in order to initiate a purchase flow.
*
* @param context the context in which tracing happens.
* @param amount the amount of the purchase in Rials. Notice that this has some limitations which you should consider based on BazaarPay documentation.
* @param destination the destination of the purchase which you should get it from the BazaarPay team.
* @param serviceName the serviceName of the purchase which you should get it from the BazaarPay team.
* @param onSuccess the callback when init checkout payment successfully offering its [InitCheckoutResult].
* @param onFailure the callback for an unsuccessful initiating of checkout with [ErrorModel] to reason about the cause.
*/
suspend fun initCheckout(
context: Context,
amount: Long,
destination: String,
serviceName: String,
onSuccess: (InitCheckoutResult) -> Unit,
onFailure: (ErrorModel) -> Unit
) {
initSDKForAPICall(
context = context,
checkoutToken = ""
)
val payRepository: PaymentRepository = ServiceLocator.get()
payRepository.initCheckout(amount, destination, serviceName).fold(
ifSuccess = {
onSuccess.invoke(it)
},
ifFailure = onFailure
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package ir.cafebazaar.bazaarpay
* @property isInDarkMode enables *Dark Mode* for the UI elements of the payment flow, which are in *Light Mode* by default.
* @property phoneNumber the default phone number to pre-fill the login screen's input field. It uses a `null` value by default, resulting in no pre-filled input.
*/
data class BazaarPayOptions(
class BazaarPayOptions(
val checkoutToken: String,
val isInDarkMode: Boolean = false,
val phoneNumber: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import ir.cafebazaar.bazaarpay.data.payment.api.PaymentService
import ir.cafebazaar.bazaarpay.data.payment.models.getpaymentmethods.PaymentMethodsInfo
import ir.cafebazaar.bazaarpay.data.payment.models.getpaymentmethods.request.GetPaymentMethodsRequest
import ir.cafebazaar.bazaarpay.data.payment.models.merchantinfo.MerchantInfo
import ir.cafebazaar.bazaarpay.data.payment.models.pay.InitCheckoutResult
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PayResult
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PurchaseStatus
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.CommitRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.InitCheckoutRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.PayRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.TraceRequest
import ir.cafebazaar.bazaarpay.extensions.ServiceType
Expand Down Expand Up @@ -97,6 +99,19 @@ internal class PaymentRemoteDataSource {
}
}
}
suspend fun initCheckout(
amount: Long,
destination: String,
serviceName: String
): Either<InitCheckoutResult> {
return withContext(globalDispatchers.iO) {
return@withContext safeApiCall(ServiceType.BAZAARPAY) {
paymentService.initCheckout(
InitCheckoutRequest(amount, destination, serviceName)
).toInitCheckoutResult()
}
}
}

private companion object {
private val increaseBalanceRedirectUrl =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ir.cafebazaar.bazaarpay.data.payment
import ir.cafebazaar.bazaarpay.ServiceLocator
import ir.cafebazaar.bazaarpay.data.payment.models.getpaymentmethods.PaymentMethodsInfo
import ir.cafebazaar.bazaarpay.data.payment.models.merchantinfo.MerchantInfo
import ir.cafebazaar.bazaarpay.data.payment.models.pay.InitCheckoutResult
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PayResult
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PurchaseStatus
import ir.cafebazaar.bazaarpay.extensions.fold
Expand Down Expand Up @@ -49,4 +50,19 @@ internal class PaymentRepository {
}
)
}

suspend fun initCheckout(
amount: Long,
destination: String,
serviceName: String
): Either<InitCheckoutResult> {
return paymentRemoteDataSource.initCheckout(amount, destination, serviceName).fold(
ifSuccess = {
Either.Success(it)
},
ifFailure = {
Either.Failure(it)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package ir.cafebazaar.bazaarpay.data.payment.api
import ir.cafebazaar.bazaarpay.data.payment.models.getpaymentmethods.request.GetPaymentMethodsRequest
import ir.cafebazaar.bazaarpay.data.payment.models.getpaymentmethods.response.PaymentMethodsInfoDto
import ir.cafebazaar.bazaarpay.data.payment.models.merchantinfo.response.MerchantInfoDto
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PurchaseStatus
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.CommitRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.InitCheckoutRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.PayRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.request.TraceRequest
import ir.cafebazaar.bazaarpay.data.payment.models.pay.response.InitCheckoutResponse
import ir.cafebazaar.bazaarpay.data.payment.models.pay.response.PayResponse
import ir.cafebazaar.bazaarpay.data.payment.models.pay.response.TraceResponse
import okhttp3.ResponseBody
Expand Down Expand Up @@ -45,6 +46,11 @@ internal interface PaymentService {
@Body traceRequest: TraceRequest
): TraceResponse

@POST("checkout/init/")
suspend fun initCheckout(
@Body initCheckoutRequest: InitCheckoutRequest
): InitCheckoutResponse

companion object {
const val PAY_ENDPOINT_LANG = "lang"
const val CHECKOUT_TOKEN_LABEL = "checkout_token"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ir.cafebazaar.bazaarpay.data.payment.models.pay

data class InitCheckoutResult(
val checkoutToken: String,
val paymentUrl: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ir.cafebazaar.bazaarpay.data.payment.models.pay.request

import com.google.gson.annotations.SerializedName

internal data class InitCheckoutRequest(
@SerializedName("amount") val amount: Long,
@SerializedName("destination") val destination: String,
@SerializedName("service_name") val serviceName: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ir.cafebazaar.bazaarpay.data.payment.models.pay.response

import com.google.gson.annotations.SerializedName
import ir.cafebazaar.bazaarpay.data.payment.models.PaymentBaseResponse
import ir.cafebazaar.bazaarpay.data.payment.models.pay.InitCheckoutResult
import ir.cafebazaar.bazaarpay.data.payment.models.pay.PayResult

internal data class InitCheckoutResponse(
@SerializedName("checkout_token") val checkoutToken: String,
@SerializedName("payment_url") val paymentUrl: String
): PaymentBaseResponse() {

fun toInitCheckoutResult(): InitCheckoutResult {
return InitCheckoutResult(checkoutToken, paymentUrl)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import ir.cafebazaar.bazaarpay.FinishCallbacks
import ir.cafebazaar.bazaarpay.R
import ir.cafebazaar.bazaarpay.ServiceLocator
import ir.cafebazaar.bazaarpay.ServiceLocator.PHONE_NUMBER
import ir.cafebazaar.bazaarpay.models.Resource
import ir.cafebazaar.bazaarpay.models.ResourceState
import ir.cafebazaar.bazaarpay.databinding.FragmentRegisterBinding
import ir.cafebazaar.bazaarpay.data.bazaar.account.models.getotptoken.WaitingTimeWithEnableCall
import ir.cafebazaar.bazaarpay.data.bazaar.models.InvalidPhoneNumberException
import ir.cafebazaar.bazaarpay.databinding.FragmentRegisterBinding
import ir.cafebazaar.bazaarpay.extensions.fromHtml
import ir.cafebazaar.bazaarpay.extensions.getReadableErrorMessage
import ir.cafebazaar.bazaarpay.extensions.gone
Expand All @@ -31,6 +29,9 @@ import ir.cafebazaar.bazaarpay.extensions.isLandscape
import ir.cafebazaar.bazaarpay.extensions.isValidPhoneNumber
import ir.cafebazaar.bazaarpay.extensions.navigateSafe
import ir.cafebazaar.bazaarpay.extensions.setSafeOnClickListener
import ir.cafebazaar.bazaarpay.models.Resource
import ir.cafebazaar.bazaarpay.models.ResourceState
import ir.cafebazaar.bazaarpay.utils.bindWithRTLSupport

internal class RegisterFragment : Fragment() {

Expand All @@ -50,11 +51,7 @@ internal class RegisterFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentRegisterBinding.inflate(
inflater,
container,
false
)
_binding = inflater.bindWithRTLSupport(FragmentRegisterBinding::inflate, container)
return binding.root
}

Expand Down Expand Up @@ -178,6 +175,7 @@ internal class RegisterFragment : Fragment() {
handleSuccess(resource.data)
}
}

ResourceState.Error -> {
val message = if (resource.failure is InvalidPhoneNumberException) {
getString(R.string.bazaarpay_wrong_phone_number)
Expand All @@ -186,6 +184,7 @@ internal class RegisterFragment : Fragment() {
}
showError(message)
}

ResourceState.Loading -> handleLoading()
else -> Throwable("Illegal State in handleResourceState")
}
Expand Down
Loading

0 comments on commit 3d28018

Please sign in to comment.