1.0.7 • Published 2 years ago

rn-yookassa v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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:

  1. Create libs folder in android/app directory and put there ThreatMetrix 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 ❗️
  2. Make sure your android/build.gradle looks like that. Pay attention for minSdkVersion and version of com.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
        ...
    }

    ...
}
  1. Add following lines in android/app/build.gradle dependencies:
...

dependencies {
    ...

    implementation fileTree(dir: "libs", include: ["*.aar"]) // <- ADD THIS LINE
}
  1. Add following lines in android/app/src/main/res/values/strings.xml to set scheme your_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>
  1. 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:

  1. 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'

...
  1. Create Frameworks folder in ios directory and put there TMXProfiling.xcframework and TMXProfilingConnections.xcframework (this frameworks will also given you by YooKassa manager by support request).

  2. Add TMXProfiling.xcframework and TMXProfilingConnections.xcframework in Frameworks, Libraries, and Embedded Content in Xcode for the main target of the project under the General section.

  3. 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.

❗️ Do not specify 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,
});

4) Validate paymentToken with your API and return confirmationUrl

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

NameTypeDefaultDescription
clientApplicationKeystring❗️ REQUIREDKey for client apps from the YooMoney Merchant Profile (Settings section — API keys).
shopIdstring❗️ REQUIREDStore's ID in YooMoney.
titlestring❗️ REQUIREDProduct name.
subtitlestring❗️ REQUIREDProduct description.
pricenumber❗️ REQUIREDProduct price. Available payment methods can change depending on this parameter.
paymentMethodTypesPaymentMethodTypesEnum[]? (string[]?)All of PaymentMethodTypesEnumArray of needed payment methods. If you leave the field empty, the library will use all available payment methods.
authCenterClientIdstring?undefinedApp's ID for SDK authorization ru.yoomoney.sdk.auth, see Registering an app for payments via the wallet.
userPhoneNumberstring?undefinedUser's phone number. It's used for autofilling fields for payments via SberPay. Supported format: "+7XXXXXXXXXX".
gatewayIdstring?undefinedGateway ID for the store.
returnUrlstring?undefinedUrl of the page (only https supported) where you need to return after completing 3DS. If confirmPayment() is used, don't specify this parameter!
googlePaymentMethodTypesGooglePaymentMethodTypesEnum[]? (string[]?)All of GooglePaymentMethodTypesEnumArray of needed payment methods for Google Pay (❗️ required for payments via Google Pay).
applePayMerchantIdstring?undefinedApple Pay merchant ID (❗️ required for payments via Apple Pay).
isDebugboolean?falseEnter 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

NameTypeDescription
paymentTokenstringPayment token that you need to pass to your API.
paymentMethodTypePaymentMethodTypesEnum (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

NameTypeDefaultDescription
confirmationUrlstring❗️ REQUIREDConfirmation url that you have got from your API.
paymentMethodTypePaymentMethodTypesEnum (string)❗️ REQUIREDPayment method that was used in the tokenization process.

Result

NameTypeDescription
paymentMethodTypePaymentMethodTypesEnum (string)Payment method that was used.

dismiss()

Close YooKassa window. Call it after successful payment confirmation.

Troubleshooting

1) If you see errors during pod install:

[!] 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