KMPurchaseEmbeddedWindowController.swift 85 KB

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