KMPurchaseEmbeddedWindowController.swift 76 KB

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