◎위챗 : speedseoul
PayPal can either watch the WebView in the application, and provide a native payment experience in place of a website or can be invoked directly by calling a simple function. You can integrate manually into the application using the super easy steps below.
The SDK needs some additional information about the application before it can be invoked. Provide the custom scheme setup in the AndroidManifest.xml, and the App Link Redirect URL, also setup in the AndroidManifest.xml
. Provide the Client ID, being used for this Application. The operating environment can also be set.
final PYPLCheckoutEnvironment pyplEnvironment = PYPLCheckoutEnvironment.getInstance();
pyplEnvironment.setkPYPLEnvironment(Environment.SANDBOX);
pyplEnvironment.setkPYPLUrlScheme("foobarstore");
//set the redurect uri, that has the assetLinks.json.
pyplEnvironment.setkPYPLRedirectURL("https://paypalmerchant.herokuapp.com/thankyou");
//set the client ID for the merchant
pyplEnvironment.setClientId("AX93NErgg-F0VeBQ6pNLwa2VKQdw3BnKDvBnasIe_pKoprQyz6NiSf6XS7I1Qtro-VD4GP-AJdjT0Uz4");
//set the user context. 'this' should be the activity from which the experience is being called.
pyplEnvironment.setkPYPLUserContext(this);
If the preceeding integration has collisions with code around the WebView, manual intercepting of the WebView is possible, as shown in these examples.
PYPLCheckout.getInstance().shouldOverrideUrlLoading(view, url);
method can be used to intercept the webView on any redirections to PayPal. This function returns a boolean that can be used on the shouldOverrideUrlLoading()
method in the webViewClient.
Example of WebView integration with your own webViewClient.
//MainActivity.class
//in your activity where you want to call the experience.
WebView webView = new WebView(this);
//SampleWebViewIntercept is your webViewClient.
webView.setWebViewClient(new SampleWebViewIntercept());
//SampleWebViewIntercept.class
public class SampleWebViewIntercept extends WebViewClient {
//include this for integrating with Checkout.js
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//this will load a script to handle the Checkout.js integration
PYPLCheckout.getInstance().loadScript(view);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
return PYPLCheckout.getInstance().shouldOverrideUrlLoading(view, url);
}
}
Start the PayPal Checkout Experience directly, by providing a Payment Token from the back office system.
Set a call back delegate, when the checkout has completed.
pyplEnvironment.setkCheckoutDelegate(new PYPLCheckoutDelegate() {
@Override
public void completeCheckout(HashMap<String,String> returnParams) {
//return params will contain all the required information for the merchant to finish the transaction
Log.i("CheckoutFinishedWith>>", returnParams.toString());
//here is a sample of what return params consists
/**
{
from_cart=true, returnUrl=https://sampleurl.com/checkouts/?from_cart=true&key=Key&token=EC-token&PayerID=payerID&opType=payment,
token=EC-token,
key=Key,
PayerID=payerID,
opType=payment
}
**/
}
});
Set additional params that you need to pass to PayPal. This is optional.
//every option should be a string with the key and value joined with a '='
String[] pyplParamsArray = {"useraction=commit"};
pyplEnvironment.setkPYPLQueryStringParameters(pyplParamsArray);
Start the experience with the checkout token.
PYPLCheckout.getInstance().startCheckoutWithECToken(MainActivity.this, "EC-1FP91222RL3429812");
The secure browser method, implements a sandboxed in app browser (Chrome Custom Tab), and eases the integration required for a developer. This path also implements the PayPal Risk library.
To disable the UI, simply set the webBrowserOnlyMode on the Environment object.
Example of WebView Intercept with no webViewClient.
final PYPLCheckoutEnvironment pyplEnvironment = PYPLCheckoutEnvironment.getInstance();
//this will ensure that the native UI is not called
pyplEnvironment.setkPYPLWebBrowserOnly(true);
PYPLCheckout.getInstance().interceptWebView(w, this);
Example of WebView Intercept with your own webViewClient.
//MainActivity.class
final PYPLCheckoutEnvironment pyplEnvironment = PYPLCheckoutEnvironment.getInstance();
//this will ensure that the native UI is not called
pyplEnvironment.setkPYPLWebBrowserOnly(true);
//in your activity where you want to call the experience.
WebView webView = new WebView(this);
//SampleWebViewIntercept is your webViewClient.
webView.setWebViewClient(new SampleWebViewIntercept());
//SampleWebViewIntercept.class
public class SampleWebViewIntercept extends WebViewClient {
//include this for integrating with Checkout.js
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
PYPLCheckout.getInstance().loadScript(view);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
return PYPLCheckout.getInstance().shouldOverrideUrlLoading(view, url);
}
}
Example of Direct Invocation
final PYPLCheckoutEnvironment pyplEnvironment = PYPLCheckoutEnvironment.getInstance();
//this will ensure that the native UI is not called
pyplEnvironment.setkPYPLWebBrowserOnly(true);
PYPLCheckout.getInstance().startCheckoutWithECToken(MainActivity.this, "EC-1FP91222