Completed
Push — master ( 2515df...302e86 )
by Roy
02:08
created

WC_Gateway_Stripe_Bitcoin::get_icon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Class that handles Bitcoin payment method.
8
 *
9
 * @extends WC_Gateway_Stripe
10
 *
11
 * @since 4.0.0
12
 */
13
class WC_Gateway_Stripe_Bitcoin extends WC_Stripe_Payment_Gateway {
14
	/**
15
	 * Notices (array)
16
	 * @var array
17
	 */
18
	public $notices = array();
19
20
	/**
21
	 * Is test mode active?
22
	 *
23
	 * @var bool
24
	 */
25
	public $testmode;
26
27
	/**
28
	 * Alternate credit card statement name
29
	 *
30
	 * @var bool
31
	 */
32
	public $statement_descriptor;
33
34
	/**
35
	 * API access secret key
36
	 *
37
	 * @var string
38
	 */
39
	public $secret_key;
40
41
	/**
42
	 * Api access publishable key
43
	 *
44
	 * @var string
45
	 */
46
	public $publishable_key;
47
48
	/**
49
	 * Should we store the users credit cards?
50
	 *
51
	 * @var bool
52
	 */
53
	public $saved_cards;
54
55
	/**
56
	 * Instructions for Bitcoin payment.
57
	 *
58
	 * @var string
59
	 */
60
	public $instructions;
61
62
	/**
63
	 * Constructor
64
	 */
65
	public function __construct() {
66
		$this->id                   = 'stripe_bitcoin';
67
		$this->method_title         = __( 'Stripe Bitcoin', 'woocommerce-gateway-stripe' );
68
		/* translators: link */
69
		$this->method_description   = sprintf( __( 'All other general Stripe settings can be adjusted <a href="%s">here</a>.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) );
70
		$this->supports             = array(
71
			'products',
72
			'refunds',
73
		);
74
75
		// Load the form fields.
76
		$this->init_form_fields();
77
78
		// Load the settings.
79
		$this->init_settings();
80
81
		$main_settings              = get_option( 'woocommerce_stripe_settings' );
82
		$this->title                = $this->get_option( 'title' );
83
		$this->description          = $this->get_option( 'description' );
84
		$this->enabled              = $this->get_option( 'enabled' );
85
		$this->testmode             = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
86
		$this->saved_cards          = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
87
		$this->publishable_key      = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
88
		$this->secret_key           = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
89
		$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
90
91
		if ( $this->testmode ) {
92
			$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
93
			$this->secret_key      = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
94
		}
95
96
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
97
		add_action( 'admin_notices', array( $this, 'check_environment' ) );
98
		add_action( 'admin_head', array( $this, 'remove_admin_notice' ) );
99
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
100
		add_action( 'woocommerce_thankyou_stripe_bitcoin', array( $this, 'thankyou_page' ) );
101
102
		// Customer Emails
103
		add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
104
	}
105
106
	/**
107
	 * Checks to make sure environment is setup correctly to use this payment method.
108
	 *
109
	 * @since 4.0.0
110
	 * @version 4.0.0
111
	 */
112
	public function check_environment() {
113
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
114
			return;
115
		}
116
117
		$environment_warning = $this->get_environment_warning();
118
119
		if ( $environment_warning ) {
120
			$this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
121
		}
122
123
		foreach ( (array) $this->notices as $notice_key => $notice ) {
124
			echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
125
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
126
			echo '</p></div>';
127
		}
128
	}
129
130
	/**
131
	 * Checks the environment for compatibility problems. Returns a string with the first incompatibility
132
	 * found or false if the environment has no problems.
133
	 *
134
	 * @since 4.0.0
135
	 * @version 4.0.0
136
	 */
137
	public function get_environment_warning() {
138
		if ( 'yes' === $this->enabled && ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
139
			$message = __( 'Bitcoin is enabled - it requires store currency to be set to USD.', 'woocommerce-gateway-stripe' );
140
141
			return $message;
142
		}
143
144
		return false;
145
	}
146
147
	/**
148
	 * Returns all supported currencies for this payment method.
149
	 *
150
	 * @since 4.0.0
151
	 * @version 4.0.0
152
	 * @return array
153
	 */
154
	public function get_supported_currency() {
155
		return apply_filters( 'wc_stripe_bitcoin_supported_currencies', array(
156
			'USD',
157
		) );
158
	}
159
160
	/**
161
	 * Checks to see if all criteria is met before showing payment method.
162
	 *
163
	 * @since 4.0.0
164
	 * @version 4.0.0
165
	 * @return bool
166
	 */
167
	public function is_available() {
168
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
169
			return false;
170
		}
171
172
		return parent::is_available();
173
	}
174
175
	/**
176
	 * Get_icon function.
177
	 *
178
	 * @since 1.0.0
179
	 * @version 4.0.0
180
	 * @return string
181
	 */
182
	public function get_icon() {
183
		$icons = $this->payment_icons();
184
185
		$icons_str = '';
186
187
		$icons_str .= $icons['bitcoin'];
188
189
		return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
190
	}
191
192
	/**
193
	 * payment_scripts function.
194
	 *
195
	 * Outputs scripts used for stripe payment
196
	 *
197
	 * @access public
198
	 */
199
	public function payment_scripts() {
200
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
201
			return;
202
		}
203
204
		wp_enqueue_style( 'stripe_paymentfonts' );
205
		wp_enqueue_script( 'woocommerce_stripe' );
206
	}
207
208
	/**
209
	 * Initialize Gateway Settings Form Fields.
210
	 */
211
	public function init_form_fields() {
212
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-bitcoin-settings.php' );
213
	}
214
215
	/**
216
	 * Payment form on checkout page
217
	 */
218
	public function payment_fields() {
219
		$user                 = wp_get_current_user();
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
220
		$total                = WC()->cart->total;
221
222
		// If paying from order, we need to get total from order not cart.
223
		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
224
			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
225
			$total = $order->get_total();
226
		}
227
228
		if ( is_add_payment_method_page() ) {
229
			$pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
0 ignored issues
show
Unused Code introduced by
$pay_button_text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
230
			$total        = '';
231
		} else {
232
			$pay_button_text = '';
0 ignored issues
show
Unused Code introduced by
$pay_button_text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
233
		}
234
235
		echo '<div
236
			id="stripe-bitcoin-payment-data"
237
			data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
238
			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
239
240
		if ( $this->description ) {
241
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
242
		}
243
244
		echo '</div>';
245
	}
246
247
	/**
248
	 * Output for the order received page.
249
	 *
250
	 * @param int $order_id
251
	 */
252
	public function thankyou_page( $order_id ) {
253
		$this->get_instructions( $order_id );
254
	}
255
256
	/**
257
	 * Add content to the WC emails.
258
	 *
259
	 * @since 4.0.0
260
	 * @version 4.0.0
261
	 * @param WC_Order $order
262
	 * @param bool $sent_to_admin
263
	 * @param bool $plain_text
264
	 */
265
	public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
266
		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
267
268
		$payment_method = WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method();
269
270
		if ( ! $sent_to_admin && 'stripe_bitcoin' === $payment_method && $order->has_status( 'on-hold' ) ) {
271
			$this->get_instructions( $order_id, $plain_text );
272
		}
273
	}
274
275
	/**
276
	 * Gets the Bitcoin instructions for customer to pay.
277
	 *
278
	 * @since 4.0.0
279
	 * @version 4.0.0
280
	 * @param int $order_id
281
	 */
282
	public function get_instructions( $order_id, $plain_text = false ) {
283
		$data = get_post_meta( $order_id, '_stripe_bitcoin', true );
284
285
		if ( $plain_text ) {
286
			esc_html_e( 'Please pay the following:', 'woocommerce-gateway-stripe' ) . "\n\n";
287
			echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
288
			esc_html_e( 'Bitcoin Amount:', 'woocommerce-gateway-stripe' ) . "\n\n";
289
			echo $data['amount'] . "\n\n";
290
			echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
291
			esc_html_e( 'Receiver:', 'woocommerce-gateway-stripe' ) . "\n\n";
292
			echo $data['address'] . "\n\n";
293
			echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
294
			esc_html_e( 'URI:', 'woocommerce-gateway-stripe' ) . "\n\n";
295
			echo $data['uri'] . "\n\n";
296
		} else {
297
			?>
298
			<h3><?php esc_html_e( 'Please pay the following:', 'woocommerce-gateway-stripe' ); ?></h3>
299
			<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">
300
			<li class="woocommerce-order-overview__order order">
301
				<?php esc_html_e( 'Bitcoin Amount:', 'woocommerce-gateway-stripe' ); ?>
302
				<strong><?php echo $data['amount']; ?></strong>
303
			</li>
304
			<li class="woocommerce-order-overview__order order">
305
				<?php esc_html_e( 'Receiver:', 'woocommerce-gateway-stripe' ); ?>
306
				<strong><?php echo $data['address']; ?></strong>
307
			</li>
308
			<li class="woocommerce-order-overview__order order">
309
				<?php esc_html_e( 'URI:', 'woocommerce-gateway-stripe' ); ?>
310
				<strong>
311
				<?php
312
				/* translators: link */
313
				printf( __( '<a href="%s">Pay Bitcoin</a>', 'woocommerce-gateway-stripe' ), $data['uri'] );
314
				?>
315
				</strong>
316
			</li>
317
			</ul>
318
			<?php
319
		}
320
	}
321
322
	/**
323
	 * Saves Bitcoin information to the order meta for later use.
324
	 *
325
	 * @since 4.0.0
326
	 * @version 4.0.0
327
	 * @param object $order
328
	 * @param object $source_object
329
	 */
330
	public function save_instructions( $order, $source_object ) {
331
		$data = array(
332
			'amount'  => $source_object->bitcoin->amount,
333
			'address' => $source_object->bitcoin->address,
334
			'uri'     => $source_object->bitcoin->uri,
335
		);
336
337
		$order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
338
339
		update_post_meta( $order_id, '_stripe_bitcoin', $data );
340
	}
341
342
	/**
343
	 * Process the payment
344
	 *
345
	 * @param int  $order_id Reference.
346
	 * @param bool $retry Should we retry on fail.
347
	 * @param bool $force_save_source Force save the payment source.
348
	 *
349
	 * @throws Exception If payment will not be accepted.
350
	 *
351
	 * @return array|void
352
	 */
353
	public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
354
		try {
355
			$order = wc_get_order( $order_id );
356
			$source_object = ! empty( $_POST['stripe_source'] ) ? json_decode( stripslashes( $_POST['stripe_source'] ) ) : false;
357
358
			// This comes from the create account checkbox in the checkout page.
359
			$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
360
361
			if ( $create_account ) {
362
				$new_customer_id     = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
363
				$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
364
				$new_stripe_customer->create_customer();
365
			}
366
367
			$prepared_source = $this->prepare_source( get_current_user_id(), $force_save_source );
368
369
			if ( empty( $prepared_source->source ) ) {
370
				$error_msg = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' );
371
				throw new Exception( $error_msg );
372
			}
373
374
			// Store source to order meta.
375
			$this->save_source( $order, $prepared_source );
376
377
378
			// This will throw exception if not valid.
379
			$this->validate_minimum_order_amount( $order );
380
381
			$this->save_instructions( $order, $source_object );
382
383
			// Mark as on-hold (we're awaiting the payment)
384
			$order->update_status( 'on-hold', __( 'Awaiting Bitcoin payment', 'woocommerce-gateway-stripe' ) );
385
386
			wc_reduce_stock_levels( $order_id );
387
388
			// Remove cart
389
			WC()->cart->empty_cart();
390
391
			// Return thankyou redirect
392
			return array(
393
				'result'    => 'success',
394
				'redirect'  => $this->get_return_url( $order ),
395
			);
396
		} catch ( Exception $e ) {
397
			wc_add_notice( $e->getMessage(), 'error' );
398
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
399
400
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
401
402
			if ( $order->has_status( array( 'pending', 'failed' ) ) ) {
403
				$this->send_failed_order_email( $order_id );
404
			}
405
406
			return array(
407
				'result'   => 'fail',
408
				'redirect' => '',
409
			);
410
		}
411
	}
412
}
413