Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> iOS

Cách tích hợp PayU Money payment gateway trong iOS Swift

PayU Money là cổng thanh toán phổ biến tại Ấn Độ. Để tích hợp vào app iOS, làm theo quy trình SDK chính thức.

Các bước tích hợp

  1. Đăng ký tài khoản: Đăng ký tại payu.in, lấy Merchant Key, Salt, Merchant ID từ Dashboard.
  2. Clone SDK: Terminal:
    git clone --recursive https://github.com/payu-intrepos/PayUMoney-IOS-SDK.git
    
  3. Thêm vào project: Kéo thư mục PlugNPlay từ SDK clone vào project Xcode.
  4. Build Phases: Target > Build Phases > Link Binary With Libraries > Thêm PlugNPlay.framework.
  5. Bridging Header: Thêm #import "PayUServiceHelper.h" vào Bridging Header (Swift project).
  6. Cấu hình PayUServiceHelper.h: Mở file, thay thế các giá trị từ Dashboard:
    
    #define MERCHANT_KEY @"YOUR_MERCHANT_KEY"
    #define SALT @"YOUR_SALT"
    #define MERCHANT_ID @"YOUR_MERCHANT_ID"
    // Environment: 0 = Test, 1 = Production
    #define ENVIRONMENT 0
    
  7. Gọi thanh toán: Trong ViewController bất kỳ:
    
    import PlugNPlay
    
    func initiatePayment() {
        let paymentParam = PayUModelPaymentParams()
        paymentParam.key = "YOUR_MERCHANT_KEY"
        paymentParam.transactionID = "TXN_\(Date().timeIntervalSince1970)"
        paymentParam.amount = "100.00"
        paymentParam.productInfo = "Product Name"
        paymentParam.firstName = "User Name"
        paymentParam.email = "user@example.com"
        paymentParam.phone = "9999999999"
        paymentParam.surl = "https://yoursuccessurl.com"
        paymentParam.furl = "https://yourfailureurl.com"
        paymentParam.environment = .test  // .production cho live
        
        // Hash generation (server-side recommended)
        paymentParam.hashes = generateHashes(for: paymentParam)
        
        PayUServiceHelper.shared().presentPaymentViewController(with: paymentParam, on: self) { result in
            switch result {
            case .success(let response): print("Thành công: \(response)")
            case .failure(let error): print("Thất bại: \(error)")
            }
        }
    }
    

Lưu ý quan trọng

  • Hash generation nên làm server-side để bảo mật Salt. Client-side chỉ dùng cho test.
  • Kiểm tra PlugNPlay.framework có trong Embedded Binaries (General tab).
  • Test kỹ trên môi trường Test (ENVIRONMENT 0) trước khi chuyển Production.
  • Xử lý callback URL (surl/furl) trên backend để xác thực thanh toán.
  • Tuân thủ PCI-DSS nếu lưu trữ thông tin thẻ.

Tham khảo: Tài liệu chính thức PayU iOS SDK trên GitHub repo.