KMPurchaseEmbeddedWindowController.swift 85 KB

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