KMPurchaseEmbeddedWindowController.swift 80 KB

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