我现在的问题是如何在付款成功后保存回复。我尝试过以下方法:
func transactionCompleted(withResponse response : NSDictionary,errorDescription error:NSError) -> Void {
self.dismiss(animated: true){
self.showAlertViewWithTitle(title: "Message", message: "congrats! Payment is Successful")
}
}
func transactinFailed(withResponse response : NSDictionary,errorDescription error:NSError) -> Void {
self.dismiss(animated: true){
self.showAlertViewWithTitle(title: "Message", message: "Oops!!! Payment Failed"
}
}
但它无法调用以下函数
var restURL = URL(string: "https://test.payu.in/_payment")
req = NSMutableURLRequest(url: restURL!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60.0)
req.httpMethod = "POST"
var hashValue = "\(KEY_TEST)|\(transactionId)|\(Amount)|\(productInfo)|\(firstName)|\(emailID)|\(udf1)|\(udf2)|\(udf3)|\(udf4)|\(udf5)||||||\(SALT_TEST)"
let strPaymentHash = sha1(string: hashValue)
post = "key=\("\(KEY_TEST)")&amount=\("\(Amount)")&productinfo=\("\(productInfo)")&firstname=\("\(firstName)")&email=\("\(emailID)")&udf1=\("\(udf1)")&udf2=\("\(udf2)")&udf3=\("\(udf3)")&udf4=\("\(udf4)")&udf5=\("\(udf5)")&surl=\("\(SURL)")&furl=\("\(FURL)")&phone=\("\(PHONE)")&hash=\("\(strPaymentHash)")&txnid=\("\(transactionId)")"
print("Post",post)
self.req.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField:"Content-Type")
self.req.httpBody = post.data(using: .utf8)
webView_guide.load(req as URLRequest)
}
答案 0 :(得分:1)
我看到你在解雇完成区内调用self
,这通常是一种不好的做法。
在取消View Controller后执行完成块。这意味着您的视图不再出现在屏幕上。我想你想在那个视图中渲染你的警报视图,这是不可能的,因为它不在屏幕上。
您可以将事件从呈现的控制器委派给父级,并使用委托模式处理它。 Swift是面向委托/协议模式的。
为谁来处理付款响应创建一个界面,例如:
protocol MyTransactionDelegate {
func transactionDidFinish(completed: Bool, response: NSDictionary, error: NSError)
}
添加 presentsViewController 对 parentViewController 的引用:
class PresentedViewController {
weak myDelegate: MyTransactionDelegate?
}
parentViewController 符合MyTransactionDelegate
协议,当它出现 presentsViewController 时,将其自身设置为其委托。
最后要做的是将方法更改为:
func transactionCompleted(withResponse response : NSDictionary,errorDescription error:NSError) -> Void {
self.delegate?.transactionDidFinish(completed: true, response: response, error: error)
self.dismiss(animated: true)
}
func transactionFailed(withResponse response : NSDictionary,errorDescription error:NSError) -> Void {
self.delegate?.transactionDidFinish(completed: false, response: response, error: error)
self.dismiss(animated: true)
}