单军华
2016-12-30 e05aab7eeb55ede6fe6226e41c65b6bfad65957e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
//  ViewController.swift
//  demoapp-swift
//
//  Created by Afon on 15/4/30.
//  Copyright (c) 2015年 Pingplusplus. All rights reserved.
//
 
import UIKit
 
let kBackendChargeURL = "http://218.244.151.190/demo/charge" // 你的服务端创建并返回 charge 的 URL 地址,此地址仅供测试用。
// 以下参数的相关说明请查看 Objective-C 相关文件
let kAppURLScheme = "demoappswift" // 这个是你定义的 URL Scheme,支付宝、微信支付和测试模式需要。
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
    override func viewDidAppear(_ animated: Bool) {
 
        let postDict : AnyObject = NSDictionary(objects: ["alipay", "10"], forKeys: ["channel" as NSCopying, "amount" as NSCopying])
        var postData: NSData = NSData()
        do {
            try postData = JSONSerialization.data(withJSONObject: postDict, options: JSONSerialization.WritingOptions.prettyPrinted) as NSData
        } catch {
            print("Serialization error")
        }
        
        let url = NSURL(string: kBackendChargeURL)
        let session = URLSession.shared
        let request = NSMutableURLRequest(url: url! as URL)
        request.httpMethod = "POST"
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.httpBody = postData as Data
 
        let sessionTask = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
            if data != nil {
                let charge = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                print(charge! as String)
                
                    Pingpp.createPayment(charge! as String as String as NSObject!, appURLScheme: kAppURLScheme) { (result, error) -> Void in
                        print(result)
                        if error != nil {
                            print(error?.code.rawValue)
                            print(error?.getMsg())
                        }
                    }
                
            } else {
                print("response data is nil")
            }
        }
        sessionTask.resume()
    }
}