Skip to Content
Docs

Pay

Pay a session from your own screen with the card fields, a redirect payment method, or a saved method, with 3DS and redirects shown for you.

checkout.pay() submits a payment for the session using the card entered in the SDK's card fields, an alternative payment method from checkout.paymentMethods(), or a saved method. The SDK shows the 3DS challenge and any redirect over the app's current screen, and resolves once the payment ends. If you'd rather not build the screen, checkout.presentDropIn() does all of this in a ready-made sheet.

const result = await checkout.pay({ type: 'card', card: fields })

Parameters

pay() takes one request. Its type determines the payment method and which additional fields apply.

ParameterTypeRequiredDescription
type'card' | 'alternative' | 'savedPaymentMethod'YesWhat to pay with.
cardPayCardFieldsWith 'card'The fields the customer typed the card into, from usePayCardFields().
saveForFutureUsebooleanNoWith 'card', true stores the card for the customer. When the session's setup_future_usage is on_session, the card is stored anyway. Default: false.
termsAcceptedbooleanNoWith 'card', set it to true when the customer agreed to your terms and conditions on your screen. Pay.com records the agreement on the payment and the customer. Only true counts. The drop-in shows no terms, so it never sends true. Default: false.
methodstringWith 'alternative'The method's type from checkout.paymentMethods(), for example paypal. The SDK opens the redirect or app switch it needs.
dataRecord<string, string>NoValues for the fields the alternative method requires, keyed by field name (used with 'alternative'). Every value is a string.
idstringWith 'savedPaymentMethod'A saved method's id from checkout.savedPaymentMethods(). In a sale or authorization session, the method is charged. See Pay with a saved payment method. In a setup session, it becomes the customer's default instead of being charged. See Read the choice on your server.
cvcPaySavedCardCvcWith 'savedPaymentMethod', for a card whose requiresCvc is trueThe card's CVV, typed into the input from usePaySavedCardCvc(). For another card, it's optional. An empty input sends nothing, and a CVV typed into it must be valid. It isn't used in a setup session, or for a saved method that isn't a card.

While pay() runs, the SDK listens to React Native's Linking for the session's return URL, so redirects come back without code of your own. See Handle the return URL.

Returns

pay() returns a promise that resolves with a PayResult once the payment ends:

statusDescription
'succeeded'Paid, or authorized for a session created with capture: false. result.ids holds the IDs, for example the charge in result.ids.transaction. In a setup session, the method was saved or kept.
'processing'The SDK didn't get a final outcome, for example for a method that's still pending. result.ids holds the IDs. Your server confirms the outcome.
'canceled'The customer closed the 3DS or redirect screen and the SDK found no payment. They can pay again.
'failed'The payment failed. Inspect result.error.code for the failure reason, for example declined.

pay() rejects only when a call fails before sending a request. Everything that happens after a request leaves the device comes back as the result, including a decline or a changed total. After a decline, check result.error.decline.maxAttemptsReached. While it's false, the customer can try again with the same checkout. When it's true, your server needs a new session.

The result is a hint for your UI. Before you fulfill the order, your server confirms the payment with Retrieve a payment session or the payment_session.completed webhook.

Errors

pay() rejects with a PayError when a call fails before sending a request:

Error codeWhen
invalid_card_inputThe card fields are incomplete or invalid, or a saved card's CVV is missing or invalid. error.fields lists the ones to fix, for example ['cvc'].
payment_in_progressAnother pay() on this checkout is still running.
releasedThe checkout, the card fields, or the saved card's CVV in the request were released.
unsupportedA 'savedPaymentMethod' request with an id that isn't in checkout.savedPaymentMethods().
no_screenThere's no screen to show 3DS or a redirect on (no foreground activity on Android, or no view controller on iOS).
unexpected_responseThe native SDK returned a response this SDK version can't read.

pay() rejects with a TypeError instead, before anything reaches native code, when the request has an invalid shape, such as a type other than the three above, an 'alternative' request without a method or with a data value that isn't a string, or a 'savedPaymentMethod' request without an id.

When the amount changes

In every session but a setup session, including one whose flow the SDK doesn't recognize, the SDK reads the amount and currency again right before it charges, and compares them with checkout.session.

  • If either changed, nothing is charged. pay() resolves 'failed' with the code amount_changed, and checkout.session has the new amount and currency. Show them, then let the customer pay again.
  • If they can't be read, nothing is charged either. pay() resolves 'failed' with that error.

Example

import { useState } from 'react'
import { Button, Text, View } from 'react-native'
import {
  PayCardNumberField,
  PayCheckout,
  PayCvcField,
  PayError,
  PayExpiryField,
  usePayCardFields,
} from '@pay-com/react-native'

export function CardPayment({ clientSecret, onPaid }: { clientSecret: string; onPaid: () => void }) {
  const { fields, state } = usePayCardFields()
  const [busy, setBusy] = useState(false)
  const [message, setMessage] = useState<string | null>(null)

  async function pay() {
    setBusy(true)
    setMessage(null)
    let checkout: PayCheckout | undefined
    try {
      checkout = await PayCheckout.create(clientSecret, { returnUrl: 'myshop://pay' })
      const result = await checkout.pay({ type: 'card', card: fields })
      if (result.status === 'succeeded' || result.status === 'processing') {
        onPaid() // A hint for your UI: your server confirms the payment before you fulfill the order.
      } else if (result.status === 'failed') {
        setMessage(result.error.code === 'declined' ? 'Your card was declined.' : "The payment didn't go through.")
      }
      // 'canceled': the customer closed the 3DS screen, and can pay again.
    } catch (error) {
      // Nothing was sent.
      if (error instanceof PayError && error.code === 'invalid_card_input') {
        setMessage(`Check your card details: ${error.fields.join(', ')}.`)
      } else {
        setMessage('Something went wrong. Try again.')
      }
    } finally {
      checkout?.release()
      setBusy(false)
    }
  }

  return (
    <View>
      <PayCardNumberField fields={fields} />
      <PayExpiryField fields={fields} />
      <PayCvcField fields={fields} />
      {message ? <Text>{message}</Text> : null}
      <Button title="Pay" disabled={busy || !state.isComplete} onPress={pay} />
    </View>
  )
}

To pay with a redirect method instead, pass its type:

const result = await checkout.pay({ type: 'alternative', method: 'paypal' })

Next steps

Was this page helpful?