PluginProbe ʕ •ᴥ•ʔ
GlobalPayments Gateway Provider for WooCommerce / 1.8.0
GlobalPayments Gateway Provider for WooCommerce v1.8.0
1.20.2 1.20.1 1.19.3 1.19.2 1.19.1 1.19.0 1.18.4 1.18.3 trunk 1.0.0 1.0.0-b2 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.6 1.10.7 1.10.8 1.11.0 1.12.0 1.12.1 1.13.0 1.13.1 1.13.2 1.13.3 1.13.4 1.13.7 1.13.8 1.14.0 1.14.1 1.14.2 1.14.3 1.14.4 1.14.5 1.14.6 1.14.7 1.14.8 1.14.9 1.15.0 1.15.2 1.15.4 1.15.5 1.15.6 1.15.8 1.15.9 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.17.0 1.17.1 1.18.0 1.18.1 1.18.2 1.2.0 1.2.1 1.2.2 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5
global-payments-woocommerce / src / PaymentMethods / AbstractPaymentMethod.php
global-payments-woocommerce / src / PaymentMethods Last commit date
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