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

iOS应用集成Razorpay遇Swift版本兼容错误,求完整集成教程

iOS Swift 集成 Razorpay 完整教程(含版本兼容错误修复)

先解决你碰到的版本兼容错误

module compiled with swift 4.1 cannot be imported by the swift 5.6.1 compiler: /users/ravindra/documents/sample projects/ruby/pods/razorpay-pod/pod/razorpay.framework/modules/razorpay.swiftmodule/arm64.swiftmodule

说白了就是你装的Razorpay Pod版本是用Swift4.1编译的,和Xcode13.4自带的Swift5.6.1编译器不兼容。直接更新到支持Swift5.x的版本就能解决。

完整集成步骤

1. 环境确认

  • Xcode 13.4(对应Swift 5.6.1)
  • iOS 12.0+ 目标版本(Razorpay最低要求)
  • CocoaPods 1.10.0+

2. 配置Podfile

  • 打开终端,进入项目根目录,没创建过Podfile的话先执行 pod init
  • 编辑Podfile,添加兼容Swift5.x的Razorpay依赖:
platform :ios, '12.0'
use_frameworks!

target '你的项目名称' do
  pod 'Razorpay', '~> 1.10.0' # 这个版本及以上完全适配Swift5.x
end
  • 执行 pod install --repo-update,确保拉取最新的兼容版本
  • 之后必须用 你的项目名称.xcworkspace 打开项目,别再点旧的xcodeproj文件

3. 项目基础配置

  • 打开项目的 Info.plist,添加网络权限配置(避免支付流程中出现权限问题):
    新增NSAppTransportSecurity字典,在里面添加NSAllowsArbitraryLoads并设为YES(如果你的App只访问HTTPS资源,也可以配置ATS例外规则)
  • 检查项目Build Settings:
    • Swift Language Version 设为 Swift 5
    • Deployment Target 不低于 iOS 12.0

4. 支付功能代码示例

在需要调用支付的ViewController里写以下代码:

import UIKit
import Razorpay

class PaymentViewController: UIViewController, RazorpayPaymentCompletionProtocol {
    var razorpay: Razorpay!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化Razorpay,替换成你的商户Key
        razorpay = Razorpay.initWithKey("你的商户Key", delegate: self)
    }

    // 支付按钮点击事件
    @IBAction func startPayment(_ sender: UIButton) {
        let paymentOptions: [String: Any] = [
            "amount": "100", // 金额单位是最小货币单位,比如100派萨=1印度卢比
            "currency": "INR", // 货币代码,根据你的业务调整
            "description": "测试商品支付",
            "order_id": "你的后端生成的订单ID", // 必填,不能用前端生成的
            "name": "你的商户名称",
            "prefill": [
                "contact": "用户手机号",
                "email": "用户邮箱"
            ]
        ]
        razorpay.open(paymentOptions)
    }

    // 支付成功回调
    func onPaymentSuccess(_ payment_id: String) {
        print("支付成功,ID:\(payment_id)")
        let alert = UIAlertController(title: "支付成功", message: "支付ID:\(payment_id)", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true)
    }

    // 支付失败回调
    func onPaymentError(_ code: Int32, description str: String) {
        print("支付失败,错误码:\(code),描述:\(str)")
        let alert = UIAlertController(title: "支付失败", message: "错误码:\(code)\n描述:\(str)", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true)
    }
}

5. 测试支付流程

  • 用Razorpay提供的测试商户Key和测试订单生成工具生成有效订单ID
  • 运行App,点击支付按钮测试流程
  • 测试通过后,替换成生产环境的商户Key和正式订单ID即可

常见问题排查

  • 再碰到版本错误:执行 pod update Razorpay,强制拉取最新兼容版本
  • 找不到Razorpay.framework:检查Podfile配置,重新执行 pod install
  • 支付弹窗不弹出:检查商户Key、订单ID是否有效,网络连接是否正常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 06:06:06