KMPurchaseEmbeddedWindowController.swift 109 KB

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