KMPurchaseEmbeddedWindowController.swift 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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. let localizedString = NSLocalizedString("By submitting this order, I agree to the %@ and %@ for subscription products", comment: "")
  371. let privacyPolicy = NSLocalizedString("Privacy Policy", comment: "")
  372. let termsOfService = NSLocalizedString("Terms of Service", comment: "")
  373. let fullString1 = String(format: localizedString, privacyPolicy, termsOfService)
  374. let attributedString1 = NSMutableAttributedString(string: fullString1)
  375. let privacyPolicyRange = (fullString1 as NSString).range(of: privacyPolicy)
  376. let termsOfServiceRange = (fullString1 as NSString).range(of: termsOfService)
  377. let linkColor1 = NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0)
  378. let font1 = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  379. attributedString1.addAttributes([
  380. .foregroundColor: NSColor(named: "KMPurchase_99A6B2") as Any,
  381. .font: font1
  382. ], range: (fullString1 as NSString).range(of: fullString1))
  383. attributedString1.addAttributes([
  384. .foregroundColor: linkColor1,
  385. .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
  386. .font: font1
  387. ], range: privacyPolicyRange)
  388. attributedString1.addAttributes([
  389. .foregroundColor: linkColor1,
  390. .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
  391. .font: font1
  392. ], range: termsOfServiceRange)
  393. payExplainLabel.attributedStringValue = attributedString1
  394. paypalBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  395. paypalBuyNowLabel.textColor = NSColor.white
  396. paypalBuyNowLabel.font = NSFont.SFProTextBoldFont(16.0)
  397. paddleButton.title = NSLocalizedString("Continue to Pay", comment: "")
  398. paddleButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  399. paddlePopUpButton.type = .arrowDown
  400. paddlePopUpButton.removeAllItems()
  401. paddlePopUpButton.addItems(withTitles: countryKeys)
  402. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  403. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  404. paddlePopUpButton.selectItem(at: 45)
  405. } else {
  406. paddlePopUpButton.selectItem(at: 229)
  407. }
  408. paddlePopUpButton.wantsLayer = true
  409. paddlePopUpButton.layer?.backgroundColor = NSColor(named: "KMPurchase_FFFFFF")?.cgColor
  410. paddlePopUpButton.layer?.borderColor = NSColor(named: "KMPurchase_DADBDE")?.cgColor
  411. paddleBox.fillColor = NSColor(named: "KMPurchase_FFFFFF") ?? NSColor.white
  412. paddleBox.borderColor = NSColor(named: "KMPurchase_DADBDE") ?? NSColor.gray
  413. paddleTextField.placeholderString = NSLocalizedString("Postcode", comment: "")
  414. paddleTextField.delegate = self
  415. weChatButton.title = NSLocalizedString("Continue to Pay", comment: "")
  416. weChatButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  417. qrCodeImageView.imageScaling = .scaleProportionallyUpOrDown
  418. scanLabel.textColor = NSColor(named: "KMPurchase_000000")
  419. scanLabel.font = NSFont.SFProTextRegularFont(12)
  420. successfullyView.isHidden = true
  421. purchasedLabel.stringValue = NSLocalizedString("Purchased", comment: "")
  422. purchasedLabel.textColor = NSColor(named: "KMPurchase_002143")
  423. purchasedLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  424. successfullyLabel.stringValue = NSLocalizedString("Successfully", comment: "")
  425. successfullyLabel.textColor = NSColor(named: "KMPurchase_002143")
  426. successfullyLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  427. emailErrorLabel(isReveal: false)
  428. }
  429. private func _updateViewColor() {
  430. if KMAppearance.isDarkMode() {
  431. prodocumentView.layer?.borderColor = NSColor(hex: "#E2E3E5").cgColor
  432. } else {
  433. prodocumentView.layer?.borderColor = NSColor(hex: "#E2E3E5").cgColor
  434. }
  435. }
  436. private func priceRefresh(productsModel: KMListingProductsModel) -> Void {
  437. if productsModel.code == "" {
  438. return
  439. }
  440. var listPrice: Float = 0.0
  441. var discount: Float = 0.0
  442. var paySum: Float = 0.0
  443. var discount1: Float = 0.0
  444. var original1: Float = 0.0
  445. var abbreviation = "USD"
  446. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  447. abbreviation = "CNY"
  448. }
  449. let insting = Float(showSinglePriceString())
  450. listPrice = Float(String(format: "%.2f", Float((insting ?? 0) * Float(pdfCount)))) ?? 0 //单价*购买个数
  451. if _product_code == "advanced-annual-subscription-trail" ||
  452. _product_code == "advanced-annual-subscription-blackFive" ||
  453. _product_code == "advanced-permanent-blackFive" ||
  454. _product_code == KMAISubscriptionYearTrailCodeKey_DMG {
  455. // 全平台高级版年订阅 99.9 美金,带免费试用
  456. // 全平台高级版年订阅 99.9 美金,黑五订单
  457. // Mac & Win 双平台高级永久 119.99 美金,黑五订单
  458. // AI 年订阅 125.99 美金,带免费试用
  459. var tipsString = NSLocalizedString("%.2@/year, auto-renewal", comment: "")
  460. if product_Info.cycle == 4 {
  461. tipsString = NSLocalizedString("%.2@/year, auto-renewal", comment: "")
  462. }
  463. if product_Info.cycle == 3 {
  464. tipsString = NSLocalizedString("%.2@/half a year, auto-renewal", comment: "")
  465. }
  466. if product_Info.cycle == 2 {
  467. tipsString = NSLocalizedString("%.2@/quarter, auto-renewal", comment: "")
  468. }
  469. if product_Info.cycle == 1 {
  470. tipsString = NSLocalizedString("%.2@/monthly, auto-renewal", comment: "")
  471. }
  472. if product_Info.cycle == 0 {
  473. tipsString = NSLocalizedString("%.2@, One time purchase.", comment: "")
  474. }
  475. let fullString = String(format: tipsString, showOrgPriceString())
  476. prmiumLabel.stringValue = String(format: "%@ %@", abbreviation, fullString)
  477. original1 = Float(showOrgPriceString()) ?? 0
  478. discount1 = Float(showOrgShowPriceString()) ?? 0
  479. paySum = discount1
  480. discount = listPrice - paySum
  481. }
  482. if _product_code == "advanced-annual-subscription" ||
  483. _product_code == "standard-annual-subscription" ||
  484. _product_code == "ai-subscription-month" {
  485. // 全平台高级版年订阅 99.9 美金,无试用
  486. // 全平台标准版年订阅 79.9 美金
  487. // Mac & Win 双平台高级永久 119.99 美金
  488. // AI 月订阅 14.99 美金
  489. var tipsString = NSLocalizedString("%.2@/year, auto-renewal", comment: "")
  490. if product_Info.cycle == 4 {
  491. tipsString = NSLocalizedString("%.2@/year, auto-renewal", comment: "")
  492. }
  493. if product_Info.cycle == 3 {
  494. tipsString = NSLocalizedString("%.2@/half a year, auto-renewal", comment: "")
  495. }
  496. if product_Info.cycle == 2 {
  497. tipsString = NSLocalizedString("%.2@/quarter, auto-renewal", comment: "")
  498. }
  499. if product_Info.cycle == 1 {
  500. tipsString = NSLocalizedString("%.2@/monthly, auto-renewal", comment: "")
  501. }
  502. if product_Info.cycle == 0 {
  503. tipsString = NSLocalizedString("%.2@, One time purchase.", comment: "")
  504. }
  505. let fullString = String(format: tipsString, showOrgPriceString())
  506. prmiumLabel.stringValue = String(format: "%@ %@", abbreviation, fullString)
  507. original1 = Float(showOrgPriceString()) ?? 0
  508. discount1 = Float(showOrgShowPriceString()) ?? 0
  509. paySum = discount1
  510. discount = listPrice - paySum
  511. }
  512. if _product_code == "advanced-permanent" {
  513. // Mac & Win 双平台高级永久 119.99 美金
  514. var tipsString = NSLocalizedString("%.2@/year, auto-renewal", comment: "")
  515. if product_Info.cycle == 4 {
  516. tipsString = NSLocalizedString("%.2@/year, auto-renewal", comment: "")
  517. }
  518. if product_Info.cycle == 3 {
  519. tipsString = NSLocalizedString("%.2@/half a year, auto-renewal", comment: "")
  520. }
  521. if product_Info.cycle == 2 {
  522. tipsString = NSLocalizedString("%.2@/quarter, auto-renewal", comment: "")
  523. }
  524. if product_Info.cycle == 1 {
  525. tipsString = NSLocalizedString("%.2@/monthly, auto-renewal", comment: "")
  526. }
  527. if product_Info.cycle == 0 {
  528. tipsString = NSLocalizedString("%.2@, One time purchase.", comment: "")
  529. }
  530. let fullString = String(format: tipsString, showOrgPriceString())
  531. prmiumLabel.stringValue = String(format: "%@ %@", abbreviation, fullString)
  532. original1 = Float(showOrgPriceString()) ?? 0
  533. discount1 = Float(showOrgShowPriceString()) ?? 0
  534. paySum = discount1
  535. discount = listPrice - paySum
  536. }
  537. originalPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount1))
  538. if String(format: "%.2f", original1) == String(format: "%.2f", discount1) {
  539. discountPriceLabel1.isHidden = true
  540. } else {
  541. discountPriceLabel1.isHidden = false
  542. let attributedString = NSMutableAttributedString(string: String(format: "%@ %@", abbreviation, String(format: "%.2f", original1)))
  543. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  544. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  545. discountPriceLabel1.attributedStringValue = attributedString
  546. }
  547. lastPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", listPrice))
  548. discountLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount))
  549. paySumLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", paySum))
  550. discountLayoutConstraint.constant = discountView.isHidden ? -CGRectGetHeight(discountView.bounds) : 8
  551. }
  552. // MARK: Active
  553. @IBAction func amountButtonAction(_ sender: NSButton) {
  554. if sender.tag == 0 || sender.tag == 1 {
  555. if sender.tag == 0 {
  556. if pdfCount > 1 {
  557. pdfCount -= 1
  558. }
  559. }
  560. if sender.tag == 1 {
  561. if pdfCount < 9999 {
  562. pdfCount += 1
  563. }
  564. }
  565. if pdfCount > 1 {
  566. removeButton1.isEnabled = true
  567. } else {
  568. removeButton1.isEnabled = false
  569. }
  570. if pdfCount > 100 {
  571. wechatPayButton2.isHidden = false
  572. cardButton.isHidden = true
  573. wechatPayButton.isHidden = true
  574. if _paymentMethod == .paddle {
  575. paymentMethod = .paypal
  576. }
  577. } else {
  578. wechatPayButton2.isHidden = true
  579. cardButton.isHidden = false
  580. wechatPayButton.isHidden = false
  581. }
  582. amountTextField1.stringValue = String(pdfCount)
  583. let viewHidden = pdfCount <= 1
  584. firmLabel.isHidden = viewHidden
  585. if viewHidden {
  586. addbox.fillColor = NSColor(hex: "#F1F1F2")
  587. } else {
  588. addbox.fillColor = NSColor(named: "0E1114 1") ?? .white
  589. }
  590. couponView.isHidden = !viewHidden
  591. }
  592. // 价格刷新
  593. priceRefresh(productsModel: _product_Info)
  594. let model = KMProductModel.shared
  595. isBatchProductPriceGetFinsed = false
  596. if(pdfCount > 1) {
  597. model.getDMGBatchProductPriceInfosForMember(productId: _product_Info.id, num: pdfCount) { [weak self] success, result in
  598. guard let pricesArrays : KMMemberProductResult = result else {
  599. return
  600. }
  601. guard let batchProductPrice : KMBatchProductPriceModel = pricesArrays.batchProductPrice else {
  602. return
  603. }
  604. //原价更新
  605. let priceString = batchProductPrice.totalPrice
  606. self?.product_Info.priceString = priceString
  607. let cnyPriceString = batchProductPrice.cnyTotalPrice
  608. self?.product_Info.cnyPriceString = cnyPriceString //批量时将总价赋给priceString,只显示priceString价格了,其他的置空
  609. self?.product_Info.upgradePriceString = nil
  610. self?.product_Info.displayPriceString = nil
  611. self?.product_Info.displayCnyPriceString = nil
  612. self?.product_Info.individualPriceString = batchProductPrice.price
  613. self?.product_Info.individualCnyPriceString = batchProductPrice.cnyPrice
  614. if self?._paymentMethod == .alipay || self?._paymentMethod == .wxpay {
  615. self?.updatePrice()
  616. }
  617. self?.priceRefresh(productsModel: self?.product_Info ?? KMListingProductsModel())
  618. }
  619. } else {
  620. updateProduct_Info()
  621. }
  622. }
  623. @IBAction func payButtonAction(_ sender: NSButton) {
  624. emailErrorLabel(isReveal: false)
  625. paymentMethod = KMPaymentType(rawValue: uint32(sender.tag)) ?? .paypal
  626. // 价格刷新
  627. priceRefresh(productsModel: _product_Info)
  628. }
  629. @IBAction func couponButtonAction(_ sender: NSButton) {
  630. couponButton.isHidden = true
  631. couponBox.isHidden = false
  632. applyBox.isHidden = false
  633. }
  634. @IBAction func applyButtonAction(_ sender: NSButton) {
  635. if coupomTextField.stringValue == "" {
  636. let alert = NSAlert()
  637. alert.alertStyle = .critical
  638. alert.messageText = NSLocalizedString("Error Information", comment: "")
  639. alert.informativeText = NSLocalizedString("Please enter a coupon code.", comment: "")
  640. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  641. alert.runModal()
  642. return
  643. }
  644. if(Int(amountTextField1.stringValue) ?? 0 > 1) { //购买多个不支持优惠卷
  645. return
  646. }
  647. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  648. let alert = NSAlert()
  649. alert.alertStyle = .critical
  650. alert.messageText = NSLocalizedString("Error Information", comment: "")
  651. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  652. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  653. alert.runModal()
  654. return
  655. }
  656. self.refreshCouponError(isReveal: false)
  657. applyButton.isEnabled = false
  658. coupomTextField.isEnabled = false
  659. window?.showWaitingView()
  660. KMMemberCenterManager.manager.checkCoupon(productId: product_Info.id, userId: KMMemberInfo.shared.userID, code: coupomTextField.stringValue) {[weak self] success, result in
  661. self?.window?.hideWaitingView()
  662. KMMainThreadExecute {
  663. if(success == true) {
  664. let alert = NSAlert()
  665. alert.alertStyle = .critical
  666. alert.messageText = NSLocalizedString("Successfully", comment: "")
  667. _ = alert.runModal()
  668. guard let productResult : KMMemberProductResult = result else {
  669. return
  670. }
  671. guard let couponModel : KMCheckCouponModel = productResult.checkCoupon else {
  672. return
  673. }
  674. let priceString = couponModel.priceString
  675. self?.product_Info.priceString = priceString
  676. let cnyPriceString = couponModel.cnyPriceString
  677. self?.product_Info.cnyPriceString = cnyPriceString //批量时将总价赋给priceString,只显示priceString价格了,其他的置空
  678. self?.product_Info.upgradePriceString = couponModel.upgradePriceString
  679. self?.product_Info.displayPriceString = couponModel.displayPriceString
  680. self?.product_Info.displayCnyPriceString = couponModel.displayCnyPriceString
  681. self?.product_Info.individualPriceString = couponModel.priceString
  682. self?.product_Info.individualCnyPriceString = couponModel.cnyPriceString
  683. self?.product_Info.verifyofferCode = self?.coupomTextField.stringValue ?? ""
  684. if self?._paymentMethod == .alipay || self?._paymentMethod == .wxpay {
  685. self?.updatePrice()
  686. }
  687. self?.priceRefresh(productsModel: self?._product_Info ?? KMListingProductsModel())
  688. }else {
  689. let alert = NSAlert()
  690. alert.alertStyle = .critical
  691. alert.messageText = NSLocalizedString("Error Information", comment: "")
  692. alert.informativeText = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  693. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  694. let result = alert.runModal()
  695. if (result == .alertFirstButtonReturn) {
  696. }
  697. }
  698. }
  699. }
  700. }
  701. @IBAction func paddlePopUpAction(_ sender: NSPopUpButton) {
  702. }
  703. @IBAction func couponClearButton(_ sender: NSButton) {
  704. coupomTextField.stringValue = ""
  705. couponClearButton.isHidden = true
  706. applyButton.isEnabled = true
  707. coupomTextField.isEnabled = true
  708. refreshCouponError(isReveal: false)
  709. }
  710. @IBAction func buyAction(_ sender: NSButton) {
  711. if emailTextField.stringValue == "" {
  712. emailErrorLabel(isReveal: true)
  713. return
  714. }
  715. if paymentMethod == .paddle {
  716. if !isNumeric(paddleTextField.stringValue) && paddleTextField.stringValue != "" {
  717. let alert = NSAlert()
  718. alert.alertStyle = .critical
  719. alert.messageText = NSLocalizedString("Error Information", comment: "")
  720. alert.informativeText = NSLocalizedString("Please enter the correct postcode.", comment: "")
  721. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  722. alert.runModal()
  723. return
  724. }
  725. }
  726. if paymentMethod == .paypal {
  727. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  728. } else if paymentMethod == .paddle {
  729. paddleView.wantsLayer = true
  730. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor1")?.cgColor ?? NSColor.blue.cgColor
  731. } else if paymentMethod == .wxpay {
  732. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  733. } else if paymentMethod == .alipay {
  734. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  735. }
  736. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
  737. guard let strongSelf = self else { return }
  738. if strongSelf.paymentMethod == .paypal {
  739. strongSelf.paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  740. } else if strongSelf.paymentMethod == .paddle {
  741. strongSelf.paddleView.wantsLayer = true
  742. strongSelf.paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  743. } else if strongSelf.paymentMethod == .wxpay {
  744. strongSelf.weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  745. } else if strongSelf.paymentMethod == .alipay {
  746. strongSelf.weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  747. }
  748. }
  749. var couponCode = ""
  750. if product_Info.verifyofferCode.isEmpty == false {
  751. couponCode = product_Info.verifyofferCode
  752. }
  753. let model = KMProductModel.shared
  754. if isOneTimePurchase(_product_Info) {
  755. let price = showPayPriceString()
  756. model.creatOrder(productId: _product_Info.id,
  757. paymentMethod: getPurchasePaymentMethod(),
  758. price: price,
  759. discountFlag: getOneTimePurchaseDiscountFlag(),
  760. couponCode: couponCode,
  761. num: pdfCount) { success, result in
  762. if success {
  763. if let dataInfo = result {
  764. self.orderID = result?.createOrder?.orderId ?? ""
  765. let page_pay_url = result?.createOrder?.payHref ?? ""
  766. let qrCode = result?.createOrder?.qrCode ?? ""
  767. if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
  768. if self.orderID == "" || page_pay_url == "" {
  769. DispatchQueue.main.async {
  770. let alert = NSAlert()
  771. alert.alertStyle = .critical
  772. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  773. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  774. alert.runModal()
  775. }
  776. } else {
  777. self.openWebView(page_pay_url)
  778. }
  779. } else if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  780. if self.orderID == "" || qrCode == "" {
  781. DispatchQueue.main.async {
  782. let alert = NSAlert()
  783. alert.alertStyle = .critical
  784. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  785. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  786. alert.runModal()
  787. }
  788. } else {
  789. self.scanCode(qrCode)
  790. }
  791. }
  792. } else {
  793. DispatchQueue.main.async {
  794. let alert = NSAlert()
  795. alert.alertStyle = .critical
  796. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  797. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  798. alert.runModal()
  799. }
  800. }
  801. } else {
  802. DispatchQueue.main.async {
  803. let alert = NSAlert()
  804. alert.alertStyle = .critical
  805. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  806. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  807. alert.runModal()
  808. }
  809. }
  810. }
  811. } else {
  812. var priceString = showPayPriceString()
  813. model.createSubscriber(productId: _product_Info.id,
  814. paymentMethod: getPurchasePaymentMethod(),
  815. price: priceString,
  816. discountFlag: getOneTimePurchaseDiscountFlag(),
  817. couponCode: couponCode,
  818. num: pdfCount) { success, result in
  819. if success {
  820. if let dataInfo = result {
  821. self.orderID = result?.createOrder?.orderId ?? ""
  822. let page_pay_url = result?.createOrder?.payHref ?? ""
  823. if self.orderID == "" || page_pay_url == "" {
  824. DispatchQueue.main.async {
  825. let alert = NSAlert()
  826. alert.alertStyle = .critical
  827. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  828. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  829. alert.runModal()
  830. }
  831. } else {
  832. if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
  833. self.openWebView(page_pay_url)
  834. }
  835. }
  836. } else {
  837. DispatchQueue.main.async {
  838. let alert = NSAlert()
  839. alert.alertStyle = .critical
  840. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  841. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  842. alert.runModal()
  843. }
  844. }
  845. } else {
  846. DispatchQueue.main.async {
  847. let alert = NSAlert()
  848. alert.alertStyle = .critical
  849. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  850. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  851. alert.runModal()
  852. }
  853. }
  854. }
  855. }
  856. }
  857. func getPurchasePaymentMethod() -> Int {
  858. switch self.paymentMethod {
  859. case .paypal:
  860. return 0;
  861. case .alipay:
  862. return 1;
  863. case .wxpay:
  864. return 2;
  865. case .paddle:
  866. return 3;
  867. default:
  868. return 1;
  869. }
  870. }
  871. func getOneTimePurchaseDiscountFlag() -> Int {
  872. if pdfCount > 1 { //批量购买
  873. return 4
  874. } else {
  875. var couponCode = ""
  876. if _product_Info.verifyofferCode.isEmpty == false {
  877. couponCode = _product_Info.verifyofferCode
  878. }
  879. if(couponCode.isEmpty == false) {//判断是否有使用优惠卷
  880. if _product_code == "advanced-annual-subscription-blackFive" ||
  881. _product_code == "advanced-permanent-blackFive" {
  882. return 11
  883. } else {
  884. return 1
  885. }
  886. } else {
  887. if _product_code == "advanced-annual-subscription-blackFive"{ //黑五订阅
  888. if(_product_Info.upgradePriceString?.isEmpty == false) { //升级
  889. return 2
  890. } else {
  891. return 5
  892. }
  893. } else if _product_code == "advanced-permanent-blackFive" {//黑五买断
  894. return 5
  895. } else if _product_code == "advanced-annual-subscription" { //高级订阅
  896. if(_product_Info.upgradePriceString?.isEmpty == false) { //升级
  897. return 2
  898. } else {
  899. return 0
  900. }
  901. } else if _product_code == "advanced-annual-subscription-trail" ||
  902. _product_code == "ai-subscription-year-trail" { //试用
  903. return 5
  904. } else {
  905. return 0
  906. }
  907. }
  908. }
  909. }
  910. func getPurchaseDiscountFlag() -> Int {
  911. var couponCode = ""
  912. if coupomErrorLabel.isHidden {
  913. couponCode = coupomTextField.stringValue
  914. }
  915. if couponCode == "" {
  916. if _product_code == "advanced-annual-subscription-blackFive" ||
  917. _product_code == "advanced-permanent-blackFive" {
  918. return 5
  919. }
  920. if _product_code == "advanced-annual-subscription-trail" ||
  921. _product_code == "ai-subscription-year-trail" {
  922. return 5
  923. }
  924. return 0
  925. } else {
  926. if _product_code == "advanced-annual-subscription-blackFive" ||
  927. _product_code == "advanced-permanent-blackFive" {
  928. return 11
  929. }
  930. return 1
  931. }
  932. }
  933. // MARK: get & set
  934. var product_code : String { //设置code
  935. get {
  936. return _product_code
  937. }
  938. set {
  939. _product_code = newValue
  940. if(isSubscribeCode() == true) {
  941. addButton1.isEnabled = false
  942. removeButton1.isEnabled = false
  943. amountTextField1.isEditable = false
  944. wechatPayButton.isHidden = true
  945. alipayButton.isHidden = true
  946. prodocumentView.isHidden = true
  947. } else {
  948. addButton1.isEnabled = true
  949. removeButton1.isEnabled = true
  950. amountTextField1.isEditable = true
  951. wechatPayButton.isHidden = false
  952. alipayButton.isHidden = false
  953. prodocumentView.isHidden = false
  954. }
  955. if(newValue == "advanced-annual-subscription-trail" ||
  956. newValue == "ai-subscription-year-trail") {
  957. couponView.isHidden = true
  958. }
  959. let model = KMProductModel.shared
  960. let products : [KMListingProductsModel] = model.dmgProductDatas?.listingProducts ?? []
  961. if products.count > 0 {
  962. for product in products {
  963. if product.code == _product_code {
  964. self.product_Info = product;
  965. //更新价格
  966. self.updateProduct_Info()
  967. break
  968. }
  969. }
  970. } else {
  971. model.getDMGProductDatas() { [weak self] success, result in
  972. let products : [KMListingProductsModel] = model.dmgProductDatas?.listingProducts ?? []
  973. if products.count > 0 {
  974. for product in products {
  975. if product.code == self?.product_code {
  976. self?.product_Info = product;
  977. //更新价格
  978. self?.updateProduct_Info()
  979. break
  980. }
  981. }
  982. }
  983. }
  984. }
  985. }
  986. }
  987. var product_Info: KMListingProductsModel { //刷新产品ui+价格
  988. get {
  989. return _product_Info
  990. }
  991. set {
  992. _product_Info = newValue
  993. if isWindowDidLoad {
  994. xibInitialization()
  995. priceRefresh(productsModel: _product_Info)
  996. pdfReaderProLabel.stringValue = product_Info.productName //待区分多语
  997. }
  998. }
  999. }
  1000. private var paymentMethod : KMPaymentType {
  1001. get {
  1002. return _paymentMethod
  1003. }
  1004. set {
  1005. _paymentMethod = newValue
  1006. paypalButton.image = NSImage(named: "EmbeddedPayment19")
  1007. cardButton.image = NSImage(named: "EmbeddedPayment21")
  1008. wechatPayButton.image = NSImage(named: "EmbeddedPayment23")
  1009. wechatPayButton2.image = NSImage(named: "EmbeddedPayment23")
  1010. alipayButton.image = NSImage(named: "EmbeddedPayment25")
  1011. if _paymentMethod == .paypal {
  1012. paypalButton.image = NSImage(named: "EmbeddedPayment18")
  1013. payBox.contentView = paypalView
  1014. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1015. payBoxHeight.constant = 60.0
  1016. } else if _paymentMethod == .paddle {
  1017. cardButton.image = NSImage(named: "EmbeddedPayment20")
  1018. payBox.contentView = cardView
  1019. paddleView.wantsLayer = true
  1020. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  1021. payBoxHeight.constant = 150.0
  1022. } else if _paymentMethod == .wxpay {
  1023. wechatPayButton.image = NSImage(named: "EmbeddedPayment22")
  1024. wechatPayButton2.image = NSImage(named: "EmbeddedPayment22")
  1025. payBox.contentView = weChatPayView
  1026. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1027. payBoxHeight.constant = 92.0
  1028. } else if _paymentMethod == .alipay {
  1029. alipayButton.image = NSImage(named: "EmbeddedPayment24")
  1030. payBox.contentView = weChatPayView
  1031. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1032. payBoxHeight.constant = 92.0
  1033. }
  1034. }
  1035. }
  1036. private func updatePrice() {
  1037. if _paymentMethod == .wxpay {
  1038. wechatPayButton.image = NSImage(named: "EmbeddedPayment22")
  1039. wechatPayButton2.image = NSImage(named: "EmbeddedPayment22")
  1040. payBox.contentView = weChatPayView
  1041. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1042. payBoxHeight.constant = 92.0
  1043. } else if _paymentMethod == .alipay {
  1044. alipayButton.image = NSImage(named: "EmbeddedPayment24")
  1045. payBox.contentView = weChatPayView
  1046. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1047. payBoxHeight.constant = 92.0
  1048. }
  1049. }
  1050. func isSubscribeCode()-> Bool {//是否是订阅
  1051. if(product_code == "standard-annual-subscription" ||
  1052. product_code == "advanced-annual-subscription-blackFive" ||
  1053. product_code == "advanced-annual-subscription-trail" ||
  1054. product_code == "advanced-annual-subscription" ||
  1055. product_code == "ai-subscription-year-trail" ||
  1056. product_code == "ai-subscription-month") {
  1057. return true
  1058. }
  1059. return false
  1060. }
  1061. func isOneTimePurchaseCode()-> Bool { //是否是一次性购买
  1062. if(product_code == "advanced-permanent-blackFive" ||
  1063. product_code == "advanced-permanent") {
  1064. return true
  1065. }
  1066. return false
  1067. }
  1068. func isBlackFiveCode()-> Bool { //是否是黑五
  1069. if(product_code == "advanced-permanent-blackFive" ||
  1070. product_code == "advanced-annual-subscription-blackFive") {
  1071. return true
  1072. }
  1073. return false
  1074. }
  1075. func isHaveAvailableCoupon()-> Bool { //是否有优惠卷
  1076. return false
  1077. }
  1078. func showPriceString()->String {
  1079. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1080. var priceString = product_Info.priceString ?? ""
  1081. if(product_Info.upgradePriceString?.isEmpty == false) {
  1082. priceString = product_Info.upgradePriceString ?? ""
  1083. } else if(product_Info.displayPriceString?.isEmpty == false) {
  1084. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1085. priceString = product_Info.displayCnyPriceString ?? ""
  1086. } else {
  1087. priceString = product_Info.displayPriceString ?? ""
  1088. }
  1089. } else if(product_Info.priceString?.isEmpty == false) {
  1090. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1091. priceString = product_Info.cnyPriceString ?? ""
  1092. } else {
  1093. priceString = product_Info.priceString ?? ""
  1094. }
  1095. }
  1096. return priceString
  1097. }
  1098. func showOrgPriceString()->String { //原价
  1099. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1100. var priceString = product_Info.priceString ?? ""
  1101. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1102. priceString = product_Info.cnyPriceString ?? ""
  1103. } else {
  1104. priceString = product_Info.priceString ?? ""
  1105. }
  1106. return priceString
  1107. }
  1108. func showOrgShowPriceString()->String { //实际购买显示的价格
  1109. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1110. var priceString = product_Info.priceString ?? ""
  1111. if(product_Info.upgradePriceString?.isEmpty == false) {
  1112. priceString = product_Info.upgradePriceString ?? ""
  1113. } else if(product_Info.displayPriceString?.isEmpty == false) {
  1114. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1115. priceString = product_Info.displayCnyPriceString ?? ""
  1116. } else {
  1117. priceString = product_Info.displayPriceString ?? ""
  1118. }
  1119. } else if(product_Info.priceString?.isEmpty == false) {
  1120. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1121. priceString = product_Info.cnyPriceString ?? ""
  1122. } else {
  1123. priceString = product_Info.priceString ?? ""
  1124. }
  1125. }
  1126. return priceString
  1127. }
  1128. func showSinglePriceString()->String { //单价
  1129. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  1130. var priceString = product_Info.individualPriceString ?? ""
  1131. if NSLocalizedString("USD", tableName: "MemberCenterLocalizable", comment: "") == "CN" {
  1132. priceString = product_Info.individualCnyPriceString ?? ""
  1133. } else {
  1134. priceString = product_Info.individualPriceString ?? ""
  1135. }
  1136. return priceString
  1137. }
  1138. func showPayPriceString()->String { //实际支付的价格
  1139. let iswechatOrAlipayPay = (self.paymentMethod == .wxpay || self.paymentMethod == .alipay)
  1140. var priceString = product_Info.priceString ?? ""
  1141. if(product_Info.upgradePriceString?.isEmpty == false) {
  1142. priceString = product_Info.upgradePriceString ?? ""
  1143. } else if(product_Info.displayPriceString?.isEmpty == false) {
  1144. if(iswechatOrAlipayPay) {
  1145. priceString = product_Info.displayCnyPriceString ?? ""
  1146. } else {
  1147. priceString = product_Info.displayPriceString ?? ""
  1148. }
  1149. } else if(product_Info.priceString?.isEmpty == false) {
  1150. if(iswechatOrAlipayPay) {
  1151. priceString = product_Info.cnyPriceString ?? ""
  1152. } else {
  1153. priceString = product_Info.priceString ?? ""
  1154. }
  1155. }
  1156. return priceString
  1157. }
  1158. // MARK: Private
  1159. private func _configFirmLabel() {
  1160. firmLabel.isEditable = false
  1161. firmLabel.isSelectable = true
  1162. firmLabel.allowsEditingTextAttributes = true
  1163. firmLabel.textColor = NSColor.black
  1164. firmLabel.font = NSFont.SFProTextRegularFont(14.0)
  1165. let firmString = NSLocalizedString("If you need to manage seats in PDF Reader Pro, please purchase the %@.", tableName: "MemberCenterLocalizable", comment: "")
  1166. let enterpriseStr = NSLocalizedString("enterprise version", tableName: "MemberCenterLocalizable", comment: "")
  1167. let fireFullString = String(format: firmString, enterpriseStr)
  1168. let firmLinkColor = NSColor(named: "4982E6") ?? NSColor.blue
  1169. let paragraphStyle = NSMutableParagraphStyle()
  1170. paragraphStyle.alignment = .right // 设置为右对齐
  1171. let attri = NSMutableAttributedString(string: fireFullString, attributes: [.foregroundColor : firmLinkColor, .font : NSFont.SFProTextRegularFont(14.0),.paragraphStyle:paragraphStyle,])
  1172. // 定义链接的范围
  1173. let enterpriseRange = (fireFullString as NSString).range(of: enterpriseStr)
  1174. let firmFont = NSFont.SFProTextRegularFont(14.0) // 与普通文本相同的字体
  1175. attri.addAttributes([
  1176. .foregroundColor: NSColor(named: "0E1114") ?? NSColor.black as Any,
  1177. .underlineStyle: NSUnderlineStyle.single.rawValue,
  1178. .font: firmFont
  1179. ], range: (fireFullString as NSString).range(of: enterpriseStr))
  1180. attri.addAttributes([
  1181. .foregroundColor: firmLinkColor,
  1182. .link: kLynxpdfeditorUrl,
  1183. .font: firmFont
  1184. ], range: enterpriseRange)
  1185. firmLabel.attributedStringValue = attri
  1186. }
  1187. private func uuid() -> String {
  1188. return GetHardwareUUID()!
  1189. }
  1190. private func _trackEvent_paid() {
  1191. self.trackEvent_dmg(eventName: self.kEventName, params: self.kEventParams, platform: .AppCenter)
  1192. }
  1193. private func getProductID() -> String {
  1194. return GetHardwareUUID()!
  1195. }
  1196. private func isNumeric(_ text: String) -> Bool {
  1197. return !text.isEmpty && text.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
  1198. }
  1199. private func openWebView(_ urlPath: String) -> Void {
  1200. DispatchQueue.main.async { [weak self] in
  1201. guard let self = self else { return }
  1202. self.pollCount = 0
  1203. self.startPolling()
  1204. self.embeddedPaymentPopWC = KMEmbeddedPaymentPopWC.currentFirstTrialWC(urlPath)
  1205. if self.paymentMethod == .paddle {
  1206. self.embeddedPaymentPopWC?.isPaddle = true
  1207. self.embeddedPaymentPopWC?.countryCode = self.countryAbbreviationValues[self.paddlePopUpButton.indexOfSelectedItem]
  1208. self.embeddedPaymentPopWC?.postCode = self.paddleTextField.stringValue
  1209. }
  1210. self.embeddedPaymentPopWC?.showWindow(nil)
  1211. self.embeddedPaymentPopWC?.callback = { isClose in
  1212. }
  1213. }
  1214. }
  1215. private func convertBase64StringToNSImage(base64String: String) -> NSImage? {
  1216. let sepS = "data:image/png;base64,"
  1217. guard let baseStr = base64String.components(separatedBy: sepS).last else {
  1218. return nil
  1219. }
  1220. if let data = Data(base64Encoded: baseStr) {
  1221. return NSImage(data: data)
  1222. }
  1223. return nil
  1224. }
  1225. private func scanCode(_ urlPath: String) -> Void {
  1226. DispatchQueue.main.async { [weak self] in
  1227. guard let self = self else { return }
  1228. self.payBox.contentView = self.payResult
  1229. self.payBoxHeight.constant = 262
  1230. if self.paymentMethod == .wxpay {
  1231. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with WeChat", comment: "")
  1232. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment27")
  1233. } else if self.paymentMethod == .alipay {
  1234. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with Alipay", comment: "")
  1235. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment28")
  1236. }
  1237. let img = self.convertBase64StringToNSImage(base64String: urlPath)
  1238. self.qrCodeImageView.image = img
  1239. self.pollCount = 0
  1240. self.startPolling()
  1241. }
  1242. }
  1243. private func pollingResultNew() -> Void { //刷新订单状态
  1244. DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
  1245. guard let self = self else { return }
  1246. if self.orderID == "" { return }
  1247. KMMemberCenterManager.manager.getStateByOrderId(orderId: self.orderID) { success, info in
  1248. self.startPolling()
  1249. if (success) {
  1250. let dic: NSDictionary = info ?? [:]
  1251. let requestDic: NSDictionary = dic["result"] as? NSDictionary ?? [:]
  1252. let status:String = requestDic["status"] as? String ?? ""
  1253. if(status.uppercased() == "COMPLETED") {
  1254. KMUserInfoVCModel().refreshUserInfo { success, msg,dic in
  1255. DispatchQueue.main.async {
  1256. NotificationCenter.default.post(name: NSNotification.Name.KMIAPSubscriptionLoaded, object: nil)
  1257. }
  1258. }
  1259. self.stopPolling()
  1260. self.close()
  1261. DispatchQueue.main.async {
  1262. KMPurchaseSuccessWindowController.shared.showWindow(nil)
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. }
  1269. private func startPolling() {
  1270. timer = Timer(timeInterval: interval, repeats: false) { [weak self] _ in
  1271. guard let self = self else { return }
  1272. self.performTask()
  1273. }
  1274. RunLoop.main.add(timer!, forMode: .common)
  1275. }
  1276. @objc private func performTask() {
  1277. guard pollCount < maxPolls else {
  1278. timer?.invalidate()
  1279. timer = nil
  1280. return
  1281. }
  1282. // 执行你的轮询任务
  1283. print("Performing polling task \(pollCount + 1)/\(maxPolls)")
  1284. pollCount += 1
  1285. pollingResultNew()
  1286. }
  1287. private func stopPolling() {
  1288. timer?.invalidate()
  1289. timer = nil
  1290. pollCount = 0
  1291. }
  1292. private func emailErrorLabel(isReveal: Bool) {
  1293. errorView.isHidden = !isReveal
  1294. errorLabelTopLayout.constant = errorView.isHidden ? -CGRectGetHeight(errorView.bounds) : 4
  1295. }
  1296. private func refreshApplyButton(isReveal: Bool) {
  1297. self.applyButton.isEnabled = isReveal
  1298. }
  1299. private func refreshLicenseCodeView(isReveal: Bool) {
  1300. }
  1301. private func refreshLicenseCodeErrorView(isReveal: Bool) {
  1302. }
  1303. private func refreshCouponError(isReveal: Bool) {
  1304. coupomErrorLabel.isHidden = !isReveal
  1305. coupomErrorTopLayout.constant = coupomErrorLabel.isHidden ? -CGRectGetHeight(coupomErrorLabel.bounds) : 8.0
  1306. }
  1307. var activityAlertViewController: KMActivityALertViewController? {
  1308. if _activityAlertViewController == nil {
  1309. let blockSelf = self
  1310. _activityAlertViewController = KMActivityALertViewController()
  1311. _activityAlertViewController?.callback = {
  1312. blockSelf.close()
  1313. }
  1314. }
  1315. return _activityAlertViewController
  1316. }
  1317. }
  1318. extension KMPurchaseEmbeddedWindowController: NSTextFieldDelegate {
  1319. func controlTextDidEndEditing(_ obj: Notification) {
  1320. let textField = obj.object as? NSTextField
  1321. if textField == amountTextField1 {
  1322. if !isNumeric(textField!.stringValue) {
  1323. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1324. } else {
  1325. if textField == amountTextField1 {
  1326. pdfCount = Int(textField!.stringValue) ?? 1
  1327. if Int(textField!.stringValue)! > 9999 {
  1328. pdfCount = 9999
  1329. textField?.stringValue = String(pdfCount)
  1330. } else if Int(textField!.stringValue)! < 1 {
  1331. pdfCount = 1
  1332. textField?.stringValue = String(pdfCount)
  1333. }
  1334. }
  1335. if pdfCount > 100 {
  1336. wechatPayButton2.isHidden = false
  1337. cardButton.isHidden = true
  1338. wechatPayButton.isHidden = true
  1339. if _paymentMethod == .paddle {
  1340. paymentMethod = .paypal
  1341. }
  1342. } else {
  1343. wechatPayButton2.isHidden = true
  1344. cardButton.isHidden = false
  1345. wechatPayButton.isHidden = false
  1346. }
  1347. }
  1348. } else if textField == emailTextField {
  1349. emailErrorLabel(isReveal: false)
  1350. } else if textField == paddleTextField {
  1351. }
  1352. }
  1353. func controlTextDidChange(_ obj: Notification) {
  1354. let textField = obj.object as? NSTextField
  1355. if textField == amountTextField1 {
  1356. if !isNumeric(textField!.stringValue) {
  1357. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1358. } else {
  1359. if Int(textField!.stringValue)! > 9999 {
  1360. textField?.stringValue = String(pdfCount)
  1361. } else if Int(textField!.stringValue)! < 1 {
  1362. textField?.stringValue = String(pdfCount)
  1363. }
  1364. }
  1365. }
  1366. }
  1367. }
  1368. extension KMPurchaseEmbeddedWindowController: NSWindowDelegate {
  1369. func windowWillClose(_ notification: Notification) {
  1370. if let data = self.window?.isEqual(to: notification.object), data {
  1371. // 窗口关闭,关闭轮询
  1372. stopPolling()
  1373. }
  1374. if self.embeddedPaymentPopWC != nil {
  1375. self.embeddedPaymentPopWC?.close()
  1376. self.embeddedPaymentPopWC = nil
  1377. }
  1378. KMPurchaseEmbeddedWindowController.currentWindowController = nil
  1379. }
  1380. }
  1381. #endif