Completed
Pull Request — master (#1285)
by
unknown
01:38
created

WC_Gateway_Stripe_Checkout::__construct()   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 40
nop 0
dl 0
loc 31
rs 8.4906
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Class that handles Stripe (New) Checkout payment method.
8
 *
9
 * @extends WC_Gateway_Stripe
10
 *
11
 * @since 4.6.0
12
 */
13
class WC_Gateway_Stripe_Checkout extends WC_Stripe_Payment_Gateway {
14
	/**
15
	 * Is test mode active?
16
	 *
17
	 * @var bool
18
	 */
19
	public $testmode;
20
21
	/**
22
	 * API access secret key
23
	 *
24
	 * @var string
25
	 */
26
	public $secret_key;
27
28
	/**
29
	 * Constructor
30
	 */
31
	public function __construct() {
32
		$this->id           = 'stripe_checkout';
33
		$this->method_title = __( 'Stripe Checkout', 'woocommerce-gateway-stripe' );
34
		/* translators: link */
35
		$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' ) );
36
		$this->supports           = array(
37
			'products',
38
			'refunds',
39
		);
40
41
		// Load the form fields.
42
		$this->init_form_fields();
43
44
		// Load the settings.
45
		$this->init_settings();
46
47
		$main_settings              = get_option( 'woocommerce_stripe_settings' );
48
		$this->title                = $this->get_option( 'title' );
49
		$this->description          = $this->get_option( 'description' );
50
		$this->enabled              = $this->get_option( 'enabled' );
51
		$this->testmode             = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
52
		$this->secret_key           = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
53
54
		if ( $this->testmode ) {
55
			$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
56
			$this->secret_key      = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
57
		}
58
59
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
60
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
61
	}
62
63
	/**
64
	 * Returns all supported currencies for this payment method.
65
	 *
66
	 * @since 4.6.0
67
	 * @return array
68
	 */
69
	public function get_supported_currency() {
70
		return apply_filters(
71
			'wc_stripe_checkout_supported_currencies',
72
			array(
73
				'USD',
74
				'EUR',
75
			)
76
		);
77
	}
78
79
	/**
80
	 * Checks to see if all criteria is met before showing payment method.
81
	 *
82
	 * @since 4.6.0
83
	 * @return bool
84
	 */
85
	public function is_available() {
86
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
87
			return false;
88
		}
89
90
		return parent::is_available();
91
	}
92
93
	/**
94
	 * Initialize Gateway Settings Form Fields.
95
	 *
96
	 * @since 4.6.0
97
	 */
98
	public function init_form_fields() {
99
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-checkout-settings.php' );
100
	}
101
102
	public function payment_scripts() {
103
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
104
			return;
105
		}
106
107
		wp_enqueue_style( 'stripe_styles' );
108
		wp_enqueue_script( 'woocommerce_stripe' );
109
	}
110
111
	/**
112
	 * Payment form on checkout page.
113
	 *
114
	 * @since 4.6.0
115
	 */
116
	public function payment_fields() {
117
		$description = $this->get_description();		
118
119
		// TODO: Handle paying from pay_for_order.
120
121
		// Request session from stripe.
122
		$stripe_session = new WC_Stripe_Session();
123
		$stripe_session->create_session(
124
			[ 'success_url' => $this->get_return_url() ]
125
		);
126
127
		echo '<div id="stripe-checkout-data" data-secret="' . esc_attr( $stripe_session->get_id() ) . '"></div>';
128
129
		if ( $description ) {
130
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
131
		}
132
	}
133
}
134