Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> iOS

วิธีผสานรวม PayU Money ใน iOS ด้วย Swift

PayU Money เป็น Payment Gateway ยอดนิยมในอินเดีย ผสานรวมผ่าน iOS SDK

ขั้นตอนการผสานรวม

1. สมัครบัญชี PayU Money

  • สมัครที่ payu.in
  • ได้ Merchant Key, Salt, Merchant ID จาก Dashboard

2. ติดตั้ง SDK


# Terminal
git clone --recursive https://github.com/payu-intrepos/PayUMoney-IOS-SDK.git

# หรือ CocoaPods (ถ้ามี)
pod 'PayUMoney-PnP-SDK'

3. เพิ่ม Framework ใน Xcode

  1. ลากโฟลเดอร์ PlugNPlay เข้า Project
  2. Target → Build Phases → Link Binary With Libraries → Add PlugNPlay.framework
  3. Build Settings → Framework Search Paths เพิ่ม Path ของ Framework

4. Bridging Header (Swift Project)


// ProjectName-Bridging-Header.h
#import "PayUServiceHelper.h"

Build Settings → Swift Compiler - General → Objective-C Bridging Header → Path ไป Bridging Header

5. กำหนดค่าใน PayUServiceHelper.h


// PayUServiceHelper.h
#define MERCHANT_KEY @"YOUR_MERCHANT_KEY"
#define SALT @"YOUR_SALT"
#define MERCHANT_ID @"YOUR_MERCHANT_ID"
#define ENVIRONMENT @"TEST" // หรือ "PRODUCTION"

6. เรียกใช้ Payment


import PlugNPlay

class PaymentViewController: UIViewController {
    
    func initiatePayment() {
        let paymentParams = PayUModelPaymentParams()
        paymentParams.key = MERCHANT_KEY
        paymentParams.transactionID = "TXN_\(Date().timeIntervalSince1970)"
        paymentParams.amount = "100.00"
        paymentParams.productInfo = "iPhone Case"
        paymentParams.firstName = "John"
        paymentParams.email = "john@example.com"
        paymentParams.phone = "9876543210"
        paymentParams.surl = "https://yoursite.com/success"
        paymentParams.furl = "https://yoursite.com/failure"
        paymentParams.udf1 = "Custom1"
        paymentParams.udf2 = "Custom2"
        paymentParams.udf3 = "Custom3"
        paymentParams.udf4 = "Custom4"
        paymentParams.udf5 = "Custom5"
        
        // เรียก Payment
        PayUServiceHelper.shared().getPaymentRelatedDetail(forPaymentParams: paymentParams) { (response, error) in
            if let error = error {
                print("Error: \(error)")
                return
            }
            // Navigate to PayU Payment Page
            if let payuVC = response as? UIViewController {
                self.present(payuVC, animated: true)
            }
        }
    }
}

Delegate/Callback

Implement PayUServiceHelperDelegate เพื่อรับผลลัพธ์


extension PaymentViewController: PayUServiceHelperDelegate {
    func payUServiceHelper(_ helper: PayUServiceHelper, didFinishWithResult result: PayUModelPaymentResponse) {
        print("Payment Success: \(result.transactionID ?? "")")
        // Handle Success
    }
    
    func payUServiceHelper(_ helper: PayUServiceHelper, didFailWithError error: Error) {
        print("Payment Failed: \(error)")
        // Handle Failure
    }
}

⚠️ ข้อควรระวัง

  • SDK นี้เฉพาะ PayU India (PayU Money) ไม่ใช่ PayU Global
  • ต้องทดสอบบน Device จริง (Simulator ไม่ทำงาน)
  • Hash Generation ต้องทำฝั่ง Server เพื่อ Security
  • ตรวจสอบ Version ใหม่ล่าสุดจาก GitHub Repo