braintree / popup-bridge-android

PopupBridge allows WebViews to open popup windows in a browser and send data back to the WebView
MIT License
53 stars 28 forks source link

Getting "Error: Order could not be captured" on webview #29

Open allanascano-otus opened 4 years ago

allanascano-otus commented 4 years ago

I think I may have a real head-scratcher with this one.

Im trying to integrate PayPal Smart Payment Buttons on my android app.

I found out that for the integration to work, I need to use this library.

Before integrating, I created a quick html to test the paypal smart buttons. Here is the file https://treats4them.s3.amazonaws.com/android_paypal.html

The html source : `<!DOCTYPE html>

`

Everything works on this html page when viewing it on a PC browser. Sadly, when I try to open the same html on my android webview, I get the error - "Error: Order could not be captured" when attempting to submit a payment.

The paypal API was loaded corrrectly on the webview because I got a success callback on my javascript function. Its when clicking the Pay Now button on the paypal popup that results to this error.

Here is my android integration:

AndroidManifest.xml `
<activity android:name="com.treats4themdev.android.PayPalActivity" android:theme="@style/AppTheme.NoActionBar" android:configChanges="orientation|screenSize" />

    <activity android:name="com.braintreepayments.popupbridge.PopupBridgeActivity"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="${applicationId}.popupbridge" />
        </intent-filter>

`

PayPalActivity.java ` package com.treats4themdev.android;

import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.webkit.JavascriptInterface; import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

import com.braintreepayments.popupbridge.PopupBridge; import com.braintreepayments.popupbridge.PopupBridgeNavigationListener; import com.paypal.android.sdk.data.collector.PayPalDataCollector;

public class PayPalActivity extends AppCompatActivity implements PopupBridgeNavigationListener {

private WebView mWebView;
private PopupBridge mPopupBridge;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_paypal);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
    mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    WebView.setWebContentsDebuggingEnabled(true);

    mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

    mPopupBridge = PopupBridge.newInstance(this, mWebView);
    mPopupBridge.setNavigationListener(this);

    //mWebView.loadUrl("file:///android_asset/android_paypal.html");
    mWebView.loadUrl("https://treats4them.s3.amazonaws.com/android_paypal.html");
}

public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void paymentSuccess(String msg) {
        Log.d("MyApplication", "success!!!! " + msg);
    }
}

@Override
public void onUrlOpened(String url) {
    String id = "";
    Uri uri = Uri.parse(url);

    String checkoutId = uri.getQueryParameter("token");
    String vaultId = uri.getQueryParameter("ba_token");

    if (!TextUtils.isEmpty(checkoutId)) {
        id = checkoutId;
    } else if (!TextUtils.isEmpty(vaultId)) {
        id = vaultId;
    }

    Log.d("result", PayPalDataCollector.getClientMetadataId(this, id));
}

}

`

Here is the screenshot on the emulator - Screenshot_1597985763

Also tried it on my android phone and I got the same error. But when using the Debit or Credit card button instead of the Pay with PayPal button , I was able to submit a payment and it came back with status COMPLETED.

I also tried loading the html file locally but I still get the same error. Hoping someone can help me on this. Thanks in advance.

allanascano-otus commented 4 years ago

I still cant solve this problem. If anyone have an idea please help. Thanks.

sshropshire commented 4 years ago

Hello @allanascano-otus thank you for using the Braintree SDK for Android. We're currently looking into this issue and hope to have an update soon.

allanascano-otus commented 4 years ago

I updated to the latest version but Im still getting the same error. But I noticed that after updating the library version and rebuilding my app, on first run after clicking Pay with Paypal button, there was no popup but instead an inline popup was shown: success

The transaction was successful with this inline popup. But the succeeding checkouts opens a popup instead and I still get the same Order could not be captured errors.

Is there a way to force the inline popup after clicking Pay with Paypal?

dredd00 commented 3 years ago

I'm having the same issue as well.