api-actions
2 years ago
rest-endpoints
2 years ago
scenarios-connectors
2 years ago
scenarios-logic
2 years ago
scenarios-views
2 years ago
tab-handlers
2 years ago
controller.php
2 years ago
scenarios-logic-manager.php
2 years ago
scenarios-manager.php
2 years ago
scenarios-view-manager.php
2 years ago
controller.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Gateways\Paypal; |
| 4 | |
| 5 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 6 | use Jet_Form_Builder\Classes\Tools; |
| 7 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 8 | use JFB_Modules\Gateways\Module; |
| 9 | use JFB_Modules\Gateways\Paypal\Api_Actions\Get_Token; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class Controller extends \Jet_Form_Builder\Gateways\Base_Scenario_Gateway { |
| 17 | |
| 18 | public function __construct() { |
| 19 | // install tab handlers for Settings page |
| 20 | Tab_Handler_Manager::instance()->install( new Tab_Handlers\Paypal_Handler() ); |
| 21 | } |
| 22 | |
| 23 | const ID = 'paypal'; |
| 24 | |
| 25 | public $data = false; |
| 26 | public $message = false; |
| 27 | public $redirect = false; |
| 28 | |
| 29 | protected $token_query_name = 'token'; |
| 30 | |
| 31 | /** |
| 32 | * Returns current gateway ID |
| 33 | * |
| 34 | * @return [type] [description] |
| 35 | */ |
| 36 | public function get_id() { |
| 37 | return self::ID; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns current gateway name |
| 42 | * |
| 43 | * @return [type] [description] |
| 44 | */ |
| 45 | public function get_name() { |
| 46 | return _x( 'PayPal Checkout', 'Paypal gateways editor data', 'jet-form-builder' ); |
| 47 | } |
| 48 | |
| 49 | protected function options_list() { |
| 50 | return array( |
| 51 | 'client_id' => array( |
| 52 | 'label' => _x( 'Client ID', 'Paypal gateways editor data', 'jet-form-builder' ), |
| 53 | ), |
| 54 | 'secret' => array( |
| 55 | 'label' => _x( 'Secret Key', 'Paypal gateways editor data', 'jet-form-builder' ), |
| 56 | ), |
| 57 | 'currency' => array( |
| 58 | 'required' => false, |
| 59 | ), |
| 60 | 'use_global' => array( |
| 61 | 'label' => _x( 'Use Global Settings', 'Paypal gateways editor data', 'jet-form-builder' ), |
| 62 | 'required' => false, |
| 63 | ), |
| 64 | 'gateway_type' => array( |
| 65 | 'label' => _x( 'Gateway Action', 'Paypal gateways editor data', 'jet-form-builder' ), |
| 66 | 'default' => Scenarios_Logic\Pay_Now::scenario_id(), |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | public function custom_labels(): array { |
| 72 | return array( |
| 73 | 'scenario' => Scenarios_Manager::instance()->view()->get_editor_labels(), |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | public function additional_editor_data(): array { |
| 78 | return array_merge( |
| 79 | array( |
| 80 | 'version' => 1, |
| 81 | 'scenarios' => Tools::with_placeholder( |
| 82 | Scenarios_Manager::instance()->view()->get_items_list(), |
| 83 | __( 'Choose scenario...', 'jet-form-builder' ) |
| 84 | ), |
| 85 | ), |
| 86 | Scenarios_Manager::instance()->view()->get_editor_data() |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | public static function get_credentials() { |
| 91 | return Module::instance()->get_global_settings( self::ID ); |
| 92 | } |
| 93 | |
| 94 | public static function get_credentials_by_form( $form_id ) { |
| 95 | if ( ! $form_id ) { |
| 96 | return self::get_credentials(); |
| 97 | } |
| 98 | $credits = Module::instance()->get_form_gateways_by_id( $form_id )[ self::ID ] ?? array(); |
| 99 | |
| 100 | if ( ! empty( $credits['client_id'] ) && |
| 101 | ! empty( $credits['secret'] ) && |
| 102 | empty( $credits['use_global'] ) |
| 103 | ) { |
| 104 | return $credits; |
| 105 | } |
| 106 | |
| 107 | return self::get_credentials(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns auth token for current client_id and secret combination |
| 112 | * |
| 113 | * @return mixed|void [description] |
| 114 | * @throws Gateway_Exception |
| 115 | */ |
| 116 | public function get_current_token() { |
| 117 | $client_id = $this->current_gateway( 'client_id' ); |
| 118 | $secret = $this->current_gateway( 'secret' ); |
| 119 | |
| 120 | return self::get_token_with_credits( $client_id, $secret ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param $form_id |
| 125 | * |
| 126 | * @return mixed|string |
| 127 | * @throws Gateway_Exception |
| 128 | */ |
| 129 | public static function get_token_by_form_id( $form_id ) { |
| 130 | if ( ! $form_id ) { |
| 131 | return self::get_token_global(); |
| 132 | } |
| 133 | |
| 134 | $paypal = self::get_credentials_by_form( $form_id ); |
| 135 | |
| 136 | return self::get_token_with_credits( |
| 137 | $paypal['client_id'] ?? '', |
| 138 | $paypal['secret'] ?? '' |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return mixed|string |
| 144 | * @throws Gateway_Exception |
| 145 | */ |
| 146 | public static function get_token_global() { |
| 147 | $credits = self::get_credentials(); |
| 148 | |
| 149 | $secret = $credits['secret'] ?? false; |
| 150 | $client_id = $credits['client_id'] ?? false; |
| 151 | |
| 152 | return self::get_token_with_credits( $client_id, $secret ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param $client_id |
| 157 | * @param $secret |
| 158 | * |
| 159 | * @return mixed|string |
| 160 | * @throws Gateway_Exception |
| 161 | */ |
| 162 | public static function get_token_with_credits( $client_id, $secret ) { |
| 163 | if ( ! $client_id || ! $secret ) { |
| 164 | throw new Gateway_Exception( |
| 165 | 'Empty `client_id` or `secret_key`.', |
| 166 | esc_html( $client_id ), |
| 167 | esc_html( $secret ) |
| 168 | ); |
| 169 | } |
| 170 | $hash = 'jet_fb_pp_token_' . md5( $client_id . $secret ); |
| 171 | $token = get_transient( $hash ); |
| 172 | |
| 173 | if ( $token ) { |
| 174 | return $token; |
| 175 | } |
| 176 | |
| 177 | $request = ( new Get_Token() ) |
| 178 | ->set_credentials( $client_id, $secret ); |
| 179 | |
| 180 | $response = $request->send_request(); |
| 181 | |
| 182 | if ( empty( $response['access_token'] ) ) { |
| 183 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 184 | throw new Gateway_Exception( esc_html( $response['error_description'] ), $response, $request->get_request_args() ); |
| 185 | } |
| 186 | |
| 187 | $token = $response['access_token']; |
| 188 | |
| 189 | set_transient( $hash, $token, $response['expires_in'] * 0.9 ); |
| 190 | |
| 191 | return $token; |
| 192 | } |
| 193 | |
| 194 | } |
| 195 |