KMPurchaseEmbeddedWindowController.swift 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. //
  2. // KMPurchaseEmbeddedWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/8/29.
  6. //
  7. import Cocoa
  8. // paypal alipay wxpay paddle
  9. @objc enum KMPaymentType: UInt32 {
  10. case paypal = 0
  11. case paddle
  12. case wxpay
  13. case alipay
  14. }
  15. class KMPurchaseEmbeddedWindowController: NSWindowController {
  16. @IBOutlet weak var yourOrderLabel: NSTextField!
  17. @IBOutlet weak var pdfReaderProLabel: NSTextField!
  18. @IBOutlet weak var prmiumLabel: NSTextField!
  19. @IBOutlet weak var oneLicenseLabel: NSTextField!
  20. @IBOutlet weak var removeButton1: NSButton! // 减少按钮1
  21. @IBOutlet weak var addButton1: NSButton! // 增加按钮1
  22. @IBOutlet weak var amountTextField1: NSTextField! // 数目1
  23. @IBOutlet weak var originalPriceLabel1: NSTextField! // 原价
  24. @IBOutlet weak var discountPriceLabel1: NSTextField! // 折扣
  25. @IBOutlet weak var aiAddOnView: NSView!
  26. @IBOutlet weak var aiAddOnButton: NSButton!
  27. @IBOutlet weak var aiAddOnLabel: NSTextField!
  28. @IBOutlet weak var IndividualLabel: NSTextField!
  29. @IBOutlet weak var removeButton2: NSButton! // 减少按钮2
  30. @IBOutlet weak var addButton2: NSButton! // 增加按钮2
  31. @IBOutlet weak var amountTextField2: NSTextField! // 数目2
  32. @IBOutlet weak var originalPriceLabel2: NSTextField! // 原价
  33. @IBOutlet weak var discountPriceLabel2: NSTextField! // 折扣
  34. @IBOutlet weak var extendedView: NSView!
  35. @IBOutlet weak var extendedButton: NSButton!
  36. @IBOutlet weak var extendedLabel: NSTextField!
  37. @IBOutlet weak var extendedImageView: NSImageView!
  38. @IBOutlet weak var accessLabel: NSTextField!
  39. @IBOutlet weak var removeButton3: NSButton! // 减少按钮3
  40. @IBOutlet weak var addButton3: NSButton! // 增加按钮3
  41. @IBOutlet weak var amountTextField3: NSTextField! // 数目3
  42. @IBOutlet weak var originalPriceLabel3: NSTextField! // 原价
  43. @IBOutlet weak var discountPriceLabel3: NSTextField! // 折扣
  44. @IBOutlet weak var couponButton: NSButton!
  45. @IBOutlet weak var couponBox: NSBox!
  46. @IBOutlet weak var coupomTextField: NSTextField!
  47. @IBOutlet weak var applyBox: NSBox!
  48. @IBOutlet weak var applyButton: NSButton!
  49. @IBOutlet weak var lastPriceLabel: NSTextField!
  50. @IBOutlet weak var lastPriceLabel1: NSTextField!
  51. @IBOutlet weak var discountLabel: NSTextField!
  52. @IBOutlet weak var discountLabel1: NSTextField!
  53. @IBOutlet weak var paySumLabel: NSTextField!
  54. @IBOutlet weak var paySumLabel1: NSTextField!
  55. @IBOutlet weak var privacyLabel: NSTextField!
  56. @IBOutlet weak var billInformationLabel: NSTextField!
  57. @IBOutlet weak var emailLabel: NSTextField!
  58. @IBOutlet weak var emailTextField: NSTextField!
  59. @IBOutlet weak var errorView: NSView!
  60. @IBOutlet weak var errorLabel: NSTextField!
  61. @IBOutlet weak var errorLabelTopLayout: NSLayoutConstraint!
  62. @IBOutlet weak var paymentMethodLabel: NSTextField!
  63. @IBOutlet weak var paypalButton: NSButton!
  64. @IBOutlet weak var cardButton: NSButton!
  65. @IBOutlet weak var wechatPayButton: NSButton!
  66. @IBOutlet weak var alipayButton: NSButton!
  67. @IBOutlet weak var payBox: NSBox!
  68. @IBOutlet weak var payBoxHeight: NSLayoutConstraint!
  69. @IBOutlet weak var payExplainLabel: NSTextField!
  70. @IBOutlet var paypalView: NSView!
  71. @IBOutlet weak var paypalBox: NSBox!
  72. @IBOutlet weak var paypalBuyNowLabel: NSTextField!
  73. @IBOutlet var cardView: NSView!
  74. @IBOutlet var paddleView: NSView!
  75. @IBOutlet weak var paddleButton: NSButton!
  76. @IBOutlet weak var paddlePopUpButton: KMPopUpButton!
  77. @IBOutlet weak var paddleBox: NSBox!
  78. @IBOutlet weak var paddleTextField: NSTextField!
  79. @IBOutlet var weChatPayView: NSView!
  80. @IBOutlet weak var weChatBox: NSBox!
  81. @IBOutlet weak var weChatButton: NSButton!
  82. @IBOutlet var payResult: NSView!
  83. @IBOutlet weak var scanLabel: NSTextField!
  84. @IBOutlet weak var qrCodeImageView: NSImageView!
  85. @IBOutlet weak var payTypeImageView: NSImageView!
  86. @IBOutlet weak var successfullyView: NSView!
  87. @IBOutlet weak var purchasedLabel: NSTextField!
  88. @IBOutlet weak var successfullyLabel: NSTextField!
  89. private var pdfCount: Int = 1
  90. private var pdfOriginalPrice: Float = 79.99
  91. private var pdfDiscountPrice: Float = 59.99
  92. private var aiAddOnOriginalPrice: Float = 14.99
  93. private var aiAddOnDiscountPrice: Float = 14.99
  94. private var extendedOriginalPrice: Float = 39.99
  95. private var extendedDiscountPrice: Float = 79.99
  96. private var listPrice: Float = 79.99
  97. private var discount: Float = 79.99
  98. private var _paymentMethod: KMPaymentType = .paypal
  99. private var _productType: ProductType = .ipaConvert
  100. private var productID: String = "com.brother.pdfreaderpro.mac.product_2"
  101. private var originalProductID: String = "com.brother.pdfreaderpro.mac.product_1"
  102. // private var discountId: String = "" // 优惠码
  103. private var discountAmount: String = "" // 优惠金额
  104. private var discountScale: String = "" // 优惠比例
  105. private var orderID: String = "" // 订单ID
  106. private var timer: Timer?
  107. private var pollCount = 0
  108. private let maxPolls = 18
  109. private let interval: TimeInterval = 5
  110. private let countryKeys = [
  111. "Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa",
  112. "Andorra", "Angola", "Anguilla", "Antigua and Barbuda", "Argentina",
  113. "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas",
  114. "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
  115. "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire, Sint Eustatius and Saba",
  116. "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil",
  117. "Brit. Indian Ocean", "British Virgin Islands", "Brunei Darussalam", "Bulgaria",
  118. "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde",
  119. "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
  120. "Christmas Island", "Cocos Islands", "Colombia", "Comoros", "Congo",
  121. "Cook Islands", "Costa Rica", "Cote D'Ivoire", "Croatia", "Curaçao", "Cyprus",
  122. "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  123. "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
  124. "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
  125. "French Guiana", "French Polynesia", "French Southern Terr.", "Gabon", "Gambia",
  126. "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada",
  127. "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau",
  128. "Guyana", "Haiti", "Heard/ Mcdonald Islands", "Holy See/ Vatican City", "Honduras",
  129. "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iraq", "Ireland",
  130. "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan",
  131. "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Lao People's DR",
  132. "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein",
  133. "Lithuania", "Luxembourg", "Macao", "Macedonia", "Madagascar", "Malawi",
  134. "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
  135. "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
  136. "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique",
  137. "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
  138. "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue",
  139. "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan",
  140. "Palau", "Palestinian Territory", "Panama", "Papua New Guinea", "Paraguay",
  141. "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar",
  142. "Republic of Serbia", "Reunion", "Romania", "Russian Federation", "Rwanda",
  143. "S. Georgia/ Sandwich Islands", "Saint Helena", "Saint Kitts and Nevis",
  144. "Saint Lucia", "Saint Martin", "Saint Pierre and Miquelon", "Saint Vincent/ Grenadines",
  145. "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal",
  146. "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
  147. "Somalia", "South Africa", "South Korea", "Spain", "Sri Lanka", "Sudan",
  148. "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Taiwan",
  149. "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga",
  150. "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands",
  151. "Tuvalu", "U.S. Virgin Islands", "Uganda", "Ukraine", "United Arab Emirates",
  152. "United Kingdom", "United States", "United States (M.O.I.)", "Uruguay", "Uzbekistan",
  153. "Vanuatu", "Venezuela", "Viet Nam", "Wallis and Futuna", "Western Sahara", "Yemen",
  154. "Zambia", "Zimbabwe"
  155. ]
  156. private let countryAbbreviationValues = [
  157. "AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB",
  158. "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "VG", "BN", "BG", "BF", "BI", "KH",
  159. "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CK", "CR", "CI", "HR", "CW", "CY",
  160. "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF",
  161. "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM",
  162. "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI",
  163. "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT",
  164. "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL",
  165. "AN", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH",
  166. "PN", "PL", "PT", "PR", "QA", "RS", "RE", "RO", "RU", "RW", "GS", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM",
  167. "ST", "SA", "SN", "SC", "SL", "SG", "SK", "SI", "SB", "SO", "ZA", "KR", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE",
  168. "CH", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "VI", "UG", "UA", "AE",
  169. "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "WF", "EH", "YE", "ZM", "ZW"
  170. ]
  171. private var handler: ((Int) -> Void)!
  172. static var currentWindowController: KMPurchaseEmbeddedWindowController!
  173. private var embeddedPaymentPopWC: KMEmbeddedPaymentPopWC?
  174. @objc static func currentFirstTrialWC(_ productId: String) -> KMPurchaseEmbeddedWindowController {
  175. if currentWindowController != nil {
  176. currentWindowController.productID = productId
  177. return currentWindowController
  178. } else {
  179. let configWC: KMPurchaseEmbeddedWindowController = KMPurchaseEmbeddedWindowController.init(windowNibName: "KMPurchaseEmbeddedWindowController")
  180. currentWindowController = configWC;
  181. currentWindowController.productID = productId
  182. return currentWindowController
  183. }
  184. }
  185. override func windowDidLoad() {
  186. super.windowDidLoad()
  187. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  188. productType = self.productToType(productID)
  189. originalProductID = productID
  190. xibInitialization()
  191. paymentMethod = .paypal
  192. priceRefresh()
  193. self.window?.delegate = self;
  194. }
  195. // MARK: UI
  196. private func xibInitialization() -> Void {
  197. yourOrderLabel.stringValue = NSLocalizedString("Your Order", comment: "")
  198. yourOrderLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  199. yourOrderLabel.font = NSFont.SFProTextBoldFont(20.0)
  200. pdfReaderProLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  201. pdfReaderProLabel.font = NSFont.SFProTextRegularFont(14.0)
  202. prmiumLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  203. prmiumLabel.font = NSFont.SFProTextRegularFont(12.0)
  204. oneLicenseLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  205. oneLicenseLabel.font = NSFont.SFProTextRegularFont(12.0)
  206. originalPriceLabel1.textColor = NSColor(named: "KMPurchaseTitleColor")
  207. originalPriceLabel1.font = NSFont.SFProTextRegularFont(16.0)
  208. discountPriceLabel1.textColor = NSColor(named: "KMPurchaseDiscountColor")
  209. discountPriceLabel1.font = NSFont.SFProTextRegularFont(9.0)
  210. if productType == .ipaConvert || productType == .ipaAI {
  211. aiAddOnView.isHidden = true
  212. extendedView.isHidden = true
  213. if productType == .ipaConvert {
  214. pdfReaderProLabel.stringValue = NSLocalizedString("PDF to Office", comment: "")
  215. prmiumLabel.stringValue = NSLocalizedString("Export PDF to Word(.docx), Excel (.xlsx), PowerPoint(.pptx) and more offline. It's one time purchase.", comment: "")
  216. oneLicenseLabel.isHidden = true
  217. } else if productType == .ipaAI {
  218. pdfReaderProLabel.stringValue = NSLocalizedString("PDF Reader Pro AI Tools", comment: "")
  219. prmiumLabel.stringValue = NSLocalizedString("50 credits for 30 days", comment: "")
  220. oneLicenseLabel.stringValue = NSLocalizedString("Manually renew at USD 14.99", comment: "")
  221. }
  222. } else {
  223. pdfReaderProLabel.stringValue = NSLocalizedString("PDF Reader Pro for Mac", comment: "")
  224. prmiumLabel.stringValue = NSLocalizedString("Premium and PDF to Office Converter", comment: "")
  225. oneLicenseLabel.stringValue = NSLocalizedString("One License for One Mac. One Time Purchase.", comment: "")
  226. aiAddOnButton.image = NSImage(named: "EmbeddedPayment06")
  227. aiAddOnLabel.stringValue = NSLocalizedString("AI Add-on", comment: "")
  228. aiAddOnLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  229. aiAddOnLabel.font = NSFont.SFProTextRegularFont(14.0)
  230. IndividualLabel.stringValue = NSLocalizedString("Individual monthly plan. Manually renew.", comment: "")
  231. IndividualLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  232. IndividualLabel.font = NSFont.SFProTextRegularFont(12.0)
  233. originalPriceLabel2.textColor = NSColor(named: "KMPurchaseTitleColor")
  234. originalPriceLabel2.font = NSFont.SFProTextRegularFont(16.0)
  235. discountPriceLabel2.textColor = NSColor(named: "KMPurchaseDiscountColor")
  236. discountPriceLabel2.font = NSFont.SFProTextRegularFont(9.0)
  237. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  238. extendedLabel.stringValue = NSLocalizedString("Extended Device Access", comment: "")
  239. accessLabel.stringValue = NSLocalizedString("Get access to your plan on up to 2 devices. (Mac or Windows)", comment: "")
  240. extendedImageView.isHidden = false
  241. } else {
  242. extendedLabel.stringValue = NSLocalizedString("PDF to Office Pack", comment: "")
  243. accessLabel.stringValue = NSLocalizedString("One time purchase", comment: "")
  244. extendedImageView.isHidden = true
  245. }
  246. extendedButton.image = NSImage(named: "EmbeddedPayment06")
  247. extendedLabel.textColor = NSColor(named: "KMPurchaseTitleColor")
  248. extendedLabel.font = NSFont.SFProTextRegularFont(14.0)
  249. accessLabel.textColor = NSColor(named: "KMPurchaseSubTitleColor")
  250. accessLabel.font = NSFont.SFProTextRegularFont(12.0)
  251. originalPriceLabel3.textColor = NSColor(named: "KMPurchaseTitleColor")
  252. originalPriceLabel3.font = NSFont.SFProTextRegularFont(16.0)
  253. discountPriceLabel3.textColor = NSColor(named: "KMPurchaseDiscountColor")
  254. discountPriceLabel3.font = NSFont.SFProTextRegularFont(9.0)
  255. }
  256. removeButton1.isEnabled = false
  257. amountTextField1.delegate = self
  258. amountTextField2.delegate = self
  259. couponButton.title = NSLocalizedString("PDF Reader Pro for Mac", comment: "")
  260. couponButton.setTitleColor(color: NSColor(named: "KMPurchaseCouponColor")!, font: NSFont.SFProTextRegularFont(14))
  261. coupomTextField.placeholderString = NSLocalizedString("Enter your coupon code and Apply it", comment: "")
  262. applyButton.title = NSLocalizedString("Apply", comment: "")
  263. applyButton.setTitleColor(color: NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black, font: NSFont.SFProTextRegularFont(13))
  264. couponBox.isHidden = true
  265. couponBox.borderColor = NSColor(named: "KMPurchaseCouponBoxColor") ?? NSColor.gray
  266. couponBox.fillColor = NSColor(named: "KMPurchaseCouponBoxFillColor") ?? NSColor.gray
  267. applyBox.isHidden = true
  268. applyBox.borderColor = NSColor(named: "KMPurchaseApplyColor") ?? NSColor.black
  269. lastPriceLabel.stringValue = NSLocalizedString("List Price", comment: "")
  270. lastPriceLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
  271. lastPriceLabel.font = NSFont.SFProTextRegularFont(16.0)
  272. discountLabel.stringValue = NSLocalizedString("Discount", comment: "")
  273. discountLabel.textColor = NSColor(named: "KMPurchaseListPriceColor")
  274. discountLabel.font = NSFont.SFProTextRegularFont(16.0)
  275. paySumLabel.stringValue = NSLocalizedString("Pay sum", comment: "")
  276. paySumLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  277. paySumLabel.font = NSFont.SFProTextBoldFont(24.0)
  278. paySumLabel1.textColor = NSColor(named: "KMPurchaseCouponColor")
  279. paySumLabel1.font = NSFont.SFProTextBoldFont(24.0)
  280. privacyLabel.textColor = NSColor.black
  281. let tipsString = NSLocalizedString("Are you a student or a professor? %@ / Want a volume purchase? Please %@.", comment: "")
  282. let specialOffer = NSLocalizedString("Get Special Offer", comment: "")
  283. let contactsUs = NSLocalizedString("Contact Us", comment: "")
  284. let fullString = String(format: tipsString, specialOffer, contactsUs)
  285. let attributedString = NSMutableAttributedString(string: fullString)
  286. let specialOfferRange = (fullString as NSString).range(of: specialOffer)
  287. let contactsUsRange = (fullString as NSString).range(of: contactsUs)
  288. attributedString.addAttribute(.foregroundColor, value: NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0), range: specialOfferRange)
  289. attributedString.addAttribute(.link, value: "GetSpecialOffer://", range: specialOfferRange)
  290. attributedString.addAttribute(.foregroundColor, value: NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0), range: contactsUsRange)
  291. attributedString.addAttribute(.link, value: "www.baidu.com", range: contactsUsRange)
  292. // attributedString.addAttributes([.link : "www.baidu.com"], range: contactsUsRange)
  293. privacyLabel.attributedStringValue = attributedString
  294. privacyLabel.font = NSFont.SFProTextRegularFont(16.0)
  295. privacyLabel.delegate = self
  296. billInformationLabel.stringValue = NSLocalizedString("Bill Information", comment: "")
  297. billInformationLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  298. billInformationLabel.font = NSFont.SFProTextRegularFont(14.0)
  299. emailLabel.stringValue = NSLocalizedString("Email", comment: "")
  300. emailLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  301. emailLabel.font = NSFont.SFProTextRegularFont(14.0)
  302. emailTextField.placeholderString = NSLocalizedString("Email to receive license code", comment: "")
  303. errorLabel.stringValue = NSLocalizedString("Please enter the email address number to receive the sequence code", comment: "")
  304. if VerificationManager.default().email != nil {
  305. emailTextField.stringValue = VerificationManager.default().email
  306. }
  307. errorLabel.textColor = NSColor(named: "KMPurchaseErrorColor")
  308. errorLabel.font = NSFont.SFProTextRegularFont(11.0)
  309. errorLabel.isHidden = true
  310. errorLabelTopLayout.constant = -CGRectGetHeight(errorView.bounds)
  311. paymentMethodLabel.stringValue = NSLocalizedString("Payment Method", comment: "")
  312. paymentMethodLabel.textColor = NSColor(named: "KMPurchaseTitleColor1")
  313. paymentMethodLabel.font = NSFont.SFProTextRegularFont(14.0)
  314. payExplainLabel.textColor = NSColor.black
  315. let localizedString = NSLocalizedString("By submitting this order, I agree to the %@ and %@ for subscription products", comment: "")
  316. let privacyPolicy = NSLocalizedString("Privacy Policy", comment: "")
  317. let termsOfService = NSLocalizedString("Terms of Service", comment: "")
  318. let fullString1 = String(format: localizedString, privacyPolicy, termsOfService)
  319. let attributedString1 = NSMutableAttributedString(string: fullString1)
  320. let privacyPolicyRange = (fullString as NSString).range(of: privacyPolicy)
  321. let termsOfServiceRange = (fullString as NSString).range(of: termsOfService)
  322. attributedString1.addAttribute(.foregroundColor, value: NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0), range: privacyPolicyRange)
  323. attributedString1.addAttribute(.link, value: "PrivacyPolicy://", range: privacyPolicyRange)
  324. attributedString1.addAttribute(.foregroundColor, value: NSColor(red: 73/255.0, green: 130/255.0, blue: 230/255.0, alpha: 1.0), range: termsOfServiceRange)
  325. // attributedString1.addAttribute(.link, value: "TermsofService://", range: termsOfServiceRange)
  326. attributedString1.addAttributes([.link : "www.baidu.com"], range: termsOfServiceRange)
  327. payExplainLabel.attributedStringValue = attributedString
  328. payExplainLabel.font = NSFont.SFProTextRegularFont(11.0)
  329. payExplainLabel.delegate = self
  330. paypalBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  331. paypalBuyNowLabel.textColor = NSColor.white
  332. paypalBuyNowLabel.font = NSFont.SFProTextBoldFont(16.0)
  333. paddleButton.title = NSLocalizedString("Continue to Pay", comment: "")
  334. paddleButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  335. paddlePopUpButton.type = .arrowDown
  336. paddlePopUpButton.removeAllItems()
  337. paddlePopUpButton.addItems(withTitles: countryKeys)
  338. paddlePopUpButton.selectItem(at: 229)
  339. paddlePopUpButton.wantsLayer = true
  340. paddlePopUpButton.layer?.backgroundColor = NSColor(named: "KMPurchase_FFFFFF")?.cgColor
  341. paddlePopUpButton.layer?.borderColor = NSColor(named: "KMPurchase_DADBDE")?.cgColor
  342. paddleBox.fillColor = NSColor(named: "KMPurchase_FFFFFF") ?? NSColor.white
  343. paddleBox.borderColor = NSColor(named: "KMPurchase_DADBDE") ?? NSColor.gray
  344. paddleTextField.delegate = self
  345. weChatButton.title = NSLocalizedString("Continue to Pay", comment: "")
  346. weChatButton.setTitleColor(color: NSColor.white, font: NSFont.SFProTextBoldFont(16))
  347. qrCodeImageView.imageScaling = .scaleProportionallyUpOrDown
  348. scanLabel.textColor = NSColor(named: "KMPurchase_000000")
  349. scanLabel.font = NSFont.SFProTextRegularFont(12)
  350. successfullyView.isHidden = true
  351. purchasedLabel.stringValue = NSLocalizedString("Purchased", comment: "")
  352. purchasedLabel.textColor = NSColor(named: "KMPurchase_002143")
  353. purchasedLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  354. successfullyLabel.stringValue = NSLocalizedString("Successfully", comment: "")
  355. successfullyLabel.textColor = NSColor(named: "KMPurchase_002143")
  356. successfullyLabel.font = NSFont(name: "Segoe UI", size: 18.0)
  357. }
  358. private func priceRefresh() -> Void {
  359. if KMDMGProductsManager.shareInstance.productDatas == nil {
  360. let alert = NSAlert()
  361. alert.alertStyle = .critical
  362. alert.messageText = NSLocalizedString("Connection Error", comment: "")
  363. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  364. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  365. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  366. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  367. } else {
  368. alert.runModal()
  369. }
  370. return
  371. }
  372. KMDMGProductsManager.shareInstance.getAllPrice(productID)
  373. let activity_cny_price = KMDMGProductsManager.shareInstance.activityPrice_CNY
  374. let activity_price = KMDMGProductsManager.shareInstance.activityPrice
  375. let price = KMDMGProductsManager.shareInstance.price
  376. let cny_price = KMDMGProductsManager.shareInstance.price_CNY
  377. var discountPrice = activity_price
  378. var originalPrice = price
  379. var abbreviation = "USD"
  380. var discountTotalPrice = activity_price
  381. var discountAmount1 = "0.00"
  382. var originalTotalPrice = price
  383. if NSLocalizedString("USD", comment: "") == "CNY" {
  384. abbreviation = "CNY"
  385. discountPrice = activity_cny_price
  386. originalPrice = cny_price
  387. }
  388. if productType == .ipaConvert_checkout || productType == .ipaAI {
  389. originalPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, discountPrice)
  390. if Float(originalPrice) == Float(discountPrice) {
  391. discountPriceLabel1.isHidden = true
  392. } else {
  393. discountPriceLabel1.isHidden = false
  394. let attributedString = NSMutableAttributedString(string: String(format: "%@", originalPrice))
  395. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  396. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  397. discountPriceLabel1.attributedStringValue = attributedString
  398. }
  399. discountTotalPrice = String(format: "%.2f", Float(discountPrice)! * Float(amountTextField1.stringValue)!)
  400. originalTotalPrice = String(format: "%.2f", Float(originalPrice)! * Float(amountTextField1.stringValue)!)
  401. discountAmount1 = String(format: "%.2f", Float(originalPrice)! - Float(discountPrice)!)
  402. } else if productType == .ipaPremium ||
  403. productType == .ipaPermanent ||
  404. productType == .ipaPremiumCrossPlatform ||
  405. productType == .ipaPermanentCrossPlatform ||
  406. productType == .ipaPremiumAI ||
  407. productType == .ipaPermanentAI ||
  408. productType == .ipaPermanentCrossPlatformAI ||
  409. productType == .ipaConvert {
  410. var pdfDiscountPrice = "0.00"
  411. var pdfOriginalPrice = "0.00"
  412. var aiDiscountPrice = "0.00" // AI捆绑包 折扣
  413. var aiOriginalPrice = "0.00" // AI捆绑包 原始
  414. var crossDiscountPrice = "0.00" // 跨平台捆绑包 折扣
  415. var crossOriginalPrice = "0.00" // 跨平台捆绑包 原始
  416. if NSLocalizedString("USD", comment: "") == "CNY" {
  417. if productType == .ipaPermanent ||
  418. productType == .ipaPermanentCrossPlatform ||
  419. productType == .ipaPermanentAI ||
  420. productType == . ipaPermanentCrossPlatformAI {
  421. pdfDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_3")
  422. pdfOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_3")
  423. aiDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  424. aiOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  425. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  426. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.cross.platform.product_3")
  427. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.cross.platform.product_3")
  428. } else {
  429. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_2.checkout")
  430. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_2.checkout")
  431. }
  432. } else if productType == .ipaConvert {
  433. pdfDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_3")
  434. pdfOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_3")
  435. aiDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  436. aiOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  437. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_2.checkout")
  438. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_2.checkout")
  439. } else {
  440. pdfDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_1")
  441. pdfOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_1")
  442. aiDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")
  443. aiOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")
  444. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  445. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.cross.platform.product_3")
  446. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.cross.platform.product_3")
  447. } else {
  448. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPriceCNY("com.brother.pdfreaderpro.mac.product_2.checkout")
  449. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPriceCNY("com.brother.pdfreaderpro.mac.product_2.checkout")
  450. }
  451. }
  452. } else {
  453. if productType == .ipaPermanent ||
  454. productType == .ipaPermanentCrossPlatform ||
  455. productType == .ipaPermanentAI ||
  456. productType == . ipaPermanentCrossPlatformAI {
  457. pdfDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")
  458. pdfOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")
  459. aiDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  460. aiOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  461. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  462. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")
  463. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3")
  464. } else {
  465. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")
  466. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")
  467. }
  468. } else if productType == .ipaConvert {
  469. pdfDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3")
  470. pdfOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3")
  471. aiDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  472. aiOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1")
  473. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")
  474. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")
  475. } else {
  476. pdfDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_1")
  477. pdfOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_")
  478. aiDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")
  479. aiOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1")
  480. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  481. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.cross.platform.product_3")
  482. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.cross.platform.product_3")
  483. } else {
  484. crossDiscountPrice = KMDMGProductsManager.shareInstance.getActivityPrice("com.brother.pdfreaderpro.mac.product_2.checkout")
  485. crossOriginalPrice = KMDMGProductsManager.shareInstance.getPrice("com.brother.pdfreaderpro.mac.product_2.checkout")
  486. }
  487. }
  488. }
  489. let discount2 = String(format: "%.2f", Float(aiDiscountPrice)! - Float(pdfDiscountPrice)!)
  490. let original2 = String(format: "%.2f", Float(aiOriginalPrice)! - Float(pdfOriginalPrice)!)
  491. var discount3 = ""
  492. var original3 = ""
  493. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  494. discount3 = String(format: "%.2f", Float(crossDiscountPrice)! - Float(pdfOriginalPrice)!)
  495. original3 = String(format: "%.2f", Float(crossOriginalPrice)! - Float(pdfOriginalPrice)!)
  496. } else {
  497. discount3 = crossDiscountPrice
  498. original3 = crossOriginalPrice
  499. }
  500. // if productType == .ipaPremium || productType == .ipaPermanent {
  501. //
  502. // } else if productType == .ipaPremiumCrossPlatform || productType == .ipaPermanentCrossPlatform {
  503. //
  504. // } else if productType == .ipaPremiumAI || productType == .ipaPermanentAI || productType == .ipaPermanentCrossPlatformAI {
  505. //
  506. // }
  507. let dis1 = Float(pdfDiscountPrice)! * Float(amountTextField1.stringValue)!
  508. let dis2 = Float(discount2)! * Float(amountTextField2.stringValue)!
  509. let dis3 = Float(discount3)! * Float(amountTextField3.stringValue)!
  510. let ori1 = Float(pdfOriginalPrice)! * Float(amountTextField1.stringValue)!
  511. let ori2 = Float(original2)! * Float(amountTextField2.stringValue)!
  512. let ori3 = Float(original3)! * Float(amountTextField3.stringValue)!
  513. if aiAddOnButton.state == .on {
  514. if extendedButton.state == .on {
  515. discountTotalPrice = String(format: "%.2f", dis1 + dis2 + dis3)
  516. originalTotalPrice = String(format: "%.2f", ori1 + ori2 + ori3)
  517. } else {
  518. discountTotalPrice = String(format: "%.2f", dis1 + dis2)
  519. originalTotalPrice = String(format: "%.2f", ori1 + ori2)
  520. }
  521. } else {
  522. if extendedButton.state == .on {
  523. discountTotalPrice = String(format: "%.2f", dis1 + dis3)
  524. originalTotalPrice = String(format: "%.2f", ori1 + ori3)
  525. } else {
  526. discountTotalPrice = String(format: "%.2f", dis1)
  527. originalTotalPrice = String(format: "%.2f", ori1)
  528. }
  529. }
  530. discountAmount1 = String(format: "%.2f", Float(originalPrice)! - Float(discountPrice)!)
  531. originalPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, pdfDiscountPrice)
  532. if Float(originalPrice) == Float(discountPrice) {
  533. discountPriceLabel1.isHidden = true
  534. } else {
  535. discountPriceLabel1.isHidden = false
  536. let attributedString = NSMutableAttributedString(string: String(format: "%@", pdfOriginalPrice))
  537. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  538. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  539. discountPriceLabel1.attributedStringValue = attributedString
  540. }
  541. originalPriceLabel2.stringValue = String(format: "%@ %@", abbreviation, discount2)
  542. if Float(original2) == Float(discount2) {
  543. discountPriceLabel2.isHidden = true
  544. } else {
  545. discountPriceLabel2.isHidden = false
  546. let attributedString = NSMutableAttributedString(string: String(format: "%@", original2))
  547. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  548. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  549. discountPriceLabel2.attributedStringValue = attributedString
  550. }
  551. originalPriceLabel3.stringValue = String(format: "%@ %@", abbreviation, discount3)
  552. if Float(original3) == Float(discount3) {
  553. discountPriceLabel3.isHidden = true
  554. } else {
  555. discountPriceLabel3.isHidden = false
  556. let attributedString = NSMutableAttributedString(string: String(format: "%@", original3))
  557. attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
  558. attributedString.addAttribute(.strikethroughColor, value: NSColor(named: "KMPurchaseDiscountColor") as Any, range: NSMakeRange(0, attributedString.length))
  559. discountPriceLabel3.attributedStringValue = attributedString
  560. }
  561. }
  562. if self.discountScale != "" || self.discountAmount != "" {
  563. var discountTotalPriceF = Float(discountTotalPrice)
  564. let originalTotalPriceF = Float(originalTotalPrice)
  565. if self.discountScale != "" {
  566. discountTotalPrice = String(format: "%.2f", discountTotalPriceF! * Float(self.discountScale)!)
  567. discountTotalPriceF = discountTotalPriceF! * Float(self.discountScale)!
  568. discountAmount1 = String(format: "%.2f", originalTotalPriceF! - discountTotalPriceF!)
  569. } else {
  570. var discountAmountF = Float(self.discountAmount)
  571. if NSLocalizedString("USD", comment: "") == "CNY" {
  572. discountAmountF = discountAmountF! * 6.5
  573. discountTotalPrice = String(format: "%.2f", discountTotalPriceF! - discountAmountF!)
  574. discountTotalPriceF = discountTotalPriceF! - discountAmountF!
  575. discountAmount1 = String(format: "%.2f", originalTotalPriceF! - discountTotalPriceF!)
  576. }
  577. }
  578. }
  579. lastPriceLabel1.stringValue = String(format: "%@ %@", abbreviation, originalTotalPrice)
  580. discountLabel1.stringValue = String(format: "%@ %@", abbreviation, discountAmount1)
  581. paySumLabel1.stringValue = String(format: "%@ %@", abbreviation, discountTotalPrice)
  582. }
  583. // MARK: Active
  584. @IBAction func amountButtonAction(_ sender: NSButton) {
  585. if sender.tag == 0 || sender.tag == 1 {
  586. if sender.tag == 0 {
  587. if pdfCount > 1 {
  588. pdfCount -= 1
  589. }
  590. }
  591. if sender.tag == 1 {
  592. pdfCount += 1
  593. }
  594. if pdfCount > 1 {
  595. removeButton1.isEnabled = true
  596. } else {
  597. removeButton1.isEnabled = false
  598. }
  599. amountTextField1.stringValue = String(pdfCount)
  600. }
  601. // 价格刷新
  602. priceRefresh()
  603. }
  604. @IBAction func aiAddOnButtonAction(_ sender: NSButton) {
  605. let type: ProductType = productType
  606. if sender.state == .on {
  607. aiAddOnButton.image = NSImage(named: "EmbeddedPayment07")
  608. if extendedButton.state == .on {
  609. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  610. productType = .ipaPermanentCrossPlatformAI
  611. } else {
  612. productType = .ipaConvert
  613. }
  614. } else {
  615. if type == .ipaPremium || type == .ipaPremiumCrossPlatform || type == .ipaPremiumAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  616. productType = .ipaPremiumAI
  617. } else if type == .ipaPermanent || type == .ipaPermanentCrossPlatform || type == .ipaPermanentAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  618. productType = .ipaPermanentAI
  619. }
  620. }
  621. } else {
  622. aiAddOnButton.image = NSImage(named: "EmbeddedPayment06")
  623. if extendedButton.state == .on {
  624. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  625. if type == .ipaPremium || type == .ipaPremiumCrossPlatform || type == .ipaPremiumAI || type == .ipaPermanentCrossPlatformAI {
  626. productType = .ipaPremiumCrossPlatform
  627. } else if type == .ipaPermanent || type == .ipaPermanentCrossPlatform || type == .ipaPermanentAI || type == .ipaPermanentCrossPlatformAI {
  628. productType = .ipaPermanentCrossPlatform
  629. }
  630. } else {
  631. productType = .ipaConvert
  632. }
  633. } else {
  634. if type == .ipaPremium || type == .ipaPremiumCrossPlatform || type == .ipaPremiumAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  635. productType = .ipaPremium
  636. } else if type == .ipaPermanent || type == .ipaPermanentCrossPlatform || type == .ipaPermanentAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  637. productType = .ipaPermanent
  638. }
  639. }
  640. }
  641. // 价格刷新
  642. priceRefresh()
  643. }
  644. @IBAction func extendedButtonAction(_ sender: NSButton) {
  645. let type: ProductType = productType
  646. if sender.state == .on {
  647. extendedButton.image = NSImage(named: "EmbeddedPayment07")
  648. if originalProductID == "com.brother.pdfreaderpro.mac.product_3" {
  649. if aiAddOnButton.state == .on {
  650. productType = .ipaPermanentCrossPlatformAI
  651. } else {
  652. if type == .ipaPremium || type == .ipaPremiumCrossPlatform || type == .ipaPremiumAI || type == .ipaPermanentCrossPlatformAI || type == .ipaPermanentCrossPlatformAI {
  653. productType = .ipaPremiumCrossPlatform
  654. } else if type == .ipaPermanent || type == .ipaPermanentCrossPlatform || type == .ipaPermanentAI || type == .ipaPermanentCrossPlatformAI || type == .ipaPermanentCrossPlatformAI {
  655. productType = .ipaPermanentCrossPlatform
  656. }
  657. }
  658. } else {
  659. productType = .ipaConvert
  660. }
  661. } else {
  662. extendedButton.image = NSImage(named: "EmbeddedPayment06")
  663. if aiAddOnButton.state == .on {
  664. if type == .ipaPremium || type == .ipaPremiumCrossPlatform || type == .ipaPremiumAI || type == .ipaPermanentCrossPlatformAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  665. productType = .ipaPremiumAI
  666. } else if type == .ipaPermanent || type == .ipaPermanentCrossPlatform || type == .ipaPermanentAI || type == .ipaPermanentCrossPlatformAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  667. productType = .ipaPermanentAI
  668. }
  669. } else {
  670. if type == .ipaPremium || type == .ipaPremiumCrossPlatform || type == .ipaPremiumAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  671. productType = .ipaPremium
  672. } else if type == .ipaPermanent || type == .ipaPermanentCrossPlatform || type == .ipaPermanentAI || type == .ipaPermanentCrossPlatformAI || type == .ipaPermanentCrossPlatformAI || type == .ipaConvert {
  673. productType = .ipaPermanent
  674. }
  675. }
  676. }
  677. // 价格刷新
  678. priceRefresh()
  679. }
  680. @IBAction func payButtonAction(_ sender: NSButton) {
  681. paymentMethod = KMPaymentType(rawValue: uint32(sender.tag)) ?? .paypal
  682. }
  683. @IBAction func couponButtonAction(_ sender: NSButton) {
  684. couponButton.isHidden = true
  685. couponBox.isHidden = false
  686. applyBox.isHidden = false
  687. }
  688. @IBAction func applyButtonAction(_ sender: NSButton) {
  689. if coupomTextField.stringValue == "" {
  690. let alert = NSAlert()
  691. alert.alertStyle = .critical
  692. alert.messageText = NSLocalizedString("Coupon Error", comment: "")
  693. alert.informativeText = NSLocalizedString("Please enter the coupon.", comment: "")
  694. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  695. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  696. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  697. } else {
  698. alert.runModal()
  699. }
  700. return
  701. }
  702. self._getDiscount(productId: productID, discountId: coupomTextField.stringValue) { [weak self] info, err in
  703. guard let self = self else { return }
  704. if let dataInfo = info {
  705. if dataInfo.keys.contains("status") {
  706. let status = dataInfo["status"] as? String ?? ""
  707. if status == "" {
  708. let alert = NSAlert()
  709. alert.alertStyle = .critical
  710. alert.messageText = NSLocalizedString("Connection error", comment: "")
  711. alert.informativeText = NSLocalizedString("Promotional volume exception please try again.", comment: "")
  712. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  713. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  714. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  715. } else {
  716. alert.runModal()
  717. }
  718. return
  719. }
  720. if status == "unused" {
  721. if dataInfo["discount"] is NSNull && dataInfo["price"] is NSNull {
  722. let alert = NSAlert()
  723. alert.alertStyle = .critical
  724. alert.messageText = NSLocalizedString("Connection error", comment: "")
  725. alert.informativeText = NSLocalizedString("Non-concessionary amount.", comment: "")
  726. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  727. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  728. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  729. } else {
  730. alert.runModal()
  731. }
  732. return
  733. } else {
  734. if dataInfo["discount"] is NSNull {
  735. if dataInfo["price"] is NSNull {
  736. } else {
  737. if let price1 = dataInfo["price"] {
  738. self.discountAmount = String(format: "%@", price1 as! CVarArg)
  739. }
  740. }
  741. } else {
  742. if let discount1 = dataInfo["discount"] {
  743. self.discountScale = String(format: "%@", discount1 as! CVarArg)
  744. }
  745. }
  746. }
  747. }
  748. } else {
  749. if dataInfo["discount"] is NSNull && dataInfo["price"] is NSNull {
  750. let alert = NSAlert()
  751. alert.alertStyle = .critical
  752. alert.messageText = NSLocalizedString("Connection error", comment: "")
  753. alert.informativeText = NSLocalizedString("Non-concessionary amount.", comment: "")
  754. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  755. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  756. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  757. } else {
  758. alert.runModal()
  759. }
  760. return
  761. } else {
  762. if dataInfo["discount"] is NSNull {
  763. if dataInfo["price"] is NSNull {
  764. } else {
  765. if let price1 = dataInfo["price"] {
  766. self.discountAmount = String(format: "%@", price1 as! CVarArg)
  767. }
  768. }
  769. } else {
  770. if let discount1 = dataInfo["discount"] {
  771. self.discountScale = String(format: "%@", discount1 as! CVarArg)
  772. }
  773. }
  774. }
  775. }
  776. }
  777. self.priceRefresh()
  778. }
  779. }
  780. @IBAction func paddlePopUpAction(_ sender: NSPopUpButton) {
  781. buyAction(sender)
  782. }
  783. @IBAction func buyAction(_ sender: NSButton) {
  784. if emailTextField.stringValue == "" {
  785. let alert = NSAlert()
  786. alert.alertStyle = .critical
  787. alert.messageText = NSLocalizedString("Connection Error", comment: "")
  788. alert.informativeText = NSLocalizedString("The email address is empty.", comment: "")
  789. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  790. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  791. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  792. } else {
  793. alert.runModal()
  794. }
  795. return
  796. }
  797. if paymentMethod == .paddle {
  798. if !isNumeric(paddleTextField.stringValue) && paddleTextField.stringValue != "" {
  799. let alert = NSAlert()
  800. alert.alertStyle = .critical
  801. alert.messageText = NSLocalizedString("Connection Error", comment: "")
  802. alert.informativeText = NSLocalizedString("Please enter the correct postcode.", comment: "")
  803. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  804. if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  805. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  806. } else {
  807. alert.runModal()
  808. }
  809. return
  810. }
  811. }
  812. var licenseCode = ""
  813. if let license = VerificationManager.default().originLicenseCode, license.count > 0 {
  814. licenseCode = license
  815. }
  816. self._buyProduct(productID, count: pdfCount, discountId: coupomTextField.stringValue, payment: paymentMethod, license: licenseCode, email: emailTextField.stringValue) { [weak self] info, err in
  817. guard let self = self else { return }
  818. if err != nil {return}
  819. if let dataInfo = info {
  820. self.orderID = dataInfo["trade_no"] as! String
  821. let page_pay_url = dataInfo["page_pay_url"] as! String
  822. if self.paymentMethod == .paddle || self.paymentMethod == .paypal {
  823. if page_pay_url != "" {
  824. self.openWebView(page_pay_url)
  825. }
  826. } else if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  827. if page_pay_url != "" {
  828. self.scanCode(page_pay_url)
  829. }
  830. }
  831. } else {
  832. // 数据错误
  833. }
  834. }
  835. }
  836. // MARK: get & set
  837. private var paymentMethod : KMPaymentType {
  838. get {
  839. return _paymentMethod
  840. }
  841. set {
  842. _paymentMethod = newValue
  843. paypalButton.image = NSImage(named: "EmbeddedPayment19")
  844. cardButton.image = NSImage(named: "EmbeddedPayment21")
  845. wechatPayButton.image = NSImage(named: "EmbeddedPayment23")
  846. alipayButton.image = NSImage(named: "EmbeddedPayment25")
  847. if _paymentMethod == .paypal {
  848. paypalButton.image = NSImage(named: "EmbeddedPayment18")
  849. payBox.contentView = paypalView
  850. paypalBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  851. payBoxHeight.constant = 60.0
  852. } else if _paymentMethod == .paddle {
  853. cardButton.image = NSImage(named: "EmbeddedPayment20")
  854. payBox.contentView = cardView
  855. paddleView.wantsLayer = true
  856. paddleView.layer?.backgroundColor = NSColor(named: "KMPurchaseBoxColor")?.cgColor ?? NSColor.blue.cgColor
  857. payBoxHeight.constant = 150.0
  858. } else if _paymentMethod == .wxpay {
  859. wechatPayButton.image = NSImage(named: "EmbeddedPayment22")
  860. payBox.contentView = weChatPayView
  861. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  862. payBoxHeight.constant = 92.0
  863. } else if _paymentMethod == .alipay {
  864. alipayButton.image = NSImage(named: "EmbeddedPayment24")
  865. payBox.contentView = weChatPayView
  866. weChatBox.fillColor = NSColor(named: "KMPurchaseBoxColor") ?? NSColor.blue
  867. payBoxHeight.constant = 92.0
  868. }
  869. }
  870. }
  871. var productType: ProductType {
  872. get {
  873. return _productType
  874. }
  875. set {
  876. _productType = newValue
  877. if _productType == .ipaConvert {
  878. productID = "com.brother.pdfreaderpro.mac.product_2"
  879. } else if _productType == .ipaConvert_checkout {
  880. productID = "com.brother.pdfreaderpro.mac.product_2.checkout"
  881. } else if _productType == .ipaPremium {
  882. productID = "com.brother.pdfreaderpro.mac.product_1"
  883. } else if _productType == .ipaPermanent {
  884. productID = "com.brother.pdfreaderpro.mac.product_3"
  885. } else if _productType == .ipaPremiumCrossPlatform {
  886. productID = "com.brother.pdfreaderpro.cross.platform.product_1"
  887. } else if _productType == .ipaPermanentCrossPlatform {
  888. productID = "com.brother.pdfreaderpro.cross.platform.product_3"
  889. } else if _productType == .ipaAI {
  890. productID = "com.brother.pdfreaderpro.ai.product_1"
  891. } else if _productType == .ipaPremiumAI {
  892. productID = "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1"
  893. } else if _productType == .ipaPermanentAI {
  894. productID = "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1"
  895. } else if _productType == .ipaPermanentCrossPlatformAI {
  896. productID = "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1"
  897. }
  898. }
  899. }
  900. // MARK: ——
  901. // 获取价格
  902. private func _getProductDatas(callback: @escaping (([String:Any]?, Any?)->Void)) {
  903. // func getPriceUrl() {
  904. let urlString = kVerificationServer + "/api/product/get-price/v2"
  905. KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: nil) { task, responseObject, error in
  906. if (error == nil && responseObject != nil) {
  907. let array = responseObject?["data"] as? NSDictionary
  908. if array != nil {
  909. //解析数据
  910. let productsDict = array?.object(forKey: "data") as? NSDictionary
  911. callback(productsDict as? [String : Any], nil)
  912. } else {
  913. callback([:], error.debugDescription)
  914. }
  915. } else {
  916. callback(nil, error.debugDescription)
  917. }
  918. }
  919. }
  920. // 获取优惠卷
  921. private func _getDiscount(productId: String, discountId: String, callback: @escaping (([String : Any]?, Any?)->Void)) {
  922. let urlString = kVerificationServer + "/api/coupons/retrieval"
  923. let app_code = MainBundle.km_infoDictionary()?["CFBundleIdentifier"] ?? ""
  924. let params = [
  925. // 优惠卷id
  926. "code" : discountId,
  927. "app_code" : app_code,
  928. "product_code" : productId]
  929. KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: params) { task, responseObject, error in
  930. if (error == nil && responseObject != nil) {
  931. if let dataDict = responseObject?["data"] as? NSDictionary {
  932. if let swiftDict = dataDict as? [String: Any] {
  933. if swiftDict.keys.contains("price") || swiftDict.keys.contains("discount") {
  934. callback(dataDict as? [String : Any], nil)
  935. } else if swiftDict.keys.contains("coupon_provider") {
  936. if let couponDict = swiftDict["coupon_provider"] as? [String: Any] {
  937. callback(couponDict, nil)
  938. }
  939. }
  940. }
  941. } else if let coupon_provider = responseObject?["coupon_provider"] as? NSDictionary {
  942. callback(coupon_provider as? [String : Any], nil)
  943. } else {
  944. callback([:], error.debugDescription)
  945. }
  946. } else {
  947. callback(nil, error.debugDescription)
  948. }
  949. }
  950. }
  951. // 获取订单状态
  952. private func _getOrderStatus(tradeNo: String, callback: @escaping (([String:Any]?, Any?)->Void)) {
  953. let urlString = kVerificationServer + "/api/orders/getOrderStatus"
  954. var jsonStr: String = ""
  955. if let jsonData = try?JSONSerialization.data(withJSONObject: ["trade_no" : tradeNo], options:[]),
  956. let data = String(data: jsonData, encoding: .utf8) {
  957. jsonStr = data
  958. }
  959. let rsa = KMVerificationRSA()
  960. let publicKey = rsa.loadPublicKey()
  961. let encryptedData = rsa.encrypt(plainText: jsonStr, publicKey: publicKey!)
  962. let tokenStr = encryptedData?.base64EncodedString()
  963. var postData = try!JSONSerialization.data(withJSONObject: ["token" : tokenStr])
  964. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  965. request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  966. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  967. request.httpMethod = "POST"
  968. request.httpBody = postData
  969. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  970. guard let data = data else {
  971. callback(nil, String(describing: error))
  972. return
  973. }
  974. if let jsonDict = try?JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary ?? [:] {
  975. let dataStr = jsonDict["data"] as? String
  976. let rsa = KMVerificationRSA()
  977. if let deData = (rsa.decrypt(source: dataStr as? NSString ?? "") as? String)?.data(using: .utf8) {
  978. if let dict = try?JSONSerialization.jsonObject(with: deData, options: []) as? NSDictionary ?? [:] {
  979. return callback(dict as? [String : Any], nil)
  980. }
  981. }
  982. callback([:], String(describing: error))
  983. } else {
  984. // 数据错误
  985. callback([:], String(describing: error))
  986. }
  987. }
  988. task.resume()
  989. }
  990. private func _buyProduct(_ productId: String, count: Int, discountId: String = "", payment: KMPaymentType, license: String = "", email: String, username: String = "", callback: @escaping (([String:Any]?, Any?)->Void)) {
  991. let urlString = kVerificationServer + "/api/subscriptions"
  992. let app_code = MainBundle.km_infoDictionary()?["CFBundleIdentifier"] ?? ""
  993. let paymentStr = self._paymentTypeString(type: payment)
  994. let country = countryAbbreviationValues[paddlePopUpButton.indexOfSelectedItem]
  995. let postcode = paddleTextField.stringValue ?? ""
  996. let paraDict = ["data" :
  997. ["product_code": productId,
  998. "app_code": app_code,
  999. // 购买数量
  1000. "amount": "\(count)",
  1001. // 优惠卷id
  1002. "coupon_code": discountId,
  1003. // 支付类型 paypal alipay wxpay paddle
  1004. "payment": paymentStr,
  1005. // 地区
  1006. "customer_country": country,
  1007. // 邮编码
  1008. "customer_postcode" : postcode,
  1009. "unique_sn" : uuid(),
  1010. // 序列码 标准包 升级 高级包 需要
  1011. "upgrade_cdkey" : license,
  1012. "email" : email,
  1013. // 用户名
  1014. "contact_name" : username]]
  1015. var postData = try! JSONSerialization.data(withJSONObject: paraDict)
  1016. var request = URLRequest(url: URL(string: urlString)!,timeoutInterval: Double.infinity)
  1017. request.addValue("Apifox/1.0.0 (https://www.apifox.cn)", forHTTPHeaderField: "User-Agent")
  1018. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  1019. request.httpMethod = "POST"
  1020. request.httpBody = postData
  1021. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  1022. guard let theData = data else {
  1023. callback(nil, String(describing: error))
  1024. return
  1025. }
  1026. if let jsonDict = try? JSONSerialization.jsonObject(with: theData, options: []) as? NSDictionary ?? [:] {
  1027. let data1: NSDictionary = jsonDict["data"] as? NSDictionary ?? [:]
  1028. let subscriptionInfo = data1.object(forKey: "subscription") as? NSDictionary
  1029. let orderKey = self._paymentTypeString(type: payment) + "_order"
  1030. let order_info = subscriptionInfo?.object(forKey: orderKey) as? NSDictionary
  1031. if let dataInfo = order_info, dataInfo.count > 0 {
  1032. callback(dataInfo as? [String : Any], nil)
  1033. } else {
  1034. // 数据错误
  1035. callback([:], String(describing: error))
  1036. }
  1037. } else {
  1038. // 数据错误
  1039. callback([:], String(describing: error))
  1040. }
  1041. }
  1042. task.resume()
  1043. }
  1044. // MARK: Private
  1045. private func uuid() -> String {
  1046. return GetHardwareUUID()!
  1047. }
  1048. private func _paymentTypeString(type: KMPaymentType) -> String {
  1049. if type == .paddle {
  1050. return "paddle"
  1051. } else if type == .alipay {
  1052. return "alipay"
  1053. } else if type == .wxpay {
  1054. return "wxpay"
  1055. } else if type == .paypal {
  1056. return "paypal"
  1057. }
  1058. return ""
  1059. }
  1060. private let tabDict: [ProductType: String] = [
  1061. .ipaConvert: "com.brother.pdfreaderpro.mac.product_2",
  1062. .ipaConvert_checkout: "com.brother.pdfreaderpro.mac.product_2.checkout",
  1063. .ipaPremium: "com.brother.pdfreaderpro.mac.product_1",
  1064. .ipaPermanent: "com.brother.pdfreaderpro.mac.product_3",
  1065. .ipaPremiumCrossPlatform: "com.brother.pdfreaderpro.cross.platform.product_1",
  1066. .ipaPermanentCrossPlatform: "com.brother.pdfreaderpro.cross.platform.product_3",
  1067. .ipaAI: "com.brother.pdfreaderpro.ai.product_1",
  1068. .ipaPremiumAI: "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1",
  1069. .ipaPermanentAI: "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1",
  1070. .ipaPermanentCrossPlatformAI: "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1"
  1071. ]
  1072. private func getProductID() -> String {
  1073. return GetHardwareUUID()!
  1074. }
  1075. private func productToType(_ productId: String) -> ProductType {
  1076. if productId == "com.brother.pdfreaderpro.mac.product_2" {
  1077. return .ipaConvert
  1078. } else if productId == "com.brother.pdfreaderpro.mac.product_2.checkout" {
  1079. return .ipaConvert_checkout
  1080. } else if productId == "com.brother.pdfreaderpro.mac.product_1" {
  1081. return .ipaPremium
  1082. } else if productId == "com.brother.pdfreaderpro.mac.product_3" {
  1083. return .ipaPermanent
  1084. } else if productId == "com.brother.pdfreaderpro.cross.platform.product_1" {
  1085. return .ipaPremiumCrossPlatform
  1086. } else if productId == "com.brother.pdfreaderpro.cross.platform.product_3" {
  1087. return .ipaPermanentCrossPlatform
  1088. } else if productId == "com.brother.pdfreaderpro.ai.product_1" {
  1089. return .ipaAI
  1090. } else if productId == "com.brother.pdfreaderpro.mac.product_1+com.brother.pdfreaderpro.ai.product_1" {
  1091. return .ipaPremiumAI
  1092. } else if productId == "com.brother.pdfreaderpro.mac.product_3+com.brother.pdfreaderpro.ai.product_1" {
  1093. return .ipaPermanentAI
  1094. } else if productId == "com.brother.pdfreaderpro.cross.platform.product_3+com.brother.pdfreaderpro.ai.product_1" {
  1095. return .ipaPermanentCrossPlatformAI
  1096. }
  1097. return .ipaConvert
  1098. }
  1099. private func isNumeric(_ text: String) -> Bool {
  1100. let pattern = "^[0-9]+$"
  1101. let regex = try? NSRegularExpression(pattern: pattern)
  1102. let range = NSRange(location: 0, length: text.utf16.count)
  1103. if let match = regex?.firstMatch(in: text, options: [], range: range) {
  1104. return match.range.location != NSNotFound
  1105. } else {
  1106. return false
  1107. }
  1108. }
  1109. private func openWebView(_ urlPath: String) -> Void {
  1110. DispatchQueue.main.async { [weak self] in
  1111. guard let self = self else { return }
  1112. // let wc = KMEmbeddedPaymentPopWC.init(windowNibName: "KMEmbeddedPaymentPopWC")
  1113. self.embeddedPaymentPopWC = KMEmbeddedPaymentPopWC.currentFirstTrialWC(urlPath)
  1114. self.embeddedPaymentPopWC?.showWindow(nil)
  1115. }
  1116. startPolling()
  1117. }
  1118. private func scanCode(_ urlPath: String) -> Void {
  1119. DispatchQueue.main.async { [weak self] in
  1120. guard let self = self else { return }
  1121. self.payBox.contentView = self.payResult
  1122. self.payBoxHeight.constant = 262
  1123. if self.paymentMethod == .wxpay {
  1124. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with WeChat", comment: "")
  1125. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment27")
  1126. } else if self.paymentMethod == .alipay {
  1127. self.scanLabel.stringValue = NSLocalizedString("Scan QR Code with AliPay", comment: "")
  1128. self.payTypeImageView.image = NSImage(named: "EmbeddedPayment28")
  1129. }
  1130. if let url = URL(string: urlPath) {
  1131. URLSession.shared.dataTask(with: url) { data, response, error in
  1132. if let data = data, let image = NSImage(data: data) {
  1133. DispatchQueue.main.async {
  1134. self.qrCodeImageView.image = image
  1135. self.startPolling()
  1136. }
  1137. } else {
  1138. print("Failed to load image or there was an error: \(error?.localizedDescription ?? "Unknown error")")
  1139. }
  1140. }.resume()
  1141. } else {
  1142. print("Invalid URL.")
  1143. }
  1144. }
  1145. }
  1146. private func pollingResult() -> Void {
  1147. DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
  1148. guard let self = self else { return }
  1149. if self.orderID == "" { return }
  1150. self._getOrderStatus(tradeNo: self.orderID, callback: { info, err in
  1151. if let dataInfo = info {
  1152. if dataInfo.keys.contains("status") {
  1153. let status = dataInfo["status"] as? Int
  1154. if status == 2 {
  1155. self.stopPolling()
  1156. if dataInfo.keys.contains("license") {
  1157. let license = dataInfo["license"] as? [String]
  1158. if license!.count > 0 {
  1159. self.verificationActivate(license: license![0])
  1160. }
  1161. // else {
  1162. // let alert = NSAlert()
  1163. // alert.alertStyle = .critical
  1164. // alert.messageText = NSLocalizedString("Connection Error", comment: "")
  1165. // alert.informativeText = NSLocalizedString("Get License Code is empty.", comment: "")
  1166. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1167. // if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  1168. // alert.beginSheetModal(for: self.window!, completionHandler: nil)
  1169. // } else {
  1170. // alert.runModal()
  1171. // }
  1172. // return
  1173. // }
  1174. }
  1175. if dataInfo.keys.contains("ai_license") {
  1176. let ai_license = dataInfo["ai_license"] as? [String]
  1177. if ai_license!.count > 0 {
  1178. self.verificationActivateAIInfo(license: ai_license![0])
  1179. }
  1180. // else {
  1181. // let alert = NSAlert()
  1182. // alert.alertStyle = .critical
  1183. // alert.messageText = NSLocalizedString("Connection Error", comment: "")
  1184. // alert.informativeText = NSLocalizedString("Get AI License Code is empty.", comment: "")
  1185. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  1186. // if alert.responds(to: #selector(alert.beginSheetModal(for:completionHandler:))) {
  1187. // alert.beginSheetModal(for: self.window!, completionHandler: nil)
  1188. // } else {
  1189. // alert.runModal()
  1190. // }
  1191. // return
  1192. // }
  1193. }
  1194. } else {
  1195. self.startPolling()
  1196. }
  1197. }
  1198. }
  1199. })
  1200. }
  1201. }
  1202. private func startPolling() {
  1203. timer = Timer(timeInterval: interval, repeats: false) { [weak self] _ in
  1204. guard let self = self else { return }
  1205. self.performTask()
  1206. }
  1207. RunLoop.main.add(timer!, forMode: .common)
  1208. timer!.fire()
  1209. }
  1210. @objc private func performTask() {
  1211. guard pollCount < maxPolls else {
  1212. stopPolling()
  1213. return
  1214. }
  1215. // 执行你的轮询任务
  1216. print("Performing polling task \(pollCount + 1)/\(maxPolls)")
  1217. pollCount += 1
  1218. pollingResult()
  1219. }
  1220. private func stopPolling() {
  1221. timer?.invalidate()
  1222. timer = nil
  1223. print("Polling task completed.")
  1224. if self.embeddedPaymentPopWC != nil {
  1225. DispatchQueue.main.async { [weak self] in
  1226. guard let self = self else { return }
  1227. self.embeddedPaymentPopWC?.close()
  1228. self.embeddedPaymentPopWC = nil
  1229. }
  1230. }
  1231. pollCount = 0
  1232. }
  1233. private func verificationActivateAIInfo(license: String) {
  1234. guard !license.isEmpty else {
  1235. return
  1236. }
  1237. let infoDic = ["cdkey": license]
  1238. AIInfoManager.default().activateAI(withInfo: infoDic) { info, error in
  1239. if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  1240. self.qrCodeImageView.isHidden = true
  1241. self.successfullyView.isHidden = false
  1242. }
  1243. }
  1244. }
  1245. private func verificationActivate(license: String) {
  1246. guard !license.isEmpty else {
  1247. return
  1248. }
  1249. let infoDic = ["cdkey": license]
  1250. VerificationManager.default().activateDevice(withInfo: infoDic) { status, info, error in
  1251. if self.paymentMethod == .wxpay || self.paymentMethod == .alipay {
  1252. self.qrCodeImageView.isHidden = true
  1253. self.successfullyView.isHidden = false
  1254. }
  1255. }
  1256. }
  1257. // MARK: Show Methods
  1258. // @IBAction private func dismissSheet(_ sender: NSButton) {
  1259. // NSApp.endSheet(window!)
  1260. // window!.orderOut(self)
  1261. // }
  1262. //
  1263. //
  1264. // @objc private func didEndSheet(_ sheet: NSWindow?, returnCode: Int, contextInfo: UnsafeMutableRawPointer?) {
  1265. // if contextInfo != nil && self.handler != nil {
  1266. // self.handler!(returnCode)
  1267. // }
  1268. // }
  1269. //
  1270. // @objc func beginSheetModal(for window: NSWindow?, completionHandler handler: ((Int) -> Void)?) {
  1271. // if window != nil {
  1272. // window!.beginSheet(self.window!) { ModalResponse in
  1273. // self.handler?(ModalResponse.rawValue)
  1274. // }
  1275. // }
  1276. // self.handler = handler
  1277. // }
  1278. // MARK: - Notification
  1279. func controlTextDidEndEditing(_ obj: Notification) {
  1280. let textField = obj.object as? NSTextField
  1281. if textField == amountTextField1 {
  1282. if isNumeric(textField!.stringValue) {
  1283. if textField == amountTextField1 { textField?.stringValue = String(pdfCount) }
  1284. } else {
  1285. if textField == amountTextField1 { pdfCount = Int(textField!.stringValue) ?? 1 }
  1286. }
  1287. } else if textField == emailTextField {
  1288. } else if textField == paddleTextField {
  1289. }
  1290. }
  1291. }
  1292. extension KMPurchaseEmbeddedWindowController: NSTextFieldDelegate {
  1293. func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
  1294. if let url = link as? String {
  1295. switch url {
  1296. case "GetSpecialOffer://":
  1297. NSWorkspace.shared.open(URL(string: NSLocalizedString("https://www.pdfreaderpro.com/store?mode=edu", comment: ""))!)
  1298. break
  1299. case "ContactUs://":
  1300. NSWorkspace.shared.open(URL(string: NSLocalizedString("https://www.pdfreaderpro.com/vpp-purchase-program", comment: ""))!)
  1301. break
  1302. case "PrivacyPolicy://":
  1303. NSWorkspace.shared.open(URL(string: "https://www.pdfreaderpro.com/privacy-policy")!)
  1304. break
  1305. case "TermsofService://":
  1306. NSWorkspace.shared.open(URL(string: "https://www.pdfreaderpro.com/terms_of_service")!)
  1307. break
  1308. default:
  1309. break
  1310. }
  1311. }
  1312. return true
  1313. }
  1314. }
  1315. extension KMPurchaseEmbeddedWindowController: NSWindowDelegate {
  1316. func windowWillClose(_ notification: Notification) {
  1317. if orderID != "" {
  1318. pollingResult()
  1319. }
  1320. if self.embeddedPaymentPopWC != nil {
  1321. self.embeddedPaymentPopWC?.close()
  1322. self.embeddedPaymentPopWC = nil
  1323. }
  1324. KMPurchaseEmbeddedWindowController.currentWindowController = nil
  1325. }
  1326. }