You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS端返回应用后如何获取Google Pay交易响应?

iOS Google Pay: Retrieving Transaction Response Data (Like Android Intent)

Question

I'm trying to complete a transaction using Google Pay on iOS, and I've got the initiation code working:

let paValue = "Client_upi_key" // payee address upi id
let pnValue = "Merchant Name" // payee name
let trValue = "1234ABCD" // transaction Id
let urlValue = "http://url/of/the/order/in/your/website" // url for reference
let mcValue = "1234" // retailer category code :- user id
let tnValue = "Purchase in Merchant" // transaction Note
let amValue = "1" // amount to pay
let cuValue = "INR" // currency
let str = "gpay://upi/pay?pa=\(paValue)&pn=\(pnValue)&tr=\(trValue)&mc=\(mcValue)&tn=\(tnValue)&am=\(amValue)&cu=\(cuValue)"
guard let urlString = str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
guard let url = URL(string: urlString) else { return }
UIApplication.shared.open(url)

Now when users come back to my app after the payment, I want to grab response details like transaction ID, user ID, etc. Is there an iOS equivalent to Android's Intent for this?


Answer

Great question! On iOS, we don’t have a direct 1:1 equivalent to Android’s Intent system for receiving callback data from another app, but we can absolutely capture Google Pay’s transaction response using custom URL scheme callbacks and a bit of app configuration. Here’s how to implement this:

1. Set Up a Custom URL Scheme for Your App

First, you need to define a unique URL scheme so Google Pay knows where to send the response:

  • Open your Xcode project, navigate to Target > Info > URL Types
  • Click the + button to add a new entry
  • Fill in the Identifier (e.g., com.yourcompany.yourapp) and set URL Schemes to something unique (like yourapp-gpay) – this is your app’s "callback address"

2. Add the Callback URL to Your Google Pay Payment Request

Modify your payment URL to include a url parameter pointing to your custom scheme. This tells Google Pay to send the transaction result back to your app when done:

// Define your app's callback URL
let callbackUrl = "yourapp-gpay://transaction-response"
// Update the payment string with the callback
let str = "gpay://upi/pay?pa=\(paValue)&pn=\(pnValue)&tr=\(trValue)&mc=\(mcValue)&tn=\(tnValue)&am=\(amValue)&cu=\(cuValue)&url=\(callbackUrl)"
// Don't forget to percent-encode the string as before!
guard let urlString = str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
guard let url = URL(string: urlString) else { return }
UIApplication.shared.open(url)

3. Handle the Incoming Callback in Your App

Next, you need to catch the callback URL when Google Pay opens your app. The implementation depends on whether you’re using AppDelegate or SceneDelegate:

For AppDelegate (UIKit without Scenes):

Add this method to your AppDelegate.swift:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // Check if this is a Google Pay callback
    if url.scheme == "yourapp-gpay" && url.host == "transaction-response" {
        // Parse the query parameters from the callback URL
        guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
              let queryItems = components.queryItems else { return false }
        
        // Extract the data you need (check Google's docs for full parameter list)
        let transactionId = queryItems.first(where: { $0.name == "tr" })?.value
        let paymentStatus = queryItems.first(where: { $0.name == "status" })?.value
        let utr = queryItems.first(where: { $0.name == "utr" })?.value // Unique Transaction Reference
        let responseCode = queryItems.first(where: { $0.name == "resp" })?.value
        
        // Use this data to update your app's state (e.g., mark order as paid, show success message)
        print("Transaction ID: \(transactionId ?? "N/A")")
        print("Payment Status: \(paymentStatus ?? "N/A")")
        
        return true
    }
    return false
}

For SceneDelegate (UIKit with Scenes):

Add this method to your SceneDelegate.swift:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else { return }
    
    if url.scheme == "yourapp-gpay" && url.host == "transaction-response" {
        guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
              let queryItems = components.queryItems else { return }
        
        let transactionId = queryItems.first(where: { $0.name == "tr" })?.value
        let paymentStatus = queryItems.first(where: { $0.name == "status" })?.value
        
        // Process the response here
        print("Got Google Pay response for transaction: \(transactionId ?? "N/A") | Status: \(paymentStatus ?? "N/A")")
    }
}

4. Key Response Parameters to Look For

Google Pay will return these common parameters in the callback URL:

  • tr: Your original transaction ID (use this to match the request)
  • status: Payment outcome (e.g., "SUCCESS", "FAILURE", "SUBMITTED")
  • utr: Unique Transaction Reference (only for successful payments)
  • resp: UPI response code (e.g., "00" for success)

Pro Tips

  • Always validate the response data: Cross-check the transaction ID with your original order, and verify the status to prevent tampering.
  • For better security, consider using Universal Links instead of custom URL schemes – they’re more secure and avoid app hijacking risks.
  • Refer to Google’s official UPI documentation for the full list of response parameters and edge cases.

内容的提问来源于stack exchange,提问作者Suhail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 14:12:48