· 5 years ago · Aug 18, 2020, 03:24 PM
1<?php
2
3namespace Test_Payment;
4
5defined( 'ABSPATH' ) || exit;
6
7final class PaymentGateway {
8
9 // The single instance of the class
10 protected static $instance = null;
11
12 // Singleton instance
13 public static function instance() {
14 if ( is_null( self::$instance ) ) {
15 self::$instance = new self();
16 }
17 return self::$instance;
18 }
19
20 // Constructor
21 private function __construct() {
22 $this->init();
23 }
24
25 // Called when the plugin is initialized
26 public function init() {
27 $this->init_filters();
28 $this->init_hooks();
29 }
30
31 private function init_filters() {
32 // Add our payment gateway to the store
33 add_filter( 'woocommerce_payment_gateways', array( $this, 'moneris_add_gateway_class' ) );
34 }
35
36 private function init_hooks() {
37 add_action( 'plugins_loaded', 'moneris_init_gateway_class' );
38 }
39
40 /**
41 * Registers our PHP class as a WooCommerce payment gateway
42 */
43 function moneris_add_gateway_class ( $gateways ) {
44 $gateways[] = 'WC_Moneris_Gateway';
45 return $gateways;
46 }
47}
48
49/**
50* Method to load the payment gateway
51*/
52function moneris_init_gateway_class () {
53 // The actual payment gateway class
54 class WC_Moneris_Gateway extends WC_Payment_Gateway {
55
56 // Construct a new instance of the payment gateway
57 public function __construct () {
58 $this->id = 'moneris'; // payment gateway plugin ID
59 $this->icon = ''; // URL of the icon that will be displayed on checkout page near our gateway name
60 $this->has_fields = false; // We are usign hosted checkout fields
61 $this->method_title = 'Moneris Gateway';
62 $this->method_description = 'Moneris Payment Gateway'; // will be displayed on the options page
63
64 // gateways can support subscriptions, refunds, saved payment methods
65 $this->supports = array(
66 'products'
67 );
68
69 // Method with all the options fields
70 $this->init_form_fields();
71
72 // Load the settings.
73 $this->init_settings();
74 $this->title = $this->get_option( 'title' );
75 $this->description = $this->get_option( 'description' );
76 $this->enabled = $this->get_option( 'enabled' );
77 $this->store_id = $this->get_option( 'store_id' );
78 $this->hpp_key = $this->get_option( 'hpp_key' );
79 $this->api_token = $this->get_option( 'api_token' );
80
81 // This action hook saves the settings
82 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
83 // We need custom JavaScript to obtain a token
84 add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
85 }
86
87 // Add configuration fields to wp config
88 public function init_form_fields () {
89 $this->form_fields = array(
90 'enabled' => array(
91 'title' => 'Enable/Disable',
92 'label' => 'Enable Moneris HPP Gateway',
93 'type' => 'checkbox',
94 'description' => '',
95 'default' => 'no'
96 ),
97 'title' => array(
98 'title' => 'Title',
99 'type' => 'text',
100 'description' => 'This controls the title which the user sees during checkout.',
101 'default' => 'Credit Card',
102 'desc_tip' => true,
103 ),
104 'description' => array(
105 'title' => 'Description',
106 'type' => 'textarea',
107 'description' => 'This controls the description which the user sees during checkout.',
108 'default' => 'Continue to pay with credit card.',
109 ),
110 'store_id' => array(
111 'title' => 'ID of the Moneris store',
112 'type' => 'text',
113 'description' => 'The Moneris store ID'
114 ),
115 'hpp_key' => array(
116 'title' => 'Hosted Pay Page key',
117 'type' => 'password',
118 'description' => 'The HPP key for the moneris hosted pay page. Allows us to redirect to our actual storefront.'
119 ),
120 'api_token' => array(
121 'title' => 'Moneris API key',
122 'type' => 'password',
123 'description' => 'Moneris private API key to preform actions on our store.'
124 )
125 );
126 }
127
128 public function payment_fields() {
129 }
130
131 public function payment_scripts() {
132 }
133
134 public function validate_fields() {
135 }
136
137 public function process_payment( $order_id ) {
138 }
139
140 public function webhook() {
141 }
142 }
143}
144
145?>