Skip to content

Commit

Permalink
Update launcher code
Browse files Browse the repository at this point in the history
  • Loading branch information
warmkesselj committed Sep 16, 2024
1 parent 0bded90 commit 705fc56
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,9 @@ private void launchPayPalEditFIVault(String editVaultId) {

if (result instanceof PayPalVaultEditResult.ReadyToLaunch) {
PayPalVaultEditResult.ReadyToLaunch success = (PayPalVaultEditResult.ReadyToLaunch) result;
String correlationId = success.getRiskCorrelationId();

EditFIAgreementSetup response = success.getResponse();

//TODO: Launcher? and Analytics
payPalLauncher.launch(requireActivity(), response.getApprovalURL(), "https://mobile-sdk-demo-site-838cead5d3ab.herokuapp.com/");
//TODO: Analytics
payPalLauncher.launch(requireActivity(), success);
}
});
}
Expand All @@ -194,7 +191,7 @@ private void launchEditErrorRequest(String editVaultId, String riskCorrelationId
EditFIAgreementSetup response = success.getResponse();

//TODO: Launcher? and Analytics
payPalLauncher.launch(requireActivity(), response.getApprovalURL(), "https://mobile-sdk-demo-site-838cead5d3ab.herokuapp.com/");
//payPalLauncher.launch(requireActivity(), response.getApprovalURL(), "https://mobile-sdk-demo-site-838cead5d3ab.herokuapp.com/");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.braintreepayments.api.paypal.vaultedit.PayPalVaultEditAuthCallback
import com.braintreepayments.api.paypal.vaultedit.PayPalVaultEditAuthResult
import com.braintreepayments.api.paypal.vaultedit.PayPalVaultEditCallback
import com.braintreepayments.api.paypal.vaultedit.PayPalVaultEditRequest
import com.braintreepayments.api.paypal.vaultedit.PayPalVaultEditResult
import com.braintreepayments.api.paypal.vaultedit.PayPalVaultErrorHandlingEditRequest
import com.braintreepayments.api.sharedutils.Json
import org.json.JSONException
Expand Down Expand Up @@ -97,8 +98,7 @@ class PayPalClient @VisibleForTesting internal constructor(
internalPayPalClient.sendRequest(
context,
payPalRequest
) { payPalResponse: PayPalPaymentAuthRequestParams?,
error: Exception? ->
) { payPalResponse: PayPalPaymentAuthRequestParams?, error: Exception? ->
if (payPalResponse != null) {
payPalContextId = payPalResponse.pairingId
try {
Expand Down Expand Up @@ -149,6 +149,18 @@ class PayPalClient @VisibleForTesting internal constructor(
.metadata(metadata)
}

@Throws(JSONException::class)
private fun buildBrowserSwitchOptionsForEditFI(
approvalUrl: String
): BrowserSwitchOptions {

return BrowserSwitchOptions()
.requestCode(BraintreeRequestCodes.PAYPAL.code)
.appLinkUri(braintreeClient.appLinkReturnUri)
.url(Uri.parse(approvalUrl))
.launchAsNewTask(braintreeClient.launchesBrowserSwitchAsNewTask())
}

/**
* After receiving a result from the PayPal web authentication flow via
* [PayPalLauncher.handleReturnToAppFromBrowser],
Expand Down Expand Up @@ -237,8 +249,14 @@ class PayPalClient @VisibleForTesting internal constructor(
internalPayPalClient.sendVaultEditRequest(
context,
payPalVaultEditRequest,
callback
)
{ result ->

if (result is PayPalVaultEditResult.ReadyToLaunch) {
result.browserSwitchOptions = buildBrowserSwitchOptionsForEditFI(result.response.approvalURL)
}

callback.onPayPalVaultEditResult(result)
});
}

/**
Expand All @@ -257,9 +275,15 @@ class PayPalClient @VisibleForTesting internal constructor(
callback: PayPalVaultEditCallback
) {
internalPayPalClient.sendVaultEditErrorRequest(
payPalVaultErrorHandlingEditRequest,
callback
)
payPalVaultErrorHandlingEditRequest,
{ result ->

if (result is PayPalVaultEditResult.ReadyToLaunch) {
result.browserSwitchOptions = buildBrowserSwitchOptionsForEditFI(result.response.approvalURL)
}

callback.onPayPalVaultEditResult(result)
});
}

@Throws(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,17 @@ internal class PayPalInternalClient(
fun sendVaultEditRequest(
context: Context,
payPalVaultEditRequest: PayPalVaultEditRequest,
callback: PayPalVaultEditCallback,
riskCorrelationId: String? = null
callback: PayPalVaultEditCallback
) {
if (riskCorrelationId != null) {
if (payPalVaultEditRequest.riskCorrelationId != null) {
sendVaultEditRequestWithRiskCorrelationId(
context,
payPalVaultEditRequest,
riskCorrelationId,
payPalVaultEditRequest.riskCorrelationId,
callback
)
} else {
getClientMetadataId(riskCorrelationId, context) { clientMetadataId ->
getClientMetadataId(payPalVaultEditRequest.riskCorrelationId, context) { clientMetadataId ->
if (clientMetadataId == null) {
val result = PayPalVaultEditResult.Failure(BraintreeException("Could not retrieve clientMetaDataId"))
callback.onPayPalVaultEditResult(result)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.braintreepayments.api.paypal

import android.content.Intent
import android.net.Uri
import androidx.activity.ComponentActivity
import com.braintreepayments.api.BrowserSwitchClient
import com.braintreepayments.api.BrowserSwitchException
import com.braintreepayments.api.BrowserSwitchFinalResult
import com.braintreepayments.api.BrowserSwitchOptions
import com.braintreepayments.api.BrowserSwitchStartResult
import com.braintreepayments.api.core.BraintreeException
import com.braintreepayments.api.core.BraintreeRequestCodes
import com.braintreepayments.api.core.ExperimentalBetaApi
import com.braintreepayments.api.paypal.vaultedit.PayPalVaultEditResult

/**
* Responsible for launching PayPal user authentication in a web browser
Expand Down Expand Up @@ -52,18 +51,19 @@ class PayPalLauncher internal constructor(private val browserSwitchClient: Brows
}
}

@OptIn(ExperimentalBetaApi::class)
fun launch(
activity: ComponentActivity,
approvalUrl: String,
appLinkUri: String,
result: PayPalVaultEditResult.ReadyToLaunch
) {

val browserSwitchOptions = BrowserSwitchOptions()
.requestCode(BraintreeRequestCodes.PAYPAL.code)
.appLinkUri(Uri.parse(appLinkUri))
.url(Uri.parse(approvalUrl))

browserSwitchClient.start(activity, browserSwitchOptions)
result.browserSwitchOptions?.let { options ->
when (val request = browserSwitchClient.start(activity, options)) {
is BrowserSwitchStartResult.Failure -> PayPalPendingRequest.Failure(request.error)
is BrowserSwitchStartResult.Started -> PayPalPendingRequest.Started(request.pendingRequest)
}
} ?: run {
PayPalPendingRequest.Failure(BraintreeException("BrowserSwitchOptions is null"))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.braintreepayments.api.core.ExperimentalBetaApi
@ExperimentalBetaApi
data class PayPalVaultEditRequest(
val editPayPalVaultId: String,
val merchantAccountId: String? = null,
val riskCorrelationId: String? = null
) {
val hermesPath: String = "v1/paypal_hermes/generate_edit_fi_url"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.braintreepayments.api.paypal.vaultedit

import com.braintreepayments.api.BrowserSwitchOptions
import com.braintreepayments.api.core.ExperimentalBetaApi

@ExperimentalBetaApi
Expand All @@ -12,7 +13,8 @@ sealed class PayPalVaultEditResult {
*/
class ReadyToLaunch internal constructor(
val riskCorrelationId: String,
val response: EditFIAgreementSetup
val response: EditFIAgreementSetup,
internal var browserSwitchOptions: BrowserSwitchOptions? = null
) : PayPalVaultEditResult()

/**
Expand Down

0 comments on commit 705fc56

Please sign in to comment.