1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Generates the AJAX response for refreshing submission prices. |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Payment form submission refresh prices class |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class GetPaid_Payment_Form_Submission_Refresh_Prices { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Contains the response for refreshing prices. |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public $response = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class constructor |
23
|
|
|
* |
24
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
25
|
|
|
*/ |
26
|
|
|
public function __construct( $submission ) { |
27
|
|
|
|
28
|
|
|
$this->response = array( |
29
|
|
|
'submission_id' => $submission->id, |
30
|
|
|
'has_recurring' => $submission->has_recurring, |
31
|
|
|
'is_free' => $submission->should_collect_payment_details(), |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$this->add_totals( $submission ); |
35
|
|
|
$this->add_texts( $submission ); |
36
|
|
|
$this->add_items( $submission ); |
37
|
|
|
$this->add_fees( $submission ); |
38
|
|
|
$this->add_discounts( $submission ); |
39
|
|
|
$this->add_taxes( $submission ); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds totals to a response for submission refresh prices. |
45
|
|
|
* |
46
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
47
|
|
|
*/ |
48
|
|
|
public function add_totals( $submission ) { |
49
|
|
|
|
50
|
|
|
$this->response = array_merge( |
51
|
|
|
$this->response, |
52
|
|
|
array( |
53
|
|
|
|
54
|
|
|
'totals' => array( |
55
|
|
|
'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
56
|
|
|
'discount' => $submission->format_amount( $submission->get_discount() ), |
57
|
|
|
'fees' => $submission->format_amount( $submission->get_fee() ), |
58
|
|
|
'tax' => $submission->format_amount( $submission->get_tax() ), |
59
|
|
|
'total' => $submission->format_amount( $submission->get_total() ), |
60
|
|
|
), |
61
|
|
|
|
62
|
|
|
'recurring' => array( |
63
|
|
|
'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
64
|
|
|
'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
65
|
|
|
'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
66
|
|
|
'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
67
|
|
|
'total' => $submission->format_amount( $submission->get_recurring_total() ), |
68
|
|
|
), |
69
|
|
|
|
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds texts to a response for submission refresh prices. |
77
|
|
|
* |
78
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
79
|
|
|
*/ |
80
|
|
|
public function add_texts( $submission ) { |
81
|
|
|
|
82
|
|
|
$payable = $submission->format_amount( $submission->get_total() ); |
83
|
|
|
|
84
|
|
|
if ( $submission->has_recurring != 0 ) { |
85
|
|
|
|
86
|
|
|
$recurring = new WPInv_Item( $submission->has_recurring ); |
87
|
|
|
$period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
88
|
|
|
|
89
|
|
|
if ( $submission->get_total() == $submission->get_recurring_total() ) { |
90
|
|
|
$payable = "$payable / $period"; |
91
|
|
|
} else { |
92
|
|
|
$payable = sprintf( |
93
|
|
|
__( '%1$s (renews at %2$s / %3$s)'), |
94
|
|
|
$submission->format_amount( $submission->get_total() ), |
95
|
|
|
$submission->format_amount( $submission->get_recurring_total() ), |
96
|
|
|
$period |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$texts = array( |
103
|
|
|
'.getpaid-checkout-total-payable' => $payable, |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
foreach ( $submission->get_items() as $item_id => $item ) { |
107
|
|
|
$texts[".item-$item_id .getpaid-item-initial-price"] = $submission->format_amount( $item->get_sub_total() ); |
108
|
|
|
$texts[".item-$item_id .getpaid-item-recurring-price"] = $submission->format_amount( $item->get_recurring_sub_total() ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Adds items to a response for submission refresh prices. |
117
|
|
|
* |
118
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
119
|
|
|
*/ |
120
|
|
|
public function add_items( $submission ) { |
121
|
|
|
|
122
|
|
|
// Add items. |
123
|
|
|
$items = array(); |
124
|
|
|
|
125
|
|
|
foreach ( $submission->get_items() as $item_id => $item ) { |
126
|
|
|
$items["$item_id"] = $submission->format_amount( $item->get_sub_total() ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->response = array_merge( |
130
|
|
|
$this->response, |
131
|
|
|
array( 'items' => $items ) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Adds fees to a response for submission refresh prices. |
138
|
|
|
* |
139
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
140
|
|
|
*/ |
141
|
|
|
public function add_fees( $submission ) { |
142
|
|
|
|
143
|
|
|
$fees = array(); |
144
|
|
|
|
145
|
|
|
foreach ( $submission->get_fees() as $name => $data ) { |
146
|
|
|
$fees[$name] = $submission->format_amount( $data['initial_fee'] ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->response = array_merge( |
150
|
|
|
$this->response, |
151
|
|
|
array( 'fees' => $fees ) |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Adds discounts to a response for submission refresh prices. |
158
|
|
|
* |
159
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
160
|
|
|
*/ |
161
|
|
|
public function add_discounts( $submission ) { |
162
|
|
|
|
163
|
|
|
$discounts = array(); |
164
|
|
|
|
165
|
|
|
foreach ( $submission->get_discounts() as $name => $data ) { |
166
|
|
|
$discounts[$name] = $submission->format_amount( $data['initial_discount'] ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->response = array_merge( |
170
|
|
|
$this->response, |
171
|
|
|
array( 'discounts' => $discounts ) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Adds taxes to a response for submission refresh prices. |
178
|
|
|
* |
179
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
180
|
|
|
*/ |
181
|
|
|
public function add_taxes( $submission ) { |
182
|
|
|
|
183
|
|
|
$taxes = array(); |
184
|
|
|
|
185
|
|
|
foreach ( $submission->get_taxes() as $name => $data ) { |
186
|
|
|
$taxes[$name] = $submission->format_amount( $data['initial_tax'] ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->response = array_merge( |
190
|
|
|
$this->response, |
191
|
|
|
array( 'taxes' => $taxes ) |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|