在 Swift 5.0 中,请执行以下步骤:-
1:- 按照以下步骤下载并导入项目中的库:-
从以下位置下载 SDK:-
https://github.com/Paytm-Payments/Paytm_iOS_App_Kit
在 XCode 中打开您的项目,然后从 File 菜单中选择 Add files to "your project"
在刚刚解压的目录中选择Paytm.framework
确保选中“如果需要则复制项目”,然后在项目设置的“构建阶段”选项卡中的“将二进制文件与库链接”下单击“添加”,添加 SystemConfiguration.framework
检查是否在“Link Binary With Libraries”和“Embedded Binaries”中都添加了 PaytmSDK.framework。如果没有,请单击加号图标添加。
将 PaytmSDK 导入 ViewController
import PaymentSDK
生成校验和哈希
避免篡改的安全参数。使用 Paytm 提供的服务器端校验和实用程序生成。商家必须确保这始终在服务器上生成。生成校验和哈希的实用程序可用
在您的支付 ViewController 上添加代码以执行支付流程
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)
}
}