Apm
1 year ago
BuyNowPayLater
1 year ago
DigitalWallets
9 months ago
OpenBanking
1 year ago
AbstractAsyncPaymentMethod.php
1 year ago
AbstractPaymentMethod.php
1 year ago
AsyncPaymentMethodInterface.php
2 years ago
PaymentMethodInterface.php
2 years ago
AbstractPaymentMethod.php
324 lines
| 1 | <?php |
| 2 | |
| 3 | namespace GlobalPayments\WooCommercePaymentGatewayProvider\PaymentMethods; |
| 4 | |
| 5 | use GlobalPayments\Api\Entities\Exceptions\GatewayException; |
| 6 | use GlobalPayments\Api\Entities\Transaction; |
| 7 | use GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\AbstractGateway; |
| 8 | use GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\GpApiGateway; |
| 9 | use GlobalPayments\WooCommercePaymentGatewayProvider\Plugin; |
| 10 | use GlobalPayments\WooCommercePaymentGatewayProvider\Utils\Utils; |
| 11 | use WC_Payment_Gateway; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | abstract class AbstractPaymentMethod extends WC_Payment_Gateway implements PaymentMethodInterface { |
| 16 | /** |
| 17 | * Gateway. |
| 18 | * |
| 19 | * @var GlobalPayments\WooCommercePaymentGatewayProvider\Gateways; |
| 20 | */ |
| 21 | public $gateway; |
| 22 | |
| 23 | /** |
| 24 | * Payment method default title. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $default_title; |
| 29 | |
| 30 | /** |
| 31 | * Action to perform at checkout |
| 32 | * |
| 33 | * Possible actions: |
| 34 | * |
| 35 | * - `authorize` - authorize the card without auto capturing |
| 36 | * - `sale` - authorize the card with auto capturing |
| 37 | * - `verify` - verify the card without authorizing |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $payment_action; |
| 42 | |
| 43 | /** |
| 44 | * Payment source. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $payment_source; |
| 49 | |
| 50 | public function __construct() { |
| 51 | $this->has_fields = true; |
| 52 | $this->supports = array( |
| 53 | 'refunds', |
| 54 | ); |
| 55 | |
| 56 | $this->init_gateway(); |
| 57 | $this->configure_method_settings(); |
| 58 | $this->init_form_fields(); |
| 59 | $this->init_settings(); |
| 60 | $this->configure_merchant_settings(); |
| 61 | $this->add_hooks(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Sets the necessary WooCommerce payment method settings for exposing the |
| 66 | * payment method in the WooCommerce Admin. |
| 67 | * |
| 68 | * @return |
| 69 | */ |
| 70 | abstract public function configure_method_settings(); |
| 71 | |
| 72 | /** |
| 73 | * Custom admin options to configure the payment method specific credentials, features, etc. |
| 74 | * |
| 75 | * @return array |
| 76 | */ |
| 77 | abstract public function get_payment_method_form_fields(); |
| 78 | |
| 79 | /** |
| 80 | * Required options for proper client-side configuration. |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | abstract public function get_frontend_payment_method_options(); |
| 85 | |
| 86 | /** |
| 87 | * Request type. |
| 88 | * |
| 89 | * @return GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\Requests\RequestInterface |
| 90 | */ |
| 91 | abstract public function get_request_type(); |
| 92 | |
| 93 | /** |
| 94 | * Should be overwritten to provide additional functionality before payment gateway request. |
| 95 | * |
| 96 | * @param $request |
| 97 | * @param $order |
| 98 | */ |
| 99 | public function process_payment_before_gateway_request( &$request, $order ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Should be overwritten to provide additional functionality after payment gateway response is received. |
| 105 | * |
| 106 | * @param $gateway_response |
| 107 | * @param $is_successful |
| 108 | * @param $order |
| 109 | */ |
| 110 | public function process_payment_after_gateway_response( Transaction $gateway_response, $is_successful, \WC_Order $order ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Email address of the first-line support team. |
| 116 | * |
| 117 | * @return string |
| 118 | */ |
| 119 | public function get_first_line_support_email() { |
| 120 | return $this->gateway->get_first_line_support_email(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @inheritdoc |
| 125 | */ |
| 126 | public function init_form_fields() { |
| 127 | $this->form_fields = array_merge( |
| 128 | array( |
| 129 | 'enabled' => array( |
| 130 | 'title' => __( 'Enable/Disable', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 131 | 'type' => 'checkbox', |
| 132 | 'label' => __( 'Enable payment method', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 133 | 'default' => 'no', |
| 134 | ), |
| 135 | 'title' => array( |
| 136 | 'title' => esc_html__( 'Title', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 137 | 'type' => 'text', |
| 138 | 'description' => esc_html__( 'This controls the title which the user sees during checkout.', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 139 | 'default' => esc_html__( $this->default_title, 'globalpayments-gateway-provider-for-woocommerce' ), |
| 140 | 'desc_tip' => true, |
| 141 | 'custom_attributes' => array( 'required' => 'required' ), |
| 142 | ), |
| 143 | ), |
| 144 | $this->get_payment_method_form_fields(), |
| 145 | array( |
| 146 | 'payment_action' => array( |
| 147 | 'title' => __( 'Payment Action', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 148 | 'type' => 'select', |
| 149 | 'description' => __( 'Choose whether you wish to capture funds immediately or authorize payment only for a delayed capture.', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 150 | 'default' => AbstractGateway::TXN_TYPE_SALE, |
| 151 | 'desc_tip' => true, |
| 152 | 'options' => $this->get_payment_action_options(), |
| 153 | ), |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get supported payment actions at checkout. |
| 160 | * |
| 161 | * @return array |
| 162 | */ |
| 163 | public function get_payment_action_options() { |
| 164 | return array( |
| 165 | AbstractGateway::TXN_TYPE_SALE => __( 'Authorize + Capture', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 166 | AbstractGateway::TXN_TYPE_AUTHORIZE => __( 'Authorize only', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Sets the configurable merchant settings for use elsewhere in the class. |
| 172 | * |
| 173 | * @return |
| 174 | */ |
| 175 | public function configure_merchant_settings() { |
| 176 | $this->title = $this->get_option( 'title' ); |
| 177 | $this->enabled = $this->get_option( 'enabled' ); |
| 178 | $this->payment_action = $this->get_option( 'payment_action' ); |
| 179 | |
| 180 | foreach ( $this->get_payment_method_form_fields() as $key => $options ) { |
| 181 | if ( ! property_exists( $this, $key ) ) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | $value = $this->get_option( $key ); |
| 186 | if ( 'checkbox' === $options['type'] ) { |
| 187 | $value = 'yes' === $value; |
| 188 | } |
| 189 | |
| 190 | $this->{$key} = $value; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | *Add payment method specific hooks. |
| 196 | */ |
| 197 | public function add_hooks() { |
| 198 | add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( |
| 199 | $this, |
| 200 | 'process_admin_options' |
| 201 | ) ); |
| 202 | add_filter( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Enqueues payment method specific scripts. |
| 207 | * |
| 208 | * @return |
| 209 | */ |
| 210 | public function enqueue_payment_scripts() { |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | public function admin_enqueue_scripts( $hook_suffix ) { |
| 215 | if ( 'woocommerce_page_wc-settings' !== $hook_suffix || isset( $_GET['tab'] ) && 'checkout' !== $_GET['tab'] ) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | $section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : null; |
| 220 | if ( $this->id != $section ) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | wp_enqueue_script( |
| 225 | 'globalpayments-admin', |
| 226 | Plugin::get_url( '/assets/admin/js/globalpayments-admin.js' ), |
| 227 | array( |
| 228 | 'wp-i18n' // include 'wp-i18n' for translation |
| 229 | ), |
| 230 | WC()->version, |
| 231 | true |
| 232 | ); |
| 233 | |
| 234 | // set script translation, this will look in plugin languages directory and look for .json translation file |
| 235 | wp_set_script_translations('globalpayments-admin', 'globalpayments-gateway-provider-for-woocommerce', WP_PLUGIN_DIR . '/'. basename( dirname( __FILE__ , 3 ) ) . '/languages'); |
| 236 | |
| 237 | wp_localize_script( |
| 238 | 'globalpayments-admin', |
| 239 | 'globalpayments_admin_params', |
| 240 | array( |
| 241 | 'gateway_id' => $section, |
| 242 | ) |
| 243 | ); |
| 244 | wp_enqueue_style( |
| 245 | 'globalpayments-admin', |
| 246 | Plugin::get_url( '/assets/admin/css/globalpayments-admin.css' ), |
| 247 | array(), |
| 248 | WC()->version |
| 249 | ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @inheritdoc |
| 254 | */ |
| 255 | public function payment_fields() { |
| 256 | $this->enqueue_payment_scripts(); |
| 257 | ?> |
| 258 | |
| 259 | <div class="form-row form-row-wide globalpayments <?php echo esc_attr( $this->id ); ?>"> |
| 260 | <div id="<?php echo esc_attr( $this->id ); ?>"></div> |
| 261 | </div> |
| 262 | <div class="clear"></div> |
| 263 | |
| 264 | <?php |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @inheritdoc |
| 269 | */ |
| 270 | public function process_payment( $order_id ) { |
| 271 | $order = wc_get_order( $order_id ); |
| 272 | try { |
| 273 | $request = $this->gateway->prepare_request( |
| 274 | $this->get_request_type(), |
| 275 | $order |
| 276 | ); |
| 277 | $this->process_payment_before_gateway_request( $request, $order ); |
| 278 | $gateway_response = $this->gateway->client->submit_request( $request ); |
| 279 | $is_successful = $this->gateway->handle_response( $request, $gateway_response ); |
| 280 | $this->process_payment_after_gateway_response( $gateway_response, $is_successful, $order ); |
| 281 | |
| 282 | return array( |
| 283 | 'result' => $this->get_process_payment_result( $is_successful, $order ), |
| 284 | 'redirect' => $this->get_process_payment_redirect( $is_successful, $order ), |
| 285 | ); |
| 286 | } catch ( \Exception $e ) { |
| 287 | wc_get_logger()->error( $e->getMessage() ); |
| 288 | if ( $e instanceof GatewayException ) { |
| 289 | throw new \Exception( esc_html( Utils::map_response_code_to_friendly_message() ) ); |
| 290 | } |
| 291 | throw new \Exception( esc_html( $e->getMessage() ) ); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @inheritdoc |
| 297 | */ |
| 298 | public function process_refund( $order_id, $amount = null, $reason = '' ) { |
| 299 | return $this->gateway->process_refund( $order_id, $amount, $reason ); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get payment source |
| 304 | */ |
| 305 | public function get_payment_source() { |
| 306 | return $this->payment_source; |
| 307 | } |
| 308 | |
| 309 | protected function get_process_payment_result( $is_successful, $order ) { |
| 310 | return $is_successful ? 'success' : 'failure'; |
| 311 | } |
| 312 | |
| 313 | protected function get_process_payment_redirect( $is_successful, $order ) { |
| 314 | return $is_successful ? $this->get_return_url( $order ) : false; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * init gateway. |
| 319 | */ |
| 320 | protected function init_gateway() { |
| 321 | $this->gateway = new GpApiGateway( true ); |
| 322 | } |
| 323 | } |
| 324 |