-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
PaymentContext.php
84 lines (75 loc) · 1.81 KB
/
PaymentContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Automattic\WooCommerce\StoreApi\Payments;
/**
* PaymentContext class.
*/
class PaymentContext {
/**
* Payment method ID.
*
* @var string
*/
protected $payment_method = '';
/**
* Order object for the order being paid.
*
* @var \WC_Order
*/
protected $order;
/**
* Holds data to send to the payment gateway to support payment.
*
* @var array Key value pairs.
*/
protected $payment_data = [];
/**
* Magic getter for protected properties.
*
* @param string $name Property name.
*/
public function __get( $name ) {
if ( in_array( $name, [ 'payment_method', 'order', 'payment_data' ], true ) ) {
return $this->$name;
}
return null;
}
/**
* Set the chosen payment method ID context.
*
* @param string $payment_method Payment method ID.
*/
public function set_payment_method( $payment_method ) {
$this->payment_method = (string) $payment_method;
}
/**
* Retrieve the payment method instance for the current set payment method.
*
* @return \WC_Payment_Gateway|null An instance of the payment gateway if it exists.
*/
public function get_payment_method_instance() {
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( ! isset( $available_gateways[ $this->payment_method ] ) ) {
return;
}
return $available_gateways[ $this->payment_method ];
}
/**
* Set the order context.
*
* @param \WC_Order $order Order object.
*/
public function set_order( \WC_Order $order ) {
$this->order = $order;
}
/**
* Set payment data context.
*
* @param array $payment_data Array of key value pairs of data.
*/
public function set_payment_data( $payment_data = [] ) {
$this->payment_data = [];
foreach ( $payment_data as $key => $value ) {
$this->payment_data[ (string) $key ] = (string) $value;
}
}
}