Skip to content

Commit 071680b

Browse files
committed
⏺ Add callback-based start function with PayPalWebCheckoutCallback interface
- Create PayPalWebCheckoutCallback fun interface following CardApproveOrderCallback pattern - Add overloaded start() function accepting PayPalWebCheckoutCallback
1 parent 387de57 commit 071680b

File tree

9 files changed

+253
-130
lines changed

9 files changed

+253
-130
lines changed

CorePayments/src/main/java/com/paypal/android/corepayments/APIClientError.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import androidx.annotation.RestrictTo
99
object APIClientError {
1010

1111
// 0. An unknown error occurred.
12-
fun unknownError(correlationId: String?) = PayPalSDKError(
12+
fun unknownError(correlationId: String? = null, throwable: Throwable? = null) = PayPalSDKError(
1313
code = PayPalSDKErrorCode.UNKNOWN.ordinal,
1414
errorDescription = "An unknown error occurred. Contact developer.paypal.com/support.",
15+
reason = throwable,
1516
correlationId = correlationId
1617
)
1718

@@ -91,4 +92,16 @@ object APIClientError {
9192
)
9293
return error
9394
}
95+
96+
fun payPalSDKError(
97+
code: Int,
98+
errorDescription: String?,
99+
correlationId: String? = null,
100+
reason: Throwable? = null
101+
): PayPalSDKError = PayPalSDKError(
102+
code = code,
103+
errorDescription = errorDescription.orEmpty(),
104+
correlationId = correlationId,
105+
reason = reason
106+
)
94107
}

CorePayments/src/main/java/com/paypal/android/corepayments/PayPalSDKErrorCode.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ enum class PayPalSDKErrorCode {
1515
SERVER_RESPONSE_ERROR,
1616
CHECKOUT_ERROR,
1717
NATIVE_CHECKOUT_ERROR,
18-
GRAPHQL_JSON_INVALID_ERROR
18+
GRAPHQL_JSON_INVALID_ERROR,
19+
NO_ACCESS_TOKEN_ERROR,
1920
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.paypal.android.corepayments.api
2+
3+
import androidx.annotation.RestrictTo
4+
import com.paypal.android.corepayments.APIClientError
5+
import com.paypal.android.corepayments.APIClientError.payPalSDKError
6+
import com.paypal.android.corepayments.APIRequest
7+
import com.paypal.android.corepayments.CoreConfig
8+
import com.paypal.android.corepayments.HttpMethod
9+
import com.paypal.android.corepayments.PayPalSDKErrorCode
10+
import com.paypal.android.corepayments.RestClient
11+
import com.paypal.android.corepayments.base64encoded
12+
import org.json.JSONObject
13+
14+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
15+
class AuthenticationSecureTokenServiceAPI(
16+
private val coreConfig: CoreConfig,
17+
private val restClient: RestClient = RestClient(configuration = coreConfig),
18+
) {
19+
suspend fun getClientToken(): GetClientTokenResult {
20+
val requestBody = "grant_type=client_credentials&response_type=token"
21+
22+
val credentials = "${coreConfig.clientId}:"
23+
val headers = mutableMapOf(
24+
"Authorization" to "Basic ${credentials.base64encoded()}",
25+
"Content-Type" to "application/x-www-form-urlencoded"
26+
)
27+
28+
val apiRequest = APIRequest(
29+
path = "v1/oauth2/token",
30+
method = HttpMethod.POST,
31+
body = requestBody,
32+
headers = headers
33+
)
34+
35+
return runCatching {
36+
val httpResponse = restClient.send(apiRequest)
37+
val correlationId = httpResponse.headers["paypal-debug-id"]
38+
if (httpResponse.isSuccessful && httpResponse.body != null) {
39+
val jsonObject = JSONObject(httpResponse.body)
40+
val token = jsonObject.optString("access_token")
41+
if (token.isNotEmpty()) {
42+
GetClientTokenResult.Success(token)
43+
} else {
44+
val error = payPalSDKError(
45+
code = PayPalSDKErrorCode.NO_ACCESS_TOKEN_ERROR.ordinal,
46+
errorDescription = "Missing access_token in response",
47+
correlationId = correlationId
48+
)
49+
GetClientTokenResult.Failure(error)
50+
}
51+
} else {
52+
val error = httpResponse.run {
53+
payPalSDKError(
54+
code = status,
55+
errorDescription = body,
56+
correlationId = correlationId
57+
)
58+
}
59+
GetClientTokenResult.Failure(error)
60+
}
61+
}.getOrElse { throwable ->
62+
val error = APIClientError.unknownError(throwable = throwable)
63+
GetClientTokenResult.Failure(error)
64+
}
65+
}
66+
}

CorePayments/src/main/java/com/paypal/android/corepayments/api/FetchClientToken.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.paypal.android.corepayments.api
2+
3+
import androidx.annotation.RestrictTo
4+
import com.paypal.android.corepayments.PayPalSDKError
5+
6+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
7+
sealed class GetClientTokenResult {
8+
/**
9+
* The request to get client token was successful.
10+
*
11+
* @property token the access token from the response
12+
*/
13+
data class Success(val token: String) : GetClientTokenResult()
14+
15+
/**
16+
* There was an error with the request to get client token.
17+
*
18+
* @property error the error that occurred
19+
*/
20+
data class Failure(val error: PayPalSDKError) : GetClientTokenResult()
21+
}

0 commit comments

Comments
 (0)