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

API_Action   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A callback_rest_api_init() 0 11 1
A callback_wps_gateway_paypal() 0 12 2
A callback_wps_gateway_stripe() 0 19 2
1
<?php
2
/**
3
 * Gestion API.
4
 *
5
 * @author    Eoxia <[email protected]>
6
 * @copyright (c) 2011-2018 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
 * API Action Class.
21
 */
22
class API_Action {
23
24
	/**
25
	 * Constructeur
26
	 *
27
	 * @since 2.0.0
28
	 */
29
	public function __construct() {
30
		add_action( 'rest_api_init', array( $this, 'callback_rest_api_init' ) );
31
	}
32
33
	/**
34
	 * Ajoutes la route pour PayPal.
35
	 *
36
	 * @since 2.0.0
37
	 */
38
	public function callback_rest_api_init() {
39
		register_rest_route( 'wpshop/v2', '/wps_gateway_paypal', array(
40
			'methods'  => array( 'GET', 'POST' ),
41
			'callback' => array( $this, 'callback_wps_gateway_paypal' ),
42
		) );
43
44
		register_rest_route( 'wpshop/v2', '/wps_gateway_stripe', array(
45
			'methods'  => array( 'GET', 'POST' ),
46
			'callback' => array( $this, 'callback_wps_gateway_stripe' ),
47
		) );
48
	}
49
50
	/**
51
	 * Gestion de la route Paypal.
52
	 *
53
	 * @since 2.0.0
54
	 *
55
	 * @param  WP_Request $request L'objet contenant les informations de la
56
	 * requête.
57
	 */
58
	public function callback_wps_gateway_paypal( $request ) {
59
		$data   = $request->get_body_params();
60
		$txn_id = get_post_meta( $data['custom'], 'payment_txn_id', true );
61
62
		if ( $txn_id !== $data['txn_id'] ) {
63
			update_post_meta( $data['custom'], 'payment_data', $data );
64
			update_post_meta( $data['custom'], 'payment_txn_id', $data['txn_id'] );
65
			update_post_meta( $data['custom'], 'payment_method', 'paypal' );
66
67
			do_action( 'wps_gateway_paypal', $data );
68
		}
69
	}
70
71
	/**
72
	 * Gestion de la route Stripe.
73
	 *
74
	 * @since 2.0.0
75
	 *
76
	 * @param  WP_Request $request L'objet contenant les informations de la
77
	 * requête.
78
	 */
79
	public function callback_wps_gateway_stripe( $request ) {
80
		$param = json_decode( $request->get_body(), true );
81
82
		$order = Doli_Order::g()->get( array(
83
			'meta_key'     => '_external_data',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
84
			'meta_compare' => 'LIKE',
85
			'meta_value'   => $param['data']['object']['id'],
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
86
		), true );
87
88
		if ( ! empty( $order ) ) {
89
			update_post_meta( $order->data['id'], 'payment_data', $param );
90
			update_post_meta( $order->data['id'], 'payment_txn_id', $param['data']['object']['id'] );
91
			update_post_meta( $order->data['id'], 'payment_method', 'stripe' );
92
93
			$param['custom'] = $order->data['id'];
94
95
			do_action( 'wps_gateway_stripe', $param );
96
		}
97
	}
98
}
99
100
new API_Action();
101