/
MainActivity.kt
111 lines (97 loc) · 3.84 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.alsatpardakht.alsatipgandroidkotlinexample
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.alsatpardakht.alsatipgandroid.AlsatIPG
import com.alsatpardakht.alsatipgcore.domain.model.PaymentType
class MainActivity : AppCompatActivity() {
private val API = "ENTER YOUR API KEY HERE"
private val paymentType = PaymentType.Mostaghim
private val alsatIPG = AlsatIPG.getInstance()
private lateinit var logTextView: TextView
private lateinit var signPaymentButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
logTextView = findViewById(R.id.logTextView)
signPaymentButton = findViewById(R.id.signPaymentButton)
signPaymentButton.setOnClickListener {
signPaymentButtonOnClick()
}
observeToPaymentSignStatus()
observeToPaymentValidationStatus()
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.data?.let { data ->
log("intent and Uri is not null")
when (paymentType) {
PaymentType.Mostaghim -> alsatIPG.validationMostaghim(API, data)
PaymentType.Vaset -> alsatIPG.validationVaset(API, data)
}
} ?: log("intent or Uri is null")
}
private fun observeToPaymentSignStatus() {
alsatIPG.paymentSignStatus.observe(this) { paymentSignResult ->
when {
paymentSignResult.isSuccessful -> {
log("payment Sign Success url = ${paymentSignResult.url}")
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(paymentSignResult.url))
startActivity(intent)
}
paymentSignResult.isLoading -> {
log("payment Sign Loading ...")
}
else -> {
log("payment Sign error = ${paymentSignResult.error?.message}")
}
}
}
}
private fun observeToPaymentValidationStatus() {
alsatIPG.paymentValidationStatus.observe(this) { paymentValidationResult ->
when {
paymentValidationResult.isSuccessful -> {
log("payment Validation Success data = ${paymentValidationResult.data}")
if (
(paymentValidationResult.data?.PSP?.IsSuccess == true) &&
(paymentValidationResult.data?.VERIFY?.IsSuccess == true)
) {
log("money transferred")
} else {
log("money has not been transferred")
}
}
paymentValidationResult.isLoading -> {
log("payment Validation Loading ...")
}
else -> {
log("payment Validation error = ${paymentValidationResult.error?.message}")
}
}
}
}
private fun signPaymentButtonOnClick() {
when (paymentType) {
PaymentType.Mostaghim -> alsatIPG.signMostaghim(
Api = API,
Amount = 10_000,
InvoiceNumber = "123456",
RedirectAddress = "http://www.example.com/some_path"
)
PaymentType.Vaset -> alsatIPG.signVaset(
Api = API,
Amount = 20_000,
RedirectAddress = "http://www.example.com/some_path",
Tashim = emptyList(),
InvoiceNumber = "123456"
)
}
}
private fun log(message: String) {
logTextView.text = "${logTextView.text}$message\n"
}
}