Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Stripe_Action::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gestion des actions de Stripe.
4
 *
5
 * @author    Eoxia <[email protected]>
6
 * @copyright (c) 2011-2019 Eoxia <[email protected]>.
7
 *
8
 * @license   AGPLv3 <https://spdx.org/licenses/AGPL-3.0-or-later.html>
9
 *
10
 * @package   WPshop\Classes
11
 *
12
 * @since     2.0.0
13
 */
14
15
namespace wpshop;
16
17
defined( 'ABSPATH' ) || exit;
18
19
/**
20
 * Stripe Action Class.
21
 */
22
class Stripe_Action {
23
	/**
24
	 * Constructeur.
25
	 *
26
	 * @since 2.0.0
27
	 */
28
	public function __construct() {
29
		add_action( 'wp_enqueue_scripts', array( $this, 'callback_enqueue_scripts' ), 9 );
30
31
		add_action( 'wps_setting_payment_method_stripe', array( $this, 'callback_setting_payment_method' ), 10, 0 );
32
		add_action( 'admin_post_wps_update_method_payment_stripe', array( $this, 'update_method_payment_stripe' ) );
33
34
		add_action( 'wps_gateway_stripe', array( $this, 'callback_wps_gateway_stripe' ), 10, 1 );
35
	}
36
37
	/**
38
	 * Inclus le JS de stripe.
39
	 *
40
	 * @since 2.0.0
41
	 *
42
	 * @todo: Inclusions que si on est sur la page de paieemnt.
43
	 */
44
	public function callback_enqueue_scripts() {
45
		wp_enqueue_script( 'wpshop-stripe', 'https://js.stripe.com/v3/', array(), \eoxia\Config_Util::$init['wpshop']->version );
46
	}
47
48
	/**
49
	 * Ajoutes la page pour configurer le paiement Stripe.
50
	 *
51
	 * @since 2.0.0
52
	 */
53
	public function callback_setting_payment_method() {
54
		$stripe_options = Payment::g()->get_payment_option( 'stripe' );
55
		\eoxia\View_Util::exec( 'wpshop', 'stripe', 'form-setting', array(
56
			'stripe_options' => $stripe_options,
57
		) );
58
	}
59
60
	/**
61
	 * Enregistres les configurations de Stripe en base de donnée.
62
	 *
63
	 * @since 2.0.0
64
	 */
65
	public function update_method_payment_stripe() {
66
		check_admin_referer( 'update_method_payment_stripe' );
67
68
		if ( ! current_user_can( 'manage_options' ) ) {
69
			wp_die();
70
		}
71
72
		$title              = ! empty( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
73
		$description        = ! empty( $_POST['description'] ) ? sanitize_text_field( $_POST['description'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
74
		$publish_key        = ! empty( $_POST['publish_key'] ) ? sanitize_text_field( $_POST['publish_key'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
75
		$secret_key         = ! empty( $_POST['secret_key'] ) ? sanitize_text_field( $_POST['secret_key'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
76
		$use_stripe_sandbox = ( isset( $_POST['use_stripe_sandbox'] ) && 'on' == $_POST['use_stripe_sandbox'] ) ? true : false;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
77
78
		$payment_methods_option = get_option( 'wps_payment_methods', Payment::g()->default_options );
79
80
		$payment_methods_option['stripe']['title']              = $title;
81
		$payment_methods_option['stripe']['description']        = $description;
82
		$payment_methods_option['stripe']['publish_key']        = $publish_key;
83
		$payment_methods_option['stripe']['secret_key']         = $secret_key;
84
		$payment_methods_option['stripe']['use_stripe_sandbox'] = $use_stripe_sandbox;
85
86
		update_option( 'wps_payment_methods', $payment_methods_option );
87
88
		set_transient( 'updated_wpshop_option_' . get_current_user_id(), __( 'Your settings have been saved.', 'wpshop' ), 30 );
89
90
		wp_redirect( admin_url( 'admin.php?page=wps-settings&tab=payment_method&section=stripe' ) );
91
	}
92
93
	/**
94
	 * Déclenches l'action pour compléter le paiement.
95
	 *
96
	 * @since 2.0.0
97
	 *
98
	 * @param  array $data Donnée reçu par Stripe.
99
	 */
100
	public function callback_wps_gateway_stripe( $data ) {
101
		do_action( 'wps_payment_complete', $data );
102
	}
103
}
104
105
new Stripe_Action();
106