forked from OnTap/gateway-woocommerce-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-mastercard.php
165 lines (142 loc) · 4.42 KB
/
woocommerce-mastercard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Plugin Name: Mastercard Payment Gateway Services
* Description: Accept payments on your WooCommerce store using Mastercard Payment Gateway Services.
* Plugin URI: https://github.com/Mastercard-Gateway/gateway-woocommerce-module/
* Author: OnTap Networks Ltd.
* Author URI: https://www.ontapgroup.com/
* Version: 1.1.0
*/
/**
* Copyright (c) 2019 Mastercard
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
class WC_Mastercard {
/**
* @var WC_Mastercard
*/
private static $instance;
/**
* WC_Mastercard constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
/**
* @return void
*/
public function init() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
define( 'MPGS_ISO3_COUNTRIES', include plugin_basename( '/iso3.php' ) );
require_once plugin_basename( '/vendor/autoload.php' );
require_once plugin_basename( '/includes/class-gateway.php' );
load_plugin_textdomain( 'mastercard', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) . 'i18n/' );
add_filter( 'woocommerce_order_actions', function ( $actions ) {
$order = new WC_Order( $_REQUEST['post'] );
if ( $order->get_payment_method() == Mastercard_Gateway::ID ) {
if ( ! $order->get_meta( '_mpgs_order_captured' ) ) {
if ( $order->get_status() == 'processing' ) {
$actions['mpgs_capture_order'] = __( 'Capture payment', 'mastercard' );
}
}
}
return $actions;
} );
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
add_action( 'rest_api_init', function () {
register_rest_route( 'mastercard/v1', '/checkoutSession/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => [ $this, 'rest_route_forward' ],
'args' => array(
'id' => array(
'validate_callback' => function ( $param, $request, $key ) {
return is_numeric( $param );
}
)
)
) );
register_rest_route( 'mastercard/v1', '/webhook', array(
'methods' => 'GET',
'callback' => [ $this, 'rest_route_forward' ],
) );
} );
}
/**
* @param WP_REST_Request $request
*
* @return array
* @throws Mastercard_GatewayResponseException
* @throws \Http\Client\Exception
*/
public function rest_route_forward( $request ) {
$gateway = new Mastercard_Gateway();
return $gateway->rest_route_processor( $request->get_route(), $request );
}
/**
* @param array $methods
*
* @return array
*/
public function add_gateways( $methods ) {
$methods[] = 'Mastercard_Gateway';
return $methods;
}
/**
* @return WC_Mastercard
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @return void
*/
public static function activation_hook() {
$environment_warning = self::get_env_warning();
if ( $environment_warning ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( $environment_warning );
}
}
/**
* @return bool
*/
public static function get_env_warning() {
// @todo: Add some php version and php library checks here
return false;
}
/**
* @param array $links
*
* @return array
*/
public function plugin_action_links( $links ) {
// todo: Add 'Support'
//
array_unshift( $links, '<a href="https://www.ontapgroup.com/uk/helpdesk/ticket/">' . __( 'Support', 'mastercard' ) . '</a>' );
array_unshift( $links, '<a href="http://wiki.ontapgroup.com/display/MPGS">' . __( 'Docs', 'mastercard' ) . '</a>' );
array_unshift( $links, '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=mpgs_gateway' ) . '">' . __( 'Settings', 'mastercard' ) . '</a>' );
return $links;
}
}
WC_Mastercard::get_instance();
register_activation_hook( __FILE__, array( 'WC_Mastercard', 'activation_hook' ) );