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

Stripe   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
A process_payment() 0 34 3
1
<?php
2
/**
3
 * Gestion 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 Class.
21
 */
22
class Stripe extends \eoxia\Singleton_Util {
23
	/**
24
	 * Constructeur.
25
	 *
26
	 * @since 2.0.0
27
	 */
28
	protected function construct() {}
29
30
	/**
31
	 * Prépares la paiement stripe.
32
	 *
33
	 * @since 2.0.0
34
	 *
35
	 * @param Order $order Les données de la commande.
36
	 *
37
	 * @return array       L'ID de la session Stripe.
38
	 */
39
	public function process_payment( $order ) {
40
		$stripe_options = Payment::g()->get_payment_option( 'stripe' );
41
42
		\Stripe\Stripe::setApiKey( $stripe_options['secret_key'] );
43
		\Stripe\Stripe::setApiVersion( '2019-03-14; checkout_sessions_beta=v1' );
44
45
		$lines = array();
46
47
		if ( ! empty( $order->data['lines'] ) ) {
48
			foreach ( $order->data['lines'] as $line ) {
49
				$lines[] = array(
50
					'amount'   => (int) $line['price_ttc'] * 100,
51
					'quantity' => $line['qty'],
52
					'name'     => $line['libelle'],
53
					'currency' => 'eur',
54
				);
55
			}
56
		}
57
58
		$session = \Stripe\Checkout\Session::create( array(
59
			'success_url'          => Pages::g()->get_valid_checkout_link() . '?order_id=' . $order->data['id'],
60
			'cancel_url'           => site_url(),
61
			'payment_method_types' => array( 'card' ),
62
			'line_items'           => array( $lines ),
63
		) );
64
65
		$order->data['external_data']['stripe_session_id'] = $session->id;
66
67
		Doli_Order::g()->update( $order->data );
68
69
		return array(
70
			'id' => $session->id,
71
		);
72
	}
73
}
74
75
Stripe::g();
76