Last active
March 24, 2021 12:55
-
-
Save AlaaZarifa/a5624be0d1ddb7e8f967fc12dede1205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentController(private val context: Activity, private val order: Order) { | |
companion object { | |
private const val API_KEY = "sk_test_baYdLKFA1rtjMscDkECBVq7Z" | |
} | |
private val sdkSession by lazy { SDKSession() } | |
fun initPayment(onSuccess: () -> Unit) { | |
GoSellSDK.init(context, API_KEY, BuildConfig.APPLICATION_ID) | |
GoSellSDK.setLocale("ar") | |
ThemeObject.getInstance().setAppearanceMode(AppearanceMode.FULLSCREEN_MODE).sdkLanguage = "en" | |
sdkSession.instantiatePaymentDataSource() | |
sdkSession.setTransactionCurrency(TapCurrency("SAR")) | |
sdkSession.transactionMode = TransactionMode.PURCHASE | |
sdkSession.setCustomer(getCustomer()) | |
sdkSession.setAmount(BigDecimal(order.totalPrice.toDouble())) | |
val list: ArrayList<PaymentItem> = arrayListOf() | |
sdkSession.setPaymentItems(list) | |
sdkSession.isUserAllowedToSaveCard(false) | |
sdkSession.setPaymentType("WEB") | |
sdkSession.setTaxes(ArrayList()) | |
sdkSession.setShipping(ArrayList()) | |
sdkSession.setPostURL("") | |
sdkSession.setPaymentDescription("") | |
sdkSession.setPaymentMetadata(HashMap()) | |
sdkSession.setPaymentReference(null) | |
sdkSession.setPaymentStatementDescriptor("") | |
sdkSession.isRequires3DSecure(true) | |
sdkSession.setReceiptSettings(null) | |
sdkSession.setAuthorizeAction(null) | |
sdkSession.setMerchantID(null) | |
sdkSession.setDefaultCardHolderName("TEST") | |
sdkSession.addSessionDelegate(object : SessionDelegate { | |
override fun paymentSucceed(charge: Charge) { | |
charge.toString().log() | |
onSuccess() | |
} | |
override fun paymentFailed(charge: Charge?) { | |
charge?.response.toString().log() | |
context.showErrorAlert("فشلت عملية الدفع، حاول مرة أخرى") | |
} | |
override fun authorizationSucceed(authorize: Authorize) { | |
authorize.toString().log() | |
} | |
override fun authorizationFailed(authorize: Authorize?) { | |
context.showErrorAlert("فشلت عملية الدفع، حاول مرة أخرى") | |
authorize.toString().log() | |
} | |
override fun cardSaved(charge: Charge) { | |
} | |
override fun cardSavingFailed(charge: Charge) { | |
} | |
override fun cardTokenizedSuccessfully(token: Token) { | |
} | |
override fun savedCardsList(cardsList: CardsList) { | |
} | |
override fun sdkError(goSellError: GoSellError?) { | |
goSellError?.errorCode?.log() | |
goSellError?.errorMessage?.log() | |
goSellError?.errorBody?.log() | |
goSellError?.throwable?.printStackTrace() | |
context.showErrorAlert("فشلت عملية الدفع، حاول مرة أخرى") | |
} | |
override fun sessionIsStarting() { | |
} | |
override fun sessionHasStarted() { | |
} | |
override fun sessionCancelled() { | |
} | |
override fun sessionFailedToStart() { | |
} | |
override fun invalidCardDetails() { | |
} | |
override fun backendUnknownError(message: String?) { | |
context.showErrorAlert("فشلت عملية الدفع، حاول مرة أخرى") | |
} | |
override fun invalidTransactionMode() { | |
} | |
override fun invalidCustomerID() { | |
} | |
override fun userEnabledSaveCardOption(saveCardEnabled: Boolean) { | |
} | |
}) | |
sdkSession.start(context) | |
} | |
private fun getCustomer(): Customer? { | |
val phoneNumber = PhoneNumber("966", order.mobile) | |
return Customer.CustomerBuilder("") | |
.email("abc@abc.com") | |
.firstName(order.name) | |
.lastName("") | |
.metadata("") | |
.phone(PhoneNumber(phoneNumber?.countryCode, phoneNumber.number)) | |
.middleName("").build() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment