Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asyncOpenURL wrapper #3764

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Onyx from 'react-native-onyx';
import {Linking} from 'react-native';
import _ from 'underscore';
import CONST from '../../CONST';
import ONYXKEYS from '../../ONYXKEYS';
import ROUTES from '../../ROUTES';
import * as API from '../API';
import {getSimplifiedIOUReport, fetchChatReportsByIDs, fetchIOUReportByIDAndUpdateChatReport} from './Report';
import Navigation from '../Navigation/Navigation';
import asyncOpenURL from '../asyncOpenURL';

/**
* @param {Object[]} requestParams
Expand Down Expand Up @@ -196,7 +196,18 @@ function payIOUReport({
const payIOUPromise = paymentMethodType === CONST.IOU.PAYMENT_TYPE.EXPENSIFY
? API.PayWithWallet({reportID})
: API.PayIOU({reportID, paymentMethodType});
payIOUPromise

// Build the url for the user's platform of choice if they have
// selected something other than a manual settlement or Expensify Wallet e.g. Venmo or PayPal.me
let url;
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
url = buildPayPalPaymentUrl(amount, submitterPayPalMeAddress, currency);
}
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.VENMO) {
url = buildVenmoPaymentURL(amount, submitterPhoneNumber);
}

asyncOpenURL(payIOUPromise
.then((response) => {
if (response.jsonCode !== 200) {
throw new Error(response.message);
Expand All @@ -209,20 +220,13 @@ function payIOUReport({
// iouReport being fetched here must be open, because only an open iouReoport can be paid.
// Therefore, we should also sync the chatReport after fetching the iouReport.
fetchIOUReportByIDAndUpdateChatReport(reportID, chatReportID);

// Once we have successfully paid the IOU we will transfer the user to their platform of choice if they have
// selected something other than a manual settlement or Expensify Wallet e.g. Venmo or PayPal.me
if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.PAYPAL_ME) {
Linking.openURL(buildPayPalPaymentUrl(amount, submitterPayPalMeAddress, currency));
} else if (paymentMethodType === CONST.IOU.PAYMENT_TYPE.VENMO) {
Linking.openURL(buildVenmoPaymentURL(amount, submitterPhoneNumber));
}
})
.catch((error) => {
console.error(`Error Paying iouReport: ${error}`);
Onyx.merge(ONYXKEYS.IOU, {error: true});
})
.finally(() => Onyx.merge(ONYXKEYS.IOU, {loading: false}));
.finally(() => Onyx.merge(ONYXKEYS.IOU, {loading: false})),
url);
}

export {
Expand Down
11 changes: 11 additions & 0 deletions src/libs/asyncOpenURL/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Linking} from 'react-native';

export default function asyncOpenURL(promise, url) {
if (!url) {
return;
}

promise.then(() => {
Linking.openURL(url);
});
}
28 changes: 28 additions & 0 deletions src/libs/asyncOpenURL/index.website.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Linking} from 'react-native';

/**
* Prevents Safari from blocking pop-up window when opened within async call.
*
* @param {Promise} promise
* @param {string} url
*/
export default function asyncOpenURL(promise, url) {
if (!url) {
return;
}

const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why are you doing a negative patten match for chrome or android here? Is there some case when the user agent would be formatted like 'chrome-xxx-safari' or something?

Copy link
Member

@rushatgabhane rushatgabhane Jun 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roryabraham
The negative pattern match is to exclude chrome, edge, and android.
All of them like to include Safari in their UA

Edge and Chrome's userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"

Also, I think all third party browsers on iOS are just skins for safari.
So chrome on iOS has this issue too. And this pattern detects that in my testing :)

Chrome on iOS detected as Safari

IMG_0430

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for answering this @rushatgabhane! Yes, that is the reason I used the negative pattern match. @roryabraham Do we have any other preferred method?


if (!isSafari) {
promise.then(() => {
Linking.openURL(url);
});
} else {
const windowRef = window.open();
promise
.then(() => {
windowRef.location = url;
})
.catch(() => windowRef.close());
}
}