Skip to content
developers

Cancel a sale

Handle sales that the merchant, Mobile Sync, or our gateway cancels, and cancel a sale from your app.

Prerequisites: Authentication · Run a sale

There are three ways a sale can be cancelled, and your app must handle each scenario:

The merchant or our gateway cancels the sale while Mobile Sync is open

If the merchant or our gateway cancels the sale while Mobile Sync is open, Mobile Sync returns to your app with status=cancelled.

The status is a hint from Mobile Sync. To confirm the result, use Step 5 of Run a sale to get the response from our gateway. If the merchant or our gateway cancelled the sale, the response includes a cancelled status.

The merchant leaves Mobile Sync

If the merchant switches back to your app or closes Mobile Sync during a sale, Mobile Sync cancels the sale. Your app gets no response, so it doesn't detect that the sale ended.

Program your app to check the result with our gateway when your app becomes active again. Your app should wait 1.5 seconds for a response from Mobile Sync. If our gateway hasn't accepted your request yet, wait for the gateway to respond, which can take up to 20 seconds. Check with our gateway up to three times, three seconds apart. If our gateway still reports that the sale is in progress, or if your app is offline, show a Check status button.

Example code

Note: The following code uses the activeClient, activeInstructionType, pendingSale, handledInstructionIds, and isWaitingForSaleReturn variables that you set in Step 3 of Run a sale, and the showResult function from Step 5 of Run a sale. It also uses pendingSignature and handleSignatureReturn from Capture a signature. If your app doesn't capture signatures, remove the .signature code: use pendingSale as the task, and call showResult for every outcome. Your app sets isWaitingForSaleReturn to false when Mobile Sync returns, and back to true if the result isn't final. If it's true when your app becomes active, Mobile Sync didn't return, or the result isn't final yet.

swift
import AppSwitchGatewayReference

/// Call when your app becomes active.
func checkForMissingReturn() {
    guard isWaitingForSaleReturn, let client = activeClient else { return }
    let type = activeInstructionType                     // .payment for a sale, .signature for a signature capture
    let task = type == .signature ? pendingSignature : pendingSale
    Task {
        try? await Task.sleep(for: .seconds(1.5))         // a real return usually arrives first
        // Wait until our gateway accepts the request. If the request failed, handleStartError has already shown the error.
        guard isWaitingForSaleReturn, let started = try? await task?.value else { return }
        let instructionId = started.instructionId

        for attempt in 0..<3 {
            if attempt > 0 { try? await Task.sleep(for: .seconds(3)) }
            guard isWaitingForSaleReturn else { return }  // a return arrived in the meantime
            // Check once; don't keep polling.
            if let outcome = try? await client.completeInstruction(type: type,
                                                                   instructionId: instructionId,
                                                                   maxAttempts: 1),
               !outcome.stillInProgress {
                isWaitingForSaleReturn = false
                handledInstructionIds.insert(instructionId)   // so that a late return doesn't show it again
                if type == .signature {
                    handleSignatureReturn(status: nil)    // from Capture a signature, Step 3
                } else {
                    showResult(outcome, hint: nil)        // from Run a sale, Step 5; there's no status hint
                }
                return
            }
        }
        // The sale is still in progress, or your app is offline. Let the merchant check again.
        showCheckStatusButton()                           // the button calls checkStatus(); see Handle errors
    }
}

Mobile Sync can also send a late cancelled response. Mobile Sync holds the response and sends it the next time the merchant opens Mobile Sync, for up to five minutes. A late response can arrive after your app has already checked with our gateway, so make sure that your app shows each outcome only once. The code in Step 5 of Run a sale and the code above use handledInstructionIds to do this.

If your app has since started a new sale, the late response can arrive while Mobile Sync is waiting for the new request. The .onOpenURL handler in Step 4 of Run a sale ignores a response that arrives before the kit sends your request, so that your app doesn't treat it as the response for the new sale.

Cancel a sale from your app

Your app is in the background while Mobile Sync runs the sale, so the merchant can't cancel from your app. You can program your app to cancel a sale only before it sends the request, or after it sends the request. The kit handles the cancellation differently in each case.

Important: Starting a new sale doesn't cancel the earlier one on our gateway. If the earlier sale must not run, cancel it.

Before the request is sent

If requestStarted is false, your app is still waiting for Mobile Sync's ready signal and the kit hasn't sent the request. Increase saleGeneration, and then cancel pendingSale, which is the task that you stored in Run a sale.

Cancelling the task doesn't stop the kit from waiting for Mobile Sync's ready signal, which can take up to 30 seconds. When you increase saleGeneration, the code from Step 3 of Run a sale records that the sale is no longer current. It doesn't send the request, even if Mobile Sync becomes ready later, and it doesn't show an error.

After the request is sent

If requestStarted is true, the kit has started to send the request. Don't cancel the stored task. Instead, wait for our gateway to answer, and then use cancelInstruction to cancel the instruction. Waiting for our gateway to answer can take up to 20 seconds.

The cancelInstruction function sends DELETE /payment-instructions/{paymentInstructionId} and returns true if our gateway cancelled the instruction.

The following function handles both cases. It uses the activeClient, pendingSale, requestStarted, and saleGeneration values that you set in Step 3 of Run a sale. Your app sets requestStarted when the kit starts to send the request, and stores the task in pendingSale.

swift
import AppSwitchGatewayReference

func cancelSale() {
    Task {
        guard let client = activeClient else { return }

        guard requestStarted else {
            // Your app is still waiting for Mobile Sync. The kit hasn't sent the request.
            saleGeneration += 1        // the sale is no longer current, so the kit never sends it
            pendingSale?.cancel()
            isWaitingForSaleReturn = false
            return
        }

        // The request has started. Wait for our gateway to answer, and then cancel the instruction.
        guard let started = try? await pendingSale?.value else {
            // The request failed, so there's no instruction to cancel.
            isWaitingForSaleReturn = false
            return
        }

        let cancelled = await client.cancelInstruction(type: .payment, instructionId: started.instructionId)
        if cancelled {
            isWaitingForSaleReturn = false
        } else {
            // Our gateway didn't cancel the instruction, for example because the sale already finished.
            // Leave isWaitingForSaleReturn as it is, and check the result as in Run a sale, Step 5.
        }
    }
}

Browse guides

Search documentation

API reference169
Guides131
Knowledge38
legal1
Solutions32
Workflows74
↑↓highlight↵openView all search results

Menu

Theme

Sign out

Your saved plans remain in your organization. This browser’s private draft and account view will be cleared.

Talk to an engineer