[英]How to integrate Paytm payment gateway in Swift
我已經檢查了所有的教程,並且在網關集成方面也做了大量的研發。 但是沒有找到集成paytm支付網關的方法。
func paymentConfiguration()
{
var orderDict = [AnyHashable: Any]()
orderDict["MID"] = "WorldP64425807474247"
orderDict["CHANNEL_ID"] = "WAP"
orderDict["INDUSTRY_TYPE_ID"] = "Retail"
orderDict["WEBSITE"] = "worldpressplg"
orderDict["TXN_AMOUNT"] = "1"
orderDict["ORDER_ID"] = ViewController.generateOrderID(withPrefix: "")
orderDict["CALLBACK_URL"] = "https://securegw.paytm.in/theia/paytmCallback?ORDER_ID=<ORDER_ID>"
orderDict["CHECKSUMHASH"] = "w2QDRMgp1/BNdEnJEAPCIOmNgQvsi+BhpqijfM9KvFfRiPmGSt3Ddzw+oTaGCLneJwxFFq5mqTMwJXdQE2EzK4px2xruDqKZjHupz9yXev4="
orderDict["REQUEST_TYPE"] = "DEFAULT"
orderDict["CUST_ID"] = "1234567890"
var order = PGOrder(params: orderDict)
}
func openPaytmController()
{
PGServerEnvironment.selectServerDialog(view, completionHandler: {(_ type: ServerType) -> Void in
var txnController = PGTransactionViewController.initTransaction(forOrder: order)
if type != eServerTypeNone {
txnController.serverType = type
txnController.merchant = mc
txnController.delegate = self
self.show(txnController)
}
})
}
任何幫助將不勝感激。 提前致謝
**
PayTM 與 swift 與細節的集成
**
##下載paytm sdk## https://github.com/Paytm-Payments/Paytm_iOS_App_Kit確保動態庫和systemConfiguration.framwork 被添加到“鏈接的二進制文件和框架”中
#import "PaymentsSDK.h"
傳遞以下參數以生成 checkSumHash
let params:[String: Any] = [
"CUST_ID”:<Your Customer ID>, // you have to generate unique customer ID (Generate random string - less than 50 length count )
"TXN_AMOUNT":"10.00", // sample amount
“MID": <Your merchant ID>,
"ORDER_ID”:<Your Order ID>, // you have to generate unique order ID (Generate random string - less than 50 length count )
"INDUSTRY_TYPE_ID":"Retail", //Staging Environment
"CHANNEL_ID":"WAP", //Staging Environment
"WEBSITE":"APPSTAGING", //Staging Environment - Mobile
"CALLBACK_URL":"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=\(<Your Order ID>)” // This should be important one and make sure the correct order ID.
]
let params:[String: Any] = [
"CUST_ID”:<Your Customer ID>, // you have to generate unique customer ID (Generate random string - less than 50 length count )
"TXN_AMOUNT":"10.00", // sample amount
“MID": <Your merchant ID>,
"ORDER_ID”:<Your Order ID>, // you have to generate unique order ID (Generate random string - less than 50 length count )
"INDUSTRY_TYPE_ID":"Retail", //Staging Environment
"CHANNEL_ID":"WAP", //Staging Environment
"WEBSITE":"APPSTAGING", //Staging Environment - Mobile
"CALLBACK_URL":"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=\(<Your Order ID>)” // This should be important one and make sure the correct order ID. ,“CHECKSUMHASH”:<your geenrated CheckSumHash key> // which you got the response of generate CheckSumHash
]
let order = PGOrder(params: params)
let txnController = PGTransactionViewController(transactionFor: order)
txnController?.serverType = eServerTypeStaging
txnController?.merchant = PGMerchantConfiguration.default()
txnController?.merchant.checksumGenerationURL = CheckSumGenerationURL
txnController?.merchant.merchantID = "FlotaS90100524961231"
txnController?.merchant.checksumValidationURL = CheckSumVerifyURL + orderID
txnController?.loggingEnabled = true
txnController?.merchant.website = "APPSTAGING"
txnController?.merchant.industryID = "Retail"
txnController?.serverType = eServerTypeStaging
txnController?.delegate = self
self.navigationController?.pushViewController(txnController!, animated: true)
func didSucceedTransaction(controller: PGTransactionViewController, response: [NSObject : AnyObject]) {
print(response)
}
func didFinishedResponse(_ controller: PGTransactionViewController!, response responseString: String!) {
print(responseString) // Response will be in string
let data = responseString.data(using: .utf8)!
let obj = JSON(data: data)
if obj["STATUS"].stringValue != "TXN_SUCCESS" {
//handle what you want
}
}
}
func didFailTransaction(_ controller: PGTransactionViewController!, error: Error!, response: [AnyHashable : Any]!) {
print(error)
}
func didCancelTrasaction(_ controller: PGTransactionViewController!) {
print("User camcelled the trasaction")
controller.navigationController?.popViewController(animated: true)
}
func errorMisssingParameter(_ controller: PGTransactionViewController!, error: Error!) {
print(error.localizedDescription)
controller.navigationController?.popViewController(animated: true)
}
試試這個代碼:
func showController(controller: PGTransactionViewController) {
if self.navigationController != nil {
self.navigationController?.pushViewController(controller, animated: true)
} else {
self.present(controller, animated: true, completion: nil)
}
}
func removeController(controller: PGTransactionViewController) {
if self.navigationController != nil {
self.navigationController?.popViewController(animated: true)
} else {
controller.dismiss(animated: true, completion: nil)
}
}
//Creat Payment----------------
func creatPayment(CheckSum: String) {
let mc = PGMerchantConfiguration.default()!
var orderDict = [String : Any]()
orderDict["MID"] = "WorldP64425807474247";
orderDict["ORDER_ID"] = ViewController.generateOrderID(withPrefix: "");
orderDict["CUST_ID"] = "1234567890";
orderDict["INDUSTRY_TYPE_ID"] = "Retail";
orderDict["CHANNEL_ID"] = "WAP";
orderDict["TXN_AMOUNT"] = self.FINAL_AMOUNT;
orderDict["WEBSITE"] = "APP_STAGING";
orderDict["CALLBACK_URL"] = "https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp";
orderDict["CHECKSUMHASH"] = CheckSum;
let pgOrder = PGOrder(params: orderDict )
let transaction = PGTransactionViewController.init(transactionFor: pgOrder)
transaction!.serverType = eServerTypeStaging
transaction!.merchant = mc
transaction!.loggingEnabled = true
transaction!.delegate = self
self.showController(controller: transaction!)
}
func didFinishedResponse(_ controller: PGTransactionViewController!, response responseString: String!) {
print(responseString)
}
func didCancelTrasaction(_ controller: PGTransactionViewController!) {
print("CANCELLED")
}
func errorMisssingParameter(_ controller: PGTransactionViewController!, error: Error!) {
self.removeController(controller: controller)
print(error)
}
您已將校驗和作為硬核提供,因此,當 paytm 解碼您的校驗和並與您的參數進行比較時,比較將失敗。 因此,您每次付款時都需要提供新的校驗和。
您需要注意的主要事情是,校驗和生成和付款設置的給定參數應該相同。 可以借助paytm提供的校驗和生成工具在后端生成校驗和
您應該在 Generate checksumApi 成功調用func creatPayment(CheckSum: String)
並將校驗和提供給函數中的參數
在 Swift 5.0 中,請按照以下步驟操作:-
1:- 按照以下步驟在您的項目中下載和導入庫:-
從以下位置下載 SDK:- https://github.com/Paytm-Payments/Paytm_iOS_App_Kit
在 XCode 中打開您的項目,然后從文件菜單中選擇將文件添加到“您的項目”
在剛剛解壓的目錄中選擇Paytm.framework
確保選中“需要時復制項目”,然后在項目設置的“構建階段”選項卡中的“鏈接二進制與庫”下單擊“添加”,添加 SystemConfiguration.framework
檢查 PaytmSDK.framework 是否添加到“Link Binary With Libraries”和“Embedded Binaries”中。 如果沒有,請單擊加號圖標添加。
將 PaytmSDK 導入 ViewController
import PaymentSDK
生成校驗和哈希
防止篡改的安全參數。 使用 Paytm 提供的服務器端校驗和實用程序生成。 商家必須確保這總是在服務器上生成。 生成校驗和哈希的實用程序可用
在您的支付視圖控制器上添加代碼以執行支付流程
var txnController = PGTransactionViewController()
var serv = PGServerEnvironment()
var params = [String:String]()
var order_ID:String?
var cust_ID:String?
func beginPayment() {
serv = serv.createStagingEnvironment()
let type :ServerType = .eServerTypeStaging
let order = PGOrder(orderID: "", customerID: "", amount: "", eMail: "", mobile: "")
order.params = ["MID": "rxazcv89315285244163",
"ORDER_ID": "order1",
"CUST_ID": "cust123",
"MOBILE_NO": "7777777777",
"EMAIL": "username@emailprovider.com",
"CHANNEL_ID": "WAP",
"WEBSITE": "WEBSTAGING",
"TXN_AMOUNT": "100.12",
"INDUSTRY_TYPE_ID": "Retail",
"CHECKSUMHASH": "oCDBVF+hvVb68JvzbKI40TOtcxlNjMdixi9FnRSh80Ub7XfjvgNr9NrfrOCPLmt65UhStCkrDnlYkclz1qE0uBMOrmuKLGlybuErulbLYSQ=",
"CALLBACK_URL": "https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=order1"]
self.txnController = self.txnController.initTransaction(for: order) as! PGTransactionViewController
self.txnController.title = "Paytm Payments"
self.txnController.setLoggingEnabled(true)
if(type != ServerType.eServerTypeNone) {
self.txnController.serverType = type;
} else {
return
}
self.txnController.merchant = PGMerchantConfiguration.defaultConfiguration()
self.txnController.delegate = self
self.navigationController?.pushViewController(self.txnController, animated: true)
}
更改“CHECKSUMHASH”:來自 API 的參數中的值
order.params["CHECKSUMHASH"] = checkSum // API checkSum value
確認“PGTransactionDelegate”的協議委托以處理錯誤和成功響應
要處理支付完成時的成功/錯誤,請實現“PGTransactionDelegate”的“didFinishedResponse”、“didCancelTrasaction”、“errorMisssingParameter”方法。 下面提供的代碼片段:-
extension PaymentViewController : PGTransactionDelegate {
//this function triggers when transaction gets finished
func didFinishedResponse(_ controller: PGTransactionViewController, response responseString: String) {
let msg : String = responseString
var titlemsg : String = ""
if let data = responseString.data(using: String.Encoding.utf8) {
do {
if let jsonresponse = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] , jsonresponse.count > 0{
titlemsg = jsonresponse["STATUS"] as? String ?? ""
}
} catch {
debugLog("Something went wrong")
}
}
let actionSheetController: UIAlertController = UIAlertController(title: titlemsg , message: msg, preferredStyle: .alert)
let cancelAction : UIAlertAction = UIAlertAction(title: "OK", style: .cancel) {
action -> Void in
controller.navigationController?.popViewController(animated: true)
}
actionSheetController.addAction(cancelAction)
self.present(actionSheetController, animated: true, completion: nil)
}
//this function triggers when transaction gets cancelled
func didCancelTrasaction(_ controller : PGTransactionViewController) {
controller.navigationController?.popViewController(animated: true)
}
//Called when a required parameter is missing.
func errorMisssingParameter(_ controller : PGTransactionViewController, error : NSError?) {
controller.navigationController?.popViewController(animated: true)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.