KMPurchaseEmbeddedWindowController.swift 75 KB

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