KMPurchaseEmbeddedWindowController.swift 89 KB

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