1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Processes items for a payment form submission. |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Payment form submission itemss class |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class GetPaid_Payment_Form_Submission_Items { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Submission items. |
17
|
|
|
* @var GetPaid_Form_Item[] |
18
|
|
|
*/ |
19
|
|
|
public $items = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class constructor |
23
|
|
|
* |
24
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
25
|
|
|
*/ |
26
|
|
|
public function __construct( $submission ) { |
27
|
|
|
|
28
|
|
|
$data = $submission->get_data(); |
29
|
|
|
$payment_form = $submission->get_payment_form(); |
30
|
|
|
|
31
|
|
|
// Prepare the selected items. |
32
|
|
|
$selected_items = array(); |
33
|
|
|
if ( ! empty( $data['getpaid-items'] ) ) { |
34
|
|
|
$selected_items = wpinv_clean( $data['getpaid-items'] ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// (Maybe) set form items. |
38
|
|
|
if ( isset( $data['getpaid-form-items'] ) ) { |
39
|
|
|
|
40
|
|
|
// Confirm items key. |
41
|
|
|
$form_items = wpinv_clean( $data['getpaid-form-items'] ); |
42
|
|
|
if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) { |
|
|
|
|
43
|
|
|
throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$items = array(); |
47
|
|
|
$item_ids = array(); |
48
|
|
|
|
49
|
|
|
foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
50
|
|
|
if ( ! in_array( $item_id, $item_ids ) ) { |
51
|
|
|
$item = new GetPaid_Form_Item( $item_id ); |
52
|
|
|
$item->set_quantity( $qty ); |
53
|
|
|
|
54
|
|
|
if ( 0 == $qty ) { |
55
|
|
|
$item->set_allow_quantities( true ); |
56
|
|
|
$item->set_is_required( false ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$item_ids[] = $item->get_id(); |
60
|
|
|
$items[] = $item; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ( ! $payment_form->is_default() ) { |
65
|
|
|
|
66
|
|
|
foreach ( $payment_form->get_items() as $item ) { |
67
|
|
|
if ( ! in_array( $item->get_id(), $item_ids ) ) { |
68
|
|
|
$item_ids[] = $item->get_id(); |
69
|
|
|
$items[] = $item; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$payment_form->set_items( $items ); |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Process each individual item. |
80
|
|
|
foreach ( $payment_form->get_items() as $item ) { |
81
|
|
|
$this->process_item( $item, $selected_items, $submission ); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Process a single item. |
88
|
|
|
* |
89
|
|
|
* @param GetPaid_Form_Item $item |
90
|
|
|
* @param array $selected_items |
91
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
92
|
|
|
*/ |
93
|
|
|
public function process_item( $item, $selected_items, $submission ) { |
94
|
|
|
|
95
|
|
|
// Abort if this is an optional item and it has not been selected. |
96
|
|
|
if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// (maybe) let customers change the quantities and prices. |
101
|
|
|
if ( isset( $selected_items[ $item->get_id() ] ) ) { |
102
|
|
|
|
103
|
|
|
// Maybe change the quantities. |
104
|
|
|
if ( $item->allows_quantities() ) { |
105
|
|
|
$item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Maybe change the price. |
109
|
|
|
if ( $item->user_can_set_their_price() ) { |
110
|
|
|
$price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
111
|
|
|
|
112
|
|
|
if ( $item->get_minimum_price() > $price ) { |
113
|
|
|
throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$item->set_price( $price ); |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ( 0 == $item->get_quantity() ) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Save the item. |
127
|
|
|
$this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item' , $item, $submission ); |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|