KMPurchaseEmbeddedWindowController.swift 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. //
  2. // KMPurchaseEmbeddedWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/8/29.
  6. //
  7. import Cocoa
  8. #if VERSION_DMG
  9. // paypal alipay wxpay paddle
  10. @objc enum KMPaymentType: UInt32 {
  11. case paypal = 0
  12. case paddle
  13. case wxpay
  14. case alipay
  15. }
  16. @objc enum KMDMGProductType: UInt32 {
  17. case advanced_annual_subscription_trail = 0 // 全平台高级版年订阅 99.9 美金,带免费试用
  18. case advanced_annual_subscription // 全平台高级版年订阅 99.9 美金
  19. case advanced_annual_subscription_blackFive // 全平台高级版年订阅 99.9 美金,黑五订单,优惠价格不为空
  20. case standard_annual_subscription // 全平台标准版年订阅 79.9 美金
  21. case advanced_permanent // Mac & Win 双平台高级永久 119.99 美金
  22. case advanced_permanent_blackFive // Mac & Win 双平台高级永久 119.99 美金,黑五订单,优惠价格不为空
  23. case ai_subscription_year_trail // AI 年订阅 125.99 美金,带免费试用
  24. case ai_subscription_month // AI 月订阅 14.99 美金
  25. }
  26. class KMPurchaseEmbeddedWindowController: NSWindowController {
  27. @IBOutlet weak var mainBox: NSBox!
  28. @IBOutlet weak var yourOrderLabel: NSTextField!
  29. @IBOutlet weak var pdfReaderProLabel: NSTextField!
  30. @IBOutlet weak var prmiumLabel: NSTextField!
  31. @IBOutlet weak var oneLicenseLabel: NSTextField!
  32. @IBOutlet weak var removeButton1: NSButton! // 减少按钮1
  33. @IBOutlet weak var addButton1: NSButton! // 增加按钮1
  34. @IBOutlet weak var amountTextField1: NSTextField! // 数目1
  35. @IBOutlet weak var originalPriceLabel1: NSTextField! // 原价
  36. @IBOutlet weak var discountPriceLabel1: NSTextField! // 折扣
  37. @IBOutlet weak var usualAddonView: NSView!
  38. @IBOutlet weak var firmLabel: NSTextField!
  39. @IBOutlet weak var addbox: NSBox!
  40. @IBOutlet weak var textbox: NSBox!
  41. @IBOutlet weak var removebox: NSBox!
  42. @IBOutlet weak var linebox: NSBox!
  43. @IBOutlet weak var prodocumentView: NSView!
  44. @IBOutlet weak var couponView: NSView!
  45. @IBOutlet private weak var couponButton: NSButton!
  46. @IBOutlet weak var couponBox: NSBox!
  47. @IBOutlet weak var coupomTextField: NSTextField!
  48. @IBOutlet weak var couponClearButton: NSButton!
  49. @IBOutlet weak var applyBox: NSBox!
  50. @IBOutlet weak var applyButton: NSButton!
  51. @IBOutlet weak var coupomErrorLabel: NSTextField!
  52. @IBOutlet weak var coupomErrorTopLayout: NSLayoutConstraint!
  53. @IBOutlet weak var lastPriceLabel: NSTextField!
  54. @IBOutlet weak var lastPriceLabel1: NSTextField!
  55. @IBOutlet weak var discountView: NSView!
  56. @IBOutlet weak var discountLabel: NSTextField!
  57. @IBOutlet weak var discountLabel1: NSTextField!
  58. @IBOutlet weak var paySumLabel: NSTextField!
  59. @IBOutlet weak var paySumLabel1: NSTextField!
  60. @IBOutlet weak var discountLayoutConstraint: NSLayoutConstraint!
  61. @IBOutlet weak var privacyLabel: NSTextField!
  62. @IBOutlet weak var billInformationLabel: NSTextField!
  63. @IBOutlet weak var emailLabel: NSTextField!
  64. @IBOutlet weak var emailTextField: NSTextField!
  65. @IBOutlet weak var emailBox: NSBox!
  66. @IBOutlet weak var errorView: NSView!
  67. @IBOutlet weak var errorLabel: NSTextField!
  68. @IBOutlet weak var errorLabelTopLayout: NSLayoutConstraint!
  69. @IBOutlet weak var paymentMethodLabel: NSTextField!
  70. @IBOutlet weak var paypalButton: NSButton!
  71. @IBOutlet weak var cardButton: NSButton!
  72. @IBOutlet weak var wechatPayButton: NSButton!
  73. @IBOutlet weak var alipayButton: NSButton!
  74. @IBOutlet weak var wechatPayButton2: NSButton!
  75. @IBOutlet weak var payBox: NSBox!
  76. @IBOutlet weak var payBoxHeight: NSLayoutConstraint!
  77. @IBOutlet weak var payExplainLabel: NSTextField!
  78. @IBOutlet var paypalView: NSView!
  79. @IBOutlet weak var paypalBox: NSBox!
  80. @IBOutlet weak var paypalBuyNowLabel: NSTextField!
  81. @IBOutlet var cardView: NSView!
  82. @IBOutlet var paddleView: NSView!
  83. @IBOutlet weak var paddleButton: NSButton!
  84. @IBOutlet weak var paddlePopUpButton: KMPopUpButton!
  85. @IBOutlet weak var paddleBox: NSBox!
  86. @IBOutlet weak var paddleTextField: NSTextField!
  87. @IBOutlet var weChatPayView: NSView!
  88. @IBOutlet weak var weChatBox: NSBox!
  89. @IBOutlet weak var weChatButton: NSButton!
  90. @IBOutlet var payResult: NSView!
  91. @IBOutlet weak var scanLabel: NSTextField!
  92. @IBOutlet weak var qrCodeImageView: NSImageView!
  93. @IBOutlet weak var payTypeImageView: NSImageView!
  94. @IBOutlet weak var successfullyView: NSView!
  95. @IBOutlet weak var purchasedLabel: NSTextField!
  96. @IBOutlet weak var successfullyLabel: NSTextField!
  97. @IBOutlet weak var imageBottomLayout: NSLayoutConstraint!
  98. @IBOutlet weak var image2BottomLayout: NSLayoutConstraint!
  99. @IBOutlet weak var cardButtonWidthConst: NSLayoutConstraint!
  100. private var _product_code : String = ""
  101. private var _product_Info: KMListingProductsModel = KMListingProductsModel(id: "23", productName: "PDF Reader Pro Advanced - Annual Plan", price: "99.99", maxDeviceNum: 4, displayPrice: "0.0", levels: "3", platforms: "Windows,Android,Mac,iOS", productLineId: 1, paymentModel: 1, cycle: 4, cnyPrice: "0.0", displayCnyPrice: "0.0", upgradePrice: "0.0", code: "advanced-annual-subscription-trail")
  102. private var pdfCount: Int = 1
  103. private var _paymentMethod: KMPaymentType = .paypal
  104. private var _productType: ProductType = .ipaConvert
  105. private var couponCode: String = "" //优惠卷
  106. private var discountAmount: String = "" // 优惠金额
  107. private var discountScale: String = "" // 优惠比例
  108. private var orderID: String = "" // 订单ID
  109. private var timer: Timer?
  110. private var pollCount = 0
  111. private let maxPolls = 36
  112. private let interval: TimeInterval = 5.0
  113. private var _activityAlertViewController: KMActivityALertViewController?
  114. private let countryKeys = [
  115. "Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa",
  116. "Andorra", "Angola", "Anguilla", "Antigua and Barbuda", "Argentina",
  117. "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas",
  118. "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
  119. "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire, Sint Eustatius and Saba",
  120. "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil",
  121. "Brit. Indian Ocean", "British Virgin Islands", "Brunei Darussalam", "Bulgaria",
  122. "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde",
  123. "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
  124. "Christmas Island", "Cocos Islands", "Colombia", "Comoros", "Congo",
  125. "Cook Islands", "Costa Rica", "Cote D'Ivoire", "Croatia", "Curaçao", "Cyprus",
  126. "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  127. "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
  128. "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
  129. "French Guiana", "French Polynesia", "French Southern Terr.", "Gabon", "Gambia",
  130. "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada",
  131. "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau",
  132. "Guyana", "Haiti", "Heard/ Mcdonald Islands", "Holy See/ Vatican City", "Honduras",
  133. "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iraq", "Ireland",
  134. "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan",
  135. "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Lao People's DR",
  136. "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein",
  137. "Lithuania", "Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi",
  138. "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
  139. "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
  140. "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique",
  141. "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
  142. "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue",
  143. "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan",
  144. "Palau", "Palestinian Territory", "Panama", "Papua New Guinea", "Paraguay",
  145. "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar",
  146. "Republic of Serbia", "Reunion", "Romania", "Russian Federation", "Rwanda",
  147. "S. Georgia/ Sandwich Islands", "Saint Helena", "Saint Kitts and Nevis",
  148. "Saint Lucia", "Saint Martin", "Saint Pierre and Miquelon", "Saint Vincent/ Grenadines",
  149. "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal",
  150. "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
  151. "Somalia", "South Africa", "South Korea", "Spain", "Sri Lanka", "Sudan",
  152. "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Taiwan",
  153. "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga",
  154. "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands",
  155. "Tuvalu", "U.S. Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates",
  156. "United Kingdom", "United States", "United States (M.O.I.)", "Uruguay", "Uzbekistan",
  157. "Vanuatu", "Venezuela", "Viet Nam", "Wallis and Futuna", "Western Sahara", "Yemen",
  158. "Zambia", "Zimbabwe"
  159. ]
  160. private let countryAbbreviationValues = [
  161. "AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB",
  162. "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "VG", "BN", "BG", "BF", "BI", "KH",
  163. "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CK", "CR", "CI", "HR", "CW", "CY",
  164. "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF",
  165. "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM",
  166. "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI",
  167. "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT",
  168. "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL",
  169. "AN", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH",
  170. "PN", "PL", "PT", "PR", "QA", "RS", "RE", "RO", "RU", "RW", "GS", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM",
  171. "ST", "SA", "SN", "SC", "SL", "SG", "SK", "SI", "SB", "SO", "ZA", "KR", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE",
  172. "CH", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "VI", "UG", "UA", "AE",
  173. "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "WF", "EH", "YE", "ZM", "ZW"
  174. ]
  175. private var handler: ((Int) -> Void)!
  176. static var currentWindowController: KMPurchaseEmbeddedWindowController!
  177. private var embeddedPaymentPopWC: KMEmbeddedPaymentPopWC?
  178. private var successfulPaymentsNumber = 0
  179. private var license1 = ""
  180. private var licenseAI1 = ""
  181. private var isApplyCoupon: Bool = false
  182. private var isWindowDidLoad: Bool = false
  183. private var isBatchProductPriceGetFinsed: Bool = false
  184. @objc static func currentWC(_ productInfo: KMListingProductsModel) -> KMPurchaseEmbeddedWindowController {
  185. if currentWindowController != nil {
  186. currentWindowController.product_Info = productInfo
  187. return currentWindowController
  188. } else {
  189. let configWC: KMPurchaseEmbeddedWindowController = KMPurchaseEmbeddedWindowController.init(windowNibName: "KMPurchaseEmbeddedWindowController")
  190. currentWindowController = configWC;
  191. currentWindowController.product_Info = productInfo
  192. return currentWindowController
  193. }
  194. }
  195. @objc static func currentCode(_ productCode: String) -> KMPurchaseEmbeddedWindowController {
  196. if currentWindowController != nil {
  197. currentWindowController.product_code = productCode
  198. return currentWindowController
  199. } else {
  200. let configWC: KMPurchaseEmbeddedWindowController = KMPurchaseEmbeddedWindowController.init(windowNibName: "KMPurchaseEmbeddedWindowController")
  201. currentWindowController = configWC;
  202. _ = configWC.window
  203. currentWindowController.product_code = productCode
  204. return currentWindowController
  205. }
  206. }
  207. override func windowDidLoad() {
  208. super.windowDidLoad()
  209. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  210. window?.title = ""
  211. window?.makeFirstResponder(nil)
  212. xibInitialization()
  213. paymentMethod = .paypal
  214. priceRefresh(productsModel: product_Info)
  215. self.window?.delegate = self;
  216. isWindowDidLoad = true
  217. _configFirmLabel()
  218. firmLabel.isHidden = true
  219. couponView.isHidden = false
  220. couponButton.isHidden = false
  221. couponBox.isHidden = true
  222. applyBox.isHidden = true
  223. }
  224. override func close() {
  225. super.close()
  226. self.clearEventData()
  227. }
  228. // MARK: DMG Action
  229. func isOneTimePurchase(_ productinfo: KMListingProductsModel) -> Bool {
  230. //买断版本才能一次购买多个,并且购买多个时,不会自动绑定,需要在后台给自己绑定才能有会员权益
  231. if productinfo.paymentModel == 2 {
  232. return true
  233. }
  234. return false
  235. }
  236. func updateProduct_Info() -> Void { //重新获取产品接口
  237. if KMMemberInfo.shared.isLogin {
  238. let model = KMProductModel.shared
  239. model.getDMGProductPriceInfosForMember(productId: product_Info.id, isEducation: 0) { [weak self] success, result in
  240. if success {
  241. guard let productResults : KMMemberProductResult = result else {
  242. return
  243. }
  244. guard let products : [KMListingProductsModel] = productResults.listingProducts else {
  245. return
  246. }
  247. guard let product : KMListingProductsModel = products.first else {
  248. return
  249. }
  250. self?.product_Info = product;
  251. for tProduct in products {
  252. if tProduct.id == self?.product_Info.id {
  253. self?.product_Info = product;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. // MARK: UI
  261. private func xibInitialization() -> Void {
  262. prodocumentView.wantsLayer = true
  263. prodocumentView.layer?.cornerRadius = 5.0
  264. prodocumentView.layer?.borderWidth = 1.0
  265. cardButton.isHidden = true
  266. cardButtonWidthConst.constant = cardButton.isHidden ? 0 : 78
  267. if pdfCount <= 1 {
  268. addbox.fillColor = NSColor(hex: "#F1F1F2")
  269. } else {
  270. addbox.fillColor = NSColor(named: "0E1114 1") ?? .white
  271. }
  272. removebox.fillColor = NSColor(named: "0E1114 1") ?? .white
  273. _updateViewColor()
  274. textbox.fillColor = NSColor(named: "0E1114 2") ?? .white
  275. yourOrderLabel.stringValue = NSLocalizedString("Your Order", comment: "")
  276. yourOrderLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  277. yourOrderLabel.font = NSFont.SFProTextBoldFont(20.0)
  278. pdfReaderProLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  279. pdfReaderProLabel.font = NSFont.SFProTextRegularFont(14.0)
  280. prmiumLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  281. prmiumLabel.font = NSFont.SFProTextRegularFont(12.0)
  282. oneLicenseLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  283. oneLicenseLabel.font = NSFont.SFProTextRegularFont(12.0)
  284. originalPriceLabel1.textColor = NSColor(named: "KMPurchaseTitleColor")
  285. originalPriceLabel1.font = NSFont.SFProTextRegularFont(16.0)
  286. discountPriceLabel1.textColor = NSColor(named: "KMPurchaseDiscountColor")
  287. discountPriceLabel1.font = NSFont.SFProTextRegularFont(9.0)
  288. refreshLicenseCodeView(isReveal: false)
  289. oneLicenseLabel.stringValue = NSLocalizedString("and more offline. It's one time purchase.", comment: "")
  290. oneLicenseLabel.isHidden = true
  291. refreshLicenseCodeView(isReveal: true)
  292. refreshLicenseCodeErrorView(isReveal: false)
  293. removeButton1.isEnabled = false
  294. amountTextField1.delegate = self
  295. couponButton.title = NSLocalizedString("I have a coupon?", comment: "")
  296. couponButton.setTitleColor(color: NSColor(named: "KMPurchaseCouponColor")!, font: NSFont.SFProTextRegularFont(14))
  297. coupomTextField.placeholderString = NSLocalizedString("Please enter a coupon code.", comment: "")
  298. applyButton.title = NSLocalizedString("Apply", comment: "")
  299. applyButton.setTitleColor(color: NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black, font: NSFont.SFProTextRegularFont(13))
  300. couponBox.borderColor = NSColor(named: "KMPurchaseCouponBoxColor") ?? NSColor.gray
  301. couponBox.fillColor = NSColor(named: "KMPurchaseCouponBoxFillColor") ?? NSColor.gray
  302. applyBox.borderColor = NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black
  303. coupomErrorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
  304. coupomErrorLabel.font = NSFont.SFProTextRegularFont(11.0)
  305. refreshCouponError(isReveal: false)
  306. lastPriceLabel.stringValue = NSLocalizedString("List Price", comment: "")
  307. lastPriceLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
  308. lastPriceLabel.font = NSFont.SFProTextRegularFont(16.0)
  309. discountLabel.stringValue = NSLocalizedString("Discount", comment: "")
  310. discountLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
  311. discountLabel.font = NSFont.SFProTextRegularFont(16.0)
  312. paySumLabel.stringValue = NSLocalizedString("Pay sum", comment: "")
  313. paySumLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  314. paySumLabel.font = NSFont.SFProTextBoldFont(24.0)
  315. paySumLabel1.textColor = NSColor(named: "KMPurchaseCouponColor")
  316. paySumLabel1.font = NSFont.SFProTextBoldFont(24.0)
  317. privacyLabel.isEditable = false
  318. privacyLabel.isSelectable = true
  319. privacyLabel.allowsEditingTextAttributes = true
  320. privacyLabel.textColor = NSColor.black
  321. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  322. let tipsString = NSLocalizedString("Are you a student or a professor? %@ / Want a volume purchase? Please %@.", comment: "")
  323. let specialOffer = NSLocalizedString("Get Special Offer", comment: "")
  324. let contactsUs = NSLocalizedString("Contact Us", comment: "")
  325. let fullString = String(format: tipsString, specialOffer, contactsUs)
  326. let attributedString = NSMutableAttributedString(string: fullString)
  327. // 定义链接的范围
  328. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  329. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  330. let linkColor = NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0)
  331. let font = NSFont.SFProTextRegularFont(16.0) // 与普通文本相同的字体
  332. attributedString.addAttributes([
  333. .foregroundColor: NSColor(named: "KMPurchase_99A6B2") as Any,
  334. .font: font
  335. ], range: (fullString as NSString).range(of: fullString))
  336. attributedString.addAttributes([
  337. .foregroundColor: linkColor,
  338. .link: NSLocalizedString("https://www.pdfreaderpro.com/store?mode=edu", comment: ""),
  339. .font: font
  340. ], range: specialOfferRange)
  341. attributedString.addAttributes([
  342. .foregroundColor: linkColor,
  343. .link: NSLocalizedString("https://www.pdfreaderpro.com/vpp-purchase-program", comment: ""),
  344. .font: font
  345. ], range: contactsUsRange)
  346. privacyLabel.attributedStringValue = attributedString
  347. billInformationLabel.stringValue = NSLocalizedString("Billing Information", comment: "")
  348. billInformationLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  349. billInformationLabel.font = NSFont.SFProTextRegularFont(14.0)
  350. emailLabel.stringValue = NSLocalizedString("You can purchase with the account linked to this email. If the email is not registered, an account will be automatically created for you upon successful purchase.", tableName: "MemberCenterLocalizable", comment: "")
  351. emailLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  352. emailLabel.font = NSFont.SFProTextRegularFont(14.0)
  353. emailTextField.placeholderString = NSLocalizedString("Email to receive license code", comment: "")
  354. emailBox.borderColor = NSColor(named: "KMPurchase_DADBDE")!
  355. // emailBox.fillColor = NSColor(named: "KMPurchase_FFFFFF")!
  356. errorLabel.stringValue = NSLocalizedString("Please enter your email for license receiving.", comment: "")
  357. emailTextField.stringValue = KMMemberInfo.shared.userEmail
  358. emailTextField.delegate = self
  359. errorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
  360. errorLabel.font = NSFont.SFProTextRegularFont(11.0)
  361. paymentMethodLabel.stringValue = NSLocalizedString("Payment Method", comment: "")
  362. paymentMethodLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  363. paymentMethodLabel.font = NSFont.SFProTextRegularFont(14.0)
  364. wechatPayButton2.isHidden = true
  365. payExplainLabel.isEditable = false
  366. payExplainLabel.isSelectable = true
  367. payExplainLabel.allowsEditingTextAttributes = true
  368. payExplainLabel.textColor = NSColor.black
  369. payExplainLabel.font = NSFont.SFProTextRegularFont(11.0)
  370. discountPriceLabel1.isHidden = true
  371. let localizedString = NSLocalizedString("By submitting this order, I agree to the %@ and %@ for subscription products", comment: "")
  372. let privacyPolicy = NSLocalizedString("Privacy Policy", comment: "")
  373. let termsOfService = NSLocalizedString("Terms of Service", comment: "")
  374. let fullString1 = String(format: localizedString, privacyPolicy, termsOfService)
  375. let attributedString1 = NSMutableAttributedString(string: fullString1)
  376. let privacyPolicyRange = (fullString1 as NSString).range(of: privacyPolicy)
  377. let termsOfServiceRange = (fullString1 as NSString).range(of: termsOfService)
  378. let linkColor1 = NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0)
  379. let font1 = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  380. attributedString1.addAttributes([
  381. .foregroundColor: NSColor(named: "KMPurchase_99A6B2") as Any,
  382. .font: font1
  383. ], range: (fullString1 as NSString).range(of: fullString1))
  384. attributedString1.addAttributes([
  385. .foregroundColor: linkColor1,
  386. .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
  387. .font: font1
  388. ], range: privacyPolicyRange)
  389. attributedString1.addAttributes([
  390. .foregroundColor: linkColor1,
  391. .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
  392. .font: font1
  393. ], range: termsOfServiceRange)
  394. payExplainLabel.attributedStringValue = attributedString1
  395. paypalBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  396. paypalBuyNowLabel.textColor = NSColor.white
  397. paypalBuyNowLabel.font = NSFont.SFProTextBoldFont(16.0)
  398. paddleButton.title = NSLocalizedString("Continue to Pay", comment: "")
  399. paddleButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  400. paddlePopUpButton.type = .arrowDown
  401. paddlePopUpButton.removeAllItems()
  402. paddlePopUpButton.addItems(withTitles: countryKeys)
  403. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  404. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  405. paddlePopUpButton.selectItem(at: 45)
  406. } else {
  407. paddlePopUpButton.selectItem(at: 229)
  408. }
  409. paddlePopUpButton.wantsLayer = true
  410. paddlePopUpButton.layer?.backgroundColor = NSColor(named: "KMPurchase_FFFFFF")?.cgColor
  411. paddlePopUpButton.layer?.borderColor = NSColor(named: "KMPurchase_DADBDE")?.cgColor
  412. paddleBox.fillColor = NSColor(named: "KMPurchase_FFFFFF") ?? NSColor.white
  413. paddleBox.borderColor = NSColor(named: "KMPurchase_DADBDE") ?? NSColor.gray
  414. paddleTextField.placeholderString = NSLocalizedString("Postcode", comment: "")
  415. paddleTextField.delegate = self
  416. weChatButton.title = NSLocalizedString("Continue to Pay", comment: "")
  417. weChatButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  418. qrCodeImageView.imageScaling = .scaleProportionallyUpOrDown
  419. scanLabel.textColor = NSColor(named: "KMPurchase_000000")
  420. scanLabel.font = NSFont.SFProTextRegularFont(12)
  421. successfullyView.isHidden = true
  422. purchasedLabel.stringValue = NSLocalizedString("Purchased", comment: "")
  423. purchasedLabel.textColor = NSColor(named: "KMPurchase_002143")
  424. purchasedLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  425. successfullyLabel.stringValue = NSLocalizedString("Successfully", comment: "")
  426. successfullyLabel.textColor = NSColor(named: "KMPurchase_002143")
  427. successfullyLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  428. emailErrorLabel(isReveal: false)
  429. }
  430. private func _updateViewColor() {
  431. if KMAppearance.isDarkMode() {
  432. prodocumentView.layer?.borderColor = NSColor(hex: "#E2E3E5").cgColor
  433. } else {
  434. prodocumentView.layer?.borderColor = NSColor(hex: "#E2E3E5").cgColor
  435. }
  436. }
  437. private func priceRefresh(productsModel: KMListingProductsModel) -> Void {
  438. if productsModel.code == "" {
  439. return
  440. }
  441. var listPrice: Float = 0.0
  442. var discount: Float = 0.0
  443. var paySum: Float = 0.0
  444. var discount1: Float = 0.0
  445. var original1: Float = 0.0
  446. var abbreviation = "USD"
  447. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  448. abbreviation = "CNY"
  449. }
  450. let insting = Float(showSinglePriceString())
  451. listPrice = Float(String(format: "%.2f", Float((insting ?? 0) * Float(pdfCount)))) ?? 0 //单价*购买个数
  452. if _product_code == "advanced-annual-subscription-trail" ||
  453. _product_code == "advanced-annual-subscription-blackFive" ||
  454. _product_code == "advanced-permanent-blackFive" ||
  455. _product_code == KMAISubscriptionYearTrailCodeKey_DMG {
  456. // 全平台高级版年订阅 99.9 美金,带免费试用
  457. // 全平台高级版年订阅 99.9 美金,黑五订单
  458. // Mac & Win 双平台高级永久 119.99 美金,黑五订单
  459. // AI 年订阅 125.99 美金,带免费试用
  460. var tipsString = NSLocalizedString("%.2f/year, auto-renewal", comment: "")
  461. if product_Info.cycle == 4 {
  462. tipsString = NSLocalizedString("%.2f/year, auto-renewal", comment: "")
  463. }
  464. if product_Info.cycle == 3 {
  465. tipsString = NSLocalizedString("%.2f/half a year, auto-renewal", comment: "")
  466. }
  467. if product_Info.cycle == 2 {
  468. tipsString = NSLocalizedString("%.2f/quarter, auto-renewal", comment: "")
  469. }
  470. if product_Info.cycle == 1 {
  471. tipsString = NSLocalizedString("%.2f/monthly, auto-renewal", comment: "")
  472. }
  473. if product_Info.cycle == 0 {
  474. tipsString = NSLocalizedString("%.2f, One time purchase.", comment: "")
  475. }
  476. let fullString = String(format: tipsString, insting ?? 0)
  477. prmiumLabel.stringValue = String(format: "%@ %@", abbreviation, fullString)
  478. original1 = Float(showOrgPriceString()) ?? 0
  479. discount1 = Float(showOrgShowPriceString()) ?? 0
  480. paySum = discount1
  481. discount = listPrice - paySum
  482. }
  483. if _product_code == "advanced-annual-subscription" ||
  484. _product_code == "standard-annual-subscription" ||
  485. _product_code == "ai-subscription-month" {
  486. // 全平台高级版年订阅 99.9 美金,无试用
  487. // 全平台标准版年订阅 79.9 美金
  488. // Mac & Win 双平台高级永久 119.99 美金
  489. // AI 月订阅 14.99 美金
  490. var tipsString = NSLocalizedString("%.2f/year, auto-renewal", comment: "")
  491. if product_Info.cycle == 4 {
  492. tipsString = NSLocalizedString("%.2f/year, auto-renewal", comment: "")
  493. }
  494. if product_Info.cycle == 3 {
  495. tipsString = NSLocalizedString("%.2f/half a year, auto-renewal", comment: "")
  496. }
  497. if product_Info.cycle == 2 {
  498. tipsString = NSLocalizedString("%.2f/quarter, auto-renewal", comment: "")
  499. }
  500. if product_Info.cycle == 1 {
  501. tipsString = NSLocalizedString("%.2f/monthly, auto-renewal", comment: "")
  502. }
  503. if product_Info.cycle == 0 {
  504. tipsString = NSLocalizedString("%.2f, One time purchase.", comment: "")
  505. }
  506. let fullString = String(format: tipsString, insting ?? 0)
  507. prmiumLabel.stringValue = String(format: "%@ %@", abbreviation, fullString)
  508. original1 = Float(showOrgPriceString()) ?? 0
  509. discount1 = Float(showOrgShowPriceString()) ?? 0
  510. paySum = discount1
  511. discount = listPrice - paySum
  512. }
  513. if _product_code == "advanced-permanent" {
  514. // Mac & Win 双平台高级永久 119.99 美金
  515. var tipsString = NSLocalizedString("%.2f/year, auto-renewal", comment: "")
  516. if product_Info.cycle == 4 {
  517. tipsString = NSLocalizedString("%.2f/year, auto-renewal", comment: "")
  518. }
  519. if product_Info.cycle == 3 {
  520. tipsString = NSLocalizedString("%.2f/half a year, auto-renewal", comment: "")
  521. }
  522. if product_Info.cycle == 2 {
  523. tipsString = NSLocalizedString("%.2f/quarter, auto-renewal", comment: "")
  524. }
  525. if product_Info.cycle == 1 {
  526. tipsString = NSLocalizedString("%.2f/monthly, auto-renewal", comment: "")
  527. }
  528. if product_Info.cycle == 0 {
  529. tipsString = NSLocalizedString("%.2f, One time purchase.", comment: "")
  530. }
  531. let fullString = String(format: tipsString, insting ?? 0)
  532. prmiumLabel.stringValue = String(format: "%@ %@", abbreviation, fullString)
  533. original1 = Float(showOrgPriceString()) ?? 0
  534. discount1 = Float(showOrgShowPriceString()) ?? 0
  535. paySum = discount1
  536. discount = listPrice - paySum
  537. }
  538. originalPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", insting ?? 0))
  539. lastPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", listPrice))
  540. discountLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount))
  541. paySumLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", paySum))
  542. discountLayoutConstraint.constant = discountView.isHidden ? -CGRectGetHeight(discountView.bounds) : 8
  543. }
  544. // MARK: Active
  545. @IBAction func amountButtonAction(_ sender: NSButton) {
  546. if sender.tag == 0 || sender.tag == 1 {
  547. if sender.tag == 0 {
  548. if pdfCount > 1 {
  549. pdfCount -= 1
  550. }
  551. }
  552. if sender.tag == 1 {
  553. if pdfCount < 9999 {
  554. pdfCount += 1
  555. }
  556. }
  557. if pdfCount > 1 {
  558. removeButton1.isEnabled = true
  559. } else {
  560. removeButton1.isEnabled = false
  561. }
  562. if pdfCount > 100 {
  563. wechatPayButton2.isHidden = false
  564. cardButton.isHidden = true
  565. wechatPayButton.isHidden = true
  566. if _paymentMethod == .paddle {
  567. paymentMethod = .paypal
  568. }
  569. } else {
  570. wechatPayButton2.isHidden = true
  571. cardButton.isHidden = false
  572. wechatPayButton.isHidden = false
  573. }
  574. amountTextField1.stringValue = String(pdfCount)
  575. let viewHidden = pdfCount <= 1
  576. firmLabel.isHidden = viewHidden
  577. if viewHidden {
  578. addbox.fillColor = NSColor(hex: "#F1F1F2")
  579. } else {
  580. addbox.fillColor = NSColor(named: "0E1114 1") ?? .white
  581. }
  582. couponView.isHidden = !viewHidden
  583. }
  584. // 价格刷新
  585. priceRefresh(productsModel: _product_Info)
  586. let model = KMProductModel.shared
  587. isBatchProductPriceGetFinsed = false
  588. if(pdfCount > 1) {
  589. model.getDMGBatchProductPriceInfosForMember(productId: _product_Info.id, num: pdfCount) { [weak self] success, result in
  590. guard let pricesArrays : KMMemberProductResult = result else {
  591. return
  592. }
  593. guard let batchProductPrice : KMBatchProductPriceModel = pricesArrays.batchProductPrice else {
  594. return
  595. }
  596. //原价更新
  597. let priceString = batchProductPrice.totalPrice
  598. self?.product_Info.priceString = priceString
  599. let cnyPriceString = batchProductPrice.cnyTotalPrice
  600. self?.product_Info.cnyPriceString = cnyPriceString //批量时将总价赋给priceString,只显示priceString价格了,其他的置空
  601. self?.product_Info.upgradePriceString = nil
  602. self?.product_Info.displayPriceString = nil
  603. self?.product_Info.displayCnyPriceString = nil
  604. self?.product_Info.individualPriceString = batchProductPrice.price
  605. self?.product_Info.individualCnyPriceString = batchProductPrice.cnyPrice
  606. if self?._paymentMethod == .alipay || self?._paymentMethod == .wxpay {
  607. self?.updatePrice()
  608. }
  609. self?.priceRefresh(productsModel: self?.product_Info ?? KMListingProductsModel())
  610. }
  611. } else {
  612. updateProduct_Info()
  613. }
  614. }
  615. @IBAction func payButtonAction(_ sender: NSButton) {
  616. emailErrorLabel(isReveal: false)
  617. paymentMethod = KMPaymentType(rawValue: uint32(sender.tag)) ?? .paypal
  618. // 价格刷新
  619. priceRefresh(productsModel: _product_Info)
  620. }
  621. @IBAction func couponButtonAction(_ sender: NSButton) {
  622. couponButton.isHidden = true
  623. couponBox.isHidden = false
  624. applyBox.isHidden = false
  625. }
  626. @IBAction func applyButtonAction(_ sender: NSButton) {
  627. if coupomTextField.stringValue == "" {
  628. let alert = NSAlert()
  629. alert.alertStyle = .critical
  630. alert.messageText = NSLocalizedString("Error Information", comment: "")
  631. alert.informativeText = NSLocalizedString("Please enter a coupon code.", comment: "")
  632. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  633. alert.runModal()
  634. return
  635. }
  636. if(Int(amountTextField1.stringValue) ?? 0 > 1) { //购买多个不支持优惠卷
  637. return
  638. }
  639. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  640. let alert = NSAlert()
  641. alert.alertStyle = .critical
  642. alert.messageText = NSLocalizedString("Error Information", comment: "")
  643. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  644. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  645. alert.runModal()
  646. return
  647. }
  648. self.refreshCouponError(isReveal: false)
  649. applyButton.isEnabled = false
  650. coupomTextField.isEnabled = false
  651. window?.showWaitingView()
  652. KMMemberCenterManager.manager.checkCoupon(productId: product_Info.id, userId: KMMemberInfo.shared.userID, code: coupomTextField.stringValue) {[weak self] success, result in
  653. self?.window?.hideWaitingView()
  654. KMMainThreadExecute {
  655. if(success == true) {
  656. let alert = NSAlert()
  657. alert.alertStyle = .critical
  658. alert.messageText = NSLocalizedString("Successfully", comment: "")
  659. _ = alert.runModal()
  660. guard let productResult : KMMemberProductResult = result else {
  661. return
  662. }
  663. guard let couponModel : KMCheckCouponModel = productResult.checkCoupon else {
  664. return
  665. }
  666. let priceString = couponModel.priceString
  667. self?.product_Info.priceString = priceString
  668. let cnyPriceString = couponModel.cnyPriceString
  669. self?.product_Info.cnyPriceString = cnyPriceString //批量时将总价赋给priceString,只显示priceString价格了,其他的置空
  670. self?.product_Info.upgradePriceString = couponModel.upgradePriceString
  671. self?.product_Info.displayPriceString = couponModel.displayPriceString
  672. self?.product_Info.displayCnyPriceString = couponModel.displayCnyPriceString
  673. self?.product_Info.individualPriceString = couponModel.priceString
  674. self?.product_Info.individualCnyPriceString = couponModel.cnyPriceString
  675. self?.product_Info.verifyofferCode = self?.coupomTextField.stringValue ?? ""
  676. if self?._paymentMethod == .alipay || self?._paymentMethod == .wxpay {
  677. self?.updatePrice()
  678. }
  679. self?.priceRefresh(productsModel: self?._product_Info ?? KMListingProductsModel())
  680. }else {
  681. let alert = NSAlert()
  682. alert.alertStyle = .critical
  683. alert.messageText = NSLocalizedString("Error Information", comment: "")
  684. alert.informativeText = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  685. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  686. let result = alert.runModal()
  687. if (result == .alertFirstButtonReturn) {
  688. }
  689. }
  690. }
  691. }
  692. }
  693. @IBAction func paddlePopUpAction(_ sender: NSPopUpButton) {
  694. }
  695. @IBAction func couponClearButton(_ sender: NSButton) {
  696. coupomTextField.stringValue = ""
  697. couponClearButton.isHidden = true
  698. applyButton.isEnabled = true
  699. coupomTextField.isEnabled = true
  700. refreshCouponError(isReveal: false)
  701. }
  702. @IBAction func buyAction(_ sender: NSButton) {
  703. if emailTextField.stringValue == "" {
  704. emailErrorLabel(isReveal: true)
  705. return
  706. }
  707. if paymentMethod == .paddle {
  708. if !isNumeric(paddleTextField.stringValue) && paddleTextField.stringValue != "" {
  709. let alert = NSAlert()
  710. alert.alertStyle = .critical
  711. alert.messageText = NSLocalizedString("Error Information", comment: "")
  712. alert.informativeText = NSLocalizedString("Please enter the correct postcode.", comment: "")
  713. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  714. alert.runModal()
  715. return
  716. }
  717. }
  718. if paymentMethod == .paypal {
  719. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  720. } else if paymentMethod == .paddle {
  721. paddleView.wantsLayer = true
  722. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor1")?.cgColor ?? NSColor.blue.cgColor
  723. } else if paymentMethod == .wxpay {
  724. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  725. } else if paymentMethod == .alipay {
  726. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  727. }
  728. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
  729. guard let strongSelf = self else { return }
  730. if strongSelf.paymentMethod == .paypal {
  731. strongSelf.paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  732. } else if strongSelf.paymentMethod == .paddle {
  733. strongSelf.paddleView.wantsLayer = true
  734. strongSelf.paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  735. } else if strongSelf.paymentMethod == .wxpay {
  736. strongSelf.weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  737. } else if strongSelf.paymentMethod == .alipay {
  738. strongSelf.weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  739. }
  740. }
  741. var couponCode = ""
  742. if product_Info.verifyofferCode.isEmpty == false {
  743. couponCode = product_Info.verifyofferCode
  744. }
  745. let model = KMProductModel.shared
  746. if isOneTimePurchase(_product_Info) {
  747. let price = showPayPriceString()
  748. model.creatOrder(productId: _product_Info.id,
  749. paymentMethod: getPurchasePaymentMethod(),
  750. price: price,
  751. discountFlag: getOneTimePurchaseDiscountFlag(),
  752. couponCode: couponCode,
  753. num: pdfCount) { success, result in
  754. if success {
  755. if let dataInfo = result {
  756. self.orderID = result?.createOrder?.orderId ?? ""
  757. let page_pay_url = result?.createOrder?.payHref ?? ""
  758. let qrCode = result?.createOrder?.qrCode ?? ""
  759. if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
  760. if self.orderID == "" || page_pay_url == "" {
  761. DispatchQueue.main.async {
  762. let alert = NSAlert()
  763. alert.alertStyle = .critical
  764. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  765. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  766. alert.runModal()
  767. }
  768. } else {
  769. self.openWebView(page_pay_url)
  770. }
  771. } else if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  772. if self.orderID == "" || qrCode == "" {
  773. DispatchQueue.main.async {
  774. let alert = NSAlert()
  775. alert.alertStyle = .critical
  776. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  777. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  778. alert.runModal()
  779. }
  780. } else {
  781. self.scanCode(qrCode)
  782. }
  783. }
  784. } else {
  785. DispatchQueue.main.async {
  786. let alert = NSAlert()
  787. alert.alertStyle = .critical
  788. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  789. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  790. alert.runModal()
  791. }
  792. }
  793. } else {
  794. DispatchQueue.main.async {
  795. let alert = NSAlert()
  796. alert.alertStyle = .critical
  797. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  798. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  799. alert.runModal()
  800. }
  801. }
  802. }
  803. } else {
  804. var priceString = showPayPriceString()
  805. model.createSubscriber(productId: _product_Info.id,
  806. paymentMethod: getPurchasePaymentMethod(),
  807. price: priceString,
  808. discountFlag: getOneTimePurchaseDiscountFlag(),
  809. couponCode: couponCode,
  810. num: pdfCount) { success, result in
  811. if success {
  812. if let dataInfo = result {
  813. self.orderID = result?.createOrder?.orderId ?? ""
  814. let page_pay_url = result?.createOrder?.payHref ?? ""
  815. if self.orderID == "" || page_pay_url == "" {
  816. DispatchQueue.main.async {
  817. let alert = NSAlert()
  818. alert.alertStyle = .critical
  819. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  820. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  821. alert.runModal()
  822. }
  823. } else {
  824. if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
  825. self.openWebView(page_pay_url)
  826. }
  827. }
  828. } else {
  829. DispatchQueue.main.async {
  830. let alert = NSAlert()
  831. alert.alertStyle = .critical
  832. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  833. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  834. alert.runModal()
  835. }
  836. }
  837. } else {
  838. DispatchQueue.main.async {
  839. let alert = NSAlert()
  840. alert.alertStyle = .critical
  841. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  842. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  843. alert.runModal()
  844. }
  845. }
  846. }
  847. }
  848. }
  849. func getPurchasePaymentMethod() -> Int {
  850. switch self.paymentMethod {
  851. case .paypal:
  852. return 0;
  853. case .alipay:
  854. return 1;
  855. case .wxpay:
  856. return 2;
  857. case .paddle:
  858. return 3;
  859. default:
  860. return 1;
  861. }
  862. }
  863. func getOneTimePurchaseDiscountFlag() -> Int {
  864. if pdfCount > 1 { //批量购买
  865. return 4
  866. } else {
  867. var couponCode = ""
  868. if _product_Info.verifyofferCode.isEmpty == false {
  869. couponCode = _product_Info.verifyofferCode
  870. }
  871. if(couponCode.isEmpty == false) {//判断是否有使用优惠卷
  872. if _product_code == "advanced-annual-subscription-blackFive" ||
  873. _product_code == "advanced-permanent-blackFive" {
  874. return 11
  875. } else {
  876. return 1
  877. }
  878. } else {
  879. if _product_code == "advanced-annual-subscription-blackFive"{ //黑五订阅
  880. if(_product_Info.upgradePriceString?.isEmpty == false) { //升级
  881. return 2
  882. } else {
  883. return 5
  884. }
  885. } else if _product_code == "advanced-permanent-blackFive" {//黑五买断
  886. return 5
  887. } else if _product_code == "advanced-annual-subscription" { //高级订阅
  888. if(_product_Info.upgradePriceString?.isEmpty == false) { //升级
  889. return 2
  890. } else {
  891. return 0
  892. }
  893. } else if _product_code == "advanced-annual-subscription-trail" ||
  894. _product_code == "ai-subscription-year-trail" { //试用
  895. return 5
  896. } else {
  897. return 0
  898. }
  899. }
  900. }
  901. }
  902. func getPurchaseDiscountFlag() -> Int {
  903. var couponCode = ""
  904. if coupomErrorLabel.isHidden {
  905. couponCode = coupomTextField.stringValue
  906. }
  907. if couponCode == "" {
  908. if _product_code == "advanced-annual-subscription-blackFive" ||
  909. _product_code == "advanced-permanent-blackFive" {
  910. return 5
  911. }
  912. if _product_code == "advanced-annual-subscription-trail" ||
  913. _product_code == "ai-subscription-year-trail" {
  914. return 5
  915. }
  916. return 0
  917. } else {
  918. if _product_code == "advanced-annual-subscription-blackFive" ||
  919. _product_code == "advanced-permanent-blackFive" {
  920. return 11
  921. }
  922. return 1
  923. }
  924. }
  925. // MARK: get & set
  926. var product_code : String { //设置code
  927. get {
  928. return _product_code
  929. }
  930. set {
  931. _product_code = newValue
  932. if(isSubscribeCode() == true) {
  933. addButton1.isEnabled = false
  934. removeButton1.isEnabled = false
  935. amountTextField1.isEditable = false
  936. wechatPayButton.isHidden = true
  937. alipayButton.isHidden = true
  938. prodocumentView.isHidden = true
  939. } else {
  940. addButton1.isEnabled = true
  941. removeButton1.isEnabled = true
  942. amountTextField1.isEditable = true
  943. wechatPayButton.isHidden = false
  944. alipayButton.isHidden = false
  945. prodocumentView.isHidden = false
  946. }
  947. if(newValue == "advanced-annual-subscription-trail" ||
  948. newValue == "ai-subscription-year-trail") {
  949. couponView.isHidden = true
  950. }
  951. let model = KMProductModel.shared
  952. let products : [KMListingProductsModel] = model.dmgProductDatas?.listingProducts ?? []
  953. if products.count > 0 {
  954. for product in products {
  955. if product.code == _product_code {
  956. self.product_Info = product;
  957. //更新价格
  958. self.updateProduct_Info()
  959. break
  960. }
  961. }
  962. } else {
  963. model.getDMGProductDatas() { [weak self] success, result in
  964. let products : [KMListingProductsModel] = model.dmgProductDatas?.listingProducts ?? []
  965. if products.count > 0 {
  966. for product in products {
  967. if product.code == self?.product_code {
  968. self?.product_Info = product;
  969. //更新价格
  970. self?.updateProduct_Info()
  971. break
  972. }
  973. }
  974. }
  975. }
  976. }
  977. }
  978. }
  979. var product_Info: KMListingProductsModel { //刷新产品ui+价格
  980. get {
  981. return _product_Info
  982. }
  983. set {
  984. _product_Info = newValue
  985. if isWindowDidLoad {
  986. xibInitialization()
  987. priceRefresh(productsModel: _product_Info)
  988. pdfReaderProLabel.stringValue = product_Info.productName //待区分多语
  989. }
  990. }
  991. }
  992. private var paymentMethod : KMPaymentType {
  993. get {
  994. return _paymentMethod
  995. }
  996. set {
  997. _paymentMethod = newValue
  998. paypalButton.image = NSImage(named: "EmbeddedPayment19")
  999. cardButton.image = NSImage(named: "EmbeddedPayment21")
  1000. wechatPayButton.image = NSImage(named: "EmbeddedPayment23")
  1001. wechatPayButton2.image = NSImage(named: "EmbeddedPayment23")
  1002. alipayButton.image = NSImage(named: "EmbeddedPayment25")
  1003. if _paymentMethod == .paypal {
  1004. paypalButton.image = NSImage(named: "EmbeddedPayment18")
  1005. payBox.contentView = paypalView
  1006. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1007. payBoxHeight.constant = 60.0
  1008. } else if _paymentMethod == .paddle {
  1009. cardButton.image = NSImage(named: "EmbeddedPayment20")
  1010. payBox.contentView = cardView
  1011. paddleView.wantsLayer = true
  1012. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  1013. payBoxHeight.constant = 150.0
  1014. } else if _paymentMethod == .wxpay {
  1015. wechatPayButton.image = NSImage(named: "EmbeddedPayment22")
  1016. wechatPayButton2.image = NSImage(named: "EmbeddedPayment22")
  1017. payBox.contentView = weChatPayView
  1018. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1019. payBoxHeight.constant = 92.0
  1020. } else if _paymentMethod == .alipay {
  1021. alipayButton.image = NSImage(named: "EmbeddedPayment24")
  1022. payBox.contentView = weChatPayView
  1023. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1024. payBoxHeight.constant = 92.0
  1025. }
  1026. }
  1027. }
  1028. private func updatePrice() {
  1029. if _paymentMethod == .wxpay {
  1030. wechatPayButton.image = NSImage(named: "EmbeddedPayment22")
  1031. wechatPayButton2.image = NSImage(named: "EmbeddedPayment22")
  1032. payBox.contentView = weChatPayView
  1033. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1034. payBoxHeight.constant = 92.0
  1035. } else if _paymentMethod == .alipay {
  1036. alipayButton.image = NSImage(named: "EmbeddedPayment24")
  1037. payBox.contentView = weChatPayView
  1038. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1039. payBoxHeight.constant = 92.0
  1040. }
  1041. }
  1042. func isSubscribeCode()-> Bool {//是否是订阅
  1043. if(product_code == "standard-annual-subscription" ||
  1044. product_code == "advanced-annual-subscription-blackFive" ||
  1045. product_code == "advanced-annual-subscription-trail" ||
  1046. product_code == "advanced-annual-subscription" ||
  1047. product_code == "ai-subscription-year-trail" ||
  1048. product_code == "ai-subscription-month") {
  1049. return true
  1050. }
  1051. return false
  1052. }
  1053. func isOneTimePurchaseCode()-> Bool { //是否是一次性购买
  1054. if(product_code == "advanced-permanent-blackFive" ||
  1055. product_code == "advanced-permanent") {
  1056. return true
  1057. }
  1058. return false
  1059. }
  1060. func isBlackFiveCode()-> Bool { //是否是黑五
  1061. if(product_code == "advanced-permanent-blackFive" ||
  1062. product_code == "advanced-annual-subscription-blackFive") {
  1063. return true
  1064. }
  1065. return false
  1066. }
  1067. func isHaveAvailableCoupon()-> Bool { //是否有优惠卷
  1068. return false
  1069. }
  1070. func showPriceString()->String {
  1071. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1072. var priceString = product_Info.priceString ?? ""
  1073. if(product_Info.upgradePriceString?.isEmpty == false) {
  1074. priceString = product_Info.upgradePriceString ?? ""
  1075. } else if(product_Info.displayPriceString?.isEmpty == false) {
  1076. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1077. priceString = product_Info.displayCnyPriceString ?? ""
  1078. } else {
  1079. priceString = product_Info.displayPriceString ?? ""
  1080. }
  1081. } else if(product_Info.priceString?.isEmpty == false) {
  1082. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1083. priceString = product_Info.cnyPriceString ?? ""
  1084. } else {
  1085. priceString = product_Info.priceString ?? ""
  1086. }
  1087. }
  1088. return priceString
  1089. }
  1090. func showOrgPriceString()->String { //原价
  1091. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1092. var priceString = product_Info.priceString ?? ""
  1093. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1094. priceString = product_Info.cnyPriceString ?? ""
  1095. } else {
  1096. priceString = product_Info.priceString ?? ""
  1097. }
  1098. return priceString
  1099. }
  1100. func showOrgShowPriceString()->String { //实际购买显示的价格
  1101. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1102. var priceString = product_Info.priceString ?? ""
  1103. if(product_Info.upgradePriceString?.isEmpty == false) {
  1104. priceString = product_Info.upgradePriceString ?? ""
  1105. } else if(product_Info.displayPriceString?.isEmpty == false) {
  1106. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1107. priceString = product_Info.displayCnyPriceString ?? ""
  1108. } else {
  1109. priceString = product_Info.displayPriceString ?? ""
  1110. }
  1111. } else if(product_Info.priceString?.isEmpty == false) {
  1112. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1113. priceString = product_Info.cnyPriceString ?? ""
  1114. } else {
  1115. priceString = product_Info.priceString ?? ""
  1116. }
  1117. }
  1118. return priceString
  1119. }
  1120. func showSinglePriceString()->String { //单价
  1121. var priceString = product_Info.individualPriceString ?? ""
  1122. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1123. priceString = product_Info.individualCnyPriceString ?? ""
  1124. } else {
  1125. priceString = product_Info.individualPriceString ?? ""
  1126. }
  1127. return priceString
  1128. }
  1129. func showPayPriceString()->String { //实际支付的价格
  1130. let iswechatOrAlipayPay = (self.paymentMethod == .wxpay || self.paymentMethod == .alipay)
  1131. var priceString = product_Info.priceString ?? ""
  1132. if(product_Info.upgradePriceString?.isEmpty == false) {
  1133. priceString = product_Info.upgradePriceString ?? ""
  1134. } else if(product_Info.displayPriceString?.isEmpty == false) {
  1135. if(iswechatOrAlipayPay) {
  1136. priceString = product_Info.displayCnyPriceString ?? ""
  1137. } else {
  1138. priceString = product_Info.displayPriceString ?? ""
  1139. }
  1140. } else if(product_Info.priceString?.isEmpty == false) {
  1141. if(iswechatOrAlipayPay) {
  1142. priceString = product_Info.cnyPriceString ?? ""
  1143. } else {
  1144. priceString = product_Info.priceString ?? ""
  1145. }
  1146. }
  1147. return priceString
  1148. }
  1149. // MARK: Private
  1150. private func _configFirmLabel() {
  1151. firmLabel.isEditable = false
  1152. firmLabel.isSelectable = true
  1153. firmLabel.allowsEditingTextAttributes = true
  1154. firmLabel.textColor = NSColor.black
  1155. firmLabel.font = NSFont.SFProTextRegularFont(14.0)
  1156. let firmString = NSLocalizedString("If you need to manage seats in PDF Reader Pro, please purchase the %@.", tableName: "MemberCenterLocalizable", comment: "")
  1157. let enterpriseStr = NSLocalizedString("enterprise version", tableName: "MemberCenterLocalizable", comment: "")
  1158. let fireFullString = String(format: firmString, enterpriseStr)
  1159. let firmLinkColor = NSColor(named: "4982E6") ?? NSColor.blue
  1160. let paragraphStyle = NSMutableParagraphStyle()
  1161. paragraphStyle.alignment = .right // 设置为右对齐
  1162. let attri = NSMutableAttributedString(string: fireFullString, attributes: [.foregroundColor : firmLinkColor, .font : NSFont.SFProTextRegularFont(14.0),.paragraphStyle:paragraphStyle,])
  1163. // 定义链接的范围
  1164. let enterpriseRange = (fireFullString as NSString).range(of: enterpriseStr)
  1165. let firmFont = NSFont.SFProTextRegularFont(14.0) // 与普通文本相同的字体
  1166. attri.addAttributes([
  1167. .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
  1168. .underlineStyle: NSUnderlineStyle.single.rawValue,
  1169. .font: firmFont
  1170. ], range: (fireFullString as NSString).range(of: enterpriseStr))
  1171. attri.addAttributes([
  1172. .foregroundColor: firmLinkColor,
  1173. .link: kLynxpdfeditorUrl,
  1174. .font: firmFont
  1175. ], range: enterpriseRange)
  1176. firmLabel.attributedStringValue = attri
  1177. }
  1178. private func uuid() -> String {
  1179. return GetHardwareUUID()!
  1180. }
  1181. private func _trackEvent_paid() {
  1182. self.trackEvent_dmg(eventName: self.kEventName, params: self.kEventParams, platform: .AppCenter)
  1183. }
  1184. private func getProductID() -> String {
  1185. return GetHardwareUUID()!
  1186. }
  1187. private func isNumeric(_ text: String) -> Bool {
  1188. return !text.isEmpty && text.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
  1189. }
  1190. private func openWebView(_ urlPath: String) -> Void {
  1191. DispatchQueue.main.async { [weak self] in
  1192. guard let self = self else { return }
  1193. self.pollCount = 0
  1194. self.startPolling()
  1195. self.embeddedPaymentPopWC = KMEmbeddedPaymentPopWC.currentFirstTrialWC(urlPath)
  1196. if self.paymentMethod == .paddle {
  1197. self.embeddedPaymentPopWC?.isPaddle = true
  1198. self.embeddedPaymentPopWC?.countryCode = self.countryAbbreviationValues[self.paddlePopUpButton.indexOfSelectedItem]
  1199. self.embeddedPaymentPopWC?.postCode = self.paddleTextField.stringValue
  1200. }
  1201. self.embeddedPaymentPopWC?.showWindow(nil)
  1202. self.embeddedPaymentPopWC?.callback = { isClose in
  1203. }
  1204. }
  1205. }
  1206. private func convertBase64StringToNSImage(base64String: String) -> NSImage? {
  1207. let sepS = "data:image/png;base64,"
  1208. guard let baseStr = base64String.components(separatedBy: sepS).last else {
  1209. return nil
  1210. }
  1211. if let data = Data(base64Encoded: baseStr) {
  1212. return NSImage(data: data)
  1213. }
  1214. return nil
  1215. }
  1216. private func scanCode(_ urlPath: String) -> Void {
  1217. DispatchQueue.main.async { [weak self] in
  1218. guard let self = self else { return }
  1219. self.payBox.contentView = self.payResult
  1220. self.payBoxHeight.constant = 262
  1221. if self.paymentMethod == .wxpay {
  1222. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with WeChat", comment: "")
  1223. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment27")
  1224. } else if self.paymentMethod == .alipay {
  1225. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with Alipay", comment: "")
  1226. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment28")
  1227. }
  1228. let img = self.convertBase64StringToNSImage(base64String: urlPath)
  1229. self.qrCodeImageView.image = img
  1230. self.pollCount = 0
  1231. self.startPolling()
  1232. }
  1233. }
  1234. private func pollingResultNew() -> Void { //刷新订单状态
  1235. DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
  1236. guard let self = self else { return }
  1237. if self.orderID == "" { return }
  1238. KMMemberCenterManager.manager.getStateByOrderId(orderId: self.orderID) { success, info in
  1239. self.startPolling()
  1240. if (success) {
  1241. let dic: NSDictionary = info ?? [:]
  1242. let requestDic: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1243. let status:String = requestDic["status"] as? String ?? ""
  1244. if(status.uppercased() == "COMPLETED") {
  1245. KMUserInfoVCModel().refreshUserInfo { success, msg,dic in
  1246. DispatchQueue.main.async {
  1247. NotificationCenter.default.post(name: NSNotification.Name.KMIAPSubscriptionLoaded, object: nil)
  1248. }
  1249. }
  1250. self.stopPolling()
  1251. self.close()
  1252. DispatchQueue.main.async {
  1253. KMPurchaseSuccessWindowController.shared.showWindow(nil)
  1254. }
  1255. }
  1256. }
  1257. }
  1258. }
  1259. }
  1260. private func startPolling() {
  1261. timer = Timer(timeInterval: interval, repeats: false) { [weak self] _ in
  1262. guard let self = self else { return }
  1263. self.performTask()
  1264. }
  1265. RunLoop.main.add(timer!, forMode: .common)
  1266. }
  1267. @objc private func performTask() {
  1268. guard pollCount < maxPolls else {
  1269. timer?.invalidate()
  1270. timer = nil
  1271. return
  1272. }
  1273. // 执行你的轮询任务
  1274. print("Performing polling task \(pollCount + 1)/\(maxPolls)")
  1275. pollCount += 1
  1276. pollingResultNew()
  1277. }
  1278. private func stopPolling() {
  1279. timer?.invalidate()
  1280. timer = nil
  1281. pollCount = 0
  1282. }
  1283. private func emailErrorLabel(isReveal: Bool) {
  1284. errorView.isHidden = !isReveal
  1285. errorLabelTopLayout.constant = errorView.isHidden ? -CGRectGetHeight(errorView.bounds) : 4
  1286. }
  1287. private func refreshApplyButton(isReveal: Bool) {
  1288. self.applyButton.isEnabled = isReveal
  1289. }
  1290. private func refreshLicenseCodeView(isReveal: Bool) {
  1291. }
  1292. private func refreshLicenseCodeErrorView(isReveal: Bool) {
  1293. }
  1294. private func refreshCouponError(isReveal: Bool) {
  1295. coupomErrorLabel.isHidden = !isReveal
  1296. coupomErrorTopLayout.constant = coupomErrorLabel.isHidden ? -CGRectGetHeight(coupomErrorLabel.bounds) : 8.0
  1297. }
  1298. var activityAlertViewController: KMActivityALertViewController? {
  1299. if _activityAlertViewController == nil {
  1300. let blockSelf = self
  1301. _activityAlertViewController = KMActivityALertViewController()
  1302. _activityAlertViewController?.callback = {
  1303. blockSelf.close()
  1304. }
  1305. }
  1306. return _activityAlertViewController
  1307. }
  1308. }
  1309. extension KMPurchaseEmbeddedWindowController: NSTextFieldDelegate {
  1310. func controlTextDidEndEditing(_ obj: Notification) {
  1311. let textField = obj.object as? NSTextField
  1312. if textField == amountTextField1 {
  1313. if !isNumeric(textField!.stringValue) {
  1314. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1315. } else {
  1316. if textField == amountTextField1 {
  1317. pdfCount = Int(textField!.stringValue) ?? 1
  1318. if Int(textField!.stringValue)! > 9999 {
  1319. pdfCount = 9999
  1320. textField?.stringValue = String(pdfCount)
  1321. } else if Int(textField!.stringValue)! < 1 {
  1322. pdfCount = 1
  1323. textField?.stringValue = String(pdfCount)
  1324. }
  1325. }
  1326. if pdfCount > 100 {
  1327. wechatPayButton2.isHidden = false
  1328. cardButton.isHidden = true
  1329. wechatPayButton.isHidden = true
  1330. if _paymentMethod == .paddle {
  1331. paymentMethod = .paypal
  1332. }
  1333. } else {
  1334. wechatPayButton2.isHidden = true
  1335. cardButton.isHidden = false
  1336. wechatPayButton.isHidden = false
  1337. }
  1338. }
  1339. } else if textField == emailTextField {
  1340. emailErrorLabel(isReveal: false)
  1341. } else if textField == paddleTextField {
  1342. }
  1343. }
  1344. func controlTextDidChange(_ obj: Notification) {
  1345. let textField = obj.object as? NSTextField
  1346. if textField == amountTextField1 {
  1347. if !isNumeric(textField!.stringValue) {
  1348. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1349. } else {
  1350. if Int(textField!.stringValue)! > 9999 {
  1351. textField?.stringValue = String(pdfCount)
  1352. } else if Int(textField!.stringValue)! < 1 {
  1353. textField?.stringValue = String(pdfCount)
  1354. }
  1355. }
  1356. }
  1357. }
  1358. }
  1359. extension KMPurchaseEmbeddedWindowController: NSWindowDelegate {
  1360. func windowWillClose(_ notification: Notification) {
  1361. if let data = self.window?.isEqual(to: notification.object), data {
  1362. // 窗口关闭,关闭轮询
  1363. stopPolling()
  1364. }
  1365. if self.embeddedPaymentPopWC != nil {
  1366. self.embeddedPaymentPopWC?.close()
  1367. self.embeddedPaymentPopWC = nil
  1368. }
  1369. KMPurchaseEmbeddedWindowController.currentWindowController = nil
  1370. }
  1371. }
  1372. #endif