KMPurchaseEmbeddedWindowController.swift 80 KB

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