KMPurchaseEmbeddedWindowController.swift 72 KB

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