KMPurchaseEmbeddedWindowController.swift 74 KB

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