BuyNowPayLater
3 years ago
DigitalWallets
3 years ago
AbstractPaymentMethod.php
3 years ago
PaymentMethodInterface.php
3 years ago
AbstractPaymentMethod.php
297 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 | public function __construct() { |
| 44 | $this->gateway = new GpApiGateway( true ); |
| 45 | $this->has_fields = true; |
| 46 | $this->supports = array( |
| 47 | 'refunds', |
| 48 | ); |
| 49 | |
| 50 | $this->configure_method_settings(); |
| 51 | $this->init_form_fields(); |
| 52 | $this->init_settings(); |
| 53 | $this->configure_merchant_settings(); |
| 54 | $this->add_hooks(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Sets the necessary WooCommerce payment method settings for exposing the |
| 59 | * payment method in the WooCommerce Admin. |
| 60 | * |
| 61 | * @return |
| 62 | */ |
| 63 | abstract public function configure_method_settings(); |
| 64 | |
| 65 | /** |
| 66 | * Custom admin options to configure the payment method specific credentials, features, etc. |
| 67 | * |
| 68 | * @return array |
| 69 | */ |
| 70 | abstract public function get_payment_method_form_fields(); |
| 71 | |
| 72 | /** |
| 73 | * Required options for proper client-side configuration. |
| 74 | * |
| 75 | * @return array |
| 76 | */ |
| 77 | abstract public function get_frontend_payment_method_options(); |
| 78 | |
| 79 | /** |
| 80 | * Request type. |
| 81 | * |
| 82 | * @return GlobalPayments\WooCommercePaymentGatewayProvider\Gateways\Requests\RequestInterface |
| 83 | */ |
| 84 | abstract public function get_request_type(); |
| 85 | |
| 86 | /** |
| 87 | * Should be overwritten to provide additional functionality before payment gateway request. |
| 88 | * |
| 89 | * @param $request |
| 90 | * @param $order |
| 91 | */ |
| 92 | public function process_payment_before_gateway_request( &$request, $order ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Should be overwritten to provide additional functionality after payment gateway response is received. |
| 98 | * |
| 99 | * @param $gateway_response |
| 100 | * @param $is_successful |
| 101 | * @param $order |
| 102 | */ |
| 103 | public function process_payment_after_gateway_response( Transaction $gateway_response, $is_successful, \WC_Order $order ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Email address of the first-line support team. |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function get_first_line_support_email() { |
| 113 | return $this->gateway->get_first_line_support_email(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @inheritdoc |
| 118 | */ |
| 119 | public function init_form_fields() { |
| 120 | $this->form_fields = array_merge( |
| 121 | array( |
| 122 | 'enabled' => array( |
| 123 | 'title' => __( 'Enable/Disable', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 124 | 'type' => 'checkbox', |
| 125 | 'label' => __( 'Enable payment method', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 126 | 'default' => 'no', |
| 127 | ), |
| 128 | 'title' => array( |
| 129 | 'title' => __( 'Title', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 130 | 'type' => 'text', |
| 131 | 'description' => __( 'This controls the title which the user sees during checkout.', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 132 | 'default' => __( $this->default_title, 'globalpayments-gateway-provider-for-woocommerce' ), |
| 133 | 'desc_tip' => true, |
| 134 | 'custom_attributes' => array( 'required' => 'required' ), |
| 135 | ), |
| 136 | ), |
| 137 | $this->get_payment_method_form_fields(), |
| 138 | array( |
| 139 | 'payment_action' => array( |
| 140 | 'title' => __( 'Payment Action', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 141 | 'type' => 'select', |
| 142 | 'description' => __( 'Choose whether you wish to capture funds immediately or authorize payment only for a delayed capture.', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 143 | 'default' => AbstractGateway::TXN_TYPE_SALE, |
| 144 | 'desc_tip' => true, |
| 145 | 'options' => $this->get_payment_action_options(), |
| 146 | ), |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get supported payment actions at checkout. |
| 153 | * |
| 154 | * @return array |
| 155 | */ |
| 156 | public function get_payment_action_options() { |
| 157 | return array( |
| 158 | AbstractGateway::TXN_TYPE_SALE => __( 'Authorize + Capture', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 159 | AbstractGateway::TXN_TYPE_AUTHORIZE => __( 'Authorize only', 'globalpayments-gateway-provider-for-woocommerce' ), |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the configurable merchant settings for use elsewhere in the class. |
| 165 | * |
| 166 | * @return |
| 167 | */ |
| 168 | public function configure_merchant_settings() { |
| 169 | $this->title = $this->get_option( 'title' ); |
| 170 | $this->enabled = $this->get_option( 'enabled' ); |
| 171 | $this->payment_action = $this->get_option( 'payment_action' ); |
| 172 | |
| 173 | foreach ( $this->get_payment_method_form_fields() as $key => $options ) { |
| 174 | if ( ! property_exists( $this, $key ) ) { |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | $value = $this->get_option( $key ); |
| 179 | if ( 'checkbox' === $options['type'] ) { |
| 180 | $value = 'yes' === $value; |
| 181 | } |
| 182 | |
| 183 | $this->{$key} = $value; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | *Add payment method specific hooks. |
| 189 | */ |
| 190 | public function add_hooks() { |
| 191 | add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( |
| 192 | $this, |
| 193 | 'process_admin_options' |
| 194 | ) ); |
| 195 | add_filter( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Enqueues payment method specific scripts. |
| 200 | * |
| 201 | * @return |
| 202 | */ |
| 203 | public function enqueue_payment_scripts() { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | public function admin_enqueue_scripts( $hook_suffix ) { |
| 208 | if ( 'woocommerce_page_wc-settings' !== $hook_suffix || isset( $_GET['tab'] ) && 'checkout' !== $_GET['tab'] ) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : null; |
| 213 | if ( $this->id != $section ) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | wp_enqueue_script( |
| 218 | 'globalpayments-admin', |
| 219 | Plugin::get_url( '/assets/admin/js/globalpayments-admin.js' ), |
| 220 | array(), |
| 221 | WC()->version, |
| 222 | true |
| 223 | ); |
| 224 | wp_localize_script( |
| 225 | 'globalpayments-admin', |
| 226 | 'globalpayments_admin_params', |
| 227 | array( |
| 228 | 'gateway_id' => $section, |
| 229 | ) |
| 230 | ); |
| 231 | wp_enqueue_style( |
| 232 | 'globalpayments-admin', |
| 233 | Plugin::get_url( '/assets/admin/css/globalpayments-admin.css' ), |
| 234 | array(), |
| 235 | WC()->version |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @inheritdoc |
| 241 | */ |
| 242 | public function payment_fields() { |
| 243 | $this->enqueue_payment_scripts(); |
| 244 | ?> |
| 245 | |
| 246 | <div class="form-row form-row-wide globalpayments <?php echo esc_attr( $this->id ); ?>"> |
| 247 | <div id="<?php echo esc_attr( $this->id ); ?>"></div> |
| 248 | </div> |
| 249 | <div class="clear"></div> |
| 250 | |
| 251 | <?php |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @inheritdoc |
| 256 | */ |
| 257 | public function process_payment( $order_id ) { |
| 258 | $order = wc_get_order( $order_id ); |
| 259 | try { |
| 260 | $request = $this->gateway->prepare_request( |
| 261 | $this->get_request_type(), |
| 262 | $order |
| 263 | ); |
| 264 | $this->process_payment_before_gateway_request( $request, $order ); |
| 265 | $gateway_response = $this->gateway->client->submit_request( $request ); |
| 266 | $is_successful = $this->gateway->handle_response( $request, $gateway_response ); |
| 267 | $this->process_payment_after_gateway_response( $gateway_response, $is_successful, $order ); |
| 268 | |
| 269 | return array( |
| 270 | 'result' => $this->get_process_payment_result( $is_successful, $order ), |
| 271 | 'redirect' => $this->get_process_payment_redirect( $is_successful, $order ), |
| 272 | ); |
| 273 | } catch ( \Exception $e ) { |
| 274 | wc_get_logger()->error( $e->getMessage() ); |
| 275 | if ( $e instanceof GatewayException ) { |
| 276 | throw new \Exception( Utils::map_response_code_to_friendly_message() ); |
| 277 | } |
| 278 | throw new \Exception( $e->getMessage() ); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @inheritdoc |
| 284 | */ |
| 285 | public function process_refund( $order_id, $amount = null, $reason = '' ) { |
| 286 | return $this->gateway->process_refund( $order_id, $amount, $reason ); |
| 287 | } |
| 288 | |
| 289 | protected function get_process_payment_result( $is_successful, $order ) { |
| 290 | return $is_successful ? 'success' : 'failure'; |
| 291 | } |
| 292 | |
| 293 | protected function get_process_payment_redirect( $is_successful, $order ) { |
| 294 | return $is_successful ? $this->get_return_url( $order ) : false; |
| 295 | } |
| 296 | } |
| 297 |