本文介绍了PayU Money Gateway iOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用swift2.0将payU Money sdk集成到我的应用程序中

I want to integrate the payU Money sdk in my app using swift2.0

我正在使用此sdk: https://github.com/payu-intrepos/Documentations/wiki/8.1-NEW-iOS-Seamless-SDK-integration

I am using this sdk: https://github.com/payu-intrepos/Documentations/wiki/8.1-NEW-iOS-Seamless-SDK-integration

在何处创建用于测试的测试帐户.

Where to create the test account for testing.

推荐答案

// Swift 4
import UIKit
import SystemConfiguration
import Foundation

class PayUMoneyViewController: UIViewController, UIWebViewDelegate, UIAlertViewDelegate {

    @IBOutlet weak var webView: UIWebView!

    var merchantKey = "YOUR_MARCHANT_KEY"
    var salt = "YOUR_SALT_KEY"
    var PayUBaseUrl = "https://secure.payu.in"
    var SUCCESS_URL = "https://www.payumoney.com/payments/guestcheckout/#/success"
    var FAILED_URL = "https://www.PayUmoney.com/mobileapp/PayUmoney/failure.php"

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    var request = NSMutableURLRequest()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView.delegate = self
        self.payPayment()

        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PayUMoneyViewController.back(sender:)))
        self.navigationItem.leftBarButtonItem = newBackButton

    }

     @objc func back(sender: UIBarButtonItem) {


        let alert = UIAlertController(title: "Cancel !", message: "Do you really want to cancel the transaction ?", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: cancelTransaction))
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))

        self.present(alert, animated: true, completion: nil)
    }

    func cancelTransaction( action : UIAlertAction)
    {
        _ = navigationController?.popToRootViewController(animated: true)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func payPayment() {

        var i = arc4random()

        let amount = "1"
        let productInfo = "Transport"
        let firstName = USER_FIRST_NAME // Geet
        let email = USER_EMAIL // [email protected]
        let phone = USER_MOBILE_NO // 1234567890
        let sUrl = "https://www.google.com"
        let fUrl = "https://www.bing.com"
        let service_provider = "payu_paisa"
        let strHash:String = self.sha1(toEncrypt: String.localizedStringWithFormat("%d%@", i, NSDate()));
        let r1 = strHash.range(of: strHash)!

        // String range to NSRange:
        let n1 = NSRange(r1, in: strHash)
        print((strHash as NSString).substring(with: n1)) //

        // NSRange back to String range:
        let r2 = Range(n1, in: strHash)!
        print(strHash.substring(with: r2))
        let rangeOfHello = Range(n1, in: strHash)!
        let txnid1 = strHash.substring(with: rangeOfHello)
        let hashValue = String.localizedStringWithFormat("%@|%@|%@|%@|%@|%@|||||||||||%@",merchantKey,txnid1,amount,productInfo,firstName,email,salt)

        let hash = self.sha1(toEncrypt: hashValue)

        let postStr = "txnid="+txnid1+"&key="+merchantKey+"&amount="+amount+"&productinfo="+productInfo+"&firstname="+firstName+"&email="+email+"&phone="+phone+"&surl="+sUrl+"&furl="+fUrl+"&hash="+hash+"&service_provider="+service_provider

        let url = NSURL(string: String.localizedStringWithFormat("%@/_payment", PayUBaseUrl))

        print("check my url", url as Any, postStr)

        let request = NSMutableURLRequest(url: url! as URL)

        do {

            let postLength = String.localizedStringWithFormat("%lu",postStr.characters.count)
            request.httpMethod = "POST"
            request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Current-Type")
            request.setValue(postLength, forHTTPHeaderField: "Content-Length")
            request.httpBody = postStr.data(using: String.Encoding.utf8)
            webView.loadRequest(request as URLRequest)
        } catch {
            print(error)

        }

    }
    func webViewDidFinishLoad(_ webView: UIWebView) {

        let requestURL = self.webView.request?.url
        let requestString:String = (requestURL?.absoluteString)!
        if (requestString == SUCCESS_URL) {
            print("success payment done")
        }
        else if (requestString == FAILED_URL) {
            print("payment failure")
        }
    }

    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        print("double failure")
    }

    func sha1(toEncrypt: String) -> String {
        var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
        if let data = toEncrypt.data(using: String.Encoding.utf8) {
            let value =  data as NSData
            CC_SHA512(value.bytes, CC_LONG(data.count), &digest)

        }
        var digestHex = ""
        for index in 0..<Int(CC_SHA512_DIGEST_LENGTH) {
            digestHex += String(format: "%02x", digest[index])
        }

        return digestHex
    }

    /*
    func sha1(toEncrypt:String) -> String {
        var data = toEncrypt.data(using: String.Encoding.utf8)!
        var digest = [UInt8](repeating: 0, count:Int(CC_SHA512_DIGEST_LENGTH))

        _ = data.withUnsafeBytes {messageBytes in
                CC_SHA512(messageBytes, CC_LONG(data.count), &digest)
            }
        let hexBytes = digest.map { String(format: "%02x", $0) }
        return hexBytes.joined(separator: "")
    }
 */
}

这篇关于PayU Money Gateway iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:37