Skip to Content
Docs

Refresh the session

Read the session's amount and currency again after your server changes them, to show the new total before the customer pays.

checkout.refreshSession() reads the session's amount and currency from Pay.com again and updates checkout.session. Call it in your own UI after your server updates the session's amount, and show the new total before the customer pays. The drop-in and payWithGooglePay() read the total again by themselves.

Paying without it is still safe. If the total changed, pay charges nothing and returns PayResult.Failed(PayError.AmountChanged).

refreshSession is a suspend function, so call it from a coroutine.

val session: PaySession = checkout.refreshSession()

Parameters

None.

Returns

The updated PaySession, which checkout.session has too. Only amount and currency can change. A setup session has no total to read, so it comes back as it is, without a request.

Errors

refreshSession() throws a PayError when it can't read the total, and checkout.session keeps the one it had:

ErrorWhen
PayError.SessionExpiredPay.com rejected the client_secret (HTTP 401). The session expired, or the secret isn't a session's.
PayError.RequestRejected(error)Pay.com returned another error status below 500.
PayError.Server(error)Pay.com failed (HTTP 5xx) or timed out (HTTP 408).
PayError.Network(detail)The request didn't reach Pay.com, or the response didn't arrive.
PayError.UnexpectedResponse(detail)Pay.com returned a response without the session's amount or currency, or one this SDK version can't read.

Example

class CheckoutActivity : ComponentActivity() {
    private val viewModel: CheckoutViewModel by viewModels()

    private fun shippingChanged(option: ShippingOption) {
        val checkout = viewModel.checkout.value ?: return
        lifecycleScope.launch {
            shopApi.updateOrder(option) // Your server updates the session's amount
            val session = try {
                checkout.refreshSession()
            } catch (e: PayError) {
                return@launch showMessage(e) // Keep the pay button off until you can show the total
            }
            showTotal(session.amount, session.currency)
        }
    }
}

Next steps

Was this page helpful?