rn-yookassa – React Native YooKassa SDK Integration
Inspired by: react-native-yookassa-payments
Android NATIVE SDK - 6.4.0
iOS NATIVE SDK - 6.2.0
Quick Navigation
Running Example App
git clone https://github.com/kurovskyi/rn-yookassa.git
Open project folder and install dependencies:
yarn
Run Example App on device:
# Android:
yarn example android
# iOS:
yarn example ios
STEP 1: Package Installation
yarn add rn-yookassa
– OR –
npm install rn-yookassa --save
STEP 2: Native Installation
Android Installation:
- Create
libs
folder inandroid/app
directory and put thereThreatMetrix Android SDK X.X-XX.aar
file (this file will given you by YooKassa manager by support request).
❗️ Required TMX Android SDK version 5.4-XX because of issues with app icon on Android❗️
- Make sure your
android/build.gradle
looks like that. Pay attention forminSdkVersion
and version ofcom.android.tools.build:gradle
. This is the minimal requirements, so if you have higher versions it's ok.
buildscript {
ext {
minSdkVersion = 21 // <- CHECK YOU HAVE THIS MINIMAL VERSION
...
}
...
dependencies {
classpath("com.android.tools.build:gradle:3.5.4") // <- CHECK YOU HAVE THIS MINIMAL VERSION
...
}
...
}
...
...
allprojects {
repositories {
mavenCentral() // <- CHECK YOU HAVE THIS
...
}
...
}
- Add following lines in
android/app/build.gradle
dependencies:
...
dependencies {
...
implementation fileTree(dir: "libs", include: ["*.aar"]) // <- ADD THIS LINE
}
- Add following lines in
android/app/src/main/res/values/strings.xml
to set schemeyour_unique_app_shceme
for processing deep links. It's required for payments via SberPay.
<resources>
...
<string name="ym_app_scheme" translatable="false">your_unique_app_shceme</string> <!-- ADD THIS LINE -->
</resources>
- Add this in AndroidManifest.xml for card scanning work:
<manifest ...>
...
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <!-- ADD THIS LINE -->
<uses-feature android:name="android.hardware.camera.flash" android:required="false" /> <!-- ADD THIS LINE -->
...
</manifest>
iOS Installation:
- Change
ios/Podfile
like this:
source 'https://github.com/CocoaPods/Specs.git' # <- ADD THIS LINE
source 'https://github.com/yoomoney/cocoa-pod-specs.git' # <- ADD THIS LINE
plugin 'cocoapods-user-defined-build-types' # <- ADD THIS LINE
enable_user_defined_build_types! # <- ADD THIS LINE
target 'ExampleApp' do
...
# ADD THIS POD:
pod 'YooKassaPayments',
:build_type => :dynamic_framework,
:git => 'https://github.com/yoomoney/yookassa-payments-swift.git',
:tag => '6.2.0'
...
-
Create
Frameworks
folder inios
directory and put thereTMXProfiling.xcframework
andTMXProfilingConnections.xcframework
(this frameworks will also given you by YooKassa manager by support request). -
Add
TMXProfiling.xcframework
andTMXProfilingConnections.xcframework
in Frameworks, Libraries, and Embedded Content in Xcode for the main target of the project under the General section. -
Run
pod install
.
* If you get[!] Invalid Podfile file: undefined method 'enable_user_defined_build_types!'
error
run command:sudo gem install cocoapods-user-defined-build-types
Usage
1) Import functions and types from package
import {
tokenize,
confirmPayment,
dismiss,
YooKassaError,
ErrorCodesEnum, // TypeScript Only
} from 'rn-yookassa';
2) Create payment trigger
const onPayPress = async () => {
try {
// Our next code will be here.
} catch (err) {
// Process errors from YooKassa module:
if (err instanceof YooKassaError) {
switch (err.code) {
case ErrorCodesEnum.E_PAYMENT_CANCELLED:
console.log('User cancelled YooKassa module.');
break;
}
}
}
};
You can check error is instance of YooKassaError
and process error codes.
To simplify work with error codes you can use ErrorCodesEnum
(TypeScript Only).
3) Run tokenization
You will get paymentToken
for payment validation via your back-end and paymentMethodType
for the next payment confirmation process.
returnUrl
if you want to confirm payment in-app.
const { paymentToken, paymentMethodType } = await tokenize({
clientApplicationKey: 'test_ABCDEF',
shopId: '123456',
title: 'iPhone 7',
subtitle: 'Best device!',
price: 1000,
// OPTIONAL:
paymentTypes: [PaymentTypesEnum.BANK_CARD, PaymentTypesEnum.APPLE_PAY],
authCenterClientId: '123abc',
userPhoneNumber: '+79301234567',
gatewayId: '123abc',
returnUrl: 'http://google.com',
googlePaymentTypes: [
GooglePaymentTypesEnum.VISA,
GooglePaymentTypesEnum.MASTERCARD,
],
applePayMerchantId: 'merchant.com.example',
isDebug: false,
});
paymentToken
with your API and return confirmationUrl
4) Validate You also can check payments error on that stage (for example, card_expired
). All error list available here.
const { confirmationUrl, paymentError } = await yourApiRequest({
confirmationUrl,
});
// Error validation here...
// Call dismiss() if you get an error and notify user.
5) Confirm payment in-app (3-D Secure or another method)
If you get confirmation_url
from the Payment object returned by API's response so you should confirm that payment.
Use here paymentMethodType
from tokenize()
result.
await confirmPayment({ confirmationUrl, paymentMethodType });
6) Close YooKassa window and show Success notification
dismiss();
tokenize({...PROPS})
Open YooKassa window and create payment token.
PROPS
Name | Type | Default | Description |
---|---|---|---|
clientApplicationKey | string |
|
Key for client apps from the YooMoney Merchant Profile (Settings section — API keys). |
shopId | string |
|
Store's ID in YooMoney. |
title | string |
|
Product name. |
subtitle | string |
|
Product description. |
price | number |
|
Product price. Available payment methods can change depending on this parameter. |
paymentMethodTypes | PaymentMethodTypesEnum[]? (string[]?) | All of PaymentMethodTypesEnum
|
Array of needed payment methods. If you leave the field empty, the library will use all available payment methods. |
authCenterClientId | string? | undefined | App's ID for SDK authorization ru.yoomoney.sdk.auth, see Registering an app for payments via the wallet. |
userPhoneNumber | string? | undefined | User's phone number. It's used for autofilling fields for payments via SberPay. Supported format: "+7XXXXXXXXXX". |
gatewayId | string? | undefined | Gateway ID for the store. |
returnUrl | string? | undefined | Url of the page (only https supported) where you need to return after completing 3DS. If confirmPayment() is used, don't specify this parameter! |
googlePaymentMethodTypes | GooglePaymentMethodTypesEnum[]? (string[]?) | All of GooglePaymentMethodTypesEnum
|
Array of needed payment methods for Google Pay ( |
applePayMerchantId | string? | undefined | Apple Pay merchant ID ( |
isDebug | boolean? | false | Enter to the Debug mode to test payments. In this mode you will receive fake tokens. If you want to test real tokens try to set up Test Shop in the YooKassa Panel. |
Result
Name | Type | Description |
---|---|---|
paymentToken | string | Payment token that you need to pass to your API. |
paymentMethodType | PaymentMethodTypesEnum (string) | Payment method that was used. This will be used in the confirmation process. |
confirmPayment({...PROPS})
Call this after you get confirmation_url
from your API. Make sure you aren't specify returnUrl
in the tokenize()
function and didn't dismiss YooKassa window yet.
PROPS
Name | Type | Default | Description |
---|---|---|---|
confirmationUrl | string |
|
Confirmation url that you have got from your API. |
paymentMethodType | PaymentMethodTypesEnum (string) |
|
Payment method that was used in the tokenization process. |
Result
Name | Type | Description |
---|---|---|
paymentMethodType | PaymentMethodTypesEnum (string) | Payment method that was used. |
dismiss()
Close YooKassa window. Call it after successful payment confirmation.
Troubleshooting
pod install
:
1) If you see errors during [!] Invalid Podfile file: undefined method `enable_user_defined_build_types!'
You need to install CocoaPods additional package:
sudo gem install cocoapods-user-defined-build-types
Then run pod install
again.
2) If you see errors in Xcode Project like this:
Failed to build module 'MoneyAuth' from its module interface...
Compipiling for iOS 10.0, but module 'FunctiionalSwift' has a minimum deployment target iOS 11.0...
Typedef redefinition with different types ('uint8_t' (aka 'unsigned char'))...
You can resolve it by adding post_install in your Podfile:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if target.name == 'FunctionalSwift' || target.name == 'YooMoneyCoreApi'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '10.0'
else
if target.name == 'RCT-Folly'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
else
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
end
end
end
end
end
end
https://github.com/yoomoney/yookassa-payments-swift/issues/93
For using your custom realization of 3DSecure confirmation, specify returnUrl
: string for redirect to your link. Not use confirmPayment()
method with returnUrl
.
License
MIT