Skip to content

Commit

Permalink
test version
Browse files Browse the repository at this point in the history
  • Loading branch information
bulanyurij committed May 25, 2021
1 parent fc3f6dd commit be37883
Show file tree
Hide file tree
Showing 33 changed files with 312 additions and 423 deletions.
9 changes: 1 addition & 8 deletions Liqpay/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ android {

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
versionName "0.1.0"
}

buildTypes {
Expand All @@ -30,8 +26,5 @@ dependencies {
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}
24 changes: 0 additions & 24 deletions Liqpay/src/androidTest/java/ua/liqpay/ExampleInstrumentedTest.kt

This file was deleted.

5 changes: 5 additions & 0 deletions Liqpay/src/main/java/ua/liqpay/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ package ua.liqpay
*/
internal const val LIQPAY_URL_CHECKOUT = "https://www.liqpay.ua/api/3/checkout"

/**
* Current Liqpay API version.
*/
internal const val LIQPAY_API_VERSION = "3"

/**
* Privat24 application package name.
*/
Expand Down
146 changes: 78 additions & 68 deletions Liqpay/src/main/java/ua/liqpay/LiqPay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,107 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Looper
import org.json.JSONObject
import ua.liqpay.request.ErrorCode
import ua.liqpay.request.HttpRequest
import ua.liqpay.request.LIQPAY_API_URL_REQUEST
import ua.liqpay.utils.base64
import ua.liqpay.utils.getDeviceId
import ua.liqpay.utils.isNetworkAvailable
import ua.liqpay.utils.signature
import ua.liqpay.view.BUNDLE_LIQPAY_DATA
import ua.liqpay.view.BUNDLE_DATA
import ua.liqpay.view.LiqpayActivity
import java.io.IOException
import java.net.URLEncoder
import java.util.*
import kotlin.collections.HashMap

const val BROADCAST_RECEIVER_ACTION = "ua.liqpay.action"
internal const val LIQPAY_BROADCAST_RECEIVER_ACTION = "ua.liqpay.action"
internal const val LIQPAY_DATA_KEY = "data"
internal const val LIQPAY_SIGNATURE_KEY = "signature"

class LiqPay(private val context: Context,
private val callback: LiqpayCallback) {
class LiqPay(
private val context: Context,
private val callback: LiqpayCallback
) {

/**
* Start liqpay checkout
* Start checkout page.
*
* @param privateKey Secret access key for API
* @param publicKey Unique ID of your company in LiqPay system
* @param action Transaction type. Possible values: pay - payment,
* hold - amount of hold on sender's account, subscribe - regular payment,
* paydonate - donation, auth - card preauth
* @param amount Payment amount. For example: 5, 7.34
* @param currency Payment currency. Possible values: USD, EUR, RUB, UAH, BYN, KZT
* @param description Payment description
* @param orderId Unique purchase ID in your shop. Maximum length is 255 symbols.
* @param language Customer's language ru, uk, en
*
* More info: https://www.liqpay.ua/documentation/en/api/aquiring/checkout/doc
*
* @param privateKey Liqpay private key
* @param params Other params
*/
fun checkout(privateKey: String, params: Map<String, Any?> = hashMapOf()) {
val base64Data = JSONObject(params).toString().base64()
val signature = signature(base64Data, privateKey)
checkout(base64Data, signature)
}

fun checkout(
base64Data: String,
signature: String
privateKey: String,
publicKey: String,
action: String = "pay",
amount: Double,
currency: String = "UAH",
description: String,
orderId: String,
language: String = "uk"
) {
if (!isNetworkAvailable(context)) {
callback.onError(ErrorCode.FAIL_INTERNET_CONNECTION)
} else {
val liqPay = LiqPay(context, callback)
context.registerReceiver(
liqPay.eventReceiver,
IntentFilter(BROADCAST_RECEIVER_ACTION)
)
LiqpayActivity.start(context, base64Data, signature)
}
val params = hashMapOf(
"action" to action,
"amount" to amount,
"currency" to currency,
"description" to description,
"order_id" to orderId,
"language" to language
)
checkout(privateKey, publicKey, params)
}

fun api(
context: Context,
path: String,
params: HashMap<String, String?> = hashMapOf(),
privateKey: String
) {
val base64Data = JSONObject(params as Map<*, *>).toString().base64()
/**
* Start checkout page.
*
* @param privateKey Secret access key for API
* @param publicKey Unique ID of your company in LiqPay system
* @param params Custom API params. Info: https://www.liqpay.ua/documentation/en/api/aquiring/checkout/doc
*
*/
fun checkout(privateKey: String, publicKey: String, params: Map<String, Any?> = hashMapOf()) {
val tempMap = params.toMutableMap().apply {
put("public_key", publicKey)
if (!containsKey("version")) {
put("version", LIQPAY_API_VERSION)
}
}
val base64Data = JSONObject(tempMap).toString().base64()
val signature = signature(base64Data, privateKey)
api(context, path, base64Data, signature)
checkout(base64Data, signature)
}

private fun api(
context: Context,
path: String,
base64Data: String?,
signature: String?
/**
* Start checkout page.
*
* @param base64Data Checkout data encoded by the base64
* @param signature The unique signature of each request base64_encode(sha1(private_key + data + private_key))
*
*/
fun checkout(
base64Data: String,
signature: String
) {
if (!isNetworkAvailable(context)) {
callback.onError(ErrorCode.FAIL_INTERNET_CONNECTION)
} else if (Looper.myLooper() == Looper.getMainLooper()) {
callback.onError(ErrorCode.NEED_NON_UI_THREAD)
} else {
val postParams = HashMap<String, String?>()
postParams["data"] = base64Data
postParams["signature"] = signature
try {
val request = HttpRequest()
val resp = request.post(LIQPAY_API_URL_REQUEST + path, postParams)
callback.onSuccess(resp)
} catch (e: IOException) {
e.printStackTrace()
callback.onError(ErrorCode.OTHER)
}
}
val liqPay = LiqPay(context, callback)
context.registerReceiver(
liqPay.eventReceiver,
IntentFilter(LIQPAY_BROADCAST_RECEIVER_ACTION)
)
LiqpayActivity.start(context, base64Data, signature)
}

/**
* Result event receiver.
*/
private val eventReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == BROADCAST_RECEIVER_ACTION) {
val response = intent.getStringExtra("data")
if (intent.action == LIQPAY_BROADCAST_RECEIVER_ACTION) {
val response = intent.getStringExtra(BUNDLE_DATA)
if (response.isNullOrEmpty()) {
callback.onError(ErrorCode.CHECKOUT_CANCELED)
callback.onCancel()
} else {
callback.onSuccess(response)
}
Expand Down
9 changes: 6 additions & 3 deletions Liqpay/src/main/java/ua/liqpay/LiqpayCallback.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ua.liqpay

import ua.liqpay.request.ErrorCode


/**
* Liqpay result callback.
*/
interface LiqpayCallback {

fun onSuccess(response: String?)

fun onError(error: ErrorCode)
fun onError()

fun onCancel()
}
4 changes: 0 additions & 4 deletions Liqpay/src/main/java/ua/liqpay/request/Api.kt

This file was deleted.

8 changes: 0 additions & 8 deletions Liqpay/src/main/java/ua/liqpay/request/ErrorCode.kt

This file was deleted.

103 changes: 0 additions & 103 deletions Liqpay/src/main/java/ua/liqpay/request/HttpRequest.kt

This file was deleted.

6 changes: 0 additions & 6 deletions Liqpay/src/main/java/ua/liqpay/request/Method.kt

This file was deleted.

8 changes: 0 additions & 8 deletions Liqpay/src/main/java/ua/liqpay/request/Request.kt

This file was deleted.

Loading

0 comments on commit be37883

Please sign in to comment.