Skip to content

Commit 172817b

Browse files
wwwdataHMobileClub
authored andcommitted
BrowserSwitch fixes from PR
pikaju#140
1 parent 5cd6dd3 commit 172817b

File tree

9 files changed

+174
-81
lines changed

9 files changed

+174
-81
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ dependencies {
3333

3434
#### PayPal / Venmo / 3D Secure
3535

36+
##### With Drop-In
37+
3638
In order for this plugin to support PayPal, Venmo or 3D Secure payments, you must allow for the
3739
browser switch by adding an intent filter to your `AndroidManifest.xml` (inside the `<application>` body):
3840

@@ -53,6 +55,30 @@ browser switch by adding an intent filter to your `AndroidManifest.xml` (inside
5355
**Important:** Your app's URL scheme must begin with your app's package ID and end with `.braintree`. For example, if the Package ID is `com.your-company.your-app`, then your URL scheme should be `com.your-company.your-app.braintree`. `${applicationId}` is automatically applied with your app's package when using Gradle.
5456
**Note:** The scheme you define must use all lowercase letters. If your package contains underscores, the underscores should be removed when specifying the scheme in your Android Manifest.
5557

58+
##### Without Drop-In
59+
60+
If you want to use PayPal without Drop-In, you need to declare another intent filter to your `AndroidManifest.xml` (inside the `<application>` body):
61+
62+
```xml
63+
<activity android:name="com.example.flutter_braintree.FlutterBraintreeCustom"
64+
android:theme="@style/bt_transparent_activity" android:exported="true"
65+
android:launchMode="singleInstance">
66+
<intent-filter>
67+
<action android:name="android.intent.action.VIEW" />
68+
<category android:name="android.intent.category.DEFAULT" />
69+
<category android:name="android.intent.category.BROWSABLE" />
70+
<data android:scheme="${applicationId}.return.from.braintree" />
71+
</intent-filter>
72+
</activity>
73+
<activity android:name="com.braintreepayments.api.ThreeDSecureActivity" android:theme="@style/Theme.AppCompat.Light" android:exported="true">
74+
</activity>
75+
```
76+
77+
Please note that intent filter scheme is different from drop-in's one.
78+
79+
**Important:** Your app's URL scheme must begin with your app's package ID and end with `.return.from.braintree`. For example, if the Package ID is `com.your-company.your-app`, then your URL scheme should be `com.your-company.your-app.return.from.braintree`. `${applicationId}` is automatically applied with your app's package when using Gradle.
80+
**Note:** The scheme you define must use all lowercase letters. If your package contains underscores, the underscores should be removed when specifying the scheme in your Android Manifest.
81+
5682
#### Google Pay
5783

5884
Add the wallet enabled meta-data tag to your `AndroidManifest.xml` (inside the `<application>` body):

android/src/main/java/com/example/flutter_braintree/FlutterBraintreeCustom.java

+50-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.example.flutter_braintree;
2+
import static com.braintreepayments.api.PayPalCheckoutRequest.USER_ACTION_COMMIT;
3+
import static com.braintreepayments.api.PayPalCheckoutRequest.USER_ACTION_DEFAULT;
4+
25
import androidx.annotation.NonNull;
6+
import androidx.annotation.Nullable;
37
import androidx.appcompat.app.AppCompatActivity;
48

59
import android.content.Intent;
610
import android.os.Bundle;
11+
import android.util.Log;
712

813
import com.braintreepayments.api.BraintreeClient;
914
import com.braintreepayments.api.Card;
@@ -14,6 +19,7 @@
1419
import com.braintreepayments.api.PayPalCheckoutRequest;
1520
import com.braintreepayments.api.PayPalClient;
1621
import com.braintreepayments.api.PayPalListener;
22+
import com.braintreepayments.api.PayPalPaymentIntent;
1723
import com.braintreepayments.api.PayPalRequest;
1824
import com.braintreepayments.api.PayPalVaultRequest;
1925
import com.braintreepayments.api.PaymentMethodNonce;
@@ -26,14 +32,19 @@ public class FlutterBraintreeCustom extends AppCompatActivity implements PayPalL
2632
private BraintreeClient braintreeClient;
2733
private PayPalClient payPalClient;
2834
private Boolean started = false;
35+
private long creationTimestamp = -1;
2936

3037
@Override
3138
protected void onCreate(Bundle savedInstanceState) {
3239
super.onCreate(savedInstanceState);
40+
41+
creationTimestamp = System.currentTimeMillis();
42+
3343
setContentView(R.layout.activity_flutter_braintree_custom);
3444
try {
3545
Intent intent = getIntent();
36-
braintreeClient = new BraintreeClient(this, intent.getStringExtra("authorization"));
46+
String returnUrlScheme = (getPackageName() + ".return.from.braintree").replace("_", "").toLowerCase();
47+
braintreeClient = new BraintreeClient(this, intent.getStringExtra("authorization"), returnUrlScheme);
3748
String type = intent.getStringExtra("type");
3849
if (type.equals("tokenizeCreditCard")) {
3950
tokenizeCreditCard();
@@ -59,16 +70,15 @@ protected void onNewIntent(Intent newIntent) {
5970
setIntent(newIntent);
6071
}
6172

62-
@Override
63-
protected void onStart() {
64-
super.onStart();
65-
66-
}
73+
// @Override
74+
// protected void onStart() {
75+
// super.onStart();
76+
// }
6777

68-
@Override
69-
protected void onResume() {
70-
super.onResume();
71-
}
78+
// @Override
79+
// protected void onResume() {
80+
// super.onResume();
81+
// }
7282

7383
protected void tokenizeCreditCard() {
7484
Intent intent = getIntent();
@@ -104,6 +114,32 @@ protected void requestPaypalNonce() {
104114
// Checkout flow
105115
PayPalCheckoutRequest checkOutRequest = new PayPalCheckoutRequest(intent.getStringExtra("amount"));
106116
checkOutRequest.setCurrencyCode(intent.getStringExtra("currencyCode"));
117+
checkOutRequest.setDisplayName(intent.getStringExtra("displayName"));
118+
checkOutRequest.setBillingAgreementDescription(intent.getStringExtra("billingAgreementDescription"));
119+
120+
String userAction;
121+
switch (intent.getStringExtra("payPalPaymentUserAction")) {
122+
case "commit":
123+
userAction = USER_ACTION_COMMIT;
124+
break;
125+
default:
126+
userAction = USER_ACTION_DEFAULT;
127+
}
128+
checkOutRequest.setUserAction(userAction);
129+
130+
String paymentIntent;
131+
switch (intent.getStringExtra("payPalPaymentIntent")) {
132+
case "order":
133+
paymentIntent = PayPalPaymentIntent.ORDER;
134+
break;
135+
case "sale":
136+
paymentIntent = PayPalPaymentIntent.SALE;
137+
break;
138+
default:
139+
paymentIntent = PayPalPaymentIntent.AUTHORIZE;
140+
}
141+
checkOutRequest.setIntent(paymentIntent);
142+
107143
payPalClient.tokenizePayPalAccount(this, checkOutRequest);
108144
}
109145
}
@@ -149,12 +185,14 @@ public void onPayPalSuccess(@NonNull PayPalAccountNonce payPalAccountNonce) {
149185
@Override
150186
public void onPayPalFailure(@NonNull Exception error) {
151187
if (error instanceof UserCanceledException) {
152-
if(((UserCanceledException) error).isExplicitCancelation()){
188+
if(((UserCanceledException) error).isExplicitCancelation() || System.currentTimeMillis() - creationTimestamp > 500)
189+
{
190+
// PayPal sometimes sends a UserCanceledException early for no reason: filter it out
191+
// Otherwise take every cancellation event
153192
onCancel();
154193
}
155194
} else {
156195
onError(error);
157196
}
158-
159197
}
160198
}

example/android/app/src/main/AndroidManifest.xml

+19-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
<application
1010
android:icon="@mipmap/ic_launcher">
1111
<activity
12-
android:name="io.flutter.embedding.android.FlutterActivity"
12+
android:name=".MainActivity"
1313
android:theme="@style/LaunchTheme"
1414
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
1515
android:hardwareAccelerated="true"
16-
android:windowSoftInputMode="adjustResize">
16+
android:windowSoftInputMode="adjustResize"
17+
android:exported="true"
18+
android:launchMode="singleInstance">
1719
<!-- This keeps the window background of the activity showing
1820
until Flutter renders its first frame. It can be removed if
1921
there is no splash screen (such as the default splash screen
@@ -22,8 +24,8 @@
2224
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
2325
android:value="true" />
2426
<intent-filter>
25-
<action android:name="android.intent.action.MAIN"/>
26-
<category android:name="android.intent.category.LAUNCHER"/>
27+
<action android:name="android.intent.action.MAIN" />
28+
<category android:name="android.intent.category.LAUNCHER" />
2729
</intent-filter>
2830
</activity>
2931

@@ -36,15 +38,24 @@
3638
<data android:scheme="com.example.flutterbraintreeexample.braintree" />
3739
</intent-filter>
3840
</activity>
39-
<activity android:name="com.braintreepayments.api.ThreeDSecureActivity" android:exported="true">
41+
<activity android:name="com.braintreepayments.api.ThreeDSecureActivity"
42+
android:exported="true">
4043
</activity>
41-
<activity android:name="com.example.flutter_braintree.FlutterBraintreeCustom" android:theme="@style/Theme.AppCompat.Light" android:exported="true">
44+
<activity android:name="com.example.flutter_braintree.FlutterBraintreeCustom"
45+
android:theme="@style/bt_transparent_activity" android:exported="true"
46+
android:launchMode="singleInstance">
47+
<intent-filter>
48+
<action android:name="android.intent.action.VIEW" />
49+
<category android:name="android.intent.category.DEFAULT" />
50+
<category android:name="android.intent.category.BROWSABLE" />
51+
<data android:scheme="com.example.flutterbraintreeexample.return.from.braintree" />
52+
</intent-filter>
4253
</activity>
4354

44-
<meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true"/>
55+
<meta-data android:name="com.google.android.gms.wallet.api.enabled" android:value="true" />
4556

4657
<meta-data
4758
android:name="flutterEmbedding"
4859
android:value="2" />
4960
</application>
50-
</manifest>
61+
</manifest>

example/android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:4.1.0'
8+
classpath 'com.android.tools.build:gradle:7.4.2'
99
}
1010
}
1111

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Fri Jun 23 08:50:38 CEST 2017
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

example/lib/main.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ class _MyAppState extends State<MyApp> {
116116
onPressed: () async {
117117
final request = BraintreePayPalRequest(
118118
amount: null,
119-
billingAgreementDescription:
120-
'I hereby agree that flutter_braintree is great.',
119+
billingAgreementDescription: 'I hereby agree that flutter_braintree is great.',
121120
displayName: 'Your Company',
122121
);
123122
final result = await Braintree.requestPaypalNonce(
@@ -132,7 +131,10 @@ class _MyAppState extends State<MyApp> {
132131
),
133132
ElevatedButton(
134133
onPressed: () async {
135-
final request = BraintreePayPalRequest(amount: '13.37');
134+
final request = BraintreePayPalRequest(
135+
amount: '13.37',
136+
payPalPaymentUserAction: PayPalPaymentUserAction.commit,
137+
);
136138
final result = await Braintree.requestPaypalNonce(
137139
tokenizationKey,
138140
request,

0 commit comments

Comments
 (0)