| @@ -1,42 +1,34 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Admin\Form\InitJs; |
| 4 | 4 | |
| 5 | -use BitCode\BitForm\Admin\Form\Helpers; | |
| 6 | -use BitCode\BitForm\Core\Integration\IntegrationHandler; | |
| 5 | +class Paypal | |
| 6 | +{ | |
| 7 | + public static function init() | |
| 8 | + { | |
| 9 | + return <<<PAYPAL_INIT_JS | |
| 10 | + const paypalUrlParams = {'client-id': fldData.clientId}; | |
| 11 | + if('locale' in fldData){ | |
| 12 | + paypalUrlParams.locale = fldData.locale; | |
| 13 | + } | |
| 14 | + if('disableFunding' in fldData && fldData.disableFunding.length){ | |
| 15 | + paypalUrlParams['disable-funding'] = fldData.disableFunding; | |
| 16 | + } | |
| 17 | + if(fldData?.payType === 'subscription'){ | |
| 18 | + paypalUrlParams.vault = 'true'; | |
| 19 | + paypalUrlParams.intent = 'subscription'; | |
| 20 | + } else if('currency' in fldData){ | |
| 21 | + paypalUrlParams.currency = fldData.currency; | |
| 22 | + } | |
| 7 | 23 | |
| 8 | -class Paypal { | |
| 9 | - public static function init($fk, $config, $contentId) { | |
| 10 | - $integrationHandler = new IntegrationHandler(0); | |
| 11 | - $paypalIntegration = $integrationHandler->getAIntegration(json_decode($config)->payIntegID); | |
| 12 | - $integrationDetails = json_decode($paypalIntegration[0]->integration_details); | |
| 13 | - $clientID = $integrationDetails->clientID; | |
| 14 | - $config = json_decode($config); | |
| 15 | - $config->clientId = $clientID; | |
| 16 | - $config->fieldKey = $fk; | |
| 17 | - $namespace = str_replace('_', '-', $contentId); | |
| 18 | - $config->namespace = $namespace; | |
| 19 | - $config->contentId = $contentId; | |
| 20 | - $urlQueryParams = [ | |
| 21 | - 'client-id' => $clientID, | |
| 22 | - ]; | |
| 23 | - if (isset($config->locale)) { | |
| 24 | - $urlQueryParams['locale'] = $config->locale; | |
| 25 | - } | |
| 26 | - if (!empty($config->disableFunding)) { | |
| 27 | - $urlQueryParams['disable-funding'] = $config->disableFunding; | |
| 28 | - } | |
| 29 | - if ('subscription' === $config->payType) { | |
| 30 | - $urlQueryParams['vault'] = 'true'; | |
| 31 | - $urlQueryParams['intent'] = 'subscription'; | |
| 32 | - } elseif (isset($config->currency)) { | |
| 33 | - $urlQueryParams['currency'] = $config->currency; | |
| 34 | - } | |
| 35 | - $url = 'https://www.paypal.com/sdk/js?' . http_build_query($urlQueryParams); | |
| 36 | - $attr = [ | |
| 37 | - 'data-namespace' => $namespace, | |
| 38 | - ]; | |
| 39 | - $config = json_encode($config); | |
| 40 | - return Helpers::scriptLoader($url, "bit_paypal_script-{$namespace}", "window.bf_globals.{$contentId}.inits['{$fk}'] = new bit_paypal_field(fld,{$config})", ".{$fk}-paypal-wrp", $attr, '', "{$contentId}"); | |
| 24 | + const paypalUrl = 'https://www.paypal.com/sdk/js?' + Object.entries(paypalUrlParams).map(([key, val]) => key+'='+val).join('&'); | |
| 25 | + const attrs = {'data-namespace': 'bit-paypal-'+ contentId}; | |
| 26 | + const id = 'bit_paypal_script-' + contentId; | |
| 27 | + const initFunc = function(){ | |
| 28 | + bf_globals[contentId].inits[fldKey] = getFldInstance(contentId, fldKey, fldType) | |
| 29 | + }; | |
| 30 | + | |
| 31 | + scriptLoader(paypalUrl, '', attrs, id, initFunc); | |
| 32 | +PAYPAL_INIT_JS; | |
| 41 | 33 | } |
| 42 | 34 | } |