controller.php
320 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Gateways\Paypal; |
| 4 | |
| 5 | use Jet_Form_Builder\Actions\Action_Handler; |
| 6 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 7 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 8 | use Jet_Form_Builder\Plugin; |
| 9 | use Jet_Form_Builder\Gateways\Base_Gateway; |
| 10 | |
| 11 | class Controller extends Base_Gateway { |
| 12 | |
| 13 | public $data = false; |
| 14 | public $message = false; |
| 15 | public $redirect = false; |
| 16 | |
| 17 | protected $token_query_name = 'token'; |
| 18 | |
| 19 | /** |
| 20 | * Returns current gateway ID |
| 21 | * |
| 22 | * @return [type] [description] |
| 23 | */ |
| 24 | public function get_id() { |
| 25 | return 'paypal'; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Returns current gateway name |
| 30 | * |
| 31 | * @return [type] [description] |
| 32 | */ |
| 33 | public function get_name() { |
| 34 | return __( 'PayPal Checkout', 'jet-form-builder' ); |
| 35 | } |
| 36 | |
| 37 | protected function options_list() { |
| 38 | return array( |
| 39 | 'client_id' => array( |
| 40 | 'label' => __( 'Client ID', 'jet-form-builder' ) |
| 41 | ), |
| 42 | 'secret' => array( |
| 43 | 'label' => __( 'Secret Key', 'jet-form-builder' ) |
| 44 | ), |
| 45 | 'currency' => array( |
| 46 | 'label' => __( 'Currency Code', 'jet-form-builder' ) |
| 47 | ), |
| 48 | 'use_global' => array( |
| 49 | 'label' => __( 'Use Global Settings', 'jet-form-builder' ), |
| 50 | 'required' => false |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | public function failed_statuses() { |
| 56 | return array( 'VOIDED' ); |
| 57 | } |
| 58 | |
| 59 | protected function retrieve_gateway_meta() { |
| 60 | return Plugin::instance()->post_type->get_gateways( $this->data['form_id'] ); |
| 61 | } |
| 62 | |
| 63 | protected function retrieve_payment_instance() { |
| 64 | $meta = $this->gateways_meta[ $this->get_id() ]; |
| 65 | $client_id = $meta['client_id']; |
| 66 | $secret = $meta['secret']; |
| 67 | |
| 68 | $payment = $this->request( |
| 69 | 'v2/checkout/orders/' . $this->payment_token . '/capture', |
| 70 | array(), |
| 71 | null, |
| 72 | $this->get_token( $client_id, $secret ) |
| 73 | ); |
| 74 | |
| 75 | return json_decode( $payment, true ); |
| 76 | } |
| 77 | |
| 78 | protected function set_gateway_data_on_result() { |
| 79 | try { |
| 80 | $this->set_current_gateway_options(); |
| 81 | $this->set_payment_status(); |
| 82 | $this->set_payment_amount(); |
| 83 | $this->set_payer(); |
| 84 | |
| 85 | } catch ( Gateway_Exception $exception ) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | private function set_payment_status() { |
| 93 | list( $default_status ) = $this->failed_statuses(); |
| 94 | |
| 95 | $this->data['status'] = $this->payment_instance['status'] ?? $default_status; |
| 96 | } |
| 97 | |
| 98 | private function set_payer() { |
| 99 | if ( ! empty( $this->payment_instance['payer'] ) ) { |
| 100 | $this->data['payer'] = array( |
| 101 | 'first_name' => $this->payment_instance['payer']['name']['given_name'], |
| 102 | 'last_name' => $this->payment_instance['payer']['name']['surname'], |
| 103 | 'email' => $this->payment_instance['payer']['email_address'], |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private function set_payment_amount() { |
| 109 | if ( ! empty( $this->payment_instance['purchase_units'][0]['payments']['captures'] ) ) { |
| 110 | $payment_unit = $this->payment_instance['purchase_units'][0]['payments']['captures'][0]; |
| 111 | $this->data['amount'] = $payment_unit['amount']; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | protected function query_order_token( $order_id, $form_id ) { |
| 117 | return $this->get_token( |
| 118 | $this->options['client_id'], |
| 119 | $this->options['secret'] |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Process gateway payment |
| 125 | * |
| 126 | * @param $action_handler |
| 127 | * |
| 128 | * @return void [type] [description] |
| 129 | */ |
| 130 | public function after_actions( Action_Handler $action_handler ) { |
| 131 | |
| 132 | if ( ! $this->set_gateway_data( $action_handler ) ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $order = $this->request( |
| 137 | 'v2/checkout/orders', |
| 138 | array(), |
| 139 | array( |
| 140 | 'intent' => 'CAPTURE', |
| 141 | 'application_context' => array( |
| 142 | 'landing_page' => 'BILLING', |
| 143 | 'user_action' => 'PAY_NOW', |
| 144 | 'brand_name' => get_option( 'blogname' ), |
| 145 | 'return_url' => $this->get_refer_url( self::SUCCESS_TYPE ), |
| 146 | 'cancel_url' => $this->get_refer_url( self::FAILED_TYPE ), |
| 147 | ), |
| 148 | 'purchase_units' => array( |
| 149 | array( |
| 150 | 'custom_id' => $action_handler->form_id . '-' . $this->order_id, |
| 151 | 'amount' => array( |
| 152 | 'currency_code' => $this->options['currency'], |
| 153 | 'value' => $this->price, |
| 154 | ), |
| 155 | ), |
| 156 | ), |
| 157 | ) |
| 158 | ); |
| 159 | |
| 160 | if ( empty( $order ) || is_wp_error( $order ) ) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | $order = json_decode( $order, true ); |
| 165 | |
| 166 | if ( ! $order ) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if ( empty( $order['id'] ) ) { |
| 171 | throw ( new Action_Exception( $order['message'], $order ) )->dynamic_error(); |
| 172 | } |
| 173 | |
| 174 | update_post_meta( |
| 175 | $this->order_id, |
| 176 | self::GATEWAY_META_KEY, |
| 177 | wp_json_encode( array( |
| 178 | 'payment_id' => $order['id'], |
| 179 | 'order_id' => $this->order_id, |
| 180 | 'form_id' => $action_handler->form_id, |
| 181 | 'form_data' => $action_handler->request_data, |
| 182 | ), JSON_UNESCAPED_UNICODE ) |
| 183 | ); |
| 184 | |
| 185 | $this->maybe_redirect_to_checkout( $order ); |
| 186 | } |
| 187 | |
| 188 | private function maybe_redirect_to_checkout( $order ) { |
| 189 | foreach ( $order['links'] as $link ) { |
| 190 | if ( ! empty( $link['rel'] ) && 'approve' === $link['rel'] ) { |
| 191 | $this->action_handler->add_response( array( |
| 192 | 'redirect' => $link['href'] |
| 193 | ) ); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Return API url |
| 200 | * |
| 201 | * @param $endpoint |
| 202 | * |
| 203 | * @return string |
| 204 | */ |
| 205 | public function api_url( $endpoint ) { |
| 206 | |
| 207 | $sandbox = apply_filters( 'jet-form-builder/gateways/paypal/sandbox-mode', false ); |
| 208 | |
| 209 | if ( $sandbox ) { |
| 210 | $url = 'https://api-m.sandbox.paypal.com/'; |
| 211 | } else { |
| 212 | $url = 'https://api-m.paypal.com/'; |
| 213 | } |
| 214 | |
| 215 | return esc_url_raw( $url . $endpoint ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns auth token for current client_id and secret combination |
| 220 | * |
| 221 | * @param bool $client_id |
| 222 | * @param bool $secret |
| 223 | * |
| 224 | * @return mixed|void [description] |
| 225 | * @throws Action_Exception |
| 226 | */ |
| 227 | public function get_token( $client_id = false, $secret = false ) { |
| 228 | $hash = md5( $client_id . $secret ); |
| 229 | $token = get_transient( $hash ); |
| 230 | |
| 231 | if ( $token ) { |
| 232 | return $token; |
| 233 | } |
| 234 | |
| 235 | if ( ! $client_id || ! $secret ) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | $key = base64_encode( $client_id . ':' . $secret ); |
| 240 | |
| 241 | $response = $this->request( |
| 242 | 'v1/oauth2/token', |
| 243 | array( |
| 244 | 'Accept' => 'application/json', |
| 245 | 'Accept-Language' => get_locale(), |
| 246 | 'Authorization' => 'Basic ' . $key, |
| 247 | ), |
| 248 | 'grant_type=client_credentials' |
| 249 | ); |
| 250 | |
| 251 | $response = json_decode( $response, true ); |
| 252 | |
| 253 | if ( empty( $response['access_token'] ) ) { |
| 254 | throw ( new Action_Exception( $response['error_description'] ) )->dynamic_error(); |
| 255 | } |
| 256 | |
| 257 | $token = $response['access_token']; |
| 258 | |
| 259 | set_transient( $hash, $token, 3 * HOUR_IN_SECONDS ); |
| 260 | |
| 261 | return $token; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Make a request |
| 267 | * |
| 268 | * @param null $endpoint |
| 269 | * @param array $headers [description] |
| 270 | * @param array $body [description] |
| 271 | * |
| 272 | * @param bool $token |
| 273 | * @param string $method |
| 274 | * |
| 275 | * @return string [type] [description] |
| 276 | * @throws Action_Exception |
| 277 | */ |
| 278 | public function request( $endpoint = null, $headers = array(), $body = array(), $token = false, $method = 'post' ) { |
| 279 | |
| 280 | $url = $this->api_url( $endpoint ); |
| 281 | |
| 282 | if ( empty( $headers ) ) { |
| 283 | |
| 284 | $headers = array( |
| 285 | 'Content-Type' => 'application/json', |
| 286 | 'Accept-Language' => get_locale(), |
| 287 | 'Authorization' => 'Bearer ' . ( $token ? $token : $this->order_token ), |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | if ( is_array( $body ) ) { |
| 292 | |
| 293 | // phpcs:disable WordPress.PHP.IniSet.Risky |
| 294 | if ( version_compare( phpversion(), '7.1', '>=' ) ) { |
| 295 | ini_set( 'precision', 17 ); |
| 296 | ini_set( 'serialize_precision', - 1 ); |
| 297 | } |
| 298 | // phpcs:enable WordPress.PHP.IniSet.Risky |
| 299 | |
| 300 | $body = wp_json_encode( $body ); |
| 301 | } |
| 302 | |
| 303 | $args = array( |
| 304 | 'timeout' => 45, |
| 305 | 'headers' => $headers, |
| 306 | ); |
| 307 | |
| 308 | if ( 'post' === $method ) { |
| 309 | $args['method'] = 'POST'; |
| 310 | $args['body'] = $body; |
| 311 | $response = wp_remote_post( $url, $args ); |
| 312 | } else { |
| 313 | $response = wp_remote_get( $url, $args ); |
| 314 | } |
| 315 | |
| 316 | return wp_remote_retrieve_body( $response ); |
| 317 | } |
| 318 | |
| 319 | } |
| 320 |