This guide walks you through the process of setting up a new React Native project with In-App Payments SDK. See the React Native In-App Payments SDK Technical Reference for more detailed information about the methods available.
- You will need a Square account enabled for payment processing. If you have not enabled payment processing on your account (or you are not sure), visit squareup.com/activate.
- Install yarn from yarnpkg.com
- Follow the Building Projects with Native Code instructions in the React Native Getting Started guide to setup your React Native development environment.
- Step 1: Create a React Native project
- Step 2: Install the React Native plugin for In-App Payments SDK
- Step 3a: Add the In-App Payments SDK to your iOS project without Cocoapods
- Step 3b: Add the In-App Payments SDK to your iOS project with Cocoapods
- Step 4: Configure your Android project
Card Entry Usage
- Step 1: Get a Square Application ID
- Step 2: Initialize the In-App Payments SDK
- Step 3: Customize card entry appearance
- Step 4: Implement the Payment flow
Digital Wallets
In a terminal, create a folder for your React Native project and run the following command in the new folder:
react-native init myRNInAppPaymentsSample
This command creates a new React Native project with an App.js
file and folders
for Android and iOS specific code. The following steps in this
guide update code in the files created by the command.
Note: If the previous
react-native
command returnscommand not found
on OS X orreact-native is not recognized as an internal or external command
on Windows, then the React Native command line interface (CLI) is not installed. To fix this, runnpm install -g react-native-cli
or if you have installed the Yarn dependency manager,yarn global add react-native-cli
.
At a terminal prompt, run the following commands in the project root folder:
yarn add react-native-square-in-app-payments
To use the In-App Payments plugin on iOS devices, install In-App Payments SDK for iOS to make it an available resource for the React Native plugin.
- Open your iOS project
myRNInAppPaymentsSample.xcodeproj
with Xcode and install In-App Payments SDK by following
Install In-App Payments SDK - Swift(iOS) Option 3: Manual Installation. You MUST put the SquareInAppPaymentsSDK.framework in folder<YOUR_PROJECT_DIRECTORY>/ios
, otherwise the project won't compile.Check the supported SDK version in the top of root README.
- Set the
iOS Deployment Target
to 13.0 or above - Add an In-App Payments SDK build phase:
- Open the Xcode project for your application.
- In the Build Phases tab for your application target, click the + button at the top of the pane.
- Select New Run Script Phase.
- Paste the following into the editor panel of the new run script:
FRAMEWORKS="${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}" "${FRAMEWORKS}/SquareInAppPaymentsSDK.framework/setup"
- Correct the resource bundle location in your project target
myRNInAppPaymentsSample
- In the
Link Binary With Libraries
build phase, removeRNSquareInAppPayments-Resources.bundle
. - In the
Copy Bundle Resources
build phase, addRNSquareInAppPayments-Resources.bundle
fromLibraries
->RNSquareInAppPayments.xcodeproj
.
- In the
If your iOS project is configured with CocoaPods (Podfile
should be found in folder ios
), your iOS project will be able
to download In-App Payments SDK automatically by following the steps below.
-
Run
pod --version
, Make sure you have cocoapods version greater than1.7.0
-
Open file
ios/Podfile
- make sure you set the platform to
13.0
or above - verify if
RNSquareInAppPayments
pod dependency is added
platform :ios, '13.0' # set the platform version to 13.0 target 'myRNInAppPaymentsSample' do use_frameworks! # enalbe use_frameworks! # pod dependencies ... pod 'RNSquareInAppPayments', :path => '../node_modules/react-native-square-in-app-payments' end
- make sure you set the platform to
-
In folder
<YOUR_PROJECT_DIRECTORY>/ios
, run the following command to install updated pod dependencies:pod install
-
Add an In-App Payments SDK build phase:
- Open the Xcode project for your application.
- In the Build Phases tab for your application target, click the + button at the top of the pane.
- Select New Run Script Phase.
- Paste the following into the editor panel of the new run script:
FRAMEWORKS="${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}" "${FRAMEWORKS}/SquareInAppPaymentsSDK.framework/setup"
-
Open
android/build.gradle
and set yourminSdkVersion
to21
or greater. ThebuildToolsVersion
,compileSdkVersion
,targetSdkVersion
, andsupportLibVersion
values are just examples. -
(Optional) If you want to pick a different In-App Payments Android SDK version, you can override the
sqipVersion
.Check the minimum supported SDK version in the top of root README.
buildscript { ext { buildToolsVersion = "33.0.2" minSdkVersion = 24 compileSdkVersion = 33 targetSdkVersion = 33 supportLibVersion = "33.0.0" sqipVersion="1.6.6" } ... }
Complete the following steps to add the In-App Payments card entry screen to your React Native project and use the card entry screen to get a nonce.
- Open the Square Application Dashboard.
- Create a new Square application.
- Click on the new application to bring up the Square application settings pages.
- You will need the Application ID from the Credentials page to configure In-App Payments SDK in the next steps.
- Add code
App.js
to initialize the In-App Payments SDK.import { SQIPCore, } from 'react-native-square-in-app-payments'; export default class App extends Component { ... async componentDidMount() { await SQIPCore.setSquareApplicationId('REPLACE_WITH_APPLICATION_ID'); ... } ... }
- Replace
APPLICATION_ID
with the application ID from the application dashboard.
The Android and iOS platforms allow customization of the card entry screen but provide different customization mechanisms.
To customize the card entry appearance for Android devices, update code in the Android project folder as described in customizing the payment entry form in the Square developer documentation..
For iOS devices, set the card entry error text and background color, keyboard appearance and message color.
- Here is an example that creates a card entry theme that customize save button
and keyboard appearance.
import { SQIPCardEntry, } from 'react-native-square-in-app-payments'; export default class App extends Component { ... async componentDidMount() { ... if (Platform.OS === 'ios') { await SQIPCardEntry.setIOSCardEntryTheme({ saveButtonFont: { size: 25, }, saveButtonTitle: 'Pay 💳 ', keyboardAppearance: 'Light', saveButtonTextColor: { r: 255, g: 0, b: 125, a: 0.5, }, }); } } ... }
Add a button and start the card entry in button onPress
event handler.
import {
SQIPCardEntry,
} from 'react-native-square-in-app-payments';
export default class App extends Component {
constructor() {
super();
// bind 'this' to the methods' context
this.onStartCardEntry = this.onStartCardEntry.bind(this);
this.onCardNonceRequestSuccess = this.onCardNonceRequestSuccess.bind(this);
}
...
/**
* Callback when the card entry is closed after call 'SQIPCardEntry.completeCardEntry'
*/
onCardEntryComplete() {
// Update UI to notify user that the payment flow is completed
}
/**
* Callback when successfully get the card nonce details for processig
* card entry is still open and waiting for processing card nonce details
* @param {*} cardDetails
*/
async onCardNonceRequestSuccess(cardDetails) {
try {
// take payment with the card details
// await chargeCard(cardDetails);
// payment finished successfully
// you must call this method to close card entry
await SQIPCardEntry.completeCardEntry(
this.onCardEntryComplete(),
);
} catch (ex) {
// payment failed to complete due to error
// notify card entry to show processing error
await SQIPCardEntry.showCardNonceProcessingError(ex.message);
}
}
/**
* Callback when card entry is cancelled and UI is closed
*/
onCardEntryCancel() {
// Handle the cancel callback
}
/**
* An event listener to start card entry flow
*/
async onStartCardEntry() {
const cardEntryConfig = {
collectPostalCode: false,
};
await SQIPCardEntry.startCardEntryFlow(
cardEntryConfig,
this.onCardNonceRequestSuccess,
this.onCardEntryCancel,
);
}
render() {
return (
<View style={styles.container}>
...
<Button
onPress={this.onStartCardEntry}
title="Start Card Entry"
/>
</View>
);
}
}
Note: To start the payment flow with Strong Customer Authentication, you should call startCardEntryFlowWithBuyerVerification
.
Note: the chargeCard
method in this example shows a typical REST request on a backend process
that uses the Payments API to take a payment with the supplied nonce.
See BackendQuickStart Sample to learn about building an app that processes payment nonces on a server.