1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13 14 15 16
17 abstract class WC_Payment_Gateway extends WC_Settings_API {
18
19
20 var $id;
21
22
23 var $order_button_text;
24
25
26 var $title;
27
28
29 var $chosen;
30
31
32 var $has_fields;
33
34
35 var $countries;
36
37
38 var $availability;
39
40
41 var $enabled;
42
43
44 var $icon;
45
46
47 var $description;
48
49
50 var $supports = array( 'products' );
51
52
53 public $max_amount = 0;
54
55 56 57 58 59 60 61
62 public function get_return_url( $order = '' ) {
63 if ( $order ) {
64 $return_url = $order->get_checkout_order_received_url();
65 } else {
66 $return_url = wc_get_endpoint_url( 'order-received', '', get_permalink( wc_get_page_id( 'checkout' ) ) );
67 }
68
69 if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' ) {
70 $return_url = str_replace( 'http:', 'https:', $return_url );
71 }
72
73 return apply_filters( 'woocommerce_get_return_url', $return_url );
74 }
75
76 77 78 79 80
81 protected function get_order_total() {
82 $total = 0;
83 $order_id = absint( get_query_var( 'order-pay' ) );
84
85
86 if ( 0 < $order_id ) {
87 $order = new WC_Order( $order_id );
88 $total = (float) $order->get_total();
89
90
91 } elseif ( 0 < WC()->cart->total ) {
92 $total = (float) WC()->cart->total;
93 }
94
95 return $total;
96 }
97
98 99 100 101 102 103
104 public function is_available() {
105 $is_available = ( 'yes' === $this->enabled ) ? true : false;
106
107 if ( WC()->cart && 0 < $this->get_order_total() && $this->max_amount >= $this->get_order_total() ) {
108 $is_available = false;
109 }
110
111 return $is_available;
112 }
113
114 115 116 117 118 119
120 public function has_fields() {
121 return $this->has_fields ? true : false;
122 }
123
124 125 126 127 128 129
130 public function get_title() {
131 return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id );
132 }
133
134 135 136 137 138 139
140 public function get_description() {
141 return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id );
142 }
143
144 145 146 147 148 149
150 public function get_icon() {
151
152 $icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
153
154 return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
155 }
156
157 158 159 160 161 162 163 164
165 public function set_current() {
166 $this->chosen = true;
167 }
168
169 170 171 172 173 174 175 176 177
178 public function process_payment( $order_id ) {}
179
180 181 182 183 184 185 186 187
188 public function validate_fields() { return true; }
189
190 191 192 193 194 195 196
197 public function payment_fields() {
198 if ( $description = $this->get_description() ) {
199 echo wpautop( wptexturize( $description ) );
200 }
201
202 if ( $this->supports( 'default_credit_card_form' ) ) {
203 $this->credit_card_form();
204 }
205 }
206
207 208 209 210 211 212 213 214 215 216 217
218 public function supports( $feature ) {
219 return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this );
220 }
221
222 223 224 225 226
227 public function credit_card_form( $args = array(), $fields = array() ) {
228 wp_enqueue_script( 'wc-credit-card-form' );
229
230 $default_args = array(
231 'fields_have_names' => true,
232 );
233
234 $args = wp_parse_args( $args, apply_filters( 'woocommerce_credit_card_form_args', $default_args, $this->id ) );
235
236 $default_fields = array(
237 'card-number-field' => '<p class="form-row form-row-wide">
238 <label for="' . esc_attr( $this->id ) . '-card-number">' . __( 'Card Number', 'woocommerce' ) . ' <span class="required">*</span></label>
239 <input id="' . esc_attr( $this->id ) . '-card-number" class="input-text wc-credit-card-form-card-number" type="text" maxlength="20" autocomplete="off" placeholder="•••• •••• •••• ••••" name="' . ( $args['fields_have_names'] ? $this->id . '-card-number' : '' ) . '" />
240 </p>',
241 'card-expiry-field' => '<p class="form-row form-row-first">
242 <label for="' . esc_attr( $this->id ) . '-card-expiry">' . __( 'Expiry (MM/YY)', 'woocommerce' ) . ' <span class="required">*</span></label>
243 <input id="' . esc_attr( $this->id ) . '-card-expiry" class="input-text wc-credit-card-form-card-expiry" type="text" autocomplete="off" placeholder="' . __( 'MM / YY', 'woocommerce' ) . '" name="' . ( $args['fields_have_names'] ? $this->id . '-card-expiry' : '' ) . '" />
244 </p>',
245 'card-cvc-field' => '<p class="form-row form-row-last">
246 <label for="' . esc_attr( $this->id ) . '-card-cvc">' . __( 'Card Code', 'woocommerce' ) . ' <span class="required">*</span></label>
247 <input id="' . esc_attr( $this->id ) . '-card-cvc" class="input-text wc-credit-card-form-card-cvc" type="text" autocomplete="off" placeholder="' . __( 'CVC', 'woocommerce' ) . '" name="' . ( $args['fields_have_names'] ? $this->id . '-card-cvc' : '' ) . '" />
248 </p>'
249 );
250
251 $fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) );
252 ?>
253 <fieldset id="<?php echo $this->id; ?>-cc-form">
254 <?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
255 <?php
256 foreach ( $fields as $field ) {
257 echo $field;
258 }
259 ?>
260 <?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
261 <div class="clear"></div>
262 </fieldset>
263 <?php
264 }
265 }
266