Skip to content

Commit cf9bece

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 cf9bece

File tree

9 files changed

+250
-162
lines changed

9 files changed

+250
-162
lines changed

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

Lines changed: 2 additions & 29 deletions
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

@@ -37,12 +38,6 @@ object APIClientError {
3738
correlationId = correlationId
3839
)
3940

40-
// 4. There was an error constructing the URLRequest.
41-
val invalidUrlRequest = PayPalSDKError(
42-
code = PayPalSDKErrorCode.INVALID_URL_REQUEST.ordinal,
43-
errorDescription = "An error occurred constructing an HTTP request. Contact developer.paypal.com/support."
44-
)
45-
4641
// 5. The server's response body returned an error message.
4742
fun serverResponseError(correlationId: String?) = PayPalSDKError(
4843
code = PayPalSDKErrorCode.SERVER_RESPONSE_ERROR.ordinal,
@@ -58,28 +53,6 @@ object APIClientError {
5853
correlationId = correlationId
5954
)
6055

61-
val payPalCheckoutError: (description: String) -> PayPalSDKError = { description ->
62-
PayPalSDKError(
63-
code = PayPalSDKErrorCode.CHECKOUT_ERROR.ordinal,
64-
errorDescription = description
65-
)
66-
}
67-
68-
val payPalNativeCheckoutError: (description: String, reason: Exception) -> PayPalSDKError =
69-
{ description, reason ->
70-
PayPalSDKError(
71-
code = PayPalSDKErrorCode.NATIVE_CHECKOUT_ERROR.ordinal,
72-
errorDescription = description,
73-
reason = reason
74-
)
75-
}
76-
77-
fun clientIDNotFoundError(code: Int, correlationId: String?) = PayPalSDKError(
78-
code = code,
79-
errorDescription = "Error fetching clientId. Contact developer.paypal.com/support.",
80-
correlationId = correlationId
81-
)
82-
8356
fun graphQLJSONParseError(correlationId: String?, reason: Exception): PayPalSDKError {
8457
val message =
8558
"An error occurred while parsing the GraphQL response JSON. Contact developer.paypal.com/support."

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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.APIRequest
6+
import com.paypal.android.corepayments.CoreConfig
7+
import com.paypal.android.corepayments.HttpMethod
8+
import com.paypal.android.corepayments.PayPalSDKError
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 createLowScopedAccessToken(): CreateLowScopedAccessTokenResult {
20+
val requestBody = "grant_type=client_credentials&response_type=token"
21+
22+
val headers = mutableMapOf(
23+
"Authorization" to "Basic ${coreConfig.clientId.base64encoded()}:",
24+
"Content-Type" to "application/x-www-form-urlencoded"
25+
)
26+
27+
val apiRequest = APIRequest(
28+
path = "v1/oauth2/token",
29+
method = HttpMethod.POST,
30+
body = requestBody,
31+
headers = headers
32+
)
33+
34+
return runCatching {
35+
val httpResponse = restClient.send(apiRequest)
36+
val correlationId = httpResponse.headers["paypal-debug-id"]
37+
if (httpResponse.isSuccessful && httpResponse.body != null) {
38+
val jsonObject = JSONObject(httpResponse.body)
39+
val token = jsonObject.optString("access_token")
40+
if (token.isNotEmpty()) {
41+
CreateLowScopedAccessTokenResult.Success(token)
42+
} else {
43+
val error = PayPalSDKError(
44+
code = PayPalSDKErrorCode.NO_ACCESS_TOKEN_ERROR.ordinal,
45+
errorDescription = "Missing access_token in response",
46+
correlationId = correlationId
47+
)
48+
CreateLowScopedAccessTokenResult.Failure(error)
49+
}
50+
} else {
51+
val error = httpResponse.run {
52+
PayPalSDKError(
53+
code = status,
54+
errorDescription = body ?: "Unknown error",
55+
correlationId = correlationId
56+
)
57+
}
58+
CreateLowScopedAccessTokenResult.Failure(error)
59+
}
60+
}.getOrElse { throwable ->
61+
val error = APIClientError.unknownError(throwable = throwable)
62+
CreateLowScopedAccessTokenResult.Failure(error)
63+
}
64+
}
65+
}
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 CreateLowScopedAccessTokenResult {
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) : CreateLowScopedAccessTokenResult()
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) : CreateLowScopedAccessTokenResult()
21+
}

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)