KMPurchaseEmbeddedWindowController.swift 76 KB

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