@@ -1,2 +1,2 @@ |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | # Silence is golden. |
3 | 3 | \ No newline at end of file |
@@ -1,2 +1,2 @@ |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | # Silence is golden. |
3 | 3 | \ No newline at end of file |
@@ -1,2 +1,2 @@ |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | # Silence is golden. |
3 | 3 | \ No newline at end of file |
@@ -1,2 +1,2 @@ |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | # Silence is golden. |
3 | 3 | \ No newline at end of file |
@@ -1,2 +1,2 @@ |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | # Silence is golden. |
3 | 3 | \ No newline at end of file |
@@ -1,2 +1,2 @@ |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | # Silence is golden. |
3 | 3 | \ No newline at end of file |
@@ -10,211 +10,211 @@ |
||
10 | 10 | */ |
11 | 11 | class GetPaid_PayPal_API { |
12 | 12 | |
13 | - /** |
|
14 | - * Retrieves the bearer token. |
|
15 | - * |
|
13 | + /** |
|
14 | + * Retrieves the bearer token. |
|
15 | + * |
|
16 | 16 | * @return string|\WP_Error |
17 | - */ |
|
18 | - public static function get_token( $mode = 'live' ) { |
|
17 | + */ |
|
18 | + public static function get_token( $mode = 'live' ) { |
|
19 | 19 | |
20 | - $token = get_transient( 'getpaid_paypal_' . $mode . '_token' ); |
|
20 | + $token = get_transient( 'getpaid_paypal_' . $mode . '_token' ); |
|
21 | 21 | |
22 | - if ( $token ) { |
|
23 | - return $token; |
|
24 | - } |
|
22 | + if ( $token ) { |
|
23 | + return $token; |
|
24 | + } |
|
25 | 25 | |
26 | - $client_id = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' ); |
|
27 | - $secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret_key' ) : wpinv_get_option( 'paypal_sandbox_secret_key' ); |
|
28 | - $url = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode ); |
|
26 | + $client_id = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' ); |
|
27 | + $secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret_key' ) : wpinv_get_option( 'paypal_sandbox_secret_key' ); |
|
28 | + $url = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode ); |
|
29 | 29 | |
30 | 30 | if ( empty( $client_id ) || empty( $secret_key ) ) { |
31 | 31 | return new \WP_Error( 'invalid_request', 'Missing client id or secret key.', array( 'status' => 400 ) ); |
32 | 32 | } |
33 | 33 | |
34 | - $args = array( |
|
35 | - 'method' => 'POST', |
|
36 | - 'timeout' => 30, |
|
37 | - 'headers' => array( |
|
38 | - 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
39 | - 'Accept' => 'application/json', |
|
40 | - 'Content-Type' => 'application/x-www-form-urlencoded', |
|
41 | - ), |
|
42 | - ); |
|
43 | - |
|
44 | - $response = self::response_or_error( wp_remote_post( $url, $args ) ); |
|
45 | - |
|
46 | - if ( is_wp_error( $response ) ) { |
|
47 | - return $response; |
|
48 | - } |
|
49 | - |
|
50 | - if ( ! isset( $response->access_token ) ) { |
|
51 | - return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) ); |
|
52 | - } |
|
53 | - |
|
54 | - set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 ); |
|
55 | - return $response->access_token; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Retrieves the PayPal API URL. |
|
60 | - * |
|
61 | - * @param string $endpoint |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public static function get_api_url( $endpoint = '', $mode = 'live' ) { |
|
65 | - $endpoint = ltrim( $endpoint, '/' ); |
|
66 | - return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Handles a post request. |
|
71 | - * |
|
72 | - * @param string $path The path to the endpoint. |
|
73 | - * @param mixed $data The data to send. |
|
74 | - * @param string $method The method to use. |
|
75 | - * |
|
76 | - * @return true|\WP_Error |
|
77 | - */ |
|
78 | - public static function post( $path, $data, $mode = 'live', $method = 'POST' ) { |
|
79 | - |
|
80 | - $access_token = self::get_token( $mode ); |
|
81 | - |
|
82 | - if ( is_wp_error( $access_token ) ) { |
|
83 | - return $access_token; |
|
84 | - } |
|
85 | - |
|
86 | - $url = self::get_api_url( $path, $mode ); |
|
87 | - $args = array( |
|
88 | - 'method' => $method, |
|
89 | - 'headers' => array( |
|
90 | - 'Authorization' => 'Bearer ' . $access_token, |
|
91 | - 'Content-Type' => 'application/json', |
|
92 | - ), |
|
93 | - 'body' => wp_json_encode( $data ), |
|
94 | - ); |
|
95 | - |
|
96 | - return self::response_or_error( wp_remote_post( $url, $args ) ); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Handles a get request. |
|
101 | - * |
|
102 | - * @param string $path The path to the endpoint. |
|
103 | - * @param string $method |
|
104 | - * @return object|\WP_Error |
|
105 | - */ |
|
106 | - public static function get( $path, $mode = 'live', $method = 'GET' ) { |
|
107 | - |
|
108 | - $access_token = self::get_token( $mode ); |
|
109 | - |
|
110 | - if ( is_wp_error( $access_token ) ) { |
|
111 | - return $access_token; |
|
112 | - } |
|
113 | - |
|
114 | - $url = self::get_api_url( $path, $mode ); |
|
115 | - $args = array( |
|
116 | - 'method' => $method, |
|
117 | - 'headers' => array( |
|
118 | - 'Authorization' => 'Bearer ' . $access_token, |
|
119 | - ), |
|
120 | - ); |
|
121 | - |
|
122 | - return self::response_or_error( wp_remote_get( $url, $args ) ); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns the response body |
|
127 | - * |
|
128 | - * @since 1.0.0 |
|
129 | - * @version 1.0.0 |
|
130 | - * @param \WP_Error|array $response |
|
131 | - * @return \WP_Error|object |
|
132 | - */ |
|
133 | - public static function response_or_error( $response ) { |
|
134 | - |
|
135 | - if ( is_wp_error( $response ) ) { |
|
136 | - return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) ); |
|
137 | - } |
|
138 | - |
|
139 | - if ( empty( $response['body'] ) ) { |
|
140 | - return true; |
|
141 | - } |
|
142 | - |
|
143 | - $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
144 | - |
|
145 | - if ( wp_remote_retrieve_response_code( $response ) > 299 ) { |
|
146 | - |
|
147 | - // Normal errors. |
|
148 | - if ( $response_body && isset( $response_body->message ) ) { |
|
149 | - $error_message = $response_body->message; |
|
150 | - |
|
151 | - // Identity errors. |
|
152 | - } elseif ( $response_body && isset( $response_body->error_description ) ) { |
|
153 | - $error_message = $response_body->error_description; |
|
154 | - return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) ); |
|
155 | - } else { |
|
156 | - $error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' ); |
|
157 | - } |
|
158 | - |
|
159 | - return new \WP_Error( 'paypal_error', $error_message ); |
|
160 | - } |
|
161 | - |
|
162 | - return $response_body; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Fetches an order. |
|
167 | - * |
|
168 | - * @since 1.0.0 |
|
169 | - * @version 1.0.0 |
|
170 | - * @param string $order_id |
|
171 | - * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get |
|
172 | - * @return \WP_Error|object |
|
173 | - */ |
|
174 | - public static function get_order( $order_id, $mode = 'live' ) { |
|
175 | - return self::get( '/v2/checkout/orders/' . $order_id, $mode ); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Fetches a subscription. |
|
180 | - * |
|
181 | - * @since 1.0.0 |
|
182 | - * @version 1.0.0 |
|
183 | - * @param string $subscription_id |
|
184 | - * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get |
|
185 | - * @return \WP_Error|object |
|
186 | - */ |
|
187 | - public static function get_subscription( $subscription_id, $mode = 'live' ) { |
|
188 | - return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode ); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Fetches a subscription's latest transactions (limits search to last one day). |
|
193 | - * |
|
194 | - * @since 1.0.0 |
|
195 | - * @version 1.0.0 |
|
196 | - * @param string $subscription_id |
|
197 | - * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions |
|
198 | - * @return \WP_Error|object |
|
199 | - */ |
|
200 | - public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) { |
|
201 | - $start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) ); |
|
202 | - $end_time = gmdate( 'Y-m-d\TH:i:s\Z' ); |
|
203 | - return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode ); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Refunds a capture. |
|
208 | - * |
|
209 | - * @since 1.0.0 |
|
210 | - * @version 1.0.0 |
|
211 | - * @param string $capture_id |
|
212 | - * @param array $args |
|
213 | - * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund |
|
214 | - * @return \WP_Error|object |
|
215 | - */ |
|
216 | - public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) { |
|
217 | - return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode ); |
|
218 | - } |
|
34 | + $args = array( |
|
35 | + 'method' => 'POST', |
|
36 | + 'timeout' => 30, |
|
37 | + 'headers' => array( |
|
38 | + 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
39 | + 'Accept' => 'application/json', |
|
40 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
|
41 | + ), |
|
42 | + ); |
|
43 | + |
|
44 | + $response = self::response_or_error( wp_remote_post( $url, $args ) ); |
|
45 | + |
|
46 | + if ( is_wp_error( $response ) ) { |
|
47 | + return $response; |
|
48 | + } |
|
49 | + |
|
50 | + if ( ! isset( $response->access_token ) ) { |
|
51 | + return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) ); |
|
52 | + } |
|
53 | + |
|
54 | + set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 ); |
|
55 | + return $response->access_token; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Retrieves the PayPal API URL. |
|
60 | + * |
|
61 | + * @param string $endpoint |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public static function get_api_url( $endpoint = '', $mode = 'live' ) { |
|
65 | + $endpoint = ltrim( $endpoint, '/' ); |
|
66 | + return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Handles a post request. |
|
71 | + * |
|
72 | + * @param string $path The path to the endpoint. |
|
73 | + * @param mixed $data The data to send. |
|
74 | + * @param string $method The method to use. |
|
75 | + * |
|
76 | + * @return true|\WP_Error |
|
77 | + */ |
|
78 | + public static function post( $path, $data, $mode = 'live', $method = 'POST' ) { |
|
79 | + |
|
80 | + $access_token = self::get_token( $mode ); |
|
81 | + |
|
82 | + if ( is_wp_error( $access_token ) ) { |
|
83 | + return $access_token; |
|
84 | + } |
|
85 | + |
|
86 | + $url = self::get_api_url( $path, $mode ); |
|
87 | + $args = array( |
|
88 | + 'method' => $method, |
|
89 | + 'headers' => array( |
|
90 | + 'Authorization' => 'Bearer ' . $access_token, |
|
91 | + 'Content-Type' => 'application/json', |
|
92 | + ), |
|
93 | + 'body' => wp_json_encode( $data ), |
|
94 | + ); |
|
95 | + |
|
96 | + return self::response_or_error( wp_remote_post( $url, $args ) ); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Handles a get request. |
|
101 | + * |
|
102 | + * @param string $path The path to the endpoint. |
|
103 | + * @param string $method |
|
104 | + * @return object|\WP_Error |
|
105 | + */ |
|
106 | + public static function get( $path, $mode = 'live', $method = 'GET' ) { |
|
107 | + |
|
108 | + $access_token = self::get_token( $mode ); |
|
109 | + |
|
110 | + if ( is_wp_error( $access_token ) ) { |
|
111 | + return $access_token; |
|
112 | + } |
|
113 | + |
|
114 | + $url = self::get_api_url( $path, $mode ); |
|
115 | + $args = array( |
|
116 | + 'method' => $method, |
|
117 | + 'headers' => array( |
|
118 | + 'Authorization' => 'Bearer ' . $access_token, |
|
119 | + ), |
|
120 | + ); |
|
121 | + |
|
122 | + return self::response_or_error( wp_remote_get( $url, $args ) ); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns the response body |
|
127 | + * |
|
128 | + * @since 1.0.0 |
|
129 | + * @version 1.0.0 |
|
130 | + * @param \WP_Error|array $response |
|
131 | + * @return \WP_Error|object |
|
132 | + */ |
|
133 | + public static function response_or_error( $response ) { |
|
134 | + |
|
135 | + if ( is_wp_error( $response ) ) { |
|
136 | + return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) ); |
|
137 | + } |
|
138 | + |
|
139 | + if ( empty( $response['body'] ) ) { |
|
140 | + return true; |
|
141 | + } |
|
142 | + |
|
143 | + $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
144 | + |
|
145 | + if ( wp_remote_retrieve_response_code( $response ) > 299 ) { |
|
146 | + |
|
147 | + // Normal errors. |
|
148 | + if ( $response_body && isset( $response_body->message ) ) { |
|
149 | + $error_message = $response_body->message; |
|
150 | + |
|
151 | + // Identity errors. |
|
152 | + } elseif ( $response_body && isset( $response_body->error_description ) ) { |
|
153 | + $error_message = $response_body->error_description; |
|
154 | + return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) ); |
|
155 | + } else { |
|
156 | + $error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' ); |
|
157 | + } |
|
158 | + |
|
159 | + return new \WP_Error( 'paypal_error', $error_message ); |
|
160 | + } |
|
161 | + |
|
162 | + return $response_body; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Fetches an order. |
|
167 | + * |
|
168 | + * @since 1.0.0 |
|
169 | + * @version 1.0.0 |
|
170 | + * @param string $order_id |
|
171 | + * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get |
|
172 | + * @return \WP_Error|object |
|
173 | + */ |
|
174 | + public static function get_order( $order_id, $mode = 'live' ) { |
|
175 | + return self::get( '/v2/checkout/orders/' . $order_id, $mode ); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Fetches a subscription. |
|
180 | + * |
|
181 | + * @since 1.0.0 |
|
182 | + * @version 1.0.0 |
|
183 | + * @param string $subscription_id |
|
184 | + * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get |
|
185 | + * @return \WP_Error|object |
|
186 | + */ |
|
187 | + public static function get_subscription( $subscription_id, $mode = 'live' ) { |
|
188 | + return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode ); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Fetches a subscription's latest transactions (limits search to last one day). |
|
193 | + * |
|
194 | + * @since 1.0.0 |
|
195 | + * @version 1.0.0 |
|
196 | + * @param string $subscription_id |
|
197 | + * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions |
|
198 | + * @return \WP_Error|object |
|
199 | + */ |
|
200 | + public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) { |
|
201 | + $start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) ); |
|
202 | + $end_time = gmdate( 'Y-m-d\TH:i:s\Z' ); |
|
203 | + return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode ); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Refunds a capture. |
|
208 | + * |
|
209 | + * @since 1.0.0 |
|
210 | + * @version 1.0.0 |
|
211 | + * @param string $capture_id |
|
212 | + * @param array $args |
|
213 | + * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund |
|
214 | + * @return \WP_Error|object |
|
215 | + */ |
|
216 | + public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) { |
|
217 | + return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode ); |
|
218 | + } |
|
219 | 219 | |
220 | 220 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | // Exit if accessed directly. |
4 | -defined( 'ABSPATH' ) || exit; |
|
4 | +defined('ABSPATH') || exit; |
|
5 | 5 | |
6 | 6 | /** |
7 | 7 | * PayPal API handler. |
@@ -15,43 +15,43 @@ discard block |
||
15 | 15 | * |
16 | 16 | * @return string|\WP_Error |
17 | 17 | */ |
18 | - public static function get_token( $mode = 'live' ) { |
|
18 | + public static function get_token($mode = 'live') { |
|
19 | 19 | |
20 | - $token = get_transient( 'getpaid_paypal_' . $mode . '_token' ); |
|
20 | + $token = get_transient('getpaid_paypal_' . $mode . '_token'); |
|
21 | 21 | |
22 | - if ( $token ) { |
|
22 | + if ($token) { |
|
23 | 23 | return $token; |
24 | 24 | } |
25 | 25 | |
26 | - $client_id = 'live' === $mode ? wpinv_get_option( 'paypal_client_id' ) : wpinv_get_option( 'paypal_sandbox_client_id' ); |
|
27 | - $secret_key = 'live' === $mode ? wpinv_get_option( 'paypal_secret_key' ) : wpinv_get_option( 'paypal_sandbox_secret_key' ); |
|
28 | - $url = self::get_api_url( 'v1/oauth2/token?grant_type=client_credentials', $mode ); |
|
26 | + $client_id = 'live' === $mode ? wpinv_get_option('paypal_client_id') : wpinv_get_option('paypal_sandbox_client_id'); |
|
27 | + $secret_key = 'live' === $mode ? wpinv_get_option('paypal_secret_key') : wpinv_get_option('paypal_sandbox_secret_key'); |
|
28 | + $url = self::get_api_url('v1/oauth2/token?grant_type=client_credentials', $mode); |
|
29 | 29 | |
30 | - if ( empty( $client_id ) || empty( $secret_key ) ) { |
|
31 | - return new \WP_Error( 'invalid_request', 'Missing client id or secret key.', array( 'status' => 400 ) ); |
|
30 | + if (empty($client_id) || empty($secret_key)) { |
|
31 | + return new \WP_Error('invalid_request', 'Missing client id or secret key.', array('status' => 400)); |
|
32 | 32 | } |
33 | 33 | |
34 | - $args = array( |
|
34 | + $args = array( |
|
35 | 35 | 'method' => 'POST', |
36 | 36 | 'timeout' => 30, |
37 | 37 | 'headers' => array( |
38 | - 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $secret_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
38 | + 'Authorization' => 'Basic ' . base64_encode($client_id . ':' . $secret_key), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
39 | 39 | 'Accept' => 'application/json', |
40 | 40 | 'Content-Type' => 'application/x-www-form-urlencoded', |
41 | 41 | ), |
42 | 42 | ); |
43 | 43 | |
44 | - $response = self::response_or_error( wp_remote_post( $url, $args ) ); |
|
44 | + $response = self::response_or_error(wp_remote_post($url, $args)); |
|
45 | 45 | |
46 | - if ( is_wp_error( $response ) ) { |
|
46 | + if (is_wp_error($response)) { |
|
47 | 47 | return $response; |
48 | 48 | } |
49 | 49 | |
50 | - if ( ! isset( $response->access_token ) ) { |
|
51 | - return new \WP_Error( 'invalid_request', 'Could not create token.', array( 'status' => 400 ) ); |
|
50 | + if (!isset($response->access_token)) { |
|
51 | + return new \WP_Error('invalid_request', 'Could not create token.', array('status' => 400)); |
|
52 | 52 | } |
53 | 53 | |
54 | - set_transient( 'getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600 ); |
|
54 | + set_transient('getpaid_paypal_' . $mode . '_token', $response->access_token, $response->expires_in - 600); |
|
55 | 55 | return $response->access_token; |
56 | 56 | } |
57 | 57 | |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | * @param string $endpoint |
62 | 62 | * @return string |
63 | 63 | */ |
64 | - public static function get_api_url( $endpoint = '', $mode = 'live' ) { |
|
65 | - $endpoint = ltrim( $endpoint, '/' ); |
|
64 | + public static function get_api_url($endpoint = '', $mode = 'live') { |
|
65 | + $endpoint = ltrim($endpoint, '/'); |
|
66 | 66 | return 'live' === $mode ? 'https://api-m.paypal.com/' . $endpoint : 'https://api-m.sandbox.paypal.com/' . $endpoint; |
67 | 67 | } |
68 | 68 | |
@@ -75,25 +75,25 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @return true|\WP_Error |
77 | 77 | */ |
78 | - public static function post( $path, $data, $mode = 'live', $method = 'POST' ) { |
|
78 | + public static function post($path, $data, $mode = 'live', $method = 'POST') { |
|
79 | 79 | |
80 | - $access_token = self::get_token( $mode ); |
|
80 | + $access_token = self::get_token($mode); |
|
81 | 81 | |
82 | - if ( is_wp_error( $access_token ) ) { |
|
82 | + if (is_wp_error($access_token)) { |
|
83 | 83 | return $access_token; |
84 | 84 | } |
85 | 85 | |
86 | - $url = self::get_api_url( $path, $mode ); |
|
86 | + $url = self::get_api_url($path, $mode); |
|
87 | 87 | $args = array( |
88 | 88 | 'method' => $method, |
89 | 89 | 'headers' => array( |
90 | 90 | 'Authorization' => 'Bearer ' . $access_token, |
91 | 91 | 'Content-Type' => 'application/json', |
92 | 92 | ), |
93 | - 'body' => wp_json_encode( $data ), |
|
93 | + 'body' => wp_json_encode($data), |
|
94 | 94 | ); |
95 | 95 | |
96 | - return self::response_or_error( wp_remote_post( $url, $args ) ); |
|
96 | + return self::response_or_error(wp_remote_post($url, $args)); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
@@ -103,15 +103,15 @@ discard block |
||
103 | 103 | * @param string $method |
104 | 104 | * @return object|\WP_Error |
105 | 105 | */ |
106 | - public static function get( $path, $mode = 'live', $method = 'GET' ) { |
|
106 | + public static function get($path, $mode = 'live', $method = 'GET') { |
|
107 | 107 | |
108 | - $access_token = self::get_token( $mode ); |
|
108 | + $access_token = self::get_token($mode); |
|
109 | 109 | |
110 | - if ( is_wp_error( $access_token ) ) { |
|
110 | + if (is_wp_error($access_token)) { |
|
111 | 111 | return $access_token; |
112 | 112 | } |
113 | 113 | |
114 | - $url = self::get_api_url( $path, $mode ); |
|
114 | + $url = self::get_api_url($path, $mode); |
|
115 | 115 | $args = array( |
116 | 116 | 'method' => $method, |
117 | 117 | 'headers' => array( |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | ), |
120 | 120 | ); |
121 | 121 | |
122 | - return self::response_or_error( wp_remote_get( $url, $args ) ); |
|
122 | + return self::response_or_error(wp_remote_get($url, $args)); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -130,33 +130,33 @@ discard block |
||
130 | 130 | * @param \WP_Error|array $response |
131 | 131 | * @return \WP_Error|object |
132 | 132 | */ |
133 | - public static function response_or_error( $response ) { |
|
133 | + public static function response_or_error($response) { |
|
134 | 134 | |
135 | - if ( is_wp_error( $response ) ) { |
|
136 | - return new \WP_Error( 'paypal_error', __( 'There was a problem connecting to the PayPal API endpoint.', 'invoicing' ) ); |
|
135 | + if (is_wp_error($response)) { |
|
136 | + return new \WP_Error('paypal_error', __('There was a problem connecting to the PayPal API endpoint.', 'invoicing')); |
|
137 | 137 | } |
138 | 138 | |
139 | - if ( empty( $response['body'] ) ) { |
|
139 | + if (empty($response['body'])) { |
|
140 | 140 | return true; |
141 | 141 | } |
142 | 142 | |
143 | - $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
143 | + $response_body = json_decode(wp_remote_retrieve_body($response)); |
|
144 | 144 | |
145 | - if ( wp_remote_retrieve_response_code( $response ) > 299 ) { |
|
145 | + if (wp_remote_retrieve_response_code($response) > 299) { |
|
146 | 146 | |
147 | 147 | // Normal errors. |
148 | - if ( $response_body && isset( $response_body->message ) ) { |
|
148 | + if ($response_body && isset($response_body->message)) { |
|
149 | 149 | $error_message = $response_body->message; |
150 | 150 | |
151 | 151 | // Identity errors. |
152 | - } elseif ( $response_body && isset( $response_body->error_description ) ) { |
|
152 | + } elseif ($response_body && isset($response_body->error_description)) { |
|
153 | 153 | $error_message = $response_body->error_description; |
154 | - return new \WP_Error( 'paypal_error', wp_kses_post( $response_body->error_description ) ); |
|
154 | + return new \WP_Error('paypal_error', wp_kses_post($response_body->error_description)); |
|
155 | 155 | } else { |
156 | - $error_message = __( 'There was an error connecting to the PayPal API endpoint.', 'invoicing' ); |
|
156 | + $error_message = __('There was an error connecting to the PayPal API endpoint.', 'invoicing'); |
|
157 | 157 | } |
158 | 158 | |
159 | - return new \WP_Error( 'paypal_error', $error_message ); |
|
159 | + return new \WP_Error('paypal_error', $error_message); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | return $response_body; |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | * @link https://developer.paypal.com/docs/api/orders/v2/#orders_get |
172 | 172 | * @return \WP_Error|object |
173 | 173 | */ |
174 | - public static function get_order( $order_id, $mode = 'live' ) { |
|
175 | - return self::get( '/v2/checkout/orders/' . $order_id, $mode ); |
|
174 | + public static function get_order($order_id, $mode = 'live') { |
|
175 | + return self::get('/v2/checkout/orders/' . $order_id, $mode); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | /** |
@@ -184,8 +184,8 @@ discard block |
||
184 | 184 | * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get |
185 | 185 | * @return \WP_Error|object |
186 | 186 | */ |
187 | - public static function get_subscription( $subscription_id, $mode = 'live' ) { |
|
188 | - return self::get( '/v1/billing/subscriptions/' . $subscription_id, $mode ); |
|
187 | + public static function get_subscription($subscription_id, $mode = 'live') { |
|
188 | + return self::get('/v1/billing/subscriptions/' . $subscription_id, $mode); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
@@ -197,10 +197,10 @@ discard block |
||
197 | 197 | * @link https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions |
198 | 198 | * @return \WP_Error|object |
199 | 199 | */ |
200 | - public static function get_subscription_transaction( $subscription_id, $mode = 'live' ) { |
|
201 | - $start_time = gmdate( 'Y-m-d\TH:i:s\Z', strtotime( '-1 day' ) ); |
|
202 | - $end_time = gmdate( 'Y-m-d\TH:i:s\Z' ); |
|
203 | - return self::get( "/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode ); |
|
200 | + public static function get_subscription_transaction($subscription_id, $mode = 'live') { |
|
201 | + $start_time = gmdate('Y-m-d\TH:i:s\Z', strtotime('-1 day')); |
|
202 | + $end_time = gmdate('Y-m-d\TH:i:s\Z'); |
|
203 | + return self::get("/v1/billing/subscriptions/$subscription_id/transactions?start_time=$start_time&end_time=$end_time", $mode); |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | /** |
@@ -213,8 +213,8 @@ discard block |
||
213 | 213 | * @link https://developer.paypal.com/docs/api/payments/v2/#captures_refund |
214 | 214 | * @return \WP_Error|object |
215 | 215 | */ |
216 | - public static function refund_capture( $capture_id, $args = array(), $mode = 'live' ) { |
|
217 | - return self::post( '/v2/payments/captures/' . $capture_id . '/refund', $args, $mode ); |
|
216 | + public static function refund_capture($capture_id, $args = array(), $mode = 'live') { |
|
217 | + return self::post('/v2/payments/captures/' . $capture_id . '/refund', $args, $mode); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | } |
@@ -13,199 +13,199 @@ |
||
13 | 13 | */ |
14 | 14 | class GetPaid_Tax { |
15 | 15 | |
16 | - /** |
|
17 | - * Calculates tax for a line item. |
|
18 | - * |
|
19 | - * @param float $price The price to calc tax on. |
|
20 | - * @param array $rates The rates to apply. |
|
21 | - * @param boolean $price_includes_tax Whether the passed price has taxes included. |
|
22 | - * @return array Array of tax name => tax amount. |
|
23 | - */ |
|
24 | - public static function calc_tax( $price, $rates, $price_includes_tax = false ) { |
|
25 | - |
|
26 | - if ( $price_includes_tax ) { |
|
27 | - $taxes = self::calc_inclusive_tax( $price, $rates ); |
|
28 | - } else { |
|
29 | - $taxes = self::calc_exclusive_tax( $price, $rates ); |
|
30 | - } |
|
31 | - |
|
32 | - return apply_filters( 'getpaid_calc_tax', $taxes, $price, $rates, $price_includes_tax ); |
|
33 | - |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Calc tax from inclusive price. |
|
38 | - * |
|
39 | - * @param float $price Price to calculate tax for. |
|
40 | - * @param array $rates Array of tax rates. |
|
41 | - * @return array |
|
42 | - */ |
|
43 | - public static function calc_inclusive_tax( $price, $rates ) { |
|
44 | - $taxes = array(); |
|
45 | - $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
46 | - |
|
47 | - // Add tax rates. |
|
48 | - $tax_rate = 1 + ( array_sum( $tax_rates ) / 100 ); |
|
49 | - |
|
50 | - foreach ( $tax_rates as $name => $rate ) { |
|
51 | - $the_rate = ( $rate / 100 ) / $tax_rate; |
|
52 | - $net_price = $price - ( $the_rate * $price ); |
|
53 | - $tax_amount = apply_filters( 'getpaid_price_inc_tax_amount', $price - $net_price, $name, $rate, $price ); |
|
54 | - $taxes[ $name ] = $tax_amount; |
|
55 | - } |
|
56 | - |
|
57 | - // Round all taxes to precision (4DP) before passing them back. |
|
58 | - $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
59 | - |
|
60 | - return $taxes; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Calc tax from exclusive price. |
|
65 | - * |
|
66 | - * @param float $price Price to calculate tax for. |
|
67 | - * @param array $rates Array of tax rates. |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - public static function calc_exclusive_tax( $price, $rates ) { |
|
71 | - $taxes = array(); |
|
72 | - $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
73 | - |
|
74 | - foreach ( $tax_rates as $name => $rate ) { |
|
75 | - |
|
76 | - $tax_amount = $price * ( $rate / 100 ); |
|
77 | - $taxes[ $name ] = apply_filters( 'getpaid_price_ex_tax_amount', $tax_amount, $name, $rate, $price ); |
|
78 | - |
|
79 | - } |
|
80 | - |
|
81 | - // Round all taxes to precision (4DP) before passing them back. |
|
82 | - $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
83 | - |
|
84 | - return $taxes; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Get's an array of all tax rates. |
|
89 | - * |
|
90 | - * @return array |
|
91 | - */ |
|
92 | - public static function get_all_tax_rates() { |
|
93 | - |
|
94 | - $rates = get_option( 'wpinv_tax_rates', array() ); |
|
95 | - |
|
96 | - return apply_filters( |
|
97 | - 'getpaid_get_all_tax_rates', |
|
98 | - array_filter( wpinv_parse_list( $rates ) ) |
|
99 | - ); |
|
100 | - |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Get's an array of default tax rates. |
|
105 | - * |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public static function get_default_tax_rates() { |
|
109 | - |
|
110 | - return apply_filters( |
|
111 | - 'getpaid_get_default_tax_rates', |
|
112 | - array( |
|
113 | - array( |
|
114 | - 'country' => wpinv_get_default_country(), |
|
115 | - 'state' => wpinv_get_default_state(), |
|
116 | - 'global' => true, |
|
117 | - 'rate' => wpinv_get_default_tax_rate(), |
|
118 | - 'name' => __( 'Tax', 'invoicing' ), |
|
119 | - ), |
|
120 | - ) |
|
121 | - ); |
|
122 | - |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Get's an array of all tax rules. |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public static function get_all_tax_rules() { |
|
131 | - |
|
132 | - $rules = get_option( |
|
133 | - 'wpinv_tax_rules', |
|
134 | - array( |
|
135 | - array( |
|
136 | - 'key' => 'physical', |
|
137 | - 'label' => __( 'Physical Item', 'invoicing' ), |
|
138 | - 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
139 | - 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
140 | - ), |
|
141 | - array( |
|
142 | - 'key' => 'digital', |
|
143 | - 'label' => __( 'Digital Item', 'invoicing' ), |
|
144 | - 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
145 | - 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
146 | - ), |
|
147 | - ) |
|
148 | - ); |
|
149 | - |
|
150 | - return apply_filters( |
|
151 | - 'getpaid_tax_rules', |
|
152 | - array_filter( array_values( wpinv_parse_list( $rules ) ) ) |
|
153 | - ); |
|
154 | - |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get's an array of tax rates for a given address. |
|
159 | - * |
|
160 | - * @param string $country |
|
161 | - * @param string $state |
|
162 | - * @return array |
|
163 | - */ |
|
164 | - public static function get_address_tax_rates( $country, $state ) { |
|
165 | - |
|
166 | - $all_tax_rates = self::get_all_tax_rates(); |
|
167 | - $matching_rates = array_merge( |
|
168 | - wp_list_filter( $all_tax_rates, array( 'country' => $country ) ), |
|
169 | - wp_list_filter( $all_tax_rates, array( 'country' => '' ) ) |
|
170 | - ); |
|
171 | - |
|
172 | - foreach ( $matching_rates as $i => $rate ) { |
|
173 | - |
|
174 | - $states = array_filter( wpinv_clean( explode( ',', strtolower( $rate['state'] ) ) ) ); |
|
175 | - if ( empty( $rate['global'] ) && ! in_array( strtolower( $state ), $states ) ) { |
|
176 | - unset( $matching_rates[ $i ] ); |
|
177 | - } |
|
16 | + /** |
|
17 | + * Calculates tax for a line item. |
|
18 | + * |
|
19 | + * @param float $price The price to calc tax on. |
|
20 | + * @param array $rates The rates to apply. |
|
21 | + * @param boolean $price_includes_tax Whether the passed price has taxes included. |
|
22 | + * @return array Array of tax name => tax amount. |
|
23 | + */ |
|
24 | + public static function calc_tax( $price, $rates, $price_includes_tax = false ) { |
|
25 | + |
|
26 | + if ( $price_includes_tax ) { |
|
27 | + $taxes = self::calc_inclusive_tax( $price, $rates ); |
|
28 | + } else { |
|
29 | + $taxes = self::calc_exclusive_tax( $price, $rates ); |
|
30 | + } |
|
31 | + |
|
32 | + return apply_filters( 'getpaid_calc_tax', $taxes, $price, $rates, $price_includes_tax ); |
|
33 | + |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Calc tax from inclusive price. |
|
38 | + * |
|
39 | + * @param float $price Price to calculate tax for. |
|
40 | + * @param array $rates Array of tax rates. |
|
41 | + * @return array |
|
42 | + */ |
|
43 | + public static function calc_inclusive_tax( $price, $rates ) { |
|
44 | + $taxes = array(); |
|
45 | + $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
46 | + |
|
47 | + // Add tax rates. |
|
48 | + $tax_rate = 1 + ( array_sum( $tax_rates ) / 100 ); |
|
49 | + |
|
50 | + foreach ( $tax_rates as $name => $rate ) { |
|
51 | + $the_rate = ( $rate / 100 ) / $tax_rate; |
|
52 | + $net_price = $price - ( $the_rate * $price ); |
|
53 | + $tax_amount = apply_filters( 'getpaid_price_inc_tax_amount', $price - $net_price, $name, $rate, $price ); |
|
54 | + $taxes[ $name ] = $tax_amount; |
|
55 | + } |
|
56 | + |
|
57 | + // Round all taxes to precision (4DP) before passing them back. |
|
58 | + $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
59 | + |
|
60 | + return $taxes; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Calc tax from exclusive price. |
|
65 | + * |
|
66 | + * @param float $price Price to calculate tax for. |
|
67 | + * @param array $rates Array of tax rates. |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + public static function calc_exclusive_tax( $price, $rates ) { |
|
71 | + $taxes = array(); |
|
72 | + $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
73 | + |
|
74 | + foreach ( $tax_rates as $name => $rate ) { |
|
75 | + |
|
76 | + $tax_amount = $price * ( $rate / 100 ); |
|
77 | + $taxes[ $name ] = apply_filters( 'getpaid_price_ex_tax_amount', $tax_amount, $name, $rate, $price ); |
|
78 | + |
|
79 | + } |
|
80 | + |
|
81 | + // Round all taxes to precision (4DP) before passing them back. |
|
82 | + $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
83 | + |
|
84 | + return $taxes; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Get's an array of all tax rates. |
|
89 | + * |
|
90 | + * @return array |
|
91 | + */ |
|
92 | + public static function get_all_tax_rates() { |
|
93 | + |
|
94 | + $rates = get_option( 'wpinv_tax_rates', array() ); |
|
95 | + |
|
96 | + return apply_filters( |
|
97 | + 'getpaid_get_all_tax_rates', |
|
98 | + array_filter( wpinv_parse_list( $rates ) ) |
|
99 | + ); |
|
100 | + |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Get's an array of default tax rates. |
|
105 | + * |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + public static function get_default_tax_rates() { |
|
109 | + |
|
110 | + return apply_filters( |
|
111 | + 'getpaid_get_default_tax_rates', |
|
112 | + array( |
|
113 | + array( |
|
114 | + 'country' => wpinv_get_default_country(), |
|
115 | + 'state' => wpinv_get_default_state(), |
|
116 | + 'global' => true, |
|
117 | + 'rate' => wpinv_get_default_tax_rate(), |
|
118 | + 'name' => __( 'Tax', 'invoicing' ), |
|
119 | + ), |
|
120 | + ) |
|
121 | + ); |
|
122 | + |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Get's an array of all tax rules. |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public static function get_all_tax_rules() { |
|
131 | + |
|
132 | + $rules = get_option( |
|
133 | + 'wpinv_tax_rules', |
|
134 | + array( |
|
135 | + array( |
|
136 | + 'key' => 'physical', |
|
137 | + 'label' => __( 'Physical Item', 'invoicing' ), |
|
138 | + 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
139 | + 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
140 | + ), |
|
141 | + array( |
|
142 | + 'key' => 'digital', |
|
143 | + 'label' => __( 'Digital Item', 'invoicing' ), |
|
144 | + 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
145 | + 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
146 | + ), |
|
147 | + ) |
|
148 | + ); |
|
149 | + |
|
150 | + return apply_filters( |
|
151 | + 'getpaid_tax_rules', |
|
152 | + array_filter( array_values( wpinv_parse_list( $rules ) ) ) |
|
153 | + ); |
|
154 | + |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get's an array of tax rates for a given address. |
|
159 | + * |
|
160 | + * @param string $country |
|
161 | + * @param string $state |
|
162 | + * @return array |
|
163 | + */ |
|
164 | + public static function get_address_tax_rates( $country, $state ) { |
|
165 | + |
|
166 | + $all_tax_rates = self::get_all_tax_rates(); |
|
167 | + $matching_rates = array_merge( |
|
168 | + wp_list_filter( $all_tax_rates, array( 'country' => $country ) ), |
|
169 | + wp_list_filter( $all_tax_rates, array( 'country' => '' ) ) |
|
170 | + ); |
|
171 | + |
|
172 | + foreach ( $matching_rates as $i => $rate ) { |
|
173 | + |
|
174 | + $states = array_filter( wpinv_clean( explode( ',', strtolower( $rate['state'] ) ) ) ); |
|
175 | + if ( empty( $rate['global'] ) && ! in_array( strtolower( $state ), $states ) ) { |
|
176 | + unset( $matching_rates[ $i ] ); |
|
177 | + } |
|
178 | 178 | } |
179 | 179 | |
180 | - return apply_filters( 'getpaid_get_address_tax_rates', $matching_rates, $country, $state ); |
|
181 | - |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Sums a set of taxes to form a single total. Result is rounded to precision. |
|
186 | - * |
|
187 | - * @param array $taxes Array of taxes. |
|
188 | - * @return float |
|
189 | - */ |
|
190 | - public static function get_tax_total( $taxes ) { |
|
191 | - return self::round( array_sum( $taxes ) ); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Round to precision. |
|
196 | - * |
|
197 | - * Filter example: to return rounding to .5 cents you'd use: |
|
198 | - * |
|
199 | - * function euro_5cent_rounding( $in ) { |
|
200 | - * return round( $in / 5, 2 ) * 5; |
|
201 | - * } |
|
202 | - * add_filter( 'getpaid_tax_round', 'euro_5cent_rounding' ); |
|
203 | - * |
|
204 | - * @param float|int $in Value to round. |
|
205 | - * @return float |
|
206 | - */ |
|
207 | - public static function round( $in ) { |
|
208 | - return apply_filters( 'getpaid_tax_round', round( $in, 4 ), $in ); |
|
209 | - } |
|
180 | + return apply_filters( 'getpaid_get_address_tax_rates', $matching_rates, $country, $state ); |
|
181 | + |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Sums a set of taxes to form a single total. Result is rounded to precision. |
|
186 | + * |
|
187 | + * @param array $taxes Array of taxes. |
|
188 | + * @return float |
|
189 | + */ |
|
190 | + public static function get_tax_total( $taxes ) { |
|
191 | + return self::round( array_sum( $taxes ) ); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Round to precision. |
|
196 | + * |
|
197 | + * Filter example: to return rounding to .5 cents you'd use: |
|
198 | + * |
|
199 | + * function euro_5cent_rounding( $in ) { |
|
200 | + * return round( $in / 5, 2 ) * 5; |
|
201 | + * } |
|
202 | + * add_filter( 'getpaid_tax_round', 'euro_5cent_rounding' ); |
|
203 | + * |
|
204 | + * @param float|int $in Value to round. |
|
205 | + * @return float |
|
206 | + */ |
|
207 | + public static function round( $in ) { |
|
208 | + return apply_filters( 'getpaid_tax_round', round( $in, 4 ), $in ); |
|
209 | + } |
|
210 | 210 | |
211 | 211 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | |
8 | -defined( 'ABSPATH' ) || exit; |
|
8 | +defined('ABSPATH') || exit; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * Class GetPaid_Tax |
@@ -21,15 +21,15 @@ discard block |
||
21 | 21 | * @param boolean $price_includes_tax Whether the passed price has taxes included. |
22 | 22 | * @return array Array of tax name => tax amount. |
23 | 23 | */ |
24 | - public static function calc_tax( $price, $rates, $price_includes_tax = false ) { |
|
24 | + public static function calc_tax($price, $rates, $price_includes_tax = false) { |
|
25 | 25 | |
26 | - if ( $price_includes_tax ) { |
|
27 | - $taxes = self::calc_inclusive_tax( $price, $rates ); |
|
26 | + if ($price_includes_tax) { |
|
27 | + $taxes = self::calc_inclusive_tax($price, $rates); |
|
28 | 28 | } else { |
29 | - $taxes = self::calc_exclusive_tax( $price, $rates ); |
|
29 | + $taxes = self::calc_exclusive_tax($price, $rates); |
|
30 | 30 | } |
31 | 31 | |
32 | - return apply_filters( 'getpaid_calc_tax', $taxes, $price, $rates, $price_includes_tax ); |
|
32 | + return apply_filters('getpaid_calc_tax', $taxes, $price, $rates, $price_includes_tax); |
|
33 | 33 | |
34 | 34 | } |
35 | 35 | |
@@ -40,22 +40,22 @@ discard block |
||
40 | 40 | * @param array $rates Array of tax rates. |
41 | 41 | * @return array |
42 | 42 | */ |
43 | - public static function calc_inclusive_tax( $price, $rates ) { |
|
43 | + public static function calc_inclusive_tax($price, $rates) { |
|
44 | 44 | $taxes = array(); |
45 | - $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
45 | + $tax_rates = wp_list_pluck($rates, 'rate', 'name'); |
|
46 | 46 | |
47 | 47 | // Add tax rates. |
48 | - $tax_rate = 1 + ( array_sum( $tax_rates ) / 100 ); |
|
48 | + $tax_rate = 1 + (array_sum($tax_rates) / 100); |
|
49 | 49 | |
50 | - foreach ( $tax_rates as $name => $rate ) { |
|
51 | - $the_rate = ( $rate / 100 ) / $tax_rate; |
|
52 | - $net_price = $price - ( $the_rate * $price ); |
|
53 | - $tax_amount = apply_filters( 'getpaid_price_inc_tax_amount', $price - $net_price, $name, $rate, $price ); |
|
54 | - $taxes[ $name ] = $tax_amount; |
|
50 | + foreach ($tax_rates as $name => $rate) { |
|
51 | + $the_rate = ($rate / 100) / $tax_rate; |
|
52 | + $net_price = $price - ($the_rate * $price); |
|
53 | + $tax_amount = apply_filters('getpaid_price_inc_tax_amount', $price - $net_price, $name, $rate, $price); |
|
54 | + $taxes[$name] = $tax_amount; |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | // Round all taxes to precision (4DP) before passing them back. |
58 | - $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
58 | + $taxes = array_map(array(__CLASS__, 'round'), $taxes); |
|
59 | 59 | |
60 | 60 | return $taxes; |
61 | 61 | } |
@@ -67,19 +67,19 @@ discard block |
||
67 | 67 | * @param array $rates Array of tax rates. |
68 | 68 | * @return array |
69 | 69 | */ |
70 | - public static function calc_exclusive_tax( $price, $rates ) { |
|
70 | + public static function calc_exclusive_tax($price, $rates) { |
|
71 | 71 | $taxes = array(); |
72 | - $tax_rates = wp_list_pluck( $rates, 'rate', 'name' ); |
|
72 | + $tax_rates = wp_list_pluck($rates, 'rate', 'name'); |
|
73 | 73 | |
74 | - foreach ( $tax_rates as $name => $rate ) { |
|
74 | + foreach ($tax_rates as $name => $rate) { |
|
75 | 75 | |
76 | - $tax_amount = $price * ( $rate / 100 ); |
|
77 | - $taxes[ $name ] = apply_filters( 'getpaid_price_ex_tax_amount', $tax_amount, $name, $rate, $price ); |
|
76 | + $tax_amount = $price * ($rate / 100); |
|
77 | + $taxes[$name] = apply_filters('getpaid_price_ex_tax_amount', $tax_amount, $name, $rate, $price); |
|
78 | 78 | |
79 | 79 | } |
80 | 80 | |
81 | 81 | // Round all taxes to precision (4DP) before passing them back. |
82 | - $taxes = array_map( array( __CLASS__, 'round' ), $taxes ); |
|
82 | + $taxes = array_map(array(__CLASS__, 'round'), $taxes); |
|
83 | 83 | |
84 | 84 | return $taxes; |
85 | 85 | } |
@@ -91,11 +91,11 @@ discard block |
||
91 | 91 | */ |
92 | 92 | public static function get_all_tax_rates() { |
93 | 93 | |
94 | - $rates = get_option( 'wpinv_tax_rates', array() ); |
|
94 | + $rates = get_option('wpinv_tax_rates', array()); |
|
95 | 95 | |
96 | 96 | return apply_filters( |
97 | 97 | 'getpaid_get_all_tax_rates', |
98 | - array_filter( wpinv_parse_list( $rates ) ) |
|
98 | + array_filter(wpinv_parse_list($rates)) |
|
99 | 99 | ); |
100 | 100 | |
101 | 101 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | 'state' => wpinv_get_default_state(), |
116 | 116 | 'global' => true, |
117 | 117 | 'rate' => wpinv_get_default_tax_rate(), |
118 | - 'name' => __( 'Tax', 'invoicing' ), |
|
118 | + 'name' => __('Tax', 'invoicing'), |
|
119 | 119 | ), |
120 | 120 | ) |
121 | 121 | ); |
@@ -134,22 +134,22 @@ discard block |
||
134 | 134 | array( |
135 | 135 | array( |
136 | 136 | 'key' => 'physical', |
137 | - 'label' => __( 'Physical Item', 'invoicing' ), |
|
138 | - 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
139 | - 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
137 | + 'label' => __('Physical Item', 'invoicing'), |
|
138 | + 'tax_base' => wpinv_get_option('tax_base', 'billing'), |
|
139 | + 'same_country_rule' => wpinv_get_option('vat_same_country_rule', 'vat_too'), |
|
140 | 140 | ), |
141 | 141 | array( |
142 | 142 | 'key' => 'digital', |
143 | - 'label' => __( 'Digital Item', 'invoicing' ), |
|
144 | - 'tax_base' => wpinv_get_option( 'tax_base', 'billing' ), |
|
145 | - 'same_country_rule' => wpinv_get_option( 'vat_same_country_rule', 'vat_too' ), |
|
143 | + 'label' => __('Digital Item', 'invoicing'), |
|
144 | + 'tax_base' => wpinv_get_option('tax_base', 'billing'), |
|
145 | + 'same_country_rule' => wpinv_get_option('vat_same_country_rule', 'vat_too'), |
|
146 | 146 | ), |
147 | 147 | ) |
148 | 148 | ); |
149 | 149 | |
150 | 150 | return apply_filters( |
151 | 151 | 'getpaid_tax_rules', |
152 | - array_filter( array_values( wpinv_parse_list( $rules ) ) ) |
|
152 | + array_filter(array_values(wpinv_parse_list($rules))) |
|
153 | 153 | ); |
154 | 154 | |
155 | 155 | } |
@@ -161,23 +161,23 @@ discard block |
||
161 | 161 | * @param string $state |
162 | 162 | * @return array |
163 | 163 | */ |
164 | - public static function get_address_tax_rates( $country, $state ) { |
|
164 | + public static function get_address_tax_rates($country, $state) { |
|
165 | 165 | |
166 | 166 | $all_tax_rates = self::get_all_tax_rates(); |
167 | 167 | $matching_rates = array_merge( |
168 | - wp_list_filter( $all_tax_rates, array( 'country' => $country ) ), |
|
169 | - wp_list_filter( $all_tax_rates, array( 'country' => '' ) ) |
|
168 | + wp_list_filter($all_tax_rates, array('country' => $country)), |
|
169 | + wp_list_filter($all_tax_rates, array('country' => '')) |
|
170 | 170 | ); |
171 | 171 | |
172 | - foreach ( $matching_rates as $i => $rate ) { |
|
172 | + foreach ($matching_rates as $i => $rate) { |
|
173 | 173 | |
174 | - $states = array_filter( wpinv_clean( explode( ',', strtolower( $rate['state'] ) ) ) ); |
|
175 | - if ( empty( $rate['global'] ) && ! in_array( strtolower( $state ), $states ) ) { |
|
176 | - unset( $matching_rates[ $i ] ); |
|
174 | + $states = array_filter(wpinv_clean(explode(',', strtolower($rate['state'])))); |
|
175 | + if (empty($rate['global']) && !in_array(strtolower($state), $states)) { |
|
176 | + unset($matching_rates[$i]); |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | |
180 | - return apply_filters( 'getpaid_get_address_tax_rates', $matching_rates, $country, $state ); |
|
180 | + return apply_filters('getpaid_get_address_tax_rates', $matching_rates, $country, $state); |
|
181 | 181 | |
182 | 182 | } |
183 | 183 | |
@@ -187,8 +187,8 @@ discard block |
||
187 | 187 | * @param array $taxes Array of taxes. |
188 | 188 | * @return float |
189 | 189 | */ |
190 | - public static function get_tax_total( $taxes ) { |
|
191 | - return self::round( array_sum( $taxes ) ); |
|
190 | + public static function get_tax_total($taxes) { |
|
191 | + return self::round(array_sum($taxes)); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | /** |
@@ -204,8 +204,8 @@ discard block |
||
204 | 204 | * @param float|int $in Value to round. |
205 | 205 | * @return float |
206 | 206 | */ |
207 | - public static function round( $in ) { |
|
208 | - return apply_filters( 'getpaid_tax_round', round( $in, 4 ), $in ); |
|
207 | + public static function round($in) { |
|
208 | + return apply_filters('getpaid_tax_round', round($in, 4), $in); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | } |
@@ -8,22 +8,22 @@ discard block |
||
8 | 8 | * @var WPInv_Invoice $invoice |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | +defined('ABSPATH') || exit; |
|
12 | 12 | |
13 | 13 | // Totals rows. |
14 | -$totals = getpaid_invoice_totals_rows( $invoice ); |
|
14 | +$totals = getpaid_invoice_totals_rows($invoice); |
|
15 | 15 | |
16 | -do_action( 'getpaid_before_email_line_totals', $invoice, $totals ); |
|
16 | +do_action('getpaid_before_email_line_totals', $invoice, $totals); |
|
17 | 17 | |
18 | 18 | ?> |
19 | 19 | |
20 | 20 | |
21 | -<?php if ( has_action( 'wpinv_email_footer_buttons' ) ) : ?> |
|
21 | +<?php if (has_action('wpinv_email_footer_buttons')) : ?> |
|
22 | 22 | |
23 | 23 | <tr class="wpinv_cart_footer_row"> |
24 | 24 | |
25 | - <td colspan="<?php echo ( (int) $column_count ); ?>"> |
|
26 | - <?php do_action( 'wpinv_email_footer_buttons' ); ?> |
|
25 | + <td colspan="<?php echo ((int) $column_count); ?>"> |
|
26 | + <?php do_action('wpinv_email_footer_buttons'); ?> |
|
27 | 27 | </td> |
28 | 28 | |
29 | 29 | </tr> |
@@ -31,49 +31,49 @@ discard block |
||
31 | 31 | <?php endif; ?> |
32 | 32 | |
33 | 33 | |
34 | -<?php foreach ( $totals as $key => $label ) : ?> |
|
34 | +<?php foreach ($totals as $key => $label) : ?> |
|
35 | 35 | |
36 | - <tr class="wpinv_cart_footer_row wpinv_cart_<?php echo esc_html( $key ); ?>_row"> |
|
36 | + <tr class="wpinv_cart_footer_row wpinv_cart_<?php echo esc_html($key); ?>_row"> |
|
37 | 37 | |
38 | - <td colspan="<?php echo absint( ( $column_count - 1 ) ); ?>" class="wpinv_cart_<?php echo esc_html( $key ); ?>_label text-right"> |
|
39 | - <strong><?php echo esc_html( $label ); ?>:</strong> |
|
38 | + <td colspan="<?php echo absint(($column_count - 1)); ?>" class="wpinv_cart_<?php echo esc_html($key); ?>_label text-right"> |
|
39 | + <strong><?php echo esc_html($label); ?>:</strong> |
|
40 | 40 | </td> |
41 | 41 | |
42 | - <td class="wpinv_cart_<?php echo esc_html( $key ); ?> text-right"> |
|
42 | + <td class="wpinv_cart_<?php echo esc_html($key); ?> text-right"> |
|
43 | 43 | |
44 | 44 | <?php |
45 | 45 | |
46 | 46 | // Total tax. |
47 | - if ( 'tax' == $key ) { |
|
48 | - wpinv_the_price( $invoice->get_total_tax(), $invoice->get_currency() ); |
|
47 | + if ('tax' == $key) { |
|
48 | + wpinv_the_price($invoice->get_total_tax(), $invoice->get_currency()); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | // Individual taxes. |
52 | - if ( 0 === strpos( $key, 'tax__' ) ) { |
|
53 | - wpinv_the_price( $invoice->get_tax_total_by_name( str_replace( 'tax__', '', $key ) ), $invoice->get_currency() ); |
|
52 | + if (0 === strpos($key, 'tax__')) { |
|
53 | + wpinv_the_price($invoice->get_tax_total_by_name(str_replace('tax__', '', $key)), $invoice->get_currency()); |
|
54 | 54 | } |
55 | 55 | |
56 | - if ( 'fee' == $key ) { |
|
57 | - wpinv_the_price( $invoice->get_total_fees(), $invoice->get_currency() ); |
|
56 | + if ('fee' == $key) { |
|
57 | + wpinv_the_price($invoice->get_total_fees(), $invoice->get_currency()); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | // Total discount. |
61 | - if ( 'discount' == $key ) { |
|
62 | - wpinv_the_price( $invoice->get_total_discount(), $invoice->get_currency() ); |
|
61 | + if ('discount' == $key) { |
|
62 | + wpinv_the_price($invoice->get_total_discount(), $invoice->get_currency()); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | // Sub total. |
66 | - if ( 'subtotal' == $key ) { |
|
67 | - wpinv_the_price( $invoice->get_subtotal(), $invoice->get_currency() ); |
|
66 | + if ('subtotal' == $key) { |
|
67 | + wpinv_the_price($invoice->get_subtotal(), $invoice->get_currency()); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | // Total. |
71 | - if ( 'total' == $key ) { |
|
72 | - wpinv_the_price( $invoice->get_total(), $invoice->get_currency() ); |
|
71 | + if ('total' == $key) { |
|
72 | + wpinv_the_price($invoice->get_total(), $invoice->get_currency()); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | // Fires when printing a cart total in an email. |
76 | - do_action( "getpaid_email_cart_totals_$key", $invoice ); |
|
76 | + do_action("getpaid_email_cart_totals_$key", $invoice); |
|
77 | 77 | |
78 | 78 | ?> |
79 | 79 | |
@@ -85,4 +85,4 @@ discard block |
||
85 | 85 | |
86 | 86 | <?php |
87 | 87 | |
88 | - do_action( 'getpaid_after_email_line_totals', $invoice, $totals ); |
|
88 | + do_action('getpaid_after_email_line_totals', $invoice, $totals); |
@@ -1,4 +1,4 @@ discard block |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | /** |
3 | 3 | * Displays invoice totals in emails. |
4 | 4 | * |
@@ -8,17 +8,17 @@ discard block |
||
8 | 8 | * @var WPInv_Invoice $invoice |
9 | 9 | */ |
10 | 10 | |
11 | -defined( 'ABSPATH' ) || exit; |
|
11 | + defined( 'ABSPATH' ) || exit; |
|
12 | 12 | |
13 | -// Totals rows. |
|
14 | -$totals = getpaid_invoice_totals_rows( $invoice ); |
|
13 | + // Totals rows. |
|
14 | + $totals = getpaid_invoice_totals_rows( $invoice ); |
|
15 | 15 | |
16 | -do_action( 'getpaid_before_email_line_totals', $invoice, $totals ); |
|
16 | + do_action( 'getpaid_before_email_line_totals', $invoice, $totals ); |
|
17 | 17 | |
18 | -?> |
|
18 | + ?> |
|
19 | + |
|
19 | 20 | |
20 | - |
|
21 | -<?php if ( has_action( 'wpinv_email_footer_buttons' ) ) : ?> |
|
21 | + <?php if ( has_action( 'wpinv_email_footer_buttons' ) ) : ?> |
|
22 | 22 | |
23 | 23 | <tr class="wpinv_cart_footer_row"> |
24 | 24 | |
@@ -29,9 +29,9 @@ discard block |
||
29 | 29 | </tr> |
30 | 30 | |
31 | 31 | <?php endif; ?> |
32 | + |
|
32 | 33 | |
33 | - |
|
34 | -<?php foreach ( $totals as $key => $label ) : ?> |
|
34 | + <?php foreach ( $totals as $key => $label ) : ?> |
|
35 | 35 | |
36 | 36 | <tr class="wpinv_cart_footer_row wpinv_cart_<?php echo esc_html( $key ); ?>_row"> |
37 | 37 | |
@@ -43,46 +43,46 @@ discard block |
||
43 | 43 | |
44 | 44 | <?php |
45 | 45 | |
46 | - // Total tax. |
|
47 | - if ( 'tax' == $key ) { |
|
48 | - wpinv_the_price( $invoice->get_total_tax(), $invoice->get_currency() ); |
|
49 | - } |
|
46 | + // Total tax. |
|
47 | + if ( 'tax' == $key ) { |
|
48 | + wpinv_the_price( $invoice->get_total_tax(), $invoice->get_currency() ); |
|
49 | + } |
|
50 | 50 | |
51 | - // Individual taxes. |
|
52 | - if ( 0 === strpos( $key, 'tax__' ) ) { |
|
53 | - wpinv_the_price( $invoice->get_tax_total_by_name( str_replace( 'tax__', '', $key ) ), $invoice->get_currency() ); |
|
54 | - } |
|
51 | + // Individual taxes. |
|
52 | + if ( 0 === strpos( $key, 'tax__' ) ) { |
|
53 | + wpinv_the_price( $invoice->get_tax_total_by_name( str_replace( 'tax__', '', $key ) ), $invoice->get_currency() ); |
|
54 | + } |
|
55 | 55 | |
56 | - if ( 'fee' == $key ) { |
|
57 | - wpinv_the_price( $invoice->get_total_fees(), $invoice->get_currency() ); |
|
58 | - } |
|
56 | + if ( 'fee' == $key ) { |
|
57 | + wpinv_the_price( $invoice->get_total_fees(), $invoice->get_currency() ); |
|
58 | + } |
|
59 | 59 | |
60 | - // Total discount. |
|
61 | - if ( 'discount' == $key ) { |
|
62 | - wpinv_the_price( $invoice->get_total_discount(), $invoice->get_currency() ); |
|
63 | - } |
|
60 | + // Total discount. |
|
61 | + if ( 'discount' == $key ) { |
|
62 | + wpinv_the_price( $invoice->get_total_discount(), $invoice->get_currency() ); |
|
63 | + } |
|
64 | 64 | |
65 | - // Sub total. |
|
66 | - if ( 'subtotal' == $key ) { |
|
67 | - wpinv_the_price( $invoice->get_subtotal(), $invoice->get_currency() ); |
|
68 | - } |
|
65 | + // Sub total. |
|
66 | + if ( 'subtotal' == $key ) { |
|
67 | + wpinv_the_price( $invoice->get_subtotal(), $invoice->get_currency() ); |
|
68 | + } |
|
69 | 69 | |
70 | - // Total. |
|
71 | - if ( 'total' == $key ) { |
|
72 | - wpinv_the_price( $invoice->get_total(), $invoice->get_currency() ); |
|
73 | - } |
|
70 | + // Total. |
|
71 | + if ( 'total' == $key ) { |
|
72 | + wpinv_the_price( $invoice->get_total(), $invoice->get_currency() ); |
|
73 | + } |
|
74 | 74 | |
75 | - // Fires when printing a cart total in an email. |
|
76 | - do_action( "getpaid_email_cart_totals_$key", $invoice ); |
|
75 | + // Fires when printing a cart total in an email. |
|
76 | + do_action( "getpaid_email_cart_totals_$key", $invoice ); |
|
77 | 77 | |
78 | - ?> |
|
78 | + ?> |
|
79 | 79 | |
80 | 80 | </td> |
81 | 81 | |
82 | 82 | </tr> |
83 | 83 | |
84 | 84 | <?php endforeach; ?> |
85 | + |
|
86 | + <?php |
|
85 | 87 | |
86 | -<?php |
|
87 | - |
|
88 | - do_action( 'getpaid_after_email_line_totals', $invoice, $totals ); |
|
88 | + do_action( 'getpaid_after_email_line_totals', $invoice, $totals ); |