1616define ('WHATS_MY_IP_URL ' , 'https://{{FRONTEND}}/ip ' );
1717define ('SW_MODULE_VERSION ' , '{{VERSION}} ' );
1818define ('SW_MODULE_NAME ' , 'synergywholesaledomains ' );
19+ define ('SW_TLDSYNC_SETTING_KEYS ' , [
20+ 'marginType ' => 'SynergyWholesaleTldSyncMarginType ' ,
21+ 'profitMargin ' => 'SynergyWholesaleTldSyncProfitMargin ' ,
22+ 'rounding ' => 'SynergyWholesaleTldSyncRounding ' ,
23+ ]);
1924
2025define ('SW_DNS_CONFIG_TYPES ' , [
2126 0 => 'Inactive ' ,
@@ -77,6 +82,102 @@ function synergywholesaledomains_helper_getNameservers(array $params)
7782 return $ nameservers ;
7883}
7984
85+ /**
86+ * Apply a minimum TLD sync price while retaining disabled and free pricing.
87+ *
88+ * @param float|int|string $price
89+ * @param float|int|string $minimum
90+ * @return float|int|string
91+ */
92+ function synergywholesaledomains_helper_applyMinimumPrice ($ price , $ minimum )
93+ {
94+ if ($ price <= 0 || $ price >= $ minimum ) {
95+ return $ price ;
96+ }
97+
98+ return (string ) $ minimum ;
99+ }
100+
101+ function synergywholesaledomains_helper_applyTldSyncMinimums ($ register , $ renew , $ transfer , $ minimumRenew , $ minimumTransfer )
102+ {
103+ // Preserve the upstream registration rule before applying renew-only settings.
104+ $ register = $ register < $ renew ? $ renew : $ register ;
105+
106+ return [
107+ $ register ,
108+ synergywholesaledomains_helper_applyMinimumPrice ($ renew , $ minimumRenew ),
109+ synergywholesaledomains_helper_applyMinimumPrice ($ transfer , $ minimumTransfer ),
110+ ];
111+ }
112+
113+ function synergywholesaledomains_isTldSyncPage ()
114+ {
115+ $ path = isset ($ _SERVER ['REQUEST_URI ' ]) ? parse_url ($ _SERVER ['REQUEST_URI ' ], PHP_URL_PATH ) : '' ;
116+
117+ return (bool ) preg_match ('#/utilities/tools/tldsync/import/?$# ' , $ path );
118+ }
119+
120+ function synergywholesaledomains_getTldSyncSettings ()
121+ {
122+ $ stored = Capsule::table ('tblconfiguration ' )
123+ ->whereIn ('setting ' , array_values (SW_TLDSYNC_SETTING_KEYS ))
124+ ->pluck ('value ' , 'setting ' );
125+ $ settings = [];
126+
127+ foreach (SW_TLDSYNC_SETTING_KEYS as $ name => $ key ) {
128+ $ settings [$ name ] = isset ($ stored [$ key ]) ? $ stored [$ key ] : null ;
129+ }
130+
131+ return array_merge ($ settings , synergywholesaledomains_getMinimumPrices ());
132+ }
133+
134+ function synergywholesaledomains_getMinimumPrices ()
135+ {
136+ $ stored = Capsule::table ('tblregistrars ' )
137+ ->where ('registrar ' , SW_MODULE_NAME )
138+ ->whereIn ('setting ' , ['minimumRenewPrice ' , 'minimumTransferPrice ' ])
139+ ->pluck ('value ' , 'setting ' );
140+
141+ $ prices = [];
142+ foreach (['minimumRenewPrice ' , 'minimumTransferPrice ' ] as $ setting ) {
143+ $ value = isset ($ stored [$ setting ]) ? localAPI ('DecryptPassword ' , ['password2 ' => $ stored [$ setting ]])['password ' ] : 0 ;
144+ $ prices [$ setting ] = is_numeric ($ value ) && $ value >= 0 ? (string ) $ value : '0 ' ;
145+ }
146+
147+ return $ prices ;
148+ }
149+
150+ function synergywholesaledomains_saveTldSyncSettings (array $ settings )
151+ {
152+ foreach (SW_TLDSYNC_SETTING_KEYS as $ name => $ key ) {
153+ Capsule::table ('tblconfiguration ' )->updateOrInsert (
154+ ['setting ' => $ key ],
155+ ['value ' => $ settings [$ name ]]
156+ );
157+ }
158+ }
159+
160+ function synergywholesaledomains_validateTldSyncSettings (array $ input )
161+ {
162+ $ marginType = isset ($ input ['margin_type ' ]) ? $ input ['margin_type ' ] : '' ;
163+ $ profitMargin = isset ($ input ['margin ' ]) ? $ input ['margin ' ] : '' ;
164+ $ rounding = isset ($ input ['rounding_value ' ]) ? $ input ['rounding_value ' ] : '' ;
165+ if (
166+ !in_array ($ marginType , ['fixed ' , 'percentage ' ], true )
167+ || !is_numeric ($ profitMargin )
168+ || $ profitMargin < 0
169+ || (!in_array ($ rounding , ['' , 'none ' ], true ) && (!is_numeric ($ rounding ) || $ rounding < 0 || $ rounding > 1 ))
170+ ) {
171+ return null ;
172+ }
173+
174+ return [
175+ 'marginType ' => $ marginType ,
176+ 'profitMargin ' => (string ) $ profitMargin ,
177+ 'rounding ' => (string ) $ rounding ,
178+ ];
179+ }
180+
80181/**
81182 * Sends the API requests to the Synergy Wholesale API.
82183 *
@@ -291,6 +392,20 @@ function synergywholesaledomains_getConfigArray(array $params)
291392 'Size ' => '1 ' ,
292393 'Description ' => 'Tick if you wish to perform a renewal on any .au domains submitted for transfer that are within 90 days of expiry. ' ,
293394 ],
395+ 'minimumRenewPrice ' => [
396+ 'FriendlyName ' => 'Minimum TLD Sync Renew Price ' ,
397+ 'Type ' => 'text ' ,
398+ 'Size ' => '10 ' ,
399+ 'Default ' => '0 ' ,
400+ 'Description ' => 'AUD. Supports sub-cent values. Applied to the fetched cost before margin and rounding; prices of zero are unchanged. ' ,
401+ ],
402+ 'minimumTransferPrice ' => [
403+ 'FriendlyName ' => 'Minimum TLD Sync Transfer Price ' ,
404+ 'Type ' => 'text ' ,
405+ 'Size ' => '10 ' ,
406+ 'Default ' => '0 ' ,
407+ 'Description ' => 'AUD. Supports sub-cent values. Applied to the fetched cost before margin and rounding; prices of zero are unchanged. ' ,
408+ ],
294409 'test_api_connection ' => [
295410 'FriendlyName ' => 'Check API Connectivity ' ,
296411 'Type ' => 'yesno ' ,
@@ -3032,6 +3147,10 @@ function synergywholesaledomains_GetTldPricing(array $params)
30323147 }
30333148
30343149 $ results = new WHMCS \Results \ResultsList ();
3150+ $ syncSettings = synergywholesaledomains_validateTldSyncSettings ($ _POST );
3151+ if (null !== $ syncSettings ) {
3152+ synergywholesaledomains_saveTldSyncSettings ($ syncSettings );
3153+ }
30353154
30363155 foreach ($ response ['pricing ' ] as $ extension ) {
30373156 $ tld = '. ' . $ extension ->tld ;
@@ -3058,9 +3177,13 @@ function synergywholesaledomains_GetTldPricing(array $params)
30583177 $ transferPrice = 0.00 ;
30593178 }
30603179
3061- if ($ registerPrice < $ renewPrice ) {
3062- $ registerPrice = $ renewPrice ;
3063- }
3180+ list ($ registerPrice , $ renewPrice , $ transferPrice ) = synergywholesaledomains_helper_applyTldSyncMinimums (
3181+ $ registerPrice ,
3182+ $ renewPrice ,
3183+ $ transferPrice ,
3184+ isset ($ params ['minimumRenewPrice ' ]) ? $ params ['minimumRenewPrice ' ] : 0 ,
3185+ isset ($ params ['minimumTransferPrice ' ]) ? $ params ['minimumTransferPrice ' ] : 0
3186+ );
30643187
30653188 $ results [] = (new WHMCS \Domain \TopLevel \ImportItem ())
30663189 ->setExtension ($ tld )
0 commit comments