|
@@ -7,6 +7,14 @@
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
+// paypal alipay wxpay paddle
|
|
|
+@objc enum KMPaymentType: UInt32 {
|
|
|
+ case paypal = 1
|
|
|
+ case alipay
|
|
|
+ case wxpay
|
|
|
+ case paddle
|
|
|
+}
|
|
|
+
|
|
|
class KMPurchaseEmbeddedWindowController: NSWindowController {
|
|
|
|
|
|
override func windowDidLoad() {
|
|
@@ -32,85 +40,109 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
|
|
|
}
|
|
|
|
|
|
@objc func btnAction() {
|
|
|
- self.subscriptions()
|
|
|
-// self.retrieval()
|
|
|
-// self.getPriceUrl()
|
|
|
+// self.subscriptions()
|
|
|
+ // 升级产品
|
|
|
+ let productCode = "com.brother.pdfreaderpro.windows.product_1"
|
|
|
+// let email = "tangchao@kdanmobile.com"
|
|
|
+// self._buyProduct(productCode, count: 1, discountId: "", payment: .alipay, license: "", email: email, username: "") { [weak self] info, err in
|
|
|
+// // paypal_order
|
|
|
+// // page_pay_url 链接
|
|
|
+// // https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3V503434MD108282U
|
|
|
+// // trade_no 订单id 用于校验订单 【22024083006591098】
|
|
|
+// if let dataInfo = info {
|
|
|
+// let trade_no = info?["trade_no"]
|
|
|
+// let page_pay_url = info?["page_pay_url"]
|
|
|
+//
|
|
|
+// DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
+// self?._getOrderStatus(tradeNo: trade_no as? String ?? "", callback: { info, err in
|
|
|
+//
|
|
|
+// })
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// // 数据错误
|
|
|
+// }
|
|
|
+// }
|
|
|
+// let discountId = "E85B-9C31-6EAB"
|
|
|
+// self._getDiscount(productId: productCode, discountId: discountId) { info, err in
|
|
|
+// if let dataInfo = info {
|
|
|
+// let status = dataInfo["status"] as? String ?? ""
|
|
|
+// if status == "unused" {
|
|
|
+// // 折扣(eg. 0.7)
|
|
|
+// let discount = dataInfo["discount"]
|
|
|
+// // 价格 (应该是美元)
|
|
|
+// let price = dataInfo["price"]
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ self._getProductDatas { info, err in
|
|
|
+ /*
|
|
|
+ activity_cny_price 优惠价(人民币)
|
|
|
+ activity_price 优惠价(美元)
|
|
|
+ price 原价(美元)
|
|
|
+ cny_price 原价(人民币)
|
|
|
+ */
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 获取价格
|
|
|
- func getPriceUrl() {
|
|
|
+ private func _getProductDatas(callback: @escaping (([String:Any]?, Any?)->Void)) {
|
|
|
+// func getPriceUrl() {
|
|
|
let urlString = kVerificationServer + "/api/product/get-price/v2"
|
|
|
- KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: nil) { [self] task, responseObject, error in
|
|
|
+ KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: nil) { task, responseObject, error in
|
|
|
if (error == nil && responseObject != nil) {
|
|
|
let array = responseObject?["data"] as? NSDictionary
|
|
|
if array != nil {
|
|
|
- /*
|
|
|
- activity_cny_price 优惠价(人民币)
|
|
|
- activity_price 优惠价(美元)
|
|
|
- price 原价(美元)
|
|
|
- cny_price 原价(人民币)
|
|
|
- */
|
|
|
//解析数据
|
|
|
let productsDict = array?.object(forKey: "data") as? NSDictionary
|
|
|
+ callback(productsDict as? [String : Any], nil)
|
|
|
} else {
|
|
|
- print("解析数据失败array")
|
|
|
+ callback([:], error.debugDescription)
|
|
|
}
|
|
|
} else {
|
|
|
- print("解析数据失败数据不存在")
|
|
|
+ callback(nil, error.debugDescription)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取优惠卷
|
|
|
- func retrieval() {
|
|
|
+ private func _getDiscount(productId: String, discountId: String, callback: @escaping (([String : Any]?, Any?)->Void)) {
|
|
|
let urlString = kVerificationServer + "/api/coupons/retrieval"
|
|
|
-
|
|
|
- let productCode = "com.brother.pdfreaderpro.windows.product_1"
|
|
|
let app_code = MainBundle.km_infoDictionary()?["CFBundleIdentifier"] ?? ""
|
|
|
let params = [
|
|
|
// 优惠卷id
|
|
|
- "code" : "E85B-9C31-6EAB",
|
|
|
+ "code" : discountId,
|
|
|
"app_code" : app_code,
|
|
|
- "product_code" : productCode]
|
|
|
- KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: params) { [self] task, responseObject, error in
|
|
|
+ "product_code" : productId]
|
|
|
+ KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: params) { task, responseObject, error in
|
|
|
if (error == nil && responseObject != nil) {
|
|
|
- let array = responseObject?["data"] as? NSDictionary
|
|
|
- if array != nil {
|
|
|
- //解析数据
|
|
|
- let status = array?.object(forKey: "status") as? String ?? ""
|
|
|
- if status == "unused" {
|
|
|
- // 折扣(eg. 0.7)
|
|
|
- let discount = array?.object(forKey: "discount")
|
|
|
- // 价格 (应该是美元)
|
|
|
- let price = array?.object(forKey: "price")
|
|
|
- }
|
|
|
+ if let dataDict = responseObject?["data"] as? NSDictionary {
|
|
|
+ callback(dataDict as? [String : Any], nil)
|
|
|
} else {
|
|
|
- print("解析数据失败array")
|
|
|
+ callback([:], error.debugDescription)
|
|
|
}
|
|
|
} else {
|
|
|
- print("解析数据失败数据不存在")
|
|
|
+ callback(nil, error.debugDescription)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func getOrderStatus(trade_no: String) {
|
|
|
+ // 获取订单状态
|
|
|
+
|
|
|
+ private func _getOrderStatus(tradeNo: String, callback: @escaping (([String:Any]?, Any?)->Void)) {
|
|
|
let urlString = kVerificationServer + "/api/orders/getOrderStatus"
|
|
|
|
|
|
- // 升级产品
|
|
|
- let paraDict = ["trade_no" : trade_no]
|
|
|
- var jsonD: String = ""
|
|
|
- if let jsonData = try? JSONSerialization.data(withJSONObject: paraDict, options:[]),
|
|
|
- let jsonString = String(data: jsonData, encoding: .utf8) {
|
|
|
- jsonD = jsonString
|
|
|
+ var jsonStr: String = ""
|
|
|
+ if let jsonData = try?JSONSerialization.data(withJSONObject: ["trade_no" : tradeNo], options:[]),
|
|
|
+ let data = String(data: jsonData, encoding: .utf8) {
|
|
|
+ jsonStr = data
|
|
|
}
|
|
|
|
|
|
- let tool = KMVerificationRSA()
|
|
|
- let publicKey = tool.loadPublicKey()
|
|
|
- let encryptedData = tool.encrypt(plainText: jsonD, publicKey: publicKey!)
|
|
|
- print("Encrypted data: \(encryptedData?.base64EncodedString() ?? "nil")")
|
|
|
- let encryptedString = encryptedData?.base64EncodedString();
|
|
|
+ let rsa = KMVerificationRSA()
|
|
|
+ let publicKey = rsa.loadPublicKey()
|
|
|
+ let encryptedData = rsa.encrypt(plainText: jsonStr, publicKey: publicKey!)
|
|
|
+ let tokenStr = encryptedData?.base64EncodedString()
|
|
|
|
|
|
- var postData = try! JSONSerialization.data(withJSONObject: ["token" : encryptedString])
|
|
|
+ var postData = try!JSONSerialization.data(withJSONObject: ["token" : tokenStr])
|
|
|
var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
|
|
|
request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
|
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
@@ -120,50 +152,48 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
|
|
|
|
|
|
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
|
|
guard let data = data else {
|
|
|
- print(String(describing: error))
|
|
|
+ callback(nil, String(describing: error))
|
|
|
return
|
|
|
}
|
|
|
- let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
|
|
|
- if jsonObject != nil {
|
|
|
- let data1 = jsonObject!["data"] as? String
|
|
|
+ if let jsonDict = try?JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:] {
|
|
|
+ let dataStr = jsonDict["data"] as? String
|
|
|
|
|
|
- let tool = KMVerificationRSA()
|
|
|
+ // ras 解密 目前没有调通
|
|
|
+ let rsa = KMVerificationRSA()
|
|
|
// XJYSy+T0k9VZbH1SJlaWj3FBKmrrDQCBk6PnAT0N4syzWNjdk3u89x3oXpZ8BZ37z9Dbv799QepQgQgGspBB/AXjtfYEICYpzs1RD2juTIglOqzakrJZOl+0s9M4R+pdbsigL+P39NnqWNXHkVZGDLrbL9ZNLuH2IztxHd/EUGmkwJUyx0ZIPCuBMD80EErlZt/iMGDa5zIww6dCrzpmT1o1qAXPkEri6WkDf/IG0JqytDsmXmEtCtOegc44Q9rpbsyBdp4YYqoeQ6/88/2TtFySd06dBIZkMZzNW8eN1MxmeWEDdo4Ji5uQz32aH/qPAi7XUXAg9Y3luBLyodt24A==
|
|
|
- let ss = tool.decrypt(source: data1! as NSString)
|
|
|
-
|
|
|
- KMPrint("")
|
|
|
+ let str = rsa.decrypt(source: dataStr as? NSString ?? "")
|
|
|
} else {
|
|
|
// 数据错误
|
|
|
+ callback([:], String(describing: error))
|
|
|
}
|
|
|
}
|
|
|
task.resume()
|
|
|
}
|
|
|
|
|
|
- func subscriptions() {
|
|
|
+ private func _buyProduct(_ productId: String, count: Int, discountId: String = "", payment: KMPaymentType, license: String = "", email: String, username: String = "", callback: @escaping (([String:Any]?, Any?)->Void)) {
|
|
|
let urlString = kVerificationServer + "/api/subscriptions"
|
|
|
|
|
|
- // 升级产品
|
|
|
- let productCode = "com.brother.pdfreaderpro.windows.product_1"
|
|
|
let app_code = MainBundle.km_infoDictionary()?["CFBundleIdentifier"] ?? ""
|
|
|
+ let paymentStr = self._paymentTypeString(type: payment)
|
|
|
let paraDict = ["data" :
|
|
|
- ["product_code": productCode,
|
|
|
+ ["product_code": productId,
|
|
|
"app_code": app_code,
|
|
|
// 购买数量
|
|
|
- "amount": "1",
|
|
|
+ "amount": "\(count)",
|
|
|
// 优惠卷id
|
|
|
- "coupon_code": "",
|
|
|
+ "coupon_code": discountId,
|
|
|
// 支付类型 paypal alipay wxpay paddle
|
|
|
- "payment": "alipay",
|
|
|
+ "payment": paymentStr,
|
|
|
// 地区
|
|
|
"customer_country": "US",
|
|
|
// 邮编码
|
|
|
"customer_postcode" : "20001",
|
|
|
"unique_sn" : uuid(),
|
|
|
// 序列码 标准包 升级 高级包 需要
|
|
|
- "upgrade_cdkey" : "",
|
|
|
- "email" : "tangchao@kdanmobile.com",
|
|
|
+ "upgrade_cdkey" : license,
|
|
|
+ "email" : email,
|
|
|
// 用户名
|
|
|
- "contact_name" : uuid()]]
|
|
|
+ "contact_name" : username]]
|
|
|
var postData = try! JSONSerialization.data(withJSONObject: paraDict)
|
|
|
var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
|
|
|
request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
|
|
@@ -173,33 +203,24 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
|
|
|
request.httpBody = postData
|
|
|
|
|
|
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
|
|
- guard let data = data else {
|
|
|
- print(String(describing: error))
|
|
|
- return
|
|
|
- }
|
|
|
- let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:]
|
|
|
- if jsonObject != nil {
|
|
|
- let data1: NSDictionary = jsonObject!["data"] as? NSDictionary ?? [:]
|
|
|
+ guard let theData = data else {
|
|
|
+ callback(nil, String(describing: error))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if let jsonDict = try? JSONSerialization.jsonObject(with: theData, options: []) as? NSDictionary ?? [:] {
|
|
|
+ let data1: NSDictionary = jsonDict["data"] as? NSDictionary ?? [:]
|
|
|
let subscriptionInfo = data1.object(forKey: "subscription") as? NSDictionary
|
|
|
-// let paypal_order_info = subscriptionInfo?.object(forKey: "paypal_order") as? NSDictionary
|
|
|
- let paypal_order_info = subscriptionInfo?.object(forKey: "alipay_order") as? NSDictionary
|
|
|
- if let dataInfo = paypal_order_info, dataInfo.count > 0 {
|
|
|
- // paypal_order
|
|
|
- // page_pay_url 链接
|
|
|
- // https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3V503434MD108282U
|
|
|
- // trade_no 订单id 用于校验订单 【22024083006591098】
|
|
|
- let trade_no = dataInfo.object(forKey: "trade_no")
|
|
|
- let page_pay_url = dataInfo.object(forKey: "page_pay_url")
|
|
|
- KMPrint("trade_no:" + "\(trade_no)" + ";" + "page_pay_url:" + "\(page_pay_url)")
|
|
|
-
|
|
|
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
- self.getOrderStatus(trade_no: trade_no as? String ?? "")
|
|
|
- }
|
|
|
+ let orderKey = self._paymentTypeString(type: payment) + "_order"
|
|
|
+ let order_info = subscriptionInfo?.object(forKey: orderKey) as? NSDictionary
|
|
|
+ if let dataInfo = order_info, dataInfo.count > 0 {
|
|
|
+ callback(dataInfo as? [String : Any], nil)
|
|
|
} else {
|
|
|
// 数据错误
|
|
|
+ callback([:], String(describing: error))
|
|
|
}
|
|
|
} else {
|
|
|
// 数据错误
|
|
|
+ callback([:], String(describing: error))
|
|
|
}
|
|
|
}
|
|
|
task.resume()
|
|
@@ -211,20 +232,17 @@ class KMPurchaseEmbeddedWindowController: NSWindowController {
|
|
|
return GetHardwareUUID()!
|
|
|
}
|
|
|
|
|
|
- private func platform() -> String {
|
|
|
- var platform = "DMG"
|
|
|
-#if VERSION_DMG
|
|
|
- platform = "DMG"
|
|
|
-#else
|
|
|
- platform = "AiStore"
|
|
|
-#endif
|
|
|
- return platform
|
|
|
- }
|
|
|
-
|
|
|
- private func app_name() -> String {
|
|
|
- var bundleID: String!
|
|
|
- bundleID = Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") as! String
|
|
|
- return bundleID;
|
|
|
+ private func _paymentTypeString(type: KMPaymentType) -> String {
|
|
|
+ if type == .paddle {
|
|
|
+ return "paddle"
|
|
|
+ } else if type == .alipay {
|
|
|
+ return "alipay"
|
|
|
+ } else if type == .wxpay {
|
|
|
+ return "wxpay"
|
|
|
+ } else if type == .paddle {
|
|
|
+ return "paddle"
|
|
|
+ }
|
|
|
+ return ""
|
|
|
}
|
|
|
|
|
|
}
|