KMPurchaseEmbeddedWindowController.swift 72 KB

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