KMPurchaseEmbeddedWindowController.swift 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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. class KMPurchaseEmbeddedWindowController: NSWindowController {
  17. @IBOutlet weak var mainBox: NSBox!
  18. @IBOutlet weak var yourOrderLabel: NSTextField!
  19. @IBOutlet weak var pdfReaderProLabel: NSTextField!
  20. @IBOutlet weak var prmiumLabel: NSTextField!
  21. @IBOutlet weak var oneLicenseLabel: NSTextField!
  22. @IBOutlet weak var removeButton1: NSButton! // 减少按钮1
  23. @IBOutlet weak var addButton1: NSButton! // 增加按钮1
  24. @IBOutlet weak var amountTextField1: NSTextField! // 数目1
  25. @IBOutlet weak var originalPriceLabel1: NSTextField! // 原价
  26. @IBOutlet weak var discountPriceLabel1: NSTextField! // 折扣
  27. @IBOutlet weak var aiAddOnView: NSView!
  28. @IBOutlet weak var aiAddOnButton: NSButton!
  29. @IBOutlet weak var aiAddOnLabel: NSTextField!
  30. @IBOutlet weak var IndividualLabel: NSTextField!
  31. @IBOutlet weak var removeButton2: NSButton! // 减少按钮2
  32. @IBOutlet weak var addButton2: NSButton! // 增加按钮2
  33. @IBOutlet weak var amountTextField2: NSTextField! // 数目2
  34. @IBOutlet weak var amountView2: NSView!
  35. @IBOutlet weak var originalPriceLabel2: NSTextField! // 原价
  36. @IBOutlet weak var discountPriceLabel2: NSTextField! // 折扣
  37. @IBOutlet weak var aiAddOnButton1: NSButton!
  38. @IBOutlet weak var extendedView: NSView!
  39. @IBOutlet weak var extendedButton: NSButton!
  40. @IBOutlet weak var extendedLabel: NSTextField!
  41. @IBOutlet weak var extendedImageView: NSImageView!
  42. @IBOutlet weak var accessLabel: NSTextField!
  43. @IBOutlet weak var removeButton3: NSButton! // 减少按钮3
  44. @IBOutlet weak var addButton3: NSButton! // 增加按钮3
  45. @IBOutlet weak var amountTextField3: NSTextField! // 数目3
  46. @IBOutlet weak var amountView3: NSView!
  47. @IBOutlet weak var originalPriceLabel3: NSTextField! // 原价
  48. @IBOutlet weak var discountPriceLabel3: NSTextField! // 折扣
  49. @IBOutlet weak var extendedButton1: NSButton!
  50. @IBOutlet weak var couponView: NSView!
  51. @IBOutlet weak var couponButton: NSButton!
  52. @IBOutlet weak var couponBox: NSBox!
  53. @IBOutlet weak var coupomTextField: NSTextField!
  54. @IBOutlet weak var couponClearButton: NSButton!
  55. @IBOutlet weak var applyBox: NSBox!
  56. @IBOutlet weak var applyButton: NSButton!
  57. @IBOutlet weak var coupomErrorLabel: NSTextField!
  58. @IBOutlet weak var coupomErrorTopLayout: NSLayoutConstraint!
  59. @IBOutlet weak var lastPriceLabel: NSTextField!
  60. @IBOutlet weak var lastPriceLabel1: NSTextField!
  61. @IBOutlet weak var discountView: NSView!
  62. @IBOutlet weak var discountLabel: NSTextField!
  63. @IBOutlet weak var discountLabel1: NSTextField!
  64. @IBOutlet weak var paySumLabel: NSTextField!
  65. @IBOutlet weak var paySumLabel1: NSTextField!
  66. @IBOutlet weak var discountLayoutConstraint: NSLayoutConstraint!
  67. @IBOutlet weak var privacyLabel: NSTextField!
  68. @IBOutlet weak var billInformationLabel: NSTextField!
  69. @IBOutlet weak var licenseCodeView: NSView!
  70. @IBOutlet weak var licenseCodeLabel: NSTextField!
  71. @IBOutlet weak var licenseCodeBox: NSBox!
  72. @IBOutlet weak var licenseCodeTextField: NSTextField!
  73. @IBOutlet weak var notFoundButton: NSButton!
  74. @IBOutlet weak var licenseCodeTopLayout: NSLayoutConstraint!
  75. @IBOutlet weak var licenseCodeErrorView: NSView!
  76. @IBOutlet weak var licenseCodeErrorLabel: NSTextField!
  77. @IBOutlet weak var licenseCodeErrorTopLayout: NSLayoutConstraint!
  78. @IBOutlet weak var emailLabel: NSTextField!
  79. @IBOutlet weak var emailTextField: NSTextField!
  80. @IBOutlet weak var emailBox: NSBox!
  81. @IBOutlet weak var errorView: NSView!
  82. @IBOutlet weak var errorLabel: NSTextField!
  83. @IBOutlet weak var errorLabelTopLayout: NSLayoutConstraint!
  84. @IBOutlet weak var paymentMethodLabel: NSTextField!
  85. @IBOutlet weak var paypalButton: NSButton!
  86. @IBOutlet weak var cardButton: NSButton!
  87. @IBOutlet weak var wechatPayButton: NSButton!
  88. @IBOutlet weak var alipayButton: NSButton!
  89. @IBOutlet weak var wechatPayButton2: NSButton!
  90. @IBOutlet weak var payBox: NSBox!
  91. @IBOutlet weak var payBoxHeight: NSLayoutConstraint!
  92. @IBOutlet weak var payExplainLabel: NSTextField!
  93. @IBOutlet var paypalView: NSView!
  94. @IBOutlet weak var paypalBox: NSBox!
  95. @IBOutlet weak var paypalBuyNowLabel: NSTextField!
  96. @IBOutlet var cardView: NSView!
  97. @IBOutlet var paddleView: NSView!
  98. @IBOutlet weak var paddleButton: NSButton!
  99. @IBOutlet weak var paddlePopUpButton: KMPopUpButton!
  100. @IBOutlet weak var paddleBox: NSBox!
  101. @IBOutlet weak var paddleTextField: NSTextField!
  102. @IBOutlet var weChatPayView: NSView!
  103. @IBOutlet weak var weChatBox: NSBox!
  104. @IBOutlet weak var weChatButton: NSButton!
  105. @IBOutlet var payResult: NSView!
  106. @IBOutlet weak var scanLabel: NSTextField!
  107. @IBOutlet weak var qrCodeImageView: NSImageView!
  108. @IBOutlet weak var payTypeImageView: NSImageView!
  109. @IBOutlet weak var successfullyView: NSView!
  110. @IBOutlet weak var purchasedLabel: NSTextField!
  111. @IBOutlet weak var successfullyLabel: NSTextField!
  112. @IBOutlet weak var imageBottomLayout: NSLayoutConstraint!
  113. @IBOutlet weak var image2BottomLayout: NSLayoutConstraint!
  114. private var pdfCount: Int = 1
  115. private var _paymentMethod: KMPaymentType = .paypal
  116. private var _productType: ProductType = .ipaConvert
  117. private var productID: String = "com.brother.pdfreaderpro.mac.product_2"
  118. private var originalProductID: String = "com.brother.pdfreaderpro.mac.product_1"
  119. var couponCode: String = "" //优惠卷
  120. private var discountAmount: String = "" // 优惠金额
  121. private var discountScale: String = "" // 优惠比例
  122. private var orderID: String = "" // 订单ID
  123. private var timer: Timer?
  124. private var pollCount = 0
  125. private let maxPolls = 18
  126. private let interval: TimeInterval = 5.0
  127. private var _activityAlertViewController: KMActivityALertViewController?
  128. private let countryKeys = [
  129. "Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa",
  130. "Andorra", "Angola", "Anguilla", "Antigua and Barbuda", "Argentina",
  131. "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas",
  132. "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
  133. "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire, Sint Eustatius and Saba",
  134. "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil",
  135. "Brit. Indian Ocean", "British Virgin Islands", "Brunei Darussalam", "Bulgaria",
  136. "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde",
  137. "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
  138. "Christmas Island", "Cocos Islands", "Colombia", "Comoros", "Congo",
  139. "Cook Islands", "Costa Rica", "Cote D'Ivoire", "Croatia", "Curaçao", "Cyprus",
  140. "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  141. "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
  142. "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
  143. "French Guiana", "French Polynesia", "French Southern Terr.", "Gabon", "Gambia",
  144. "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada",
  145. "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau",
  146. "Guyana", "Haiti", "Heard/ Mcdonald Islands", "Holy See/ Vatican City", "Honduras",
  147. "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iraq", "Ireland",
  148. "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan",
  149. "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Lao People's DR",
  150. "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein",
  151. "Lithuania", "Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi",
  152. "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
  153. "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
  154. "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique",
  155. "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
  156. "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue",
  157. "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan",
  158. "Palau", "Palestinian Territory", "Panama", "Papua New Guinea", "Paraguay",
  159. "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar",
  160. "Republic of Serbia", "Reunion", "Romania", "Russian Federation", "Rwanda",
  161. "S. Georgia/ Sandwich Islands", "Saint Helena", "Saint Kitts and Nevis",
  162. "Saint Lucia", "Saint Martin", "Saint Pierre and Miquelon", "Saint Vincent/ Grenadines",
  163. "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal",
  164. "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
  165. "Somalia", "South Africa", "South Korea", "Spain", "Sri Lanka", "Sudan",
  166. "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Taiwan",
  167. "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga",
  168. "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands",
  169. "Tuvalu", "U.S. Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates",
  170. "United Kingdom", "United States", "United States (M.O.I.)", "Uruguay", "Uzbekistan",
  171. "Vanuatu", "Venezuela", "Viet Nam", "Wallis and Futuna", "Western Sahara", "Yemen",
  172. "Zambia", "Zimbabwe"
  173. ]
  174. private let countryAbbreviationValues = [
  175. "AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB",
  176. "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "VG", "BN", "BG", "BF", "BI", "KH",
  177. "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CK", "CR", "CI", "HR", "CW", "CY",
  178. "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF",
  179. "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM",
  180. "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI",
  181. "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT",
  182. "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL",
  183. "AN", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH",
  184. "PN", "PL", "PT", "PR", "QA", "RS", "RE", "RO", "RU", "RW", "GS", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM",
  185. "ST", "SA", "SN", "SC", "SL", "SG", "SK", "SI", "SB", "SO", "ZA", "KR", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE",
  186. "CH", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "VI", "UG", "UA", "AE",
  187. "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "WF", "EH", "YE", "ZM", "ZW"
  188. ]
  189. private var handler: ((Int) -> Void)!
  190. static var currentWindowController: KMPurchaseEmbeddedWindowController!
  191. private var embeddedPaymentPopWC: KMEmbeddedPaymentPopWC?
  192. private var successfulPaymentsNumber = 0
  193. private var license1 = ""
  194. private var licenseAI1 = ""
  195. private var isApplyCoupon: Bool = false
  196. @objc static func currentFirstTrialWC(_ productId: String) -> KMPurchaseEmbeddedWindowController {
  197. if currentWindowController != nil {
  198. currentWindowController.productID = productId
  199. return currentWindowController
  200. } else {
  201. let configWC: KMPurchaseEmbeddedWindowController = KMPurchaseEmbeddedWindowController.init(windowNibName: "KMPurchaseEmbeddedWindowController")
  202. currentWindowController = configWC;
  203. currentWindowController.productID = productId
  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?.makeFirstResponder(nil)
  211. productType = self.productToType(productID)
  212. originalProductID = productID
  213. xibInitialization()
  214. paymentMethod = .paypal
  215. priceRefresh()
  216. confirmPayment(isConfirm: false)
  217. self.window?.delegate = self;
  218. }
  219. override func close() {
  220. super.close()
  221. self.clearEventData()
  222. }
  223. // MARK: UI
  224. private func xibInitialization() -> Void {
  225. yourOrderLabel.stringValue = NSLocalizedString("Your Order", comment: "")
  226. yourOrderLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  227. yourOrderLabel.font = NSFont.SFProTextBoldFont(20.0)
  228. pdfReaderProLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  229. pdfReaderProLabel.font = NSFont.SFProTextRegularFont(14.0)
  230. prmiumLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  231. prmiumLabel.font = NSFont.SFProTextRegularFont(12.0)
  232. oneLicenseLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  233. oneLicenseLabel.font = NSFont.SFProTextRegularFont(12.0)
  234. originalPriceLabel1.textColor = NSColor(named: "KMPurchaseTitleColor")
  235. originalPriceLabel1.font = NSFont.SFProTextRegularFont(16.0)
  236. discountPriceLabel1.textColor = NSColor(named: "KMPurchaseDiscountColor")
  237. discountPriceLabel1.font = NSFont.SFProTextRegularFont(9.0)
  238. refreshLicenseCodeView(isReveal: false)
  239. if productType == .ipaConvert || productType == .ipaAI {
  240. aiAddOnView.isHidden = true
  241. extendedView.isHidden = true
  242. if productType == .ipaConvert {
  243. pdfReaderProLabel.stringValue = NSLocalizedString("PDF to Office", comment: "")
  244. prmiumLabel.stringValue = NSLocalizedString("Export PDF to Word(.docx), Excel (.xlsx), PowerPoint(.pptx)", comment: "")
  245. oneLicenseLabel.stringValue = NSLocalizedString("and more offline. It's one time purchase.", comment: "")
  246. licenseCodeLabel.stringValue = NSLocalizedString("License Code", comment: "")
  247. licenseCodeLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  248. licenseCodeLabel.font = NSFont.SFProTextRegularFont(14.0)
  249. licenseCodeTextField.placeholderString = NSLocalizedString("Input license code previously activated the app", comment: "")
  250. licenseCodeTextField.delegate = self
  251. licenseCodeBox.borderColor = NSColor(named: "KMPurchase_DADBDE")!
  252. licenseCodeBox.fillColor = NSColor(named: "KMPurchase_FFFFFF")!
  253. notFoundButton.title = NSLocalizedString("Not Found?", comment: "")
  254. notFoundButton.setTitleColor(color: NSColor(named: "KMPurchaseCouponColor")!, font: NSFont(name: "body-s-regular", size: 14))
  255. licenseCodeTextField.stringValue = VerificationManager.default().originLicenseCode
  256. refreshLicenseCodeView(isReveal: true)
  257. licenseCodeErrorLabel.stringValue = NSLocalizedString("Please enter right license code. How to Retrieve License.", comment: "")
  258. licenseCodeErrorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
  259. licenseCodeErrorLabel.font = NSFont.SFProTextRegularFont(11.0)
  260. refreshLicenseCodeErrorView(isReveal: false)
  261. } else if productType == .ipaAI {
  262. pdfReaderProLabel.stringValue = NSLocalizedString("PDF Reader Pro AI Tools", comment: "")
  263. prmiumLabel.stringValue = NSLocalizedString("50 credits for 30 days", comment: "")
  264. oneLicenseLabel.stringValue = NSLocalizedString("Manually renew at USD 14.99", comment: "")
  265. }
  266. } else {
  267. pdfReaderProLabel.stringValue = NSLocalizedString("PDF Reader Pro for Mac", comment: "")
  268. if productToType(originalProductID) == .ipaPremium {
  269. prmiumLabel.stringValue = NSLocalizedString("Premium", comment: "")
  270. oneLicenseLabel.stringValue = NSLocalizedString("One license for one device. One time purchase.", comment: "")
  271. } else if productToType(originalProductID) == .ipaPermanent {
  272. prmiumLabel.stringValue = NSLocalizedString("Permanent version (with PDF to Office Pack)", comment: "")
  273. oneLicenseLabel.stringValue = NSLocalizedString("One license for one device. One time purchase.", comment: "")
  274. }
  275. aiAddOnButton.image = NSImage(named: "EmbeddedPayment06")
  276. aiAddOnLabel.stringValue = NSLocalizedString("AI Add-on", comment: "")
  277. aiAddOnLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  278. aiAddOnLabel.font = NSFont.SFProTextRegularFont(14.0)
  279. IndividualLabel.stringValue = NSLocalizedString("Individual monthly plan. Manually renew.", comment: "")
  280. IndividualLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  281. IndividualLabel.font = NSFont.SFProTextRegularFont(12.0)
  282. originalPriceLabel2.textColor = NSColor(named: "KMPurchaseTitleColor")
  283. originalPriceLabel2.font = NSFont.SFProTextRegularFont(16.0)
  284. discountPriceLabel2.textColor = NSColor(named: "KMPurchaseDiscountColor")
  285. discountPriceLabel2.font = NSFont.SFProTextRegularFont(9.0)
  286. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  287. extendedLabel.stringValue = NSLocalizedString("Extended Device Access", comment: "")
  288. accessLabel.stringValue = NSLocalizedString("Get access to your plan on up to 2 devices.", comment: "")
  289. extendedImageView.isHidden = false
  290. } else {
  291. extendedLabel.stringValue = NSLocalizedString("PDF to Office Pack", comment: "")
  292. accessLabel.stringValue = NSLocalizedString("One time purchase", comment: "")
  293. extendedImageView.isHidden = true
  294. }
  295. extendedButton.image = NSImage(named: "EmbeddedPayment06")
  296. extendedLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  297. extendedLabel.font = NSFont.SFProTextRegularFont(14.0)
  298. accessLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  299. accessLabel.font = NSFont.SFProTextRegularFont(12.0)
  300. originalPriceLabel3.textColor = NSColor(named: "KMPurchaseTitleColor")
  301. originalPriceLabel3.font = NSFont.SFProTextRegularFont(16.0)
  302. discountPriceLabel3.textColor = NSColor(named: "KMPurchaseDiscountColor")
  303. discountPriceLabel3.font = NSFont.SFProTextRegularFont(9.0)
  304. }
  305. removeButton1.isEnabled = false
  306. amountTextField1.delegate = self
  307. amountTextField2.isEditable = false
  308. amountTextField3.isEditable = false
  309. amountView2.isHidden = true
  310. amountView3.isHidden = true
  311. couponButton.title = NSLocalizedString("I have a coupon?", comment: "")
  312. couponButton.setTitleColor(color: NSColor(named: "KMPurchaseCouponColor")!, font: NSFont.SFProTextRegularFont(14))
  313. coupomTextField.placeholderString = NSLocalizedString("Please enter a coupon code.", comment: "")
  314. applyButton.title = NSLocalizedString("Apply", comment: "")
  315. applyButton.setTitleColor(color: NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black, font: NSFont.SFProTextRegularFont(13))
  316. if couponCode.count > 0 {
  317. coupomTextField.stringValue = couponCode
  318. couponButton.isHidden = true
  319. couponBox.isHidden = false
  320. couponClearButton.isHidden = false
  321. applyBox.isHidden = false
  322. } else {
  323. couponButton.isHidden = false
  324. couponBox.isHidden = true
  325. couponClearButton.isHidden = true
  326. applyBox.isHidden = true
  327. }
  328. couponBox.borderColor = NSColor(named: "KMPurchaseCouponBoxColor") ?? NSColor.gray
  329. couponBox.fillColor = NSColor(named: "KMPurchaseCouponBoxFillColor") ?? NSColor.gray
  330. applyBox.borderColor = NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black
  331. coupomErrorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
  332. coupomErrorLabel.font = NSFont.SFProTextRegularFont(11.0)
  333. refreshCouponError(isReveal: false)
  334. lastPriceLabel.stringValue = NSLocalizedString("List Price", comment: "")
  335. lastPriceLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
  336. lastPriceLabel.font = NSFont.SFProTextRegularFont(16.0)
  337. discountLabel.stringValue = NSLocalizedString("Discount", comment: "")
  338. discountLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
  339. discountLabel.font = NSFont.SFProTextRegularFont(16.0)
  340. paySumLabel.stringValue = NSLocalizedString("Pay sum", comment: "")
  341. paySumLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  342. paySumLabel.font = NSFont.SFProTextBoldFont(24.0)
  343. paySumLabel1.textColor = NSColor(named: "KMPurchaseCouponColor")
  344. paySumLabel1.font = NSFont.SFProTextBoldFont(24.0)
  345. privacyLabel.isEditable = false
  346. privacyLabel.isSelectable = true
  347. privacyLabel.allowsEditingTextAttributes = true
  348. privacyLabel.textColor = NSColor.black
  349. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  350. let tipsString = NSLocalizedString("Are you a student or a professor? %@ / Want a volume purchase? Please %@.", comment: "")
  351. let specialOffer = NSLocalizedString("Get Special Offer", comment: "")
  352. let contactsUs = NSLocalizedString("Contact Us", comment: "")
  353. let fullString = String(format: tipsString, specialOffer, contactsUs)
  354. let attributedString = NSMutableAttributedString(string: fullString)
  355. // 定义链接的范围
  356. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  357. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  358. let linkColor = NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0)
  359. let font = NSFont.SFProTextRegularFont(16.0) // 与普通文本相同的字体
  360. attributedString.addAttributes([
  361. .foregroundColor: NSColor(named: "KMPurchase_99A6B2") as Any,
  362. .font: font
  363. ], range: (fullString as NSString).range(of: fullString))
  364. attributedString.addAttributes([
  365. .foregroundColor: linkColor,
  366. .link: NSLocalizedString("https://www.pdfreaderpro.com/store?mode=edu", comment: ""),
  367. .font: font
  368. ], range: specialOfferRange)
  369. attributedString.addAttributes([
  370. .foregroundColor: linkColor,
  371. .link: NSLocalizedString("https://www.pdfreaderpro.com/vpp-purchase-program", comment: ""),
  372. .font: font
  373. ], range: contactsUsRange)
  374. privacyLabel.attributedStringValue = attributedString
  375. billInformationLabel.stringValue = NSLocalizedString("Billing Information", comment: "")
  376. billInformationLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  377. billInformationLabel.font = NSFont.SFProTextRegularFont(14.0)
  378. emailLabel.stringValue = NSLocalizedString("Email", comment: "")
  379. emailLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  380. emailLabel.font = NSFont.SFProTextRegularFont(14.0)
  381. emailTextField.placeholderString = NSLocalizedString("Email to receive license code", comment: "")
  382. emailBox.borderColor = NSColor(named: "KMPurchase_DADBDE")!
  383. // emailBox.fillColor = NSColor(named: "KMPurchase_FFFFFF")!
  384. errorLabel.stringValue = NSLocalizedString("Please enter your email for license receiving.", comment: "")
  385. if VerificationManager.default().email != nil {
  386. var emailString = ""
  387. if let email = VerificationManager.default().email, email.count > 0 {
  388. emailString = email
  389. } else {
  390. if let subscriptionEmail = VerificationManager.default().subscriptionEmail, subscriptionEmail.count > 0 {
  391. emailString = subscriptionEmail
  392. }
  393. }
  394. emailTextField.stringValue = emailString
  395. }
  396. emailTextField.delegate = self
  397. errorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
  398. errorLabel.font = NSFont.SFProTextRegularFont(11.0)
  399. paymentMethodLabel.stringValue = NSLocalizedString("Payment Method", comment: "")
  400. paymentMethodLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  401. paymentMethodLabel.font = NSFont.SFProTextRegularFont(14.0)
  402. wechatPayButton2.isHidden = true
  403. payExplainLabel.isEditable = false
  404. payExplainLabel.isSelectable = true
  405. payExplainLabel.allowsEditingTextAttributes = true
  406. payExplainLabel.textColor = NSColor.black
  407. payExplainLabel.font = NSFont.SFProTextRegularFont(11.0)
  408. let localizedString = NSLocalizedString("By submitting this order, I agree to the %@ and %@ for subscription products", comment: "")
  409. let privacyPolicy = NSLocalizedString("Privacy Policy", comment: "")
  410. let termsOfService = NSLocalizedString("Terms of Service", comment: "")
  411. let fullString1 = String(format: localizedString, privacyPolicy, termsOfService)
  412. let attributedString1 = NSMutableAttributedString(string: fullString1)
  413. let privacyPolicyRange = (fullString1 as NSString).range(of: privacyPolicy)
  414. let termsOfServiceRange = (fullString1 as NSString).range(of: termsOfService)
  415. let linkColor1 = NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0)
  416. let font1 = NSFont.SFProTextRegularFont(11.0) // 与普通文本相同的字体
  417. attributedString1.addAttributes([
  418. .foregroundColor: NSColor(named: "KMPurchase_99A6B2") as Any,
  419. .font: font1
  420. ], range: (fullString1 as NSString).range(of: fullString1))
  421. attributedString1.addAttributes([
  422. .foregroundColor: linkColor1,
  423. .link: NSLocalizedString("https://www.pdfreaderpro.com/privacy-policy", comment: ""),
  424. .font: font1
  425. ], range: privacyPolicyRange)
  426. attributedString1.addAttributes([
  427. .foregroundColor: linkColor1,
  428. .link: NSLocalizedString("https://www.pdfreaderpro.com/terms_of_service", comment: ""),
  429. .font: font1
  430. ], range: termsOfServiceRange)
  431. payExplainLabel.attributedStringValue = attributedString1
  432. paypalBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  433. paypalBuyNowLabel.textColor = NSColor.white
  434. paypalBuyNowLabel.font = NSFont.SFProTextBoldFont(16.0)
  435. paddleButton.title = NSLocalizedString("Continue to Pay", comment: "")
  436. paddleButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  437. paddlePopUpButton.type = .arrowDown
  438. paddlePopUpButton.removeAllItems()
  439. paddlePopUpButton.addItems(withTitles: countryKeys)
  440. let preferredLanguage = Locale.preferredLanguages.first ?? "en"
  441. if preferredLanguage.hasPrefix("zh-Hans") || preferredLanguage.hasPrefix("zh-Hant") {
  442. paddlePopUpButton.selectItem(at: 45)
  443. } else {
  444. paddlePopUpButton.selectItem(at: 229)
  445. }
  446. paddlePopUpButton.wantsLayer = true
  447. paddlePopUpButton.layer?.backgroundColor = NSColor(named: "KMPurchase_FFFFFF")?.cgColor
  448. paddlePopUpButton.layer?.borderColor = NSColor(named: "KMPurchase_DADBDE")?.cgColor
  449. paddleBox.fillColor = NSColor(named: "KMPurchase_FFFFFF") ?? NSColor.white
  450. paddleBox.borderColor = NSColor(named: "KMPurchase_DADBDE") ?? NSColor.gray
  451. paddleTextField.placeholderString = NSLocalizedString("Postcode", comment: "")
  452. paddleTextField.delegate = self
  453. weChatButton.title = NSLocalizedString("Continue to Pay", comment: "")
  454. weChatButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  455. qrCodeImageView.imageScaling = .scaleProportionallyUpOrDown
  456. scanLabel.textColor = NSColor(named: "KMPurchase_000000")
  457. scanLabel.font = NSFont.SFProTextRegularFont(12)
  458. successfullyView.isHidden = true
  459. purchasedLabel.stringValue = NSLocalizedString("Purchased", comment: "")
  460. purchasedLabel.textColor = NSColor(named: "KMPurchase_002143")
  461. purchasedLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  462. successfullyLabel.stringValue = NSLocalizedString("Successfully", comment: "")
  463. successfullyLabel.textColor = NSColor(named: "KMPurchase_002143")
  464. successfullyLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  465. emailErrorLabel(isReveal: false)
  466. }
  467. private func priceRefresh() -> Void {
  468. if KMDMGProductsManager.shareInstance.productDatas == nil {
  469. let alert = NSAlert()
  470. alert.alertStyle = .critical
  471. alert.messageText = NSLocalizedString("Error Information", comment: "")
  472. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  473. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  474. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  475. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  476. } else {
  477. alert.runModal()
  478. }
  479. return
  480. }
  481. var abbreviation = "USD"
  482. if NSLocalizedString("USD", comment: "") == "CNY" {
  483. abbreviation = "CNY"
  484. }
  485. var discount1: Float = 0.0
  486. var original1: Float = 0.0
  487. var discount2: Float = 0.0
  488. var original2: Float = 0.0
  489. var discount3: Float = 0.0
  490. var original3: Float = 0.0
  491. var listPrice: Float = 0.0
  492. var discount: Float = 0.0
  493. var paySum: Float = 0.0
  494. let originalType: ProductType = productToType(originalProductID)
  495. if originalType == .ipaConvert || originalType == .ipaAI {
  496. if originalType == .ipaConvert {
  497. discount1 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2")) ?? 19.99
  498. original1 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2")) ?? 19.99
  499. } else {
  500. discount1 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.ai.product_1")) ?? 14.99
  501. original1 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.ai.product_1")) ?? 14.99
  502. }
  503. listPrice = original1 * Float(amountTextField1.stringValue)!
  504. paySum = discount1 * Float(amountTextField1.stringValue)!
  505. discount = listPrice - paySum
  506. originalPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount1))
  507. if String(format: "%.2f", original1) == String(format: "%.2f", discount1) {
  508. discountPriceLabel1.isHidden = true
  509. } else {
  510. discountPriceLabel1.isHidden = false
  511. let attributedString = NSMutableAttributedString(string: String(format: "%.2f", original1))
  512. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  513. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  514. discountPriceLabel1.attributedStringValue = attributedString
  515. }
  516. } else {
  517. if productType == .ipaPremium {
  518. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")) ?? 74.98
  519. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")) ?? 74.98
  520. discount2 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.ai.product_1")) ?? 14.99
  521. original2 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.ai.product_1")) ?? 14.99
  522. discount1 = aiBinding_discount - discount2
  523. original1 = aiBinding_original - original2
  524. discount3 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  525. original3 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  526. listPrice = (original1 * Float(amountTextField1.stringValue)!)
  527. paySum = (discount1 * Float(amountTextField1.stringValue)!)
  528. discount = listPrice - paySum
  529. } else if productType == .ipaPermanent {
  530. if originalType == .ipaPremium {
  531. listPrice = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  532. paySum = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  533. discount3 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  534. original3 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  535. discount1 = paySum - discount3
  536. original1 = listPrice - original3
  537. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  538. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  539. let advanced_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  540. let advanced_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  541. discount2 = aiBinding_discount - advanced_discount
  542. original2 = aiBinding_original - advanced_original
  543. listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original3 * Float(amountTextField3.stringValue)!)
  544. paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
  545. discount = listPrice - paySum
  546. } else if originalType == .ipaPermanent {
  547. listPrice = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  548. paySum = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  549. discount1 = paySum
  550. original1 = listPrice
  551. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  552. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  553. let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
  554. let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
  555. discount2 = aiBinding_discount - discount1
  556. original2 = aiBinding_original - original1
  557. discount3 = cross_discount - discount1
  558. original3 = cross_original
  559. listPrice = (original1 * Float(amountTextField1.stringValue)!)
  560. paySum = (discount1 * Float(amountTextField1.stringValue)!)
  561. discount = listPrice - paySum
  562. }
  563. } else if productType == .ipaPremiumCrossPlatform {
  564. } else if productType == .ipaPermanentCrossPlatform {
  565. listPrice = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  566. paySum = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  567. discount1 = paySum
  568. original1 = listPrice
  569. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  570. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  571. let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
  572. let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
  573. discount2 = aiBinding_discount - discount1
  574. original2 = aiBinding_original - original1
  575. discount3 = cross_discount - discount1
  576. original3 = cross_original
  577. listPrice = (original1 * Float(amountTextField1.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
  578. paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
  579. discount = listPrice - paySum
  580. } else if productType == .ipaPremiumAI {
  581. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")) ?? 74.98
  582. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")) ?? 74.98
  583. discount2 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.ai.product_1")) ?? 14.99
  584. original2 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.ai.product_1")) ?? 14.99
  585. discount1 = aiBinding_discount - discount2
  586. original1 = aiBinding_original - original2
  587. discount3 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  588. original3 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  589. listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original2 * Float(amountTextField2.stringValue)!)
  590. paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount2 * Float(amountTextField2.stringValue)!)
  591. discount = listPrice - paySum
  592. } else if productType == .ipaPermanentAI {
  593. if originalType == .ipaPremium {
  594. listPrice = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  595. paySum = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  596. discount3 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  597. original3 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")) ?? 20.00
  598. discount1 = paySum - discount3
  599. original1 = listPrice - original3
  600. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  601. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  602. let advanced_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  603. let advanced_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  604. discount2 = aiBinding_discount - advanced_discount
  605. original2 = aiBinding_original - advanced_original
  606. listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original2 * Float(amountTextField2.stringValue)!) + (original3 * Float(amountTextField3.stringValue)!)
  607. paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount2 * Float(amountTextField2.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
  608. discount = listPrice - paySum
  609. } else if originalType == .ipaPermanent {
  610. listPrice = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  611. paySum = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  612. discount1 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  613. original1 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  614. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  615. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  616. let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
  617. let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
  618. discount2 = aiBinding_discount - discount1
  619. original2 = aiBinding_original - original1
  620. discount3 = cross_discount - discount1
  621. original3 = cross_original
  622. listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original2 * Float(amountTextField2.stringValue)!)
  623. paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount2 * Float(amountTextField2.stringValue)!)
  624. discount = listPrice - paySum
  625. }
  626. } else if productType == .ipaPermanentCrossPlatformAI {
  627. listPrice = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 149.96
  628. paySum = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 149.96
  629. discount1 = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  630. original1 = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")) ?? 79.99
  631. let aiBinding_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  632. let aiBinding_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")) ?? 94.98
  633. let cross_discount = Float(KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")) ?? 119.98
  634. let cross_original = Float(KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_2")) ?? 119.98
  635. discount2 = aiBinding_discount - discount1
  636. original2 = aiBinding_original - original1
  637. discount3 = cross_discount - discount1
  638. original3 = cross_original
  639. listPrice = (original1 * Float(amountTextField1.stringValue)!) + (original2 * Float(amountTextField2.stringValue)!) + (original3 * Float(amountTextField3.stringValue)!)
  640. paySum = (discount1 * Float(amountTextField1.stringValue)!) + (discount2 * Float(amountTextField2.stringValue)!) + (discount3 * Float(amountTextField3.stringValue)!)
  641. discount = listPrice - paySum
  642. }
  643. originalPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount1))
  644. if String(format: "%.2f", original1) == String(format: "%.2f", discount1) {
  645. discountPriceLabel1.isHidden = true
  646. } else {
  647. discountPriceLabel1.isHidden = false
  648. let attributedString = NSMutableAttributedString(string: String(format: "%.2f", original1))
  649. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  650. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  651. discountPriceLabel1.attributedStringValue = attributedString
  652. }
  653. originalPriceLabel2.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount2))
  654. if String(format: "%.2f", original2) == String(format: "%.2f", discount2) {
  655. discountPriceLabel2.isHidden = true
  656. } else {
  657. discountPriceLabel2.isHidden = false
  658. let attributedString = NSMutableAttributedString(string: String(format: "%.2f", original2))
  659. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  660. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  661. discountPriceLabel2.attributedStringValue = attributedString
  662. }
  663. originalPriceLabel3.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount3))
  664. if String(format: "%.2f", original3) == String(format: "%.2f", discount3) {
  665. discountPriceLabel3.isHidden = true
  666. } else {
  667. discountPriceLabel3.isHidden = false
  668. let attributedString = NSMutableAttributedString(string: String(format: "%.2f", original3))
  669. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  670. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  671. discountPriceLabel3.attributedStringValue = attributedString
  672. }
  673. }
  674. if self.discountScale != "" || self.discountAmount != "" {
  675. if self.discountScale != "" {
  676. paySum = paySum * Float(self.discountScale)!
  677. discount = listPrice - paySum
  678. } else {
  679. var discountAmountF = Float(self.discountAmount)
  680. if NSLocalizedString("USD", comment: "") == "CNY" {
  681. discountAmountF = discountAmountF! * 6.5
  682. }
  683. paySum = paySum - discountAmountF!
  684. discount = listPrice - paySum
  685. }
  686. }
  687. lastPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", listPrice))
  688. discountLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", discount))
  689. paySumLabel1.stringValue = String(format: "%@ %@", abbreviation, String(format: "%.2f", paySum))
  690. if self.discountScale == "" && self.discountAmount == "" {
  691. refreshApplyButton(isReveal: true)
  692. } else {
  693. refreshApplyButton(isReveal: false)
  694. }
  695. if discount == 0 {
  696. discountView.isHidden = true
  697. couponView.isHidden = false
  698. } else {
  699. discountView.isHidden = false
  700. if self.discountScale == "" && self.discountAmount == "" {
  701. couponView.isHidden = true
  702. } else {
  703. couponView.isHidden = false
  704. }
  705. }
  706. discountLayoutConstraint.constant = discountView.isHidden ? -CGRectGetHeight(discountView.bounds) : 8
  707. }
  708. // MARK: Active
  709. @IBAction func amountButtonAction(_ sender: NSButton) {
  710. if sender.tag == 0 || sender.tag == 1 {
  711. if sender.tag == 0 {
  712. if pdfCount > 1 {
  713. pdfCount -= 1
  714. }
  715. }
  716. if sender.tag == 1 {
  717. if pdfCount < 9999 {
  718. pdfCount += 1
  719. }
  720. }
  721. if pdfCount > 1 {
  722. removeButton1.isEnabled = true
  723. } else {
  724. removeButton1.isEnabled = false
  725. }
  726. if pdfCount > 100 {
  727. wechatPayButton2.isHidden = false
  728. cardButton.isHidden = true
  729. wechatPayButton.isHidden = true
  730. if _paymentMethod == .paddle {
  731. paymentMethod = .paypal
  732. }
  733. } else {
  734. wechatPayButton2.isHidden = true
  735. cardButton.isHidden = false
  736. wechatPayButton.isHidden = false
  737. }
  738. amountTextField1.stringValue = String(pdfCount)
  739. if extendedButton.state == .on {
  740. amountTextField2.stringValue = String(pdfCount * 2)
  741. } else {
  742. amountTextField2.stringValue = String(pdfCount * 1)
  743. }
  744. amountTextField3.stringValue = String(pdfCount)
  745. }
  746. // 价格刷新
  747. priceRefresh()
  748. }
  749. @IBAction func aiAddOnButtonAction(_ sender: NSButton) {
  750. let type: ProductType = productToType(originalProductID)
  751. aiAddOnButton.state = sender.state
  752. if sender.state == .on {
  753. amountView2.isHidden = false
  754. aiAddOnButton.image = NSImage(named: "EmbeddedPayment07")
  755. if extendedButton.state == .on {
  756. if type == .ipaPremium {
  757. productType = .ipaPermanentAI
  758. } else if type == .ipaPermanent {
  759. productType = .ipaPermanentCrossPlatformAI
  760. }
  761. } else {
  762. if type == .ipaPremium {
  763. productType = .ipaPremiumAI
  764. } else if type == .ipaPermanent {
  765. productType = .ipaPermanentAI
  766. }
  767. }
  768. } else {
  769. amountView2.isHidden = true
  770. aiAddOnButton.image = NSImage(named: "EmbeddedPayment06")
  771. if extendedButton.state == .on {
  772. if type == .ipaPremium {
  773. productType = .ipaPermanent
  774. } else if type == .ipaPermanent {
  775. productType = .ipaPermanentCrossPlatform
  776. }
  777. } else {
  778. if type == .ipaPremium {
  779. productType = .ipaPremium
  780. } else if type == .ipaPermanent {
  781. productType = .ipaPermanent
  782. }
  783. }
  784. }
  785. discountScale = ""
  786. discountAmount = ""
  787. coupomTextField.isEnabled = true
  788. // 价格刷新
  789. priceRefresh()
  790. }
  791. @IBAction func extendedButtonAction(_ sender: NSButton) {
  792. let type: ProductType = productToType(originalProductID)
  793. extendedButton.state = sender.state
  794. if sender.state == .on {
  795. amountView3.isHidden = false
  796. if type == .ipaPremium {
  797. amountTextField2.stringValue = String(pdfCount)
  798. } else {
  799. amountTextField2.stringValue = String(pdfCount * 2)
  800. }
  801. extendedButton.image = NSImage(named: "EmbeddedPayment07")
  802. if aiAddOnButton.state == .on {
  803. if type == .ipaPremium {
  804. productType = .ipaPermanentAI
  805. } else if type == .ipaPermanent {
  806. productType = .ipaPermanentCrossPlatformAI
  807. }
  808. } else {
  809. if type == .ipaPremium {
  810. productType = .ipaPermanent
  811. } else if type == .ipaPermanent {
  812. productType = .ipaPermanentCrossPlatform
  813. }
  814. }
  815. } else {
  816. amountView3.isHidden = true
  817. amountTextField2.stringValue = String(pdfCount)
  818. extendedButton.image = NSImage(named: "EmbeddedPayment06")
  819. if aiAddOnButton.state == .on {
  820. if type == .ipaPremium {
  821. productType = .ipaPremiumAI
  822. } else if type == .ipaPermanent {
  823. productType = .ipaPermanentAI
  824. }
  825. } else {
  826. if type == .ipaPremium {
  827. productType = .ipaPremium
  828. } else if type == .ipaPermanent {
  829. productType = .ipaPermanent
  830. }
  831. }
  832. }
  833. discountScale = ""
  834. discountAmount = ""
  835. coupomTextField.isEnabled = true
  836. // 价格刷新
  837. priceRefresh()
  838. }
  839. @IBAction func payButtonAction(_ sender: NSButton) {
  840. emailErrorLabel(isReveal: false)
  841. confirmPayment(isConfirm: false)
  842. paymentMethod = KMPaymentType(rawValue: uint32(sender.tag)) ?? .paypal
  843. }
  844. @IBAction func couponButtonAction(_ sender: NSButton) {
  845. couponButton.isHidden = true
  846. couponBox.isHidden = false
  847. applyBox.isHidden = false
  848. }
  849. @IBAction func applyButtonAction(_ sender: NSButton) {
  850. if coupomTextField.stringValue == "" {
  851. let alert = NSAlert()
  852. alert.alertStyle = .critical
  853. alert.messageText = NSLocalizedString("Error Information", comment: "")
  854. alert.informativeText = NSLocalizedString("Please enter a coupon code.", comment: "")
  855. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  856. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  857. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  858. } else {
  859. alert.runModal()
  860. }
  861. return
  862. }
  863. self.refreshCouponError(isReveal: false)
  864. applyButton.isEnabled = false
  865. coupomTextField.isEnabled = false
  866. self._getDiscount(productId: productID, discountId: coupomTextField.stringValue) { [weak self] info, err in
  867. guard let self = self else { return }
  868. if err == nil {
  869. if let dataInfo = info {
  870. if dataInfo.keys.contains("status") {
  871. let status = dataInfo["status"] as? String ?? ""
  872. if status == "" {
  873. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  874. self.refreshCouponError(isReveal: true)
  875. return
  876. }
  877. if status == "unused" {
  878. if dataInfo["discount"] is NSNull && dataInfo["price"] is NSNull {
  879. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  880. self.refreshCouponError(isReveal: true)
  881. return
  882. } else {
  883. if dataInfo.keys.contains("coupon_provider") {
  884. if let coupon = dataInfo["coupon_provider"] {
  885. if let swiftDict = coupon as? [String: Any] {
  886. let limit_product_code = swiftDict["limit_product_code"]
  887. if let limitCode = limit_product_code as? String {
  888. if limitCode.count > 0 {
  889. if self.productID != limitCode {
  890. self.couponClearButton.isHidden = false
  891. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  892. self.refreshCouponError(isReveal: true)
  893. return
  894. }
  895. }
  896. }
  897. }
  898. }
  899. }
  900. if dataInfo["discount"] is NSNull {
  901. if dataInfo["price"] is NSNull {
  902. } else {
  903. if let price1 = dataInfo["price"] {
  904. self.discountAmount = String(format: "%@", price1 as! CVarArg)
  905. }
  906. }
  907. } else {
  908. if let discount1 = dataInfo["discount"] {
  909. self.discountScale = String(format: "%@", discount1 as! CVarArg)
  910. }
  911. }
  912. }
  913. } else if status == "used" || status == "expired" {
  914. self.couponClearButton.isHidden = false
  915. if status == "used" {
  916. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code has been used, please change your coupon code.", comment: "")
  917. } else {
  918. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code has expired.", comment: "")
  919. }
  920. self.refreshCouponError(isReveal: true)
  921. }
  922. } else {
  923. if dataInfo.keys.contains("limit_product_code") {
  924. if let limit_product_code = dataInfo["limit_product_code"] {
  925. if let limitCode = limit_product_code as? String {
  926. if limitCode.count > 0 {
  927. if self.productID != limitCode {
  928. self.couponClearButton.isHidden = false
  929. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  930. self.refreshCouponError(isReveal: true)
  931. return
  932. }
  933. }
  934. }
  935. }
  936. }
  937. if dataInfo["discount"] is NSNull && dataInfo["price"] is NSNull {
  938. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  939. self.refreshCouponError(isReveal: true)
  940. } else {
  941. self.couponClearButton.isHidden = true
  942. if dataInfo["discount"] is NSNull {
  943. if dataInfo["price"] is NSNull {
  944. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  945. self.refreshCouponError(isReveal: true)
  946. } else {
  947. if let price1 = dataInfo["price"] {
  948. self.discountAmount = String(format: "%@", price1 as! CVarArg)
  949. }
  950. }
  951. } else {
  952. if let discount1 = dataInfo["discount"] {
  953. self.discountScale = String(format: "%@", discount1 as! CVarArg)
  954. }
  955. }
  956. }
  957. }
  958. }
  959. self.priceRefresh()
  960. } else {
  961. self.couponClearButton.isHidden = false
  962. self.coupomErrorLabel.stringValue = NSLocalizedString("The coupon code is invalid. Please try again.", comment: "")
  963. self.refreshCouponError(isReveal: true)
  964. }
  965. }
  966. }
  967. @IBAction func paddlePopUpAction(_ sender: NSPopUpButton) {
  968. }
  969. @IBAction func couponClearButton(_ sender: NSButton) {
  970. coupomTextField.stringValue = ""
  971. couponClearButton.isHidden = true
  972. applyButton.isEnabled = true
  973. coupomTextField.isEnabled = true
  974. refreshCouponError(isReveal: false)
  975. }
  976. @IBAction func buyAction(_ sender: NSButton) {
  977. if emailTextField.stringValue == "" {
  978. // let alert = NSAlert()
  979. // alert.alertStyle = .critical
  980. // alert.messageText = NSLocalizedString("Connection Error", comment: "")
  981. // alert.informativeText = NSLocalizedString("The email address is empty.", comment: "")
  982. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  983. // if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  984. // alert.beginSheetModal(for: self.window!, completionHandler: nil)
  985. // } else {
  986. // alert.runModal()
  987. // }
  988. emailErrorLabel(isReveal: true)
  989. return
  990. }
  991. if paymentMethod == .paddle {
  992. if !isNumeric(paddleTextField.stringValue) && paddleTextField.stringValue != "" {
  993. let alert = NSAlert()
  994. alert.alertStyle = .critical
  995. alert.messageText = NSLocalizedString("Error Information", comment: "")
  996. alert.informativeText = NSLocalizedString("Please enter the correct postcode.", comment: "")
  997. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  998. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  999. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  1000. } else {
  1001. alert.runModal()
  1002. }
  1003. return
  1004. }
  1005. }
  1006. confirmPayment(isConfirm: true)
  1007. if paymentMethod == .paypal {
  1008. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  1009. } else if paymentMethod == .paddle {
  1010. paddleView.wantsLayer = true
  1011. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor1")?.cgColor ?? NSColor.blue.cgColor
  1012. } else if paymentMethod == .wxpay {
  1013. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  1014. } else if paymentMethod == .alipay {
  1015. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor1") ?? NSColor.blue
  1016. }
  1017. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
  1018. guard let self = self else { return }
  1019. if self.paymentMethod == .paypal {
  1020. self.paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1021. } else if self._paymentMethod == .paddle {
  1022. self.paddleView.wantsLayer = true
  1023. self.paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  1024. } else if self._paymentMethod == .wxpay {
  1025. self.weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1026. } else if self._paymentMethod == .alipay {
  1027. self.weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1028. }
  1029. }
  1030. var licenseCode = ""
  1031. if productID == "com.brother.pdfreaderpro.mac.product_2" {
  1032. if licenseCodeTextField.stringValue.count > 0 {
  1033. licenseCode = licenseCodeTextField.stringValue
  1034. } else {
  1035. let alert = NSAlert()
  1036. alert.alertStyle = .critical
  1037. alert.messageText = NSLocalizedString("Error Information", comment: "")
  1038. alert.informativeText = NSLocalizedString("Input license code previously activated the app", comment: "")
  1039. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1040. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  1041. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  1042. } else {
  1043. alert.runModal()
  1044. }
  1045. return
  1046. }
  1047. if let license = VerificationManager.default().originLicenseCode, license.count > 0 {
  1048. licenseCode = license
  1049. }
  1050. }
  1051. var couponCode = ""
  1052. if coupomErrorLabel.isHidden {
  1053. couponCode = coupomTextField.stringValue
  1054. }
  1055. self._buyProduct(productID, count: pdfCount, discountId: couponCode, payment: paymentMethod, license: licenseCode, email: emailTextField.stringValue) { [weak self] info, err in
  1056. guard let self = self else { return }
  1057. if err != nil {
  1058. let alert = NSAlert()
  1059. alert.alertStyle = .critical
  1060. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  1061. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  1062. alert.runModal()
  1063. return
  1064. }
  1065. if let dataInfo = info {
  1066. self.orderID = dataInfo["trade_no"] as! String
  1067. let page_pay_url = dataInfo["page_pay_url"] as! String
  1068. if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
  1069. if page_pay_url != "" {
  1070. self.openWebView(page_pay_url)
  1071. }
  1072. } else if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  1073. if page_pay_url != "" {
  1074. self.scanCode(page_pay_url)
  1075. }
  1076. }
  1077. } else {
  1078. // 数据错误
  1079. let alert = NSAlert()
  1080. alert.alertStyle = .critical
  1081. alert.messageText = NSLocalizedString("Please check if the information is wrong or the network is error.", comment: "")
  1082. alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
  1083. alert.runModal()
  1084. }
  1085. }
  1086. }
  1087. @IBAction func notFoundAction(_ sender: NSButton) {
  1088. var url = URL(string: "https://www.pdfreaderpro.com/mac/license-retrieval")!
  1089. NSWorkspace.shared.open(url)
  1090. }
  1091. // MARK: get & set
  1092. private var paymentMethod : KMPaymentType {
  1093. get {
  1094. return _paymentMethod
  1095. }
  1096. set {
  1097. _paymentMethod = newValue
  1098. paypalButton.image = NSImage(named: "EmbeddedPayment19")
  1099. cardButton.image = NSImage(named: "EmbeddedPayment21")
  1100. wechatPayButton.image = NSImage(named: "EmbeddedPayment23")
  1101. wechatPayButton2.image = NSImage(named: "EmbeddedPayment23")
  1102. alipayButton.image = NSImage(named: "EmbeddedPayment25")
  1103. if _paymentMethod == .paypal {
  1104. paypalButton.image = NSImage(named: "EmbeddedPayment18")
  1105. payBox.contentView = paypalView
  1106. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1107. payBoxHeight.constant = 60.0
  1108. } else if _paymentMethod == .paddle {
  1109. cardButton.image = NSImage(named: "EmbeddedPayment20")
  1110. payBox.contentView = cardView
  1111. paddleView.wantsLayer = true
  1112. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  1113. payBoxHeight.constant = 150.0
  1114. } else if _paymentMethod == .wxpay {
  1115. wechatPayButton.image = NSImage(named: "EmbeddedPayment22")
  1116. wechatPayButton2.image = NSImage(named: "EmbeddedPayment22")
  1117. payBox.contentView = weChatPayView
  1118. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1119. payBoxHeight.constant = 92.0
  1120. } else if _paymentMethod == .alipay {
  1121. alipayButton.image = NSImage(named: "EmbeddedPayment24")
  1122. payBox.contentView = weChatPayView
  1123. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  1124. payBoxHeight.constant = 92.0
  1125. }
  1126. }
  1127. }
  1128. var productType: ProductType {
  1129. get {
  1130. return _productType
  1131. }
  1132. set {
  1133. _productType = newValue
  1134. if _productType == .ipaConvert {
  1135. productID = "com.brother.pdfreaderpro.mac.product_2"
  1136. } else if _productType == .ipaConvert_checkout {
  1137. productID = "com.brother.pdfreaderpro.mac.product_2.checkout"
  1138. } else if _productType == .ipaPremium {
  1139. productID = "com.brother.pdfreaderpro.mac.product_1"
  1140. } else if _productType == .ipaPermanent {
  1141. productID = "com.brother.pdfreaderpro.mac.product_3"
  1142. } else if _productType == .ipaPremiumCrossPlatform {
  1143. productID = "com.brother.pdfreaderpro.cross.platform.product_1"
  1144. } else if _productType == .ipaPermanentCrossPlatform {
  1145. productID = "com.brother.pdfreaderpro.cross.platform.product_3"
  1146. } else if _productType == .ipaAI {
  1147. productID = "com.brother.pdfreaderpro.ai.product_1"
  1148. } else if _productType == .ipaPremiumAI {
  1149. productID = "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1"
  1150. } else if _productType == .ipaPermanentAI {
  1151. productID = "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1"
  1152. } else if _productType == .ipaPermanentCrossPlatformAI {
  1153. productID = "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1"
  1154. }
  1155. }
  1156. }
  1157. // MARK: ——
  1158. // 获取价格
  1159. private func _getProductDatas(callback: @escaping (([String:Any]?, Any?)->Void)) {
  1160. // func getPriceUrl() {
  1161. let urlString = kVerificationServer + "/api/product/get-price/v2"
  1162. KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: nil) { task, responseObject, error in
  1163. if (error == nil && responseObject != nil) {
  1164. let array = responseObject?["data"] as? NSDictionary
  1165. if array != nil {
  1166. //解析数据
  1167. let productsDict = array?.object(forKey: "data") as? NSDictionary
  1168. callback(productsDict as? [String : Any], nil)
  1169. } else {
  1170. callback([:], error.debugDescription)
  1171. }
  1172. } else {
  1173. callback(nil, error.debugDescription)
  1174. }
  1175. }
  1176. }
  1177. // 获取优惠卷
  1178. private func _getDiscount(productId: String, discountId: String, callback: @escaping (([String : Any]?, Any?)->Void)) {
  1179. let urlString = kVerificationServer + "/api/coupons/retrieval"
  1180. let app_code = MainBundle.km_infoDictionary()?["CFBundleIdentifier"] ?? ""
  1181. let params = [
  1182. // 优惠卷id
  1183. "code" : discountId,
  1184. "app_code" : app_code,
  1185. "product_code" : productId]
  1186. KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: params) { task, responseObject, error in
  1187. if (error == nil && responseObject != nil) {
  1188. if let dataDict = responseObject?["data"] as? NSDictionary {
  1189. if let swiftDict = dataDict as? [String: Any] {
  1190. if swiftDict.keys.contains("price") || swiftDict.keys.contains("discount") {
  1191. callback(dataDict as? [String : Any], nil)
  1192. } else if swiftDict.keys.contains("coupon_provider") {
  1193. if let couponDict = swiftDict["coupon_provider"] as? [String: Any] {
  1194. callback(couponDict, nil)
  1195. }
  1196. }
  1197. }
  1198. } else if let coupon_provider = responseObject?["coupon_provider"] as? NSDictionary {
  1199. callback(coupon_provider as? [String : Any], nil)
  1200. } else {
  1201. callback([:], error.debugDescription)
  1202. }
  1203. } else {
  1204. callback(nil, error.debugDescription)
  1205. }
  1206. }
  1207. }
  1208. // 获取订单状态
  1209. private func _getOrderStatus(tradeNo: String, callback: @escaping (([String:Any]?, Any?)->Void)) {
  1210. let urlString = kVerificationServer + "/api/orders/getOrderStatus"
  1211. var jsonStr: String = ""
  1212. if let jsonData = try?JSONSerialization.data(withJSONObject: ["trade_no" : tradeNo], options:[]),
  1213. let data = String(data: jsonData, encoding: .utf8) {
  1214. jsonStr = data
  1215. }
  1216. let rsa = KMVerificationRSA()
  1217. let publicKey = rsa.loadPublicKey()
  1218. let encryptedData = rsa.encrypt(plainText: jsonStr, publicKey: publicKey!)
  1219. let tokenStr = encryptedData?.base64EncodedString()
  1220. var postData = try!JSONSerialization.data(withJSONObject: ["token" : tokenStr])
  1221. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  1222. request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  1223. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  1224. request.httpMethod = "POST"
  1225. request.httpBody = postData
  1226. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  1227. guard let data = data else {
  1228. callback(nil, String(describing: error))
  1229. return
  1230. }
  1231. if let jsonDict = try?JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:] {
  1232. let dataStr = jsonDict["data"] as? String
  1233. let rsa = KMVerificationRSA()
  1234. if let deData = (rsa.decrypt(source: dataStr as? NSString ?? "") as? String)?.data(using: .utf8) {
  1235. if let dict = try?JSONSerialization.jsonObject(with: deData, options: []) as? NSDictionary ?? [:] {
  1236. return callback(dict as? [String : Any], nil)
  1237. }
  1238. }
  1239. callback([:], String(describing: error))
  1240. } else {
  1241. // 数据错误
  1242. callback([:], String(describing: error))
  1243. }
  1244. }
  1245. task.resume()
  1246. }
  1247. private func _buyProduct(_ productId: String, count: Int, discountId: String = "", payment: KMPaymentType, license: String = "", email: String, username: String = "", callback: @escaping (([String:Any]?, Any?)->Void)) {
  1248. let urlString = kVerificationServer + "/api/subscriptions"
  1249. let app_code = MainBundle.km_infoDictionary()?["CFBundleIdentifier"] ?? ""
  1250. let paymentStr = self._paymentTypeString(type: payment)
  1251. let country = countryAbbreviationValues[paddlePopUpButton.indexOfSelectedItem]
  1252. let postcode = paddleTextField.stringValue
  1253. let paraDict = ["data" :
  1254. ["product_code": productId,
  1255. "app_code": app_code,
  1256. // 购买数量
  1257. "amount": "\(count)",
  1258. // 优惠卷id
  1259. "coupon_code": discountId,
  1260. // 支付类型 paypal alipay wxpay paddle
  1261. "payment": paymentStr,
  1262. // 地区
  1263. "customer_country": country,
  1264. // 邮编码
  1265. "customer_postcode" : postcode,
  1266. "unique_sn" : uuid(),
  1267. // 序列码 标准包 升级 高级包 需要
  1268. "upgrade_cdkey" : license,
  1269. "email" : email,
  1270. // 用户名
  1271. "contact_name" : username,
  1272. "platform" : "DMG"]]
  1273. var postData = try! JSONSerialization.data(withJSONObject: paraDict)
  1274. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  1275. request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  1276. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  1277. request.httpMethod = "POST"
  1278. request.httpBody = postData
  1279. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  1280. guard let theData = data else {
  1281. callback(nil, String(describing: error))
  1282. return
  1283. }
  1284. if let jsonDict = try? JSONSerialization.jsonObject(with: theData, options: []) as? NSDictionary ?? [:] {
  1285. let data1: NSDictionary = jsonDict["data"] as? NSDictionary ?? [:]
  1286. let subscriptionInfo = data1.object(forKey: "subscription") as? NSDictionary
  1287. let orderKey = self._paymentTypeString(type: payment) + "_order"
  1288. let order_info = subscriptionInfo?.object(forKey: orderKey) as? NSDictionary
  1289. if let dataInfo = order_info, dataInfo.count > 0 {
  1290. callback(dataInfo as? [String : Any], nil)
  1291. } else {
  1292. // 数据错误
  1293. callback([:], String(describing: error))
  1294. }
  1295. } else {
  1296. // 数据错误
  1297. callback([:], String(describing: error))
  1298. }
  1299. }
  1300. task.resume()
  1301. }
  1302. // MARK: Private
  1303. private func uuid() -> String {
  1304. return GetHardwareUUID()!
  1305. }
  1306. private func _paymentTypeString(type: KMPaymentType) -> String {
  1307. if type == .paddle {
  1308. return "paddle"
  1309. } else if type == .alipay {
  1310. return "alipay"
  1311. } else if type == .wxpay {
  1312. return "wxpay"
  1313. } else if type == .paypal {
  1314. return "paypal"
  1315. }
  1316. return ""
  1317. }
  1318. private func _trackEvent_paid() {
  1319. self.trackEvent_dmg(eventName: self.kEventName, params: self.kEventParams, platform: .AppCenter)
  1320. }
  1321. private let tabDict: [ProductType: String] = [
  1322. .ipaConvert: "com.brother.pdfreaderpro.mac.product_2",
  1323. .ipaConvert_checkout: "com.brother.pdfreaderpro.mac.product_2.checkout",
  1324. .ipaPremium: "com.brother.pdfreaderpro.mac.product_1",
  1325. .ipaPermanent: "com.brother.pdfreaderpro.mac.product_3",
  1326. .ipaPremiumCrossPlatform: "com.brother.pdfreaderpro.cross.platform.product_1",
  1327. .ipaPermanentCrossPlatform: "com.brother.pdfreaderpro.cross.platform.product_3",
  1328. .ipaAI: "com.brother.pdfreaderpro.ai.product_1",
  1329. .ipaPremiumAI: "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1",
  1330. .ipaPermanentAI: "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1",
  1331. .ipaPermanentCrossPlatformAI: "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1"
  1332. ]
  1333. private func getProductID() -> String {
  1334. return GetHardwareUUID()!
  1335. }
  1336. private func productToType(_ productId: String) -> ProductType {
  1337. if productId == "com.brother.pdfreaderpro.mac.product_2" {
  1338. return .ipaConvert
  1339. } else if productId == "com.brother.pdfreaderpro.mac.product_2.checkout" {
  1340. return .ipaConvert_checkout
  1341. } else if productId == "com.brother.pdfreaderpro.mac.product_1" {
  1342. return .ipaPremium
  1343. } else if productId == "com.brother.pdfreaderpro.mac.product_3" {
  1344. return .ipaPermanent
  1345. } else if productId == "com.brother.pdfreaderpro.cross.platform.product_1" {
  1346. return .ipaPremiumCrossPlatform
  1347. } else if productId == "com.brother.pdfreaderpro.cross.platform.product_3" {
  1348. return .ipaPermanentCrossPlatform
  1349. } else if productId == "com.brother.pdfreaderpro.ai.product_1" {
  1350. return .ipaAI
  1351. } else if productId == "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1" {
  1352. return .ipaPremiumAI
  1353. } else if productId == "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1" {
  1354. return .ipaPermanentAI
  1355. } else if productId == "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1" {
  1356. return .ipaPermanentCrossPlatformAI
  1357. }
  1358. return .ipaConvert
  1359. }
  1360. private func isNumeric(_ text: String) -> Bool {
  1361. return !text.isEmpty && text.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
  1362. }
  1363. private func openWebView(_ urlPath: String) -> Void {
  1364. DispatchQueue.main.async { [weak self] in
  1365. guard let self = self else { return }
  1366. // let wc = KMEmbeddedPaymentPopWC.init(windowNibName: "KMEmbeddedPaymentPopWC")
  1367. self.embeddedPaymentPopWC = KMEmbeddedPaymentPopWC.currentFirstTrialWC(urlPath)
  1368. if paymentMethod == .paddle {
  1369. self.embeddedPaymentPopWC?.isPaddle = true
  1370. self.embeddedPaymentPopWC?.countryCode = countryAbbreviationValues[paddlePopUpButton.indexOfSelectedItem]
  1371. self.embeddedPaymentPopWC?.postCode = paddleTextField.stringValue
  1372. }
  1373. self.embeddedPaymentPopWC?.showWindow(nil)
  1374. self.embeddedPaymentPopWC?.callback = { isClose in
  1375. // let alert = NSAlert()
  1376. // alert.alertStyle = .critical
  1377. // alert.messageText = NSLocalizedString("Checking payment status", comment: "")
  1378. //// alert.informativeText = NSLocalizedString("Get License Code is empty.", comment: "")
  1379. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1380. // if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  1381. // alert.beginSheetModal(for: self.window!, completionHandler: nil)
  1382. // } else {
  1383. // alert.runModal()
  1384. // }
  1385. }
  1386. }
  1387. startPolling()
  1388. }
  1389. private func scanCode(_ urlPath: String) -> Void {
  1390. DispatchQueue.main.async { [weak self] in
  1391. guard let self = self else { return }
  1392. self.payBox.contentView = self.payResult
  1393. self.payBoxHeight.constant = 262
  1394. if self.paymentMethod == .wxpay {
  1395. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with WeChat", comment: "")
  1396. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment27")
  1397. } else if self.paymentMethod == .alipay {
  1398. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with Alipay", comment: "")
  1399. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment28")
  1400. }
  1401. if let url = URL(string: urlPath) {
  1402. URLSession.shared.dataTask(with: url) { data, response, error in
  1403. if let data = data, let image = NSImage(data: data) {
  1404. DispatchQueue.main.async {
  1405. self.qrCodeImageView.image = image
  1406. self.startPolling()
  1407. }
  1408. } else {
  1409. print("Failed to load image or there was an error: \(error?.localizedDescription ?? "Unknown error")")
  1410. }
  1411. }.resume()
  1412. } else {
  1413. print("Invalid URL.")
  1414. }
  1415. }
  1416. }
  1417. private func pollingResult() -> Void {
  1418. DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
  1419. guard let self = self else { return }
  1420. if self.orderID == "" { return }
  1421. self._getOrderStatus(tradeNo: self.orderID, callback: { [self] info, err in
  1422. if err == nil {
  1423. if let dataInfo = info {
  1424. if dataInfo.keys.contains("status") {
  1425. let status = dataInfo["status"] as? Int
  1426. if status == 2 {
  1427. if self.originalProductID == "com.brother.pdfreaderpro.mac.product_2" {
  1428. VerificationManager.default().verification { status, info, error in
  1429. if (error != nil) {
  1430. let alert = NSAlert.init()
  1431. alert.messageText = NSLocalizedString("Failed to Refresh", comment: "")
  1432. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1433. alert.runModal()
  1434. }
  1435. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  1436. self.close()
  1437. }
  1438. }
  1439. } else {
  1440. self.orderID = ""
  1441. self.stopPolling()
  1442. if (dataInfo.keys.contains("license") && dataInfo.keys.contains("ai_license")) {
  1443. self.successfulPaymentsNumber = 2
  1444. if dataInfo.keys.contains("license") {
  1445. let license = dataInfo["license"] as? [String]
  1446. if license!.count > 0 {
  1447. self.verificationActivate(license: license![0])
  1448. // self.embeddedPaymentPopWC?.close()
  1449. // self.embeddedPaymentPopWC = nil
  1450. }
  1451. }
  1452. if dataInfo.keys.contains("ai_license") {
  1453. let ai_license = dataInfo["ai_license"] as? [String]
  1454. if ai_license!.count > 0 {
  1455. self.verificationActivateAIInfo(license: ai_license![0])
  1456. // self.embeddedPaymentPopWC?.close()
  1457. // self.embeddedPaymentPopWC = nil
  1458. }
  1459. }
  1460. } else {
  1461. self.successfulPaymentsNumber = 1
  1462. if dataInfo.keys.contains("license") {
  1463. let license = dataInfo["license"] as? [String]
  1464. if license!.count > 0 {
  1465. self.verificationActivate(license: license![0])
  1466. // self.embeddedPaymentPopWC?.close()
  1467. // self.embeddedPaymentPopWC = nil
  1468. }
  1469. } else if dataInfo.keys.contains("ai_license") {
  1470. let ai_license = dataInfo["ai_license"] as? [String]
  1471. if ai_license!.count > 0 {
  1472. if !AIInfoManager.default().aiInfoValid {
  1473. self.verificationActivateAIInfo(license: ai_license![0])
  1474. }
  1475. // self.embeddedPaymentPopWC?.close()
  1476. // self.embeddedPaymentPopWC = nil
  1477. }
  1478. }
  1479. }
  1480. }
  1481. } else {
  1482. if self.originalProductID == "com.brother.pdfreaderpro.mac.product_2" {
  1483. VerificationManager.default().verification { status, info, error in
  1484. if (error != nil) {
  1485. let alert = NSAlert.init()
  1486. alert.messageText = NSLocalizedString("Failed to Refresh", comment: "")
  1487. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1488. alert.runModal()
  1489. }
  1490. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  1491. self.close()
  1492. }
  1493. }
  1494. } else {
  1495. self.startPolling()
  1496. }
  1497. }
  1498. }
  1499. }
  1500. } else {
  1501. self.startPolling()
  1502. }
  1503. })
  1504. }
  1505. }
  1506. private func startPolling() {
  1507. timer = Timer(timeInterval: interval, repeats: false) { [weak self] _ in
  1508. guard let self = self else { return }
  1509. self.performTask()
  1510. }
  1511. RunLoop.main.add(timer!, forMode: .common)
  1512. }
  1513. @objc private func performTask() {
  1514. guard pollCount < maxPolls else {
  1515. timer?.invalidate()
  1516. timer = nil
  1517. DispatchQueue.main.async { [weak self] in
  1518. guard let self = self else { return }
  1519. self.embeddedPaymentPopWC?.close()
  1520. self.embeddedPaymentPopWC = nil
  1521. }
  1522. return
  1523. }
  1524. // 执行你的轮询任务
  1525. print("Performing polling task \(pollCount + 1)/\(maxPolls)")
  1526. pollCount += 1
  1527. pollingResult()
  1528. }
  1529. private func stopPolling() {
  1530. timer?.invalidate()
  1531. timer = nil
  1532. print("Polling task completed.")
  1533. if productID == "com.brother.pdfreaderpro.ai.product_1" ||
  1534. productID == "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1" ||
  1535. productID == "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1" ||
  1536. productID == "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1"{
  1537. if !AIInfoManager.default().aiInfoValid {
  1538. if self.embeddedPaymentPopWC != nil {
  1539. DispatchQueue.main.async { [weak self] in
  1540. guard let self = self else { return }
  1541. self.embeddedPaymentPopWC?.close()
  1542. self.embeddedPaymentPopWC = nil
  1543. }
  1544. }
  1545. }
  1546. } else {
  1547. if self.embeddedPaymentPopWC != nil {
  1548. DispatchQueue.main.async { [weak self] in
  1549. guard let self = self else { return }
  1550. self.embeddedPaymentPopWC?.close()
  1551. self.embeddedPaymentPopWC = nil
  1552. }
  1553. }
  1554. }
  1555. pollCount = 0
  1556. }
  1557. private func verificationActivateAIInfo(license: String) {
  1558. guard !license.isEmpty else {
  1559. return
  1560. }
  1561. let infoDic = ["cdkey": license]
  1562. AIInfoManager.default().activateAI(withInfo: infoDic) { info, error in
  1563. if info.isEmpty == false {
  1564. self._trackEvent_paid()
  1565. }
  1566. self.successfulPaymentsNumber -= 1
  1567. if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  1568. self.qrCodeImageView.isHidden = true
  1569. self.successfullyView.isHidden = false
  1570. if self.successfulPaymentsNumber == 0 {
  1571. if let blockSelf = self.activityAlertViewController {
  1572. blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
  1573. blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro AI features.", comment: "")
  1574. var frame = self.window?.frame
  1575. frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
  1576. frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
  1577. frame!.size.width = blockSelf.view.frame.size.width
  1578. frame!.size.height = blockSelf.view.frame.size.height
  1579. self.mainBox.contentView = blockSelf.view
  1580. self.window?.setFrame(frame!, display: true, animate: true)
  1581. self.embeddedPaymentPopWC?.close()
  1582. self.embeddedPaymentPopWC = nil
  1583. }
  1584. }
  1585. } else {
  1586. if self.successfulPaymentsNumber == 0 {
  1587. if let blockSelf = self.activityAlertViewController {
  1588. blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
  1589. blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro AI features.", comment: "")
  1590. var frame = self.window?.frame
  1591. frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
  1592. frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
  1593. frame!.size.width = blockSelf.view.frame.size.width
  1594. frame!.size.height = blockSelf.view.frame.size.height
  1595. self.mainBox.contentView = blockSelf.view
  1596. self.window?.setFrame(frame!, display: true, animate: true)
  1597. self.embeddedPaymentPopWC?.close()
  1598. self.embeddedPaymentPopWC = nil
  1599. }
  1600. }
  1601. }
  1602. }
  1603. }
  1604. private func verificationActivate(license: String) {
  1605. guard !license.isEmpty else {
  1606. return
  1607. }
  1608. let infoDic = ["cdkey": license]
  1609. VerificationManager.default().activateDevice(withInfo: infoDic) { status, info, error in
  1610. if error != nil {
  1611. let alert = NSAlert()
  1612. alert.alertStyle = .critical
  1613. alert.messageText = NSLocalizedString("Activation Error", comment: "")
  1614. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1615. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  1616. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  1617. } else {
  1618. alert.runModal()
  1619. }
  1620. return
  1621. } else {
  1622. if let data = info, data.isEmpty == false {
  1623. self._trackEvent_paid()
  1624. }
  1625. self.successfulPaymentsNumber -= 1
  1626. if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  1627. self.qrCodeImageView.isHidden = true
  1628. self.successfullyView.isHidden = false
  1629. if self.successfulPaymentsNumber == 0 {
  1630. if let blockSelf = self.activityAlertViewController {
  1631. blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
  1632. blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro features.", comment: "")
  1633. var frame = self.window?.frame
  1634. frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
  1635. frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
  1636. frame!.size.width = blockSelf.view.frame.size.width
  1637. frame!.size.height = blockSelf.view.frame.size.height
  1638. self.mainBox.contentView = blockSelf.view
  1639. self.window?.setFrame(frame!, display: true, animate: true)
  1640. self.embeddedPaymentPopWC?.close()
  1641. self.embeddedPaymentPopWC = nil
  1642. }
  1643. }
  1644. } else {
  1645. if self.successfulPaymentsNumber == 0 {
  1646. if let blockSelf = self.activityAlertViewController {
  1647. blockSelf.alertTitle = NSLocalizedString("Successful Activation!", comment: "")
  1648. blockSelf.alertMessage = NSLocalizedString("Congratulations! You can start using PDF Reader Pro features.", comment: "")
  1649. var frame = self.window?.frame
  1650. frame!.origin.y -= blockSelf.view.frame.size.height-frame!.size.height
  1651. frame!.origin.x -= (blockSelf.view.frame.size.width-frame!.size.width)/2.0
  1652. frame!.size.width = blockSelf.view.frame.size.width
  1653. frame!.size.height = blockSelf.view.frame.size.height
  1654. self.mainBox.contentView = blockSelf.view
  1655. self.window?.setFrame(frame!, display: true, animate: true)
  1656. self.embeddedPaymentPopWC?.close()
  1657. self.embeddedPaymentPopWC = nil
  1658. }
  1659. }
  1660. }
  1661. }
  1662. }
  1663. }
  1664. private func confirmPayment(isConfirm: Bool) {
  1665. if isConfirm {
  1666. removeButton1.isEnabled = false
  1667. addButton1.isEnabled = false
  1668. aiAddOnButton1.isEnabled = false
  1669. extendedButton1.isEnabled = false
  1670. couponButton.isEnabled = false
  1671. coupomTextField.isEnabled = false
  1672. applyButton.isEnabled = false
  1673. emailTextField.isEnabled = false
  1674. licenseCodeTextField.isEnabled = false
  1675. } else {
  1676. removeButton1.isEnabled = true
  1677. addButton1.isEnabled = true
  1678. aiAddOnButton1.isEnabled = true
  1679. extendedButton1.isEnabled = true
  1680. couponButton.isEnabled = true
  1681. emailTextField.isEnabled = true
  1682. licenseCodeTextField.isEnabled = true
  1683. }
  1684. }
  1685. private func emailErrorLabel(isReveal: Bool) {
  1686. errorView.isHidden = !isReveal
  1687. errorLabelTopLayout.constant = errorView.isHidden ? -CGRectGetHeight(errorView.bounds) : 4
  1688. }
  1689. private func refreshApplyButton(isReveal: Bool) {
  1690. self.applyButton.isEnabled = isReveal
  1691. }
  1692. private func refreshLicenseCodeView(isReveal: Bool) {
  1693. licenseCodeView.isHidden = !isReveal
  1694. licenseCodeTopLayout.constant = licenseCodeView.isHidden ? -CGRectGetHeight(licenseCodeView.bounds)-8 : 8.0
  1695. imageBottomLayout.constant = licenseCodeView.isHidden ? 16.0 : 8.0
  1696. image2BottomLayout.constant = licenseCodeView.isHidden ? 20.0 : 8.0
  1697. }
  1698. private func refreshLicenseCodeErrorView(isReveal: Bool) {
  1699. licenseCodeErrorView.isHidden = !isReveal
  1700. licenseCodeErrorTopLayout.constant = licenseCodeErrorView.isHidden ? -CGRectGetHeight(licenseCodeErrorView.bounds) : 4.0
  1701. }
  1702. private func refreshCouponError(isReveal: Bool) {
  1703. coupomErrorLabel.isHidden = !isReveal
  1704. coupomErrorTopLayout.constant = coupomErrorLabel.isHidden ? -CGRectGetHeight(coupomErrorLabel.bounds) : 8.0
  1705. }
  1706. var activityAlertViewController: KMActivityALertViewController? {
  1707. if _activityAlertViewController == nil {
  1708. let blockSelf = self
  1709. _activityAlertViewController = KMActivityALertViewController()
  1710. _activityAlertViewController?.callback = {
  1711. blockSelf.close()
  1712. }
  1713. }
  1714. return _activityAlertViewController
  1715. }
  1716. // MARK: Show Methods
  1717. // @IBAction private func dismissSheet(_ sender: NSButton) {
  1718. // NSApp.endSheet(window!)
  1719. // window!.orderOut(self)
  1720. // }
  1721. //
  1722. //
  1723. // @objc private func didEndSheet(_ sheet: NSWindow?, returnCode: Int, contextInfo: UnsafeMutableRawPointer?) {
  1724. // if contextInfo != nil && self.handler != nil {
  1725. // self.handler!(returnCode)
  1726. // }
  1727. // }
  1728. //
  1729. // @objc func beginSheetModal(for window: NSWindow?, completionHandler handler: ((Int) -> Void)?) {
  1730. // if window != nil {
  1731. // window!.beginSheet(self.window!) { ModalResponse in
  1732. // self.handler?(ModalResponse.rawValue)
  1733. // }
  1734. // }
  1735. // self.handler = handler
  1736. // }
  1737. }
  1738. extension KMPurchaseEmbeddedWindowController: NSTextFieldDelegate {
  1739. func controlTextDidEndEditing(_ obj: Notification) {
  1740. let textField = obj.object as? NSTextField
  1741. if textField == amountTextField1 {
  1742. if !isNumeric(textField!.stringValue) {
  1743. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1744. } else {
  1745. if textField == amountTextField1 {
  1746. pdfCount = Int(textField!.stringValue) ?? 1
  1747. if Int(textField!.stringValue)! > 9999 {
  1748. pdfCount = 9999
  1749. textField?.stringValue = String(pdfCount)
  1750. } else if Int(textField!.stringValue)! < 1 {
  1751. pdfCount = 1
  1752. textField?.stringValue = String(pdfCount)
  1753. }
  1754. }
  1755. if pdfCount > 100 {
  1756. wechatPayButton2.isHidden = false
  1757. cardButton.isHidden = true
  1758. wechatPayButton.isHidden = true
  1759. if _paymentMethod == .paddle {
  1760. paymentMethod = .paypal
  1761. }
  1762. } else {
  1763. wechatPayButton2.isHidden = true
  1764. cardButton.isHidden = false
  1765. wechatPayButton.isHidden = false
  1766. }
  1767. }
  1768. priceRefresh()
  1769. } else if textField == emailTextField {
  1770. emailErrorLabel(isReveal: false)
  1771. } else if textField == paddleTextField {
  1772. }
  1773. }
  1774. func controlTextDidChange(_ obj: Notification) {
  1775. let textField = obj.object as? NSTextField
  1776. if textField == amountTextField1 {
  1777. if !isNumeric(textField!.stringValue) {
  1778. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1779. } else {
  1780. if Int(textField!.stringValue)! > 9999 {
  1781. textField?.stringValue = String(pdfCount)
  1782. } else if Int(textField!.stringValue)! < 1 {
  1783. textField?.stringValue = String(pdfCount)
  1784. }
  1785. }
  1786. priceRefresh()
  1787. } else if textField == licenseCodeTextField {
  1788. if textField!.stringValue.count > 0 {
  1789. refreshLicenseCodeErrorView(isReveal: false)
  1790. } else {
  1791. refreshLicenseCodeErrorView(isReveal: false)
  1792. }
  1793. }
  1794. }
  1795. }
  1796. extension KMPurchaseEmbeddedWindowController: NSWindowDelegate {
  1797. func windowWillClose(_ notification: Notification) {
  1798. if orderID != "" {
  1799. pollingResult()
  1800. }
  1801. if self.embeddedPaymentPopWC != nil {
  1802. self.embeddedPaymentPopWC?.close()
  1803. self.embeddedPaymentPopWC = nil
  1804. }
  1805. KMPurchaseEmbeddedWindowController.currentWindowController = nil
  1806. }
  1807. }
  1808. #endif