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