@@ -13,462 +13,462 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class GetPaid_Payment_Gateway { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set if the place checkout button should be renamed on selection. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - public $checkout_button_text; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Boolean whether the method is enabled. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - public $enabled = true; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Payment method id. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - public $id; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Payment method order. |
|
| 39 | - * |
|
| 40 | - * @var int |
|
| 41 | - */ |
|
| 42 | - public $order = 10; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Payment method title for the frontend. |
|
| 46 | - * |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - public $title; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Payment method description for the frontend. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - public $description; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Gateway title. |
|
| 60 | - * |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - public $method_title = ''; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Gateway description. |
|
| 67 | - * |
|
| 68 | - * @var string |
|
| 69 | - */ |
|
| 70 | - public $method_description = ''; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Countries this gateway is allowed for. |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - public $countries; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Currencies this gateway is allowed for. |
|
| 81 | - * |
|
| 82 | - * @var array |
|
| 83 | - */ |
|
| 84 | - public $currencies; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Currencies this gateway is not allowed for. |
|
| 88 | - * |
|
| 89 | - * @var array |
|
| 90 | - */ |
|
| 91 | - public $exclude_currencies; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | - * |
|
| 96 | - * @var int |
|
| 97 | - */ |
|
| 98 | - public $max_amount = 0; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Optional URL to view a transaction. |
|
| 102 | - * |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $view_transaction_url = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Optional URL to view a subscription. |
|
| 109 | - * |
|
| 110 | - * @var string |
|
| 111 | - */ |
|
| 112 | - public $view_subscription_url = ''; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Optional label to show for "new payment method" in the payment |
|
| 116 | - * method/token selection radio selection. |
|
| 117 | - * |
|
| 118 | - * @var string |
|
| 119 | - */ |
|
| 120 | - public $new_method_label = ''; |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Contains a user's saved tokens for this gateway. |
|
| 124 | - * |
|
| 125 | - * @var array |
|
| 126 | - */ |
|
| 127 | - protected $tokens = array(); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * An array of features that this gateway supports. |
|
| 131 | - * |
|
| 132 | - * @var array |
|
| 133 | - */ |
|
| 134 | - protected $supports = array(); |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Class constructor. |
|
| 138 | - */ |
|
| 139 | - public function __construct() { |
|
| 140 | - |
|
| 141 | - // Register gateway. |
|
| 142 | - add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 143 | - |
|
| 144 | - $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 145 | - |
|
| 146 | - // Add support for various features. |
|
| 147 | - foreach ( $this->supports as $feature ) { |
|
| 148 | - add_filter( "wpinv_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 149 | - add_filter( "getpaid_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 150 | - add_filter( "getpaid_{$this->id}_supports_{$feature}", '__return_true' ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Invoice addons. |
|
| 154 | - if ( $this->supports( 'addons' ) ) { |
|
| 155 | - add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // Gateway settings. |
|
| 159 | - add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 160 | - |
|
| 161 | - // Gateway checkout fiellds. |
|
| 162 | - add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 163 | - |
|
| 164 | - // Process payment. |
|
| 165 | - add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 166 | - |
|
| 167 | - // Change the checkout button text. |
|
| 168 | - if ( ! empty( $this->checkout_button_text ) ) { |
|
| 169 | - add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // Check if a gateway is valid for a given currency. |
|
| 173 | - add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 174 | - |
|
| 175 | - // Generate the transaction url. |
|
| 176 | - add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 177 | - |
|
| 178 | - // Generate the subscription url. |
|
| 179 | - add_filter( 'getpaid_remote_subscription_profile_url', array( $this, 'generate_subscription_url' ), 10, 2 ); |
|
| 180 | - |
|
| 181 | - // Confirm payments. |
|
| 182 | - add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 183 | - |
|
| 184 | - // Verify IPNs. |
|
| 185 | - add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 186 | - |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Checks if this gateway is a given gateway. |
|
| 191 | - * |
|
| 192 | - * @since 1.0.19 |
|
| 193 | - * @return bool |
|
| 194 | - */ |
|
| 195 | - public function is( $gateway ) { |
|
| 196 | - return $gateway == $this->id; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Returns a users saved tokens for this gateway. |
|
| 201 | - * |
|
| 202 | - * @since 1.0.19 |
|
| 203 | - * @return array |
|
| 204 | - */ |
|
| 205 | - public function get_tokens( $sandbox = null ) { |
|
| 206 | - |
|
| 207 | - if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 208 | - $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 209 | - |
|
| 210 | - if ( is_array( $tokens ) ) { |
|
| 211 | - $this->tokens = $tokens; |
|
| 212 | - } |
|
| 16 | + /** |
|
| 17 | + * Set if the place checkout button should be renamed on selection. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + public $checkout_button_text; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Boolean whether the method is enabled. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + public $enabled = true; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Payment method id. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + public $id; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Payment method order. |
|
| 39 | + * |
|
| 40 | + * @var int |
|
| 41 | + */ |
|
| 42 | + public $order = 10; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Payment method title for the frontend. |
|
| 46 | + * |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + public $title; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Payment method description for the frontend. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + public $description; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Gateway title. |
|
| 60 | + * |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + public $method_title = ''; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Gateway description. |
|
| 67 | + * |
|
| 68 | + * @var string |
|
| 69 | + */ |
|
| 70 | + public $method_description = ''; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Countries this gateway is allowed for. |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + public $countries; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Currencies this gateway is allowed for. |
|
| 81 | + * |
|
| 82 | + * @var array |
|
| 83 | + */ |
|
| 84 | + public $currencies; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Currencies this gateway is not allowed for. |
|
| 88 | + * |
|
| 89 | + * @var array |
|
| 90 | + */ |
|
| 91 | + public $exclude_currencies; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Maximum transaction amount, zero does not define a maximum. |
|
| 95 | + * |
|
| 96 | + * @var int |
|
| 97 | + */ |
|
| 98 | + public $max_amount = 0; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Optional URL to view a transaction. |
|
| 102 | + * |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $view_transaction_url = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Optional URL to view a subscription. |
|
| 109 | + * |
|
| 110 | + * @var string |
|
| 111 | + */ |
|
| 112 | + public $view_subscription_url = ''; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Optional label to show for "new payment method" in the payment |
|
| 116 | + * method/token selection radio selection. |
|
| 117 | + * |
|
| 118 | + * @var string |
|
| 119 | + */ |
|
| 120 | + public $new_method_label = ''; |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Contains a user's saved tokens for this gateway. |
|
| 124 | + * |
|
| 125 | + * @var array |
|
| 126 | + */ |
|
| 127 | + protected $tokens = array(); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * An array of features that this gateway supports. |
|
| 131 | + * |
|
| 132 | + * @var array |
|
| 133 | + */ |
|
| 134 | + protected $supports = array(); |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Class constructor. |
|
| 138 | + */ |
|
| 139 | + public function __construct() { |
|
| 140 | + |
|
| 141 | + // Register gateway. |
|
| 142 | + add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
| 143 | + |
|
| 144 | + $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
| 145 | + |
|
| 146 | + // Add support for various features. |
|
| 147 | + foreach ( $this->supports as $feature ) { |
|
| 148 | + add_filter( "wpinv_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 149 | + add_filter( "getpaid_{$this->id}_support_{$feature}", '__return_true' ); |
|
| 150 | + add_filter( "getpaid_{$this->id}_supports_{$feature}", '__return_true' ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Invoice addons. |
|
| 154 | + if ( $this->supports( 'addons' ) ) { |
|
| 155 | + add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // Gateway settings. |
|
| 159 | + add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
| 160 | + |
|
| 161 | + // Gateway checkout fiellds. |
|
| 162 | + add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
| 163 | + |
|
| 164 | + // Process payment. |
|
| 165 | + add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
| 166 | + |
|
| 167 | + // Change the checkout button text. |
|
| 168 | + if ( ! empty( $this->checkout_button_text ) ) { |
|
| 169 | + add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // Check if a gateway is valid for a given currency. |
|
| 173 | + add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
| 174 | + |
|
| 175 | + // Generate the transaction url. |
|
| 176 | + add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
| 177 | + |
|
| 178 | + // Generate the subscription url. |
|
| 179 | + add_filter( 'getpaid_remote_subscription_profile_url', array( $this, 'generate_subscription_url' ), 10, 2 ); |
|
| 180 | + |
|
| 181 | + // Confirm payments. |
|
| 182 | + add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
| 183 | + |
|
| 184 | + // Verify IPNs. |
|
| 185 | + add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
| 186 | + |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Checks if this gateway is a given gateway. |
|
| 191 | + * |
|
| 192 | + * @since 1.0.19 |
|
| 193 | + * @return bool |
|
| 194 | + */ |
|
| 195 | + public function is( $gateway ) { |
|
| 196 | + return $gateway == $this->id; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Returns a users saved tokens for this gateway. |
|
| 201 | + * |
|
| 202 | + * @since 1.0.19 |
|
| 203 | + * @return array |
|
| 204 | + */ |
|
| 205 | + public function get_tokens( $sandbox = null ) { |
|
| 206 | + |
|
| 207 | + if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
| 208 | + $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
| 209 | + |
|
| 210 | + if ( is_array( $tokens ) ) { |
|
| 211 | + $this->tokens = $tokens; |
|
| 212 | + } |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - if ( ! is_bool( $sandbox ) ) { |
|
| 216 | - return $this->tokens; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // Filter tokens. |
|
| 220 | - $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 221 | - return wp_list_filter( $this->tokens, $args ); |
|
| 222 | - |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Saves a token for this gateway. |
|
| 227 | - * |
|
| 228 | - * @since 1.0.19 |
|
| 229 | - */ |
|
| 230 | - public function save_token( $token ) { |
|
| 231 | - |
|
| 232 | - $tokens = $this->get_tokens(); |
|
| 233 | - $tokens[] = $token; |
|
| 234 | - |
|
| 235 | - update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 236 | - |
|
| 237 | - $this->tokens = $tokens; |
|
| 238 | - |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Return the title for admin screens. |
|
| 243 | - * |
|
| 244 | - * @return string |
|
| 245 | - */ |
|
| 246 | - public function get_method_title() { |
|
| 247 | - return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Return the description for admin screens. |
|
| 252 | - * |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - public function get_method_description() { |
|
| 256 | - return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Get the success url. |
|
| 261 | - * |
|
| 262 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 263 | - * @return string |
|
| 264 | - */ |
|
| 265 | - public function get_return_url( $invoice ) { |
|
| 266 | - |
|
| 267 | - // Payment success url |
|
| 268 | - $return_url = add_query_arg( |
|
| 269 | - array( |
|
| 270 | - 'payment-confirm' => $this->id, |
|
| 271 | - 'invoice_key' => $invoice->get_key(), |
|
| 272 | - 'utm_nooverride' => 1, |
|
| 273 | - ), |
|
| 274 | - wpinv_get_success_page_uri() |
|
| 275 | - ); |
|
| 276 | - |
|
| 277 | - return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Confirms payments when rendering the success page. |
|
| 282 | - * |
|
| 283 | - * @param string $content Success page content. |
|
| 284 | - * @return string |
|
| 285 | - */ |
|
| 286 | - public function confirm_payment( $content ) { |
|
| 287 | - |
|
| 288 | - // Retrieve the invoice. |
|
| 289 | - $invoice_id = getpaid_get_current_invoice_id(); |
|
| 290 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 291 | - |
|
| 292 | - // Ensure that it exists and that it is pending payment. |
|
| 293 | - if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 294 | - return $content; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - // Can the user view this invoice?? |
|
| 298 | - if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 299 | - return $content; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - // Show payment processing indicator. |
|
| 303 | - return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Processes ipns and marks payments as complete. |
|
| 308 | - * |
|
| 309 | - * @return void |
|
| 310 | - */ |
|
| 311 | - public function verify_ipn() {} |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Processes invoice addons. |
|
| 315 | - * |
|
| 316 | - * @param WPInv_Invoice $invoice |
|
| 317 | - * @param GetPaid_Form_Item[] $items |
|
| 318 | - * @return WPInv_Invoice |
|
| 319 | - */ |
|
| 320 | - public function process_addons( $invoice, $items ) { |
|
| 321 | - |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 326 | - * |
|
| 327 | - * @param string $transaction_url transaction url. |
|
| 328 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 329 | - * @return string transaction URL, or empty string. |
|
| 330 | - */ |
|
| 331 | - public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 332 | - |
|
| 333 | - $transaction_id = $invoice->get_transaction_id(); |
|
| 334 | - |
|
| 335 | - if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 336 | - $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 337 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 338 | - $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - return $transaction_url; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 346 | - * |
|
| 347 | - * @param string $subscription_url transaction url. |
|
| 348 | - * @param WPInv_Subscription $subscription Subscription objectt. |
|
| 349 | - * @return string subscription URL, or empty string. |
|
| 350 | - */ |
|
| 351 | - public function generate_subscription_url( $subscription_url, $subscription ) { |
|
| 352 | - |
|
| 353 | - $profile_id = $subscription->get_profile_id(); |
|
| 354 | - |
|
| 355 | - if ( $this->id == $subscription->get_gateway() && ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 356 | - |
|
| 357 | - $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 358 | - $replace = $this->is_sandbox( $subscription->get_parent_invoice() ) ? 'sandbox' : ''; |
|
| 359 | - $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 360 | - |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - return $subscription_url; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Check if the gateway is available for use. |
|
| 368 | - * |
|
| 369 | - * @return bool |
|
| 370 | - */ |
|
| 371 | - public function is_available() { |
|
| 372 | - return ! empty( $this->enabled ); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Return the gateway's title. |
|
| 377 | - * |
|
| 378 | - * @return string |
|
| 379 | - */ |
|
| 380 | - public function get_title() { |
|
| 381 | - return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Return the gateway's description. |
|
| 386 | - * |
|
| 387 | - * @return string |
|
| 388 | - */ |
|
| 389 | - public function get_description() { |
|
| 390 | - return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Process Payment. |
|
| 395 | - * |
|
| 396 | - * |
|
| 397 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 398 | - * @param array $submission_data Posted checkout fields. |
|
| 399 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 400 | - * @return void |
|
| 401 | - */ |
|
| 402 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 403 | - // Process the payment then either redirect to the success page or the gateway. |
|
| 404 | - do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Process refund. |
|
| 409 | - * |
|
| 410 | - * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 411 | - * a passed in amount. |
|
| 412 | - * |
|
| 413 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 414 | - * @param float $amount Refund amount. |
|
| 415 | - * @param string $reason Refund reason. |
|
| 416 | - * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 417 | - */ |
|
| 418 | - public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 419 | - return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Displays the payment fields, credit cards etc. |
|
| 424 | - * |
|
| 425 | - * @param int $invoice_id 0 or invoice id. |
|
| 426 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 427 | - */ |
|
| 428 | - public function payment_fields( $invoice_id, $form ) { |
|
| 429 | - do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Filters the gateway settings. |
|
| 434 | - * |
|
| 435 | - * @param array $admin_settings |
|
| 436 | - */ |
|
| 437 | - public function admin_settings( $admin_settings ) { |
|
| 438 | - return $admin_settings; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Retrieves the value of a gateway setting. |
|
| 443 | - * |
|
| 444 | - * @param string $option |
|
| 445 | - */ |
|
| 446 | - public function get_option( $option, $default = false ) { |
|
| 447 | - return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Check if a gateway supports a given feature. |
|
| 452 | - * |
|
| 453 | - * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 454 | - * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 455 | - * |
|
| 456 | - * @param string $feature string The name of a feature to test support for. |
|
| 457 | - * @return bool True if the gateway supports the feature, false otherwise. |
|
| 458 | - * @since 1.0.19 |
|
| 459 | - */ |
|
| 460 | - public function supports( $feature ) { |
|
| 461 | - return getpaid_payment_gateway_supports( $this->id, $feature ); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Returns the credit card form html. |
|
| 466 | - * |
|
| 467 | - * @param bool $save whether or not to display the save button. |
|
| 468 | - */ |
|
| 215 | + if ( ! is_bool( $sandbox ) ) { |
|
| 216 | + return $this->tokens; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // Filter tokens. |
|
| 220 | + $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
| 221 | + return wp_list_filter( $this->tokens, $args ); |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Saves a token for this gateway. |
|
| 227 | + * |
|
| 228 | + * @since 1.0.19 |
|
| 229 | + */ |
|
| 230 | + public function save_token( $token ) { |
|
| 231 | + |
|
| 232 | + $tokens = $this->get_tokens(); |
|
| 233 | + $tokens[] = $token; |
|
| 234 | + |
|
| 235 | + update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
| 236 | + |
|
| 237 | + $this->tokens = $tokens; |
|
| 238 | + |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Return the title for admin screens. |
|
| 243 | + * |
|
| 244 | + * @return string |
|
| 245 | + */ |
|
| 246 | + public function get_method_title() { |
|
| 247 | + return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Return the description for admin screens. |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + public function get_method_description() { |
|
| 256 | + return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Get the success url. |
|
| 261 | + * |
|
| 262 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 263 | + * @return string |
|
| 264 | + */ |
|
| 265 | + public function get_return_url( $invoice ) { |
|
| 266 | + |
|
| 267 | + // Payment success url |
|
| 268 | + $return_url = add_query_arg( |
|
| 269 | + array( |
|
| 270 | + 'payment-confirm' => $this->id, |
|
| 271 | + 'invoice_key' => $invoice->get_key(), |
|
| 272 | + 'utm_nooverride' => 1, |
|
| 273 | + ), |
|
| 274 | + wpinv_get_success_page_uri() |
|
| 275 | + ); |
|
| 276 | + |
|
| 277 | + return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Confirms payments when rendering the success page. |
|
| 282 | + * |
|
| 283 | + * @param string $content Success page content. |
|
| 284 | + * @return string |
|
| 285 | + */ |
|
| 286 | + public function confirm_payment( $content ) { |
|
| 287 | + |
|
| 288 | + // Retrieve the invoice. |
|
| 289 | + $invoice_id = getpaid_get_current_invoice_id(); |
|
| 290 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
| 291 | + |
|
| 292 | + // Ensure that it exists and that it is pending payment. |
|
| 293 | + if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
| 294 | + return $content; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + // Can the user view this invoice?? |
|
| 298 | + if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
| 299 | + return $content; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + // Show payment processing indicator. |
|
| 303 | + return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Processes ipns and marks payments as complete. |
|
| 308 | + * |
|
| 309 | + * @return void |
|
| 310 | + */ |
|
| 311 | + public function verify_ipn() {} |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Processes invoice addons. |
|
| 315 | + * |
|
| 316 | + * @param WPInv_Invoice $invoice |
|
| 317 | + * @param GetPaid_Form_Item[] $items |
|
| 318 | + * @return WPInv_Invoice |
|
| 319 | + */ |
|
| 320 | + public function process_addons( $invoice, $items ) { |
|
| 321 | + |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
| 326 | + * |
|
| 327 | + * @param string $transaction_url transaction url. |
|
| 328 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 329 | + * @return string transaction URL, or empty string. |
|
| 330 | + */ |
|
| 331 | + public function filter_transaction_url( $transaction_url, $invoice ) { |
|
| 332 | + |
|
| 333 | + $transaction_id = $invoice->get_transaction_id(); |
|
| 334 | + |
|
| 335 | + if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
| 336 | + $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
| 337 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
| 338 | + $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + return $transaction_url; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
| 346 | + * |
|
| 347 | + * @param string $subscription_url transaction url. |
|
| 348 | + * @param WPInv_Subscription $subscription Subscription objectt. |
|
| 349 | + * @return string subscription URL, or empty string. |
|
| 350 | + */ |
|
| 351 | + public function generate_subscription_url( $subscription_url, $subscription ) { |
|
| 352 | + |
|
| 353 | + $profile_id = $subscription->get_profile_id(); |
|
| 354 | + |
|
| 355 | + if ( $this->id == $subscription->get_gateway() && ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
| 356 | + |
|
| 357 | + $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
| 358 | + $replace = $this->is_sandbox( $subscription->get_parent_invoice() ) ? 'sandbox' : ''; |
|
| 359 | + $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
| 360 | + |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + return $subscription_url; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Check if the gateway is available for use. |
|
| 368 | + * |
|
| 369 | + * @return bool |
|
| 370 | + */ |
|
| 371 | + public function is_available() { |
|
| 372 | + return ! empty( $this->enabled ); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Return the gateway's title. |
|
| 377 | + * |
|
| 378 | + * @return string |
|
| 379 | + */ |
|
| 380 | + public function get_title() { |
|
| 381 | + return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Return the gateway's description. |
|
| 386 | + * |
|
| 387 | + * @return string |
|
| 388 | + */ |
|
| 389 | + public function get_description() { |
|
| 390 | + return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Process Payment. |
|
| 395 | + * |
|
| 396 | + * |
|
| 397 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 398 | + * @param array $submission_data Posted checkout fields. |
|
| 399 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 400 | + * @return void |
|
| 401 | + */ |
|
| 402 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 403 | + // Process the payment then either redirect to the success page or the gateway. |
|
| 404 | + do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Process refund. |
|
| 409 | + * |
|
| 410 | + * If the gateway declares 'refunds' support, this will allow it to refund. |
|
| 411 | + * a passed in amount. |
|
| 412 | + * |
|
| 413 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 414 | + * @param float $amount Refund amount. |
|
| 415 | + * @param string $reason Refund reason. |
|
| 416 | + * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
| 417 | + */ |
|
| 418 | + public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
| 419 | + return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Displays the payment fields, credit cards etc. |
|
| 424 | + * |
|
| 425 | + * @param int $invoice_id 0 or invoice id. |
|
| 426 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 427 | + */ |
|
| 428 | + public function payment_fields( $invoice_id, $form ) { |
|
| 429 | + do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Filters the gateway settings. |
|
| 434 | + * |
|
| 435 | + * @param array $admin_settings |
|
| 436 | + */ |
|
| 437 | + public function admin_settings( $admin_settings ) { |
|
| 438 | + return $admin_settings; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Retrieves the value of a gateway setting. |
|
| 443 | + * |
|
| 444 | + * @param string $option |
|
| 445 | + */ |
|
| 446 | + public function get_option( $option, $default = false ) { |
|
| 447 | + return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Check if a gateway supports a given feature. |
|
| 452 | + * |
|
| 453 | + * Gateways should override this to declare support (or lack of support) for a feature. |
|
| 454 | + * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
| 455 | + * |
|
| 456 | + * @param string $feature string The name of a feature to test support for. |
|
| 457 | + * @return bool True if the gateway supports the feature, false otherwise. |
|
| 458 | + * @since 1.0.19 |
|
| 459 | + */ |
|
| 460 | + public function supports( $feature ) { |
|
| 461 | + return getpaid_payment_gateway_supports( $this->id, $feature ); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Returns the credit card form html. |
|
| 466 | + * |
|
| 467 | + * @param bool $save whether or not to display the save button. |
|
| 468 | + */ |
|
| 469 | 469 | public function get_cc_form( $save = false ) { |
| 470 | 470 | |
| 471 | - ob_start(); |
|
| 471 | + ob_start(); |
|
| 472 | 472 | |
| 473 | 473 | $id_prefix = esc_attr( uniqid( $this->id ) ); |
| 474 | 474 | |
@@ -528,9 +528,9 @@ discard block |
||
| 528 | 528 | |
| 529 | 529 | <?php |
| 530 | 530 | foreach ( $months as $key => $month ) { |
| 531 | - $key = esc_attr( $key ); |
|
| 532 | - $month = esc_html( $month ); |
|
| 533 | - echo "<option value='$key'>$month</option>" . PHP_EOL; |
|
| 531 | + $key = esc_attr( $key ); |
|
| 532 | + $month = esc_html( $month ); |
|
| 533 | + echo "<option value='$key'>$month</option>" . PHP_EOL; |
|
| 534 | 534 | } |
| 535 | 535 | ?> |
| 536 | 536 | |
@@ -543,9 +543,9 @@ discard block |
||
| 543 | 543 | |
| 544 | 544 | <?php |
| 545 | 545 | foreach ( $years as $key => $year ) { |
| 546 | - $key = esc_attr( $key ); |
|
| 547 | - $year = esc_html( $year ); |
|
| 548 | - echo "<option value='$key'>$year</option>" . PHP_EOL; |
|
| 546 | + $key = esc_attr( $key ); |
|
| 547 | + $year = esc_html( $year ); |
|
| 548 | + echo "<option value='$key'>$year</option>" . PHP_EOL; |
|
| 549 | 549 | } |
| 550 | 550 | ?> |
| 551 | 551 | |
@@ -563,11 +563,11 @@ discard block |
||
| 563 | 563 | 'name' => $this->id . '[cc_cvv2]', |
| 564 | 564 | 'id' => "$id_prefix-cc-cvv2", |
| 565 | 565 | 'label' => __( 'CCV', 'invoicing' ), |
| 566 | - 'label_type' => 'vertical', |
|
| 567 | - 'class' => 'form-control-sm', |
|
| 568 | - 'extra_attributes' => array( |
|
| 569 | - 'autocomplete' => 'cc-csc', |
|
| 570 | - ), |
|
| 566 | + 'label_type' => 'vertical', |
|
| 567 | + 'class' => 'form-control-sm', |
|
| 568 | + 'extra_attributes' => array( |
|
| 569 | + 'autocomplete' => 'cc-csc', |
|
| 570 | + ), |
|
| 571 | 571 | ) |
| 572 | 572 | ); |
| 573 | 573 | ?> |
@@ -577,192 +577,192 @@ discard block |
||
| 577 | 577 | |
| 578 | 578 | <?php |
| 579 | 579 | |
| 580 | - if ( $save ) { |
|
| 581 | - echo $this->save_payment_method_checkbox(); |
|
| 582 | - } |
|
| 580 | + if ( $save ) { |
|
| 581 | + echo $this->save_payment_method_checkbox(); |
|
| 582 | + } |
|
| 583 | 583 | |
| 584 | - ?> |
|
| 584 | + ?> |
|
| 585 | 585 | </div> |
| 586 | 586 | |
| 587 | 587 | </div> |
| 588 | 588 | <?php |
| 589 | 589 | |
| 590 | - return ob_get_clean(); |
|
| 590 | + return ob_get_clean(); |
|
| 591 | + |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Displays a new payment method entry form. |
|
| 596 | + * |
|
| 597 | + * @since 1.0.19 |
|
| 598 | + */ |
|
| 599 | + public function new_payment_method_entry( $form ) { |
|
| 600 | + echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Grab and display our saved payment methods. |
|
| 605 | + * |
|
| 606 | + * @since 1.0.19 |
|
| 607 | + */ |
|
| 608 | + public function saved_payment_methods() { |
|
| 609 | + $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 610 | + |
|
| 611 | + foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 612 | + $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + $html .= $this->get_new_payment_method_option_html(); |
|
| 616 | + $html .= '</ul>'; |
|
| 591 | 617 | |
| 618 | + echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 592 | 619 | } |
| 593 | 620 | |
| 594 | - /** |
|
| 595 | - * Displays a new payment method entry form. |
|
| 596 | - * |
|
| 597 | - * @since 1.0.19 |
|
| 598 | - */ |
|
| 599 | - public function new_payment_method_entry( $form ) { |
|
| 600 | - echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Grab and display our saved payment methods. |
|
| 605 | - * |
|
| 606 | - * @since 1.0.19 |
|
| 607 | - */ |
|
| 608 | - public function saved_payment_methods() { |
|
| 609 | - $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
| 610 | - |
|
| 611 | - foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
| 612 | - $html .= $this->get_saved_payment_method_option_html( $token ); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - $html .= $this->get_new_payment_method_option_html(); |
|
| 616 | - $html .= '</ul>'; |
|
| 617 | - |
|
| 618 | - echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Gets saved payment method HTML from a token. |
|
| 623 | - * |
|
| 624 | - * @since 1.0.19 |
|
| 625 | - * @param array $token Payment Token. |
|
| 626 | - * @return string Generated payment method HTML |
|
| 627 | - */ |
|
| 628 | - public function get_saved_payment_method_option_html( $token ) { |
|
| 629 | - |
|
| 630 | - return sprintf( |
|
| 631 | - '<li class="getpaid-payment-method form-group"> |
|
| 621 | + /** |
|
| 622 | + * Gets saved payment method HTML from a token. |
|
| 623 | + * |
|
| 624 | + * @since 1.0.19 |
|
| 625 | + * @param array $token Payment Token. |
|
| 626 | + * @return string Generated payment method HTML |
|
| 627 | + */ |
|
| 628 | + public function get_saved_payment_method_option_html( $token ) { |
|
| 629 | + |
|
| 630 | + return sprintf( |
|
| 631 | + '<li class="getpaid-payment-method form-group"> |
|
| 632 | 632 | <label> |
| 633 | 633 | <input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" data-currency="%5$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s /> |
| 634 | 634 | <span>%3$s</span> |
| 635 | 635 | </label> |
| 636 | 636 | </li>', |
| 637 | - esc_attr( $this->id ), |
|
| 638 | - esc_attr( $token['id'] ), |
|
| 639 | - esc_html( $token['name'] ), |
|
| 640 | - checked( empty( $token['default'] ), false, false ), |
|
| 641 | - empty( $token['currency'] ) ? 'none' : esc_attr( $token['currency'] ) |
|
| 642 | - ); |
|
| 643 | - |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 648 | - * |
|
| 649 | - * @since 1.0.19 |
|
| 650 | - */ |
|
| 651 | - public function get_new_payment_method_option_html() { |
|
| 652 | - |
|
| 653 | - $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 654 | - |
|
| 655 | - return sprintf( |
|
| 656 | - '<li class="getpaid-new-payment-method"> |
|
| 637 | + esc_attr( $this->id ), |
|
| 638 | + esc_attr( $token['id'] ), |
|
| 639 | + esc_html( $token['name'] ), |
|
| 640 | + checked( empty( $token['default'] ), false, false ), |
|
| 641 | + empty( $token['currency'] ) ? 'none' : esc_attr( $token['currency'] ) |
|
| 642 | + ); |
|
| 643 | + |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
| 648 | + * |
|
| 649 | + * @since 1.0.19 |
|
| 650 | + */ |
|
| 651 | + public function get_new_payment_method_option_html() { |
|
| 652 | + |
|
| 653 | + $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
| 654 | + |
|
| 655 | + return sprintf( |
|
| 656 | + '<li class="getpaid-new-payment-method"> |
|
| 657 | 657 | <label> |
| 658 | 658 | <input name="getpaid-%1$s-payment-method" type="radio" data-currency="none" value="new" style="width:auto;" /> |
| 659 | 659 | <span>%2$s</span> |
| 660 | 660 | </label> |
| 661 | 661 | </li>', |
| 662 | - esc_attr( $this->id ), |
|
| 663 | - esc_html( $label ) |
|
| 664 | - ); |
|
| 665 | - |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Outputs a checkbox for saving a new payment method to the database. |
|
| 670 | - * |
|
| 671 | - * @since 1.0.19 |
|
| 672 | - */ |
|
| 673 | - public function save_payment_method_checkbox() { |
|
| 674 | - |
|
| 675 | - return aui()->input( |
|
| 676 | - array( |
|
| 677 | - 'type' => 'checkbox', |
|
| 678 | - 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
| 679 | - 'id' => esc_attr( uniqid( $this->id ) ), |
|
| 680 | - 'required' => false, |
|
| 681 | - 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
| 682 | - 'value' => 'true', |
|
| 683 | - 'checked' => true, |
|
| 684 | - 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
| 685 | - ) |
|
| 686 | - ); |
|
| 687 | - |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Registers the gateway. |
|
| 692 | - * |
|
| 693 | - * @return array |
|
| 694 | - */ |
|
| 695 | - public function register_gateway( $gateways ) { |
|
| 696 | - |
|
| 697 | - $gateways[ $this->id ] = array( |
|
| 698 | - |
|
| 699 | - 'admin_label' => $this->method_title, |
|
| 662 | + esc_attr( $this->id ), |
|
| 663 | + esc_html( $label ) |
|
| 664 | + ); |
|
| 665 | + |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * Outputs a checkbox for saving a new payment method to the database. |
|
| 670 | + * |
|
| 671 | + * @since 1.0.19 |
|
| 672 | + */ |
|
| 673 | + public function save_payment_method_checkbox() { |
|
| 674 | + |
|
| 675 | + return aui()->input( |
|
| 676 | + array( |
|
| 677 | + 'type' => 'checkbox', |
|
| 678 | + 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
| 679 | + 'id' => esc_attr( uniqid( $this->id ) ), |
|
| 680 | + 'required' => false, |
|
| 681 | + 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
| 682 | + 'value' => 'true', |
|
| 683 | + 'checked' => true, |
|
| 684 | + 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
| 685 | + ) |
|
| 686 | + ); |
|
| 687 | + |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Registers the gateway. |
|
| 692 | + * |
|
| 693 | + * @return array |
|
| 694 | + */ |
|
| 695 | + public function register_gateway( $gateways ) { |
|
| 696 | + |
|
| 697 | + $gateways[ $this->id ] = array( |
|
| 698 | + |
|
| 699 | + 'admin_label' => $this->method_title, |
|
| 700 | 700 | 'checkout_label' => $this->title, |
| 701 | - 'ordering' => $this->order, |
|
| 701 | + 'ordering' => $this->order, |
|
| 702 | 702 | |
| 703 | - ); |
|
| 703 | + ); |
|
| 704 | 704 | |
| 705 | - return $gateways; |
|
| 705 | + return $gateways; |
|
| 706 | 706 | |
| 707 | - } |
|
| 707 | + } |
|
| 708 | 708 | |
| 709 | - /** |
|
| 710 | - * Checks whether or not this is a sandbox request. |
|
| 711 | - * |
|
| 712 | - * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 713 | - * @return bool |
|
| 714 | - */ |
|
| 715 | - public function is_sandbox( $invoice = null ) { |
|
| 709 | + /** |
|
| 710 | + * Checks whether or not this is a sandbox request. |
|
| 711 | + * |
|
| 712 | + * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
| 713 | + * @return bool |
|
| 714 | + */ |
|
| 715 | + public function is_sandbox( $invoice = null ) { |
|
| 716 | 716 | |
| 717 | - if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 718 | - return $invoice->get_mode() == 'test'; |
|
| 719 | - } |
|
| 717 | + if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
| 718 | + return $invoice->get_mode() == 'test'; |
|
| 719 | + } |
|
| 720 | 720 | |
| 721 | - return wpinv_is_test_mode( $this->id ); |
|
| 721 | + return wpinv_is_test_mode( $this->id ); |
|
| 722 | 722 | |
| 723 | - } |
|
| 723 | + } |
|
| 724 | 724 | |
| 725 | - /** |
|
| 726 | - * Renames the checkout button |
|
| 727 | - * |
|
| 728 | - * @return string |
|
| 729 | - */ |
|
| 730 | - public function rename_checkout_button() { |
|
| 731 | - return $this->checkout_button_text; |
|
| 732 | - } |
|
| 725 | + /** |
|
| 726 | + * Renames the checkout button |
|
| 727 | + * |
|
| 728 | + * @return string |
|
| 729 | + */ |
|
| 730 | + public function rename_checkout_button() { |
|
| 731 | + return $this->checkout_button_text; |
|
| 732 | + } |
|
| 733 | 733 | |
| 734 | - /** |
|
| 735 | - * Validate gateway currency |
|
| 736 | - * |
|
| 737 | - * @return bool |
|
| 738 | - */ |
|
| 739 | - public function validate_currency( $validation, $currency ) { |
|
| 734 | + /** |
|
| 735 | + * Validate gateway currency |
|
| 736 | + * |
|
| 737 | + * @return bool |
|
| 738 | + */ |
|
| 739 | + public function validate_currency( $validation, $currency ) { |
|
| 740 | 740 | |
| 741 | - // Required currencies. |
|
| 742 | - if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 743 | - return false; |
|
| 744 | - } |
|
| 741 | + // Required currencies. |
|
| 742 | + if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
| 743 | + return false; |
|
| 744 | + } |
|
| 745 | 745 | |
| 746 | - // Excluded currencies. |
|
| 747 | - if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 748 | - return false; |
|
| 749 | - } |
|
| 746 | + // Excluded currencies. |
|
| 747 | + if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
| 748 | + return false; |
|
| 749 | + } |
|
| 750 | 750 | |
| 751 | - return $validation; |
|
| 752 | - } |
|
| 751 | + return $validation; |
|
| 752 | + } |
|
| 753 | 753 | |
| 754 | - /** |
|
| 755 | - * Displays an error |
|
| 756 | - * |
|
| 757 | - */ |
|
| 758 | - public function show_error( $code, $message, $type ) { |
|
| 754 | + /** |
|
| 755 | + * Displays an error |
|
| 756 | + * |
|
| 757 | + */ |
|
| 758 | + public function show_error( $code, $message, $type ) { |
|
| 759 | 759 | |
| 760 | - if ( is_admin() ) { |
|
| 761 | - getpaid_admin()->{"show_$type"}( $message ); |
|
| 762 | - } |
|
| 760 | + if ( is_admin() ) { |
|
| 761 | + getpaid_admin()->{"show_$type"}( $message ); |
|
| 762 | + } |
|
| 763 | 763 | |
| 764 | - wpinv_set_error( $code, $message, $type ); |
|
| 764 | + wpinv_set_error( $code, $message, $type ); |
|
| 765 | 765 | |
| 766 | - } |
|
| 766 | + } |
|
| 767 | 767 | |
| 768 | 768 | } |
@@ -13,58 +13,58 @@ discard block |
||
| 13 | 13 | class GetPaid_Authorize_Net_Gateway extends GetPaid_Authorize_Net_Legacy_Gateway { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id = 'authorizenet'; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * An array of features that this gateway supports. |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 23 | + * An array of features that this gateway supports. |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | 27 | protected $supports = array( 'subscription', 'sandbox', 'tokens', 'addons', 'single_subscription_group', 'multiple_subscription_groups' ); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Payment method order. |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 30 | + * Payment method order. |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | 34 | public $order = 4; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * Endpoint for requests from Authorize.net. |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - protected $notify_url; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Endpoint for requests to Authorize.net. |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 37 | + * Endpoint for requests from Authorize.net. |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + protected $notify_url; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Endpoint for requests to Authorize.net. |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | 48 | protected $endpoint; |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | - * Currencies this gateway is allowed for. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
| 51 | + * Currencies this gateway is allowed for. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | - * URL to view a transaction. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 58 | + * URL to view a transaction. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | 62 | public $view_transaction_url = 'https://{sandbox}authorize.net/ui/themes/sandbox/Transaction/TransactionReceipt.aspx?transid=%s'; |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | - * Class constructor. |
|
| 66 | - */ |
|
| 67 | - public function __construct() { |
|
| 65 | + * Class constructor. |
|
| 66 | + */ |
|
| 67 | + public function __construct() { |
|
| 68 | 68 | |
| 69 | 69 | $this->title = __( 'Credit Card / Debit Card', 'invoicing' ); |
| 70 | 70 | $this->method_title = __( 'Authorize.Net', 'invoicing' ); |
@@ -76,11 +76,11 @@ discard block |
||
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | - * Displays the payment method select field. |
|
| 80 | - * |
|
| 81 | - * @param int $invoice_id 0 or invoice id. |
|
| 82 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 83 | - */ |
|
| 79 | + * Displays the payment method select field. |
|
| 80 | + * |
|
| 81 | + * @param int $invoice_id 0 or invoice id. |
|
| 82 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 83 | + */ |
|
| 84 | 84 | public function payment_fields( $invoice_id, $form ) { |
| 85 | 85 | |
| 86 | 86 | // Let the user select a payment method. |
@@ -91,16 +91,16 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | - * Creates a customer profile. |
|
| 95 | - * |
|
| 96 | - * |
|
| 97 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 94 | + * Creates a customer profile. |
|
| 95 | + * |
|
| 96 | + * |
|
| 97 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 98 | 98 | * @param array $submission_data Posted checkout fields. |
| 99 | 99 | * @param bool $save Whether or not to save the payment as a token. |
| 100 | 100 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
| 101 | - * @return string|WP_Error Payment profile id. |
|
| 102 | - */ |
|
| 103 | - public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
| 101 | + * @return string|WP_Error Payment profile id. |
|
| 102 | + */ |
|
| 103 | + public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
| 104 | 104 | |
| 105 | 105 | // Remove non-digits from the number |
| 106 | 106 | $submission_data['authorizenet']['cc_number'] = preg_replace( '/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
@@ -182,14 +182,14 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** |
| 185 | - * Retrieves a customer profile. |
|
| 186 | - * |
|
| 187 | - * |
|
| 188 | - * @param string $profile_id profile id. |
|
| 189 | - * @return string|WP_Error Profile id. |
|
| 185 | + * Retrieves a customer profile. |
|
| 186 | + * |
|
| 187 | + * |
|
| 188 | + * @param string $profile_id profile id. |
|
| 189 | + * @return string|WP_Error Profile id. |
|
| 190 | 190 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-profile |
| 191 | - */ |
|
| 192 | - public function get_customer_profile( $profile_id ) { |
|
| 191 | + */ |
|
| 192 | + public function get_customer_profile( $profile_id ) { |
|
| 193 | 193 | |
| 194 | 194 | // Generate args. |
| 195 | 195 | $args = array( |
@@ -204,17 +204,17 @@ discard block |
||
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | - * Creates a customer profile. |
|
| 208 | - * |
|
| 209 | - * |
|
| 207 | + * Creates a customer profile. |
|
| 208 | + * |
|
| 209 | + * |
|
| 210 | 210 | * @param string $profile_id profile id. |
| 211 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 211 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 212 | 212 | * @param array $submission_data Posted checkout fields. |
| 213 | 213 | * @param bool $save Whether or not to save the payment as a token. |
| 214 | 214 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
| 215 | - * @return string|WP_Error Profile id. |
|
| 216 | - */ |
|
| 217 | - public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
| 215 | + * @return string|WP_Error Profile id. |
|
| 216 | + */ |
|
| 217 | + public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
| 218 | 218 | |
| 219 | 219 | // Remove non-digits from the number |
| 220 | 220 | $submission_data['authorizenet']['cc_number'] = preg_replace( '/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
@@ -302,13 +302,13 @@ discard block |
||
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
| 305 | - * Retrieves payment details from cache. |
|
| 306 | - * |
|
| 307 | - * |
|
| 305 | + * Retrieves payment details from cache. |
|
| 306 | + * |
|
| 307 | + * |
|
| 308 | 308 | * @param array $payment_details. |
| 309 | - * @return array|false Profile id. |
|
| 310 | - */ |
|
| 311 | - public function retrieve_payment_profile_from_cache( $payment_details, $customer_profile, $invoice ) { |
|
| 309 | + * @return array|false Profile id. |
|
| 310 | + */ |
|
| 311 | + public function retrieve_payment_profile_from_cache( $payment_details, $customer_profile, $invoice ) { |
|
| 312 | 312 | |
| 313 | 313 | $cached_information = get_option( 'getpaid_authorize_net_cached_profiles', array() ); |
| 314 | 314 | $payment_details = hash_hmac( 'sha256', json_encode( $payment_details ), SECURE_AUTH_KEY ); |
@@ -333,13 +333,13 @@ discard block |
||
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | /** |
| 336 | - * Securely adds payment details to cache. |
|
| 337 | - * |
|
| 338 | - * |
|
| 336 | + * Securely adds payment details to cache. |
|
| 337 | + * |
|
| 338 | + * |
|
| 339 | 339 | * @param array $payment_details. |
| 340 | 340 | * @param string $payment_profile_id. |
| 341 | - */ |
|
| 342 | - public function add_payment_profile_to_cache( $payment_details, $payment_profile_id ) { |
|
| 341 | + */ |
|
| 342 | + public function add_payment_profile_to_cache( $payment_details, $payment_profile_id ) { |
|
| 343 | 343 | |
| 344 | 344 | $cached_information = get_option( 'getpaid_authorize_net_cached_profiles', array() ); |
| 345 | 345 | $cached_information = is_array( $cached_information ) ? $cached_information : array(); |
@@ -351,15 +351,15 @@ discard block |
||
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | /** |
| 354 | - * Retrieves a customer payment profile. |
|
| 355 | - * |
|
| 356 | - * |
|
| 357 | - * @param string $customer_profile_id customer profile id. |
|
| 354 | + * Retrieves a customer payment profile. |
|
| 355 | + * |
|
| 356 | + * |
|
| 357 | + * @param string $customer_profile_id customer profile id. |
|
| 358 | 358 | * @param string $payment_profile_id payment profile id. |
| 359 | - * @return string|WP_Error Profile id. |
|
| 359 | + * @return string|WP_Error Profile id. |
|
| 360 | 360 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-payment-profile |
| 361 | - */ |
|
| 362 | - public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
| 361 | + */ |
|
| 362 | + public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
| 363 | 363 | |
| 364 | 364 | // Generate args. |
| 365 | 365 | $args = array( |
@@ -375,15 +375,15 @@ discard block |
||
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
| 378 | - * Charges a customer payment profile. |
|
| 379 | - * |
|
| 378 | + * Charges a customer payment profile. |
|
| 379 | + * |
|
| 380 | 380 | * @param string $customer_profile_id customer profile id. |
| 381 | 381 | * @param string $payment_profile_id payment profile id. |
| 382 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 382 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 383 | 383 | * @link https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-customer-profile |
| 384 | - * @return WP_Error|object |
|
| 385 | - */ |
|
| 386 | - public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
| 384 | + * @return WP_Error|object |
|
| 385 | + */ |
|
| 386 | + public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
| 387 | 387 | |
| 388 | 388 | // Generate args. |
| 389 | 389 | $args = array( |
@@ -429,41 +429,41 @@ discard block |
||
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | /** |
| 432 | - * Processes a customer charge. |
|
| 433 | - * |
|
| 432 | + * Processes a customer charge. |
|
| 433 | + * |
|
| 434 | 434 | * @param stdClass $result Api response. |
| 435 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 436 | - */ |
|
| 437 | - public function process_charge_response( $result, $invoice ) { |
|
| 435 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 436 | + */ |
|
| 437 | + public function process_charge_response( $result, $invoice ) { |
|
| 438 | 438 | |
| 439 | 439 | wpinv_clear_errors(); |
| 440 | - $response_code = (int) $result->transactionResponse->responseCode; |
|
| 440 | + $response_code = (int) $result->transactionResponse->responseCode; |
|
| 441 | 441 | |
| 442 | - // Succeeded. |
|
| 443 | - if ( 1 == $response_code || 4 == $response_code ) { |
|
| 442 | + // Succeeded. |
|
| 443 | + if ( 1 == $response_code || 4 == $response_code ) { |
|
| 444 | 444 | |
| 445 | - // Maybe set a transaction id. |
|
| 446 | - if ( ! empty( $result->transactionResponse->transId ) ) { |
|
| 447 | - $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
| 448 | - } |
|
| 445 | + // Maybe set a transaction id. |
|
| 446 | + if ( ! empty( $result->transactionResponse->transId ) ) { |
|
| 447 | + $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
| 448 | + } |
|
| 449 | 449 | |
| 450 | - $invoice->add_note( sprintf( __( 'Authentication code: %1$s (%2$s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
| 450 | + $invoice->add_note( sprintf( __( 'Authentication code: %1$s (%2$s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
| 451 | 451 | |
| 452 | - if ( 1 == $response_code ) { |
|
| 453 | - return $invoice->mark_paid(); |
|
| 454 | - } |
|
| 452 | + if ( 1 == $response_code ) { |
|
| 453 | + return $invoice->mark_paid(); |
|
| 454 | + } |
|
| 455 | 455 | |
| 456 | - $invoice->set_status( 'wpi-onhold' ); |
|
| 457 | - $invoice->add_note( |
|
| 456 | + $invoice->set_status( 'wpi-onhold' ); |
|
| 457 | + $invoice->add_note( |
|
| 458 | 458 | sprintf( |
| 459 | 459 | __( 'Held for review: %s', 'invoicing' ), |
| 460 | 460 | $result->transactionResponse->messages->message[0]->description |
| 461 | 461 | ) |
| 462 | - ); |
|
| 462 | + ); |
|
| 463 | 463 | |
| 464 | - return $invoice->save(); |
|
| 464 | + return $invoice->save(); |
|
| 465 | 465 | |
| 466 | - } |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | 468 | wpinv_set_error( 'card_declined', __( 'Credit card declined.', 'invoicing' ) ); |
| 469 | 469 | |
@@ -475,13 +475,13 @@ discard block |
||
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | /** |
| 478 | - * Returns payment information. |
|
| 479 | - * |
|
| 480 | - * |
|
| 481 | - * @param array $card Card details. |
|
| 482 | - * @return array |
|
| 483 | - */ |
|
| 484 | - public function get_payment_information( $card ) { |
|
| 478 | + * Returns payment information. |
|
| 479 | + * |
|
| 480 | + * |
|
| 481 | + * @param array $card Card details. |
|
| 482 | + * @return array |
|
| 483 | + */ |
|
| 484 | + public function get_payment_information( $card ) { |
|
| 485 | 485 | return array( |
| 486 | 486 | |
| 487 | 487 | 'creditCard' => array( |
@@ -494,25 +494,25 @@ discard block |
||
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | /** |
| 497 | - * Returns the customer profile meta name. |
|
| 498 | - * |
|
| 499 | - * |
|
| 500 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 501 | - * @return string |
|
| 502 | - */ |
|
| 503 | - public function get_customer_profile_meta_name( $invoice ) { |
|
| 497 | + * Returns the customer profile meta name. |
|
| 498 | + * |
|
| 499 | + * |
|
| 500 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 501 | + * @return string |
|
| 502 | + */ |
|
| 503 | + public function get_customer_profile_meta_name( $invoice ) { |
|
| 504 | 504 | return $this->is_sandbox( $invoice ) ? 'getpaid_authorizenet_sandbox_customer_profile_id' : 'getpaid_authorizenet_customer_profile_id'; |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | /** |
| 508 | - * Validates the submitted data. |
|
| 509 | - * |
|
| 510 | - * |
|
| 511 | - * @param array $submission_data Posted checkout fields. |
|
| 508 | + * Validates the submitted data. |
|
| 509 | + * |
|
| 510 | + * |
|
| 511 | + * @param array $submission_data Posted checkout fields. |
|
| 512 | 512 | * @param WPInv_Invoice $invoice |
| 513 | - * @return WP_Error|string The payment profile id |
|
| 514 | - */ |
|
| 515 | - public function validate_submission_data( $submission_data, $invoice ) { |
|
| 513 | + * @return WP_Error|string The payment profile id |
|
| 514 | + */ |
|
| 515 | + public function validate_submission_data( $submission_data, $invoice ) { |
|
| 516 | 516 | |
| 517 | 517 | // Validate authentication details. |
| 518 | 518 | $auth = $this->get_auth_params(); |
@@ -544,13 +544,13 @@ discard block |
||
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | /** |
| 547 | - * Returns invoice line items. |
|
| 548 | - * |
|
| 549 | - * |
|
| 550 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 551 | - * @return array |
|
| 552 | - */ |
|
| 553 | - public function get_line_items( $invoice ) { |
|
| 547 | + * Returns invoice line items. |
|
| 548 | + * |
|
| 549 | + * |
|
| 550 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 551 | + * @return array |
|
| 552 | + */ |
|
| 553 | + public function get_line_items( $invoice ) { |
|
| 554 | 554 | $items = array(); |
| 555 | 555 | |
| 556 | 556 | foreach ( $invoice->get_items() as $item ) { |
@@ -587,15 +587,15 @@ discard block |
||
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
| 590 | - * Process Payment. |
|
| 591 | - * |
|
| 592 | - * |
|
| 593 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 594 | - * @param array $submission_data Posted checkout fields. |
|
| 595 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 596 | - * @return array |
|
| 597 | - */ |
|
| 598 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 590 | + * Process Payment. |
|
| 591 | + * |
|
| 592 | + * |
|
| 593 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 594 | + * @param array $submission_data Posted checkout fields. |
|
| 595 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 596 | + * @return array |
|
| 597 | + */ |
|
| 598 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 599 | 599 | |
| 600 | 600 | // Validate the submitted data. |
| 601 | 601 | $payment_profile_id = $this->validate_submission_data( $submission_data, $invoice ); |
@@ -628,45 +628,45 @@ discard block |
||
| 628 | 628 | |
| 629 | 629 | exit; |
| 630 | 630 | |
| 631 | - } |
|
| 631 | + } |
|
| 632 | 632 | |
| 633 | - /** |
|
| 634 | - * Processes the initial payment. |
|
| 635 | - * |
|
| 633 | + /** |
|
| 634 | + * Processes the initial payment. |
|
| 635 | + * |
|
| 636 | 636 | * @param WPInv_Invoice $invoice Invoice. |
| 637 | - */ |
|
| 638 | - protected function process_initial_payment( $invoice ) { |
|
| 637 | + */ |
|
| 638 | + protected function process_initial_payment( $invoice ) { |
|
| 639 | 639 | |
| 640 | - $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 640 | + $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 641 | 641 | $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
| 642 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
| 642 | + $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
| 643 | 643 | |
| 644 | - // Do we have an error? |
|
| 645 | - if ( is_wp_error( $result ) ) { |
|
| 646 | - wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
| 647 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 648 | - } |
|
| 644 | + // Do we have an error? |
|
| 645 | + if ( is_wp_error( $result ) ) { |
|
| 646 | + wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
| 647 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 648 | + } |
|
| 649 | 649 | |
| 650 | - // Process the response. |
|
| 651 | - $this->process_charge_response( $result, $invoice ); |
|
| 650 | + // Process the response. |
|
| 651 | + $this->process_charge_response( $result, $invoice ); |
|
| 652 | 652 | |
| 653 | - if ( wpinv_get_errors() ) { |
|
| 654 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 655 | - } |
|
| 653 | + if ( wpinv_get_errors() ) { |
|
| 654 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 655 | + } |
|
| 656 | 656 | |
| 657 | - } |
|
| 657 | + } |
|
| 658 | 658 | |
| 659 | 659 | /** |
| 660 | - * Processes recurring payments. |
|
| 661 | - * |
|
| 660 | + * Processes recurring payments. |
|
| 661 | + * |
|
| 662 | 662 | * @param WPInv_Invoice $invoice Invoice. |
| 663 | 663 | * @param WPInv_Subscription[]|WPInv_Subscription $subscriptions Subscriptions. |
| 664 | - */ |
|
| 665 | - public function process_subscription( $invoice, $subscriptions ) { |
|
| 664 | + */ |
|
| 665 | + public function process_subscription( $invoice, $subscriptions ) { |
|
| 666 | 666 | |
| 667 | 667 | // Check if there is an initial amount to charge. |
| 668 | 668 | if ( (float) $invoice->get_total() > 0 ) { |
| 669 | - $this->process_initial_payment( $invoice ); |
|
| 669 | + $this->process_initial_payment( $invoice ); |
|
| 670 | 670 | } |
| 671 | 671 | |
| 672 | 672 | // Activate the subscriptions. |
@@ -684,36 +684,36 @@ discard block |
||
| 684 | 684 | } |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - // Redirect to the success page. |
|
| 687 | + // Redirect to the success page. |
|
| 688 | 688 | wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
| 689 | 689 | |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | - /** |
|
| 693 | - * (Maybe) renews an authorize.net subscription profile. |
|
| 694 | - * |
|
| 695 | - * |
|
| 692 | + /** |
|
| 693 | + * (Maybe) renews an authorize.net subscription profile. |
|
| 694 | + * |
|
| 695 | + * |
|
| 696 | 696 | * @param WPInv_Subscription $subscription |
| 697 | - */ |
|
| 698 | - public function maybe_renew_subscription( $subscription ) { |
|
| 697 | + */ |
|
| 698 | + public function maybe_renew_subscription( $subscription ) { |
|
| 699 | 699 | |
| 700 | 700 | // Ensure its our subscription && it's active. |
| 701 | 701 | if ( $this->id == $subscription->get_gateway() && $subscription->has_status( 'active trialling' ) ) { |
| 702 | 702 | $this->renew_subscription( $subscription ); |
| 703 | 703 | } |
| 704 | 704 | |
| 705 | - } |
|
| 705 | + } |
|
| 706 | 706 | |
| 707 | 707 | /** |
| 708 | - * Renews a subscription. |
|
| 709 | - * |
|
| 708 | + * Renews a subscription. |
|
| 709 | + * |
|
| 710 | 710 | * @param WPInv_Subscription $subscription |
| 711 | - */ |
|
| 712 | - public function renew_subscription( $subscription ) { |
|
| 711 | + */ |
|
| 712 | + public function renew_subscription( $subscription ) { |
|
| 713 | 713 | |
| 714 | - // Generate the renewal invoice. |
|
| 715 | - $new_invoice = $subscription->create_payment(); |
|
| 716 | - $old_invoice = $subscription->get_parent_payment(); |
|
| 714 | + // Generate the renewal invoice. |
|
| 715 | + $new_invoice = $subscription->create_payment(); |
|
| 716 | + $old_invoice = $subscription->get_parent_payment(); |
|
| 717 | 717 | |
| 718 | 718 | if ( empty( $new_invoice ) ) { |
| 719 | 719 | $old_invoice->add_note( __( 'Error generating a renewal invoice.', 'invoicing' ), false, false, false ); |
@@ -722,37 +722,37 @@ discard block |
||
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | // Charge the payment method. |
| 725 | - $payment_profile_id = get_post_meta( $old_invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 726 | - $customer_profile = get_user_meta( $old_invoice->get_user_id(), $this->get_customer_profile_meta_name( $old_invoice ), true ); |
|
| 727 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
| 728 | - |
|
| 729 | - // Do we have an error? |
|
| 730 | - if ( is_wp_error( $result ) ) { |
|
| 731 | - |
|
| 732 | - $old_invoice->add_note( |
|
| 733 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
| 734 | - true, |
|
| 735 | - false, |
|
| 736 | - true |
|
| 737 | - ); |
|
| 738 | - $subscription->failing(); |
|
| 739 | - return; |
|
| 740 | - |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - // Process the response. |
|
| 744 | - $this->process_charge_response( $result, $new_invoice ); |
|
| 745 | - |
|
| 746 | - if ( wpinv_get_errors() ) { |
|
| 747 | - |
|
| 748 | - $old_invoice->add_note( |
|
| 749 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
| 750 | - true, |
|
| 751 | - false, |
|
| 752 | - true |
|
| 753 | - ); |
|
| 754 | - $subscription->failing(); |
|
| 755 | - return; |
|
| 725 | + $payment_profile_id = get_post_meta( $old_invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 726 | + $customer_profile = get_user_meta( $old_invoice->get_user_id(), $this->get_customer_profile_meta_name( $old_invoice ), true ); |
|
| 727 | + $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
| 728 | + |
|
| 729 | + // Do we have an error? |
|
| 730 | + if ( is_wp_error( $result ) ) { |
|
| 731 | + |
|
| 732 | + $old_invoice->add_note( |
|
| 733 | + sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
| 734 | + true, |
|
| 735 | + false, |
|
| 736 | + true |
|
| 737 | + ); |
|
| 738 | + $subscription->failing(); |
|
| 739 | + return; |
|
| 740 | + |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + // Process the response. |
|
| 744 | + $this->process_charge_response( $result, $new_invoice ); |
|
| 745 | + |
|
| 746 | + if ( wpinv_get_errors() ) { |
|
| 747 | + |
|
| 748 | + $old_invoice->add_note( |
|
| 749 | + sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
| 750 | + true, |
|
| 751 | + false, |
|
| 752 | + true |
|
| 753 | + ); |
|
| 754 | + $subscription->failing(); |
|
| 755 | + return; |
|
| 756 | 756 | |
| 757 | 757 | } |
| 758 | 758 | |
@@ -761,13 +761,13 @@ discard block |
||
| 761 | 761 | } |
| 762 | 762 | |
| 763 | 763 | /** |
| 764 | - * Processes invoice addons. |
|
| 765 | - * |
|
| 766 | - * @param WPInv_Invoice $invoice |
|
| 767 | - * @param GetPaid_Form_Item[] $items |
|
| 768 | - * @return WPInv_Invoice |
|
| 769 | - */ |
|
| 770 | - public function process_addons( $invoice, $items ) { |
|
| 764 | + * Processes invoice addons. |
|
| 765 | + * |
|
| 766 | + * @param WPInv_Invoice $invoice |
|
| 767 | + * @param GetPaid_Form_Item[] $items |
|
| 768 | + * @return WPInv_Invoice |
|
| 769 | + */ |
|
| 770 | + public function process_addons( $invoice, $items ) { |
|
| 771 | 771 | |
| 772 | 772 | global $getpaid_authorize_addons; |
| 773 | 773 | |
@@ -786,7 +786,7 @@ discard block |
||
| 786 | 786 | $invoice->recalculate_total(); |
| 787 | 787 | |
| 788 | 788 | $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
| 789 | - $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
| 789 | + $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
| 790 | 790 | |
| 791 | 791 | add_filter( 'getpaid_authorizenet_charge_customer_payment_profile_args', array( $this, 'filter_addons_request' ), 10, 2 ); |
| 792 | 792 | $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
@@ -801,11 +801,11 @@ discard block |
||
| 801 | 801 | } |
| 802 | 802 | |
| 803 | 803 | /** |
| 804 | - * Processes invoice addons. |
|
| 805 | - * |
|
| 804 | + * Processes invoice addons. |
|
| 805 | + * |
|
| 806 | 806 | * @param array $args |
| 807 | - * @return array |
|
| 808 | - */ |
|
| 807 | + * @return array |
|
| 808 | + */ |
|
| 809 | 809 | public function filter_addons_request( $args ) { |
| 810 | 810 | |
| 811 | 811 | global $getpaid_authorize_addons; |
@@ -839,11 +839,11 @@ discard block |
||
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | /** |
| 842 | - * Filters the gateway settings. |
|
| 843 | - * |
|
| 844 | - * @param array $admin_settings |
|
| 845 | - */ |
|
| 846 | - public function admin_settings( $admin_settings ) { |
|
| 842 | + * Filters the gateway settings. |
|
| 843 | + * |
|
| 844 | + * @param array $admin_settings |
|
| 845 | + */ |
|
| 846 | + public function admin_settings( $admin_settings ) { |
|
| 847 | 847 | |
| 848 | 848 | $currencies = sprintf( |
| 849 | 849 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -883,7 +883,7 @@ discard block |
||
| 883 | 883 | 'readonly' => true, |
| 884 | 884 | ); |
| 885 | 885 | |
| 886 | - return $admin_settings; |
|
| 887 | - } |
|
| 886 | + return $admin_settings; |
|
| 887 | + } |
|
| 888 | 888 | |
| 889 | 889 | } |
@@ -13,65 +13,65 @@ discard block |
||
| 13 | 13 | class GetPaid_Worldpay_Gateway extends GetPaid_Payment_Gateway { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id = 'worldpay'; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Payment method order. |
|
| 24 | - * |
|
| 25 | - * @var int |
|
| 26 | - */ |
|
| 23 | + * Payment method order. |
|
| 24 | + * |
|
| 25 | + * @var int |
|
| 26 | + */ |
|
| 27 | 27 | public $order = 5; |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Endpoint for requests from Worldpay. |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $notify_url; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Endpoint for requests to Worldpay. |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 30 | + * Endpoint for requests from Worldpay. |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $notify_url; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Endpoint for requests to Worldpay. |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | 41 | protected $endpoint; |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * An array of features that this gateway supports. |
|
| 45 | - * |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 44 | + * An array of features that this gateway supports. |
|
| 45 | + * |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | 48 | protected $supports = array( 'sandbox' ); |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | - * Currencies this gateway is allowed for. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
| 51 | + * Currencies this gateway is allowed for. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | - * URL to view a transaction. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 58 | + * URL to view a transaction. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | 62 | public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s'; |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | - * URL to view a subscription. |
|
| 66 | - * |
|
| 67 | - * @var string |
|
| 68 | - */ |
|
| 69 | - public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
| 65 | + * URL to view a subscription. |
|
| 66 | + * |
|
| 67 | + * @var string |
|
| 68 | + */ |
|
| 69 | + public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | - * Class constructor. |
|
| 73 | - */ |
|
| 74 | - public function __construct() { |
|
| 72 | + * Class constructor. |
|
| 73 | + */ |
|
| 74 | + public function __construct() { |
|
| 75 | 75 | |
| 76 | 76 | $this->method_title = __( 'Worldpay', 'invoicing' ); |
| 77 | 77 | $this->title = __( 'Worldpay - Credit Card / Debit Card', 'invoicing' ); |
@@ -85,15 +85,15 @@ discard block |
||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | - * Process Payment. |
|
| 89 | - * |
|
| 90 | - * |
|
| 91 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 92 | - * @param array $submission_data Posted checkout fields. |
|
| 93 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 94 | - * @return array |
|
| 95 | - */ |
|
| 96 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 88 | + * Process Payment. |
|
| 89 | + * |
|
| 90 | + * |
|
| 91 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 92 | + * @param array $submission_data Posted checkout fields. |
|
| 93 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 94 | + * @return array |
|
| 95 | + */ |
|
| 96 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 97 | 97 | |
| 98 | 98 | // Get redirect url. |
| 99 | 99 | $worldpay_redirect = esc_url( $this->get_request_url( $invoice ) ); |
@@ -128,31 +128,31 @@ discard block |
||
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | - * Get the Worldpay request URL for an invoice. |
|
| 132 | - * |
|
| 133 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function get_request_url( $invoice ) { |
|
| 131 | + * Get the Worldpay request URL for an invoice. |
|
| 132 | + * |
|
| 133 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function get_request_url( $invoice ) { |
|
| 137 | 137 | |
| 138 | 138 | // Endpoint for this request |
| 139 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
| 139 | + $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
| 140 | 140 | |
| 141 | 141 | return $this->endpoint; |
| 142 | 142 | |
| 143 | - } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | - * Get Worldpay Args for passing to Worldpay. |
|
| 147 | - * |
|
| 148 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 151 | - protected function get_worldpay_args( $invoice ) { |
|
| 152 | - |
|
| 153 | - return apply_filters( |
|
| 154 | - 'getpaid_worldpay_args', |
|
| 155 | - array( |
|
| 146 | + * Get Worldpay Args for passing to Worldpay. |
|
| 147 | + * |
|
| 148 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | + protected function get_worldpay_args( $invoice ) { |
|
| 152 | + |
|
| 153 | + return apply_filters( |
|
| 154 | + 'getpaid_worldpay_args', |
|
| 155 | + array( |
|
| 156 | 156 | 'amount' => wpinv_sanitize_amount( $invoice->get_total() ), // mandatory |
| 157 | 157 | 'cartId' => wpinv_clean( $invoice->get_number() ), // mandatory reference for the item purchased |
| 158 | 158 | 'currency' => wpinv_clean( $invoice->get_currency() ), // mandatory |
@@ -177,18 +177,18 @@ discard block |
||
| 177 | 177 | 'countryString' => wpinv_clean( wpinv_country_name( $invoice->get_country() ) ), |
| 178 | 178 | 'compName' => wpinv_clean( $invoice->get_company() ), |
| 179 | 179 | ), |
| 180 | - $invoice |
|
| 181 | - ); |
|
| 180 | + $invoice |
|
| 181 | + ); |
|
| 182 | 182 | |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
| 186 | - * Secures worldpay args with an md5 hash. |
|
| 187 | - * |
|
| 188 | - * @param array $args Gateway args. |
|
| 189 | - * @return array |
|
| 190 | - */ |
|
| 191 | - public function hash_args( $args ) { |
|
| 186 | + * Secures worldpay args with an md5 hash. |
|
| 187 | + * |
|
| 188 | + * @param array $args Gateway args. |
|
| 189 | + * @return array |
|
| 190 | + */ |
|
| 191 | + public function hash_args( $args ) { |
|
| 192 | 192 | |
| 193 | 193 | $md5_secret = $this->get_option( 'md5_secret' ); |
| 194 | 194 | |
@@ -204,16 +204,16 @@ discard block |
||
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | - * Processes ipns and marks payments as complete. |
|
| 208 | - * |
|
| 209 | - * @return void |
|
| 210 | - */ |
|
| 211 | - public function verify_ipn() { |
|
| 207 | + * Processes ipns and marks payments as complete. |
|
| 208 | + * |
|
| 209 | + * @return void |
|
| 210 | + */ |
|
| 211 | + public function verify_ipn() { |
|
| 212 | 212 | |
| 213 | 213 | // Validate the IPN. |
| 214 | 214 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
| 215 | - wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
| 216 | - } |
|
| 215 | + wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | 218 | // Process the IPN. |
| 219 | 219 | $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
@@ -229,8 +229,8 @@ discard block |
||
| 229 | 229 | $invoice->set_transaction_id( wpinv_clean( $posted['transId'] ) ); |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - // Update the ip address. |
|
| 233 | - if ( ! empty( $posted['ipAddress'] ) ) { |
|
| 232 | + // Update the ip address. |
|
| 233 | + if ( ! empty( $posted['ipAddress'] ) ) { |
|
| 234 | 234 | $invoice->set_ip( wpinv_clean( $posted['ipAddress'] ) ); |
| 235 | 235 | } |
| 236 | 236 | |
@@ -257,9 +257,9 @@ discard block |
||
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
| 260 | - * Check Worldpay IPN validity. |
|
| 261 | - */ |
|
| 262 | - public function validate_ipn() { |
|
| 260 | + * Check Worldpay IPN validity. |
|
| 261 | + */ |
|
| 262 | + public function validate_ipn() { |
|
| 263 | 263 | |
| 264 | 264 | wpinv_error_log( 'Validating Worldpay IPN response' ); |
| 265 | 265 | |
@@ -305,11 +305,11 @@ discard block |
||
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | /** |
| 308 | - * Filters the gateway settings. |
|
| 309 | - * |
|
| 310 | - * @param array $admin_settings |
|
| 311 | - */ |
|
| 312 | - public function admin_settings( $admin_settings ) { |
|
| 308 | + * Filters the gateway settings. |
|
| 309 | + * |
|
| 310 | + * @param array $admin_settings |
|
| 311 | + */ |
|
| 312 | + public function admin_settings( $admin_settings ) { |
|
| 313 | 313 | |
| 314 | 314 | $currencies = sprintf( |
| 315 | 315 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | 'readonly' => true, |
| 351 | 351 | ); |
| 352 | 352 | |
| 353 | - return $admin_settings; |
|
| 354 | - } |
|
| 353 | + return $admin_settings; |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | 356 | } |
@@ -13,299 +13,299 @@ |
||
| 13 | 13 | class GetPaid_Subscription_Notification_Emails { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * The array of subscription email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $subscription_actions; |
|
| 16 | + * The array of subscription email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $subscription_actions; |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Class constructor |
|
| 23 | + * Class constructor |
|
| 24 | 24 | * |
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->subscription_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_subscription_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | - 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | - 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | - 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | - 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | - ) |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - $this->init_hooks(); |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->subscription_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_subscription_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | + 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | + 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | + 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | + 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | + ) |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + $this->init_hooks(); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Registers email hooks. |
|
| 45 | - */ |
|
| 46 | - public function init_hooks() { |
|
| 47 | - |
|
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | - |
|
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | - |
|
| 53 | - if ( ! $email->is_active() ) { |
|
| 54 | - continue; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | - continue; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Filters subscription merge tags. |
|
| 70 | - * |
|
| 71 | - * @param array $merge_tags |
|
| 72 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | - */ |
|
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 75 | - |
|
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | - $merge_tags = array_merge( |
|
| 78 | - $merge_tags, |
|
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $merge_tags; |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Generates subscription merge tags. |
|
| 89 | - * |
|
| 90 | - * @param WPInv_Subscription $subscription |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | - |
|
| 95 | - // Abort if it does not exist. |
|
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 97 | - return array(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 101 | - return array( |
|
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | - '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Checks if we should send a notification for a subscription. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function should_send_notification( $invoice ) { |
|
| 123 | - return 0 != $invoice->get_id(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns notification recipients. |
|
| 128 | - * |
|
| 129 | - * @param WPInv_Invoice $invoice |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 134 | - |
|
| 135 | - $cc = $invoice->get_email_cc(); |
|
| 136 | - |
|
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $recipients; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Helper function to send an email. |
|
| 147 | - * |
|
| 148 | - * @param WPInv_Subscription $subscription |
|
| 149 | - * @param GetPaid_Notification_Email $email |
|
| 150 | - * @param string $type |
|
| 151 | - * @param array $extra_args Extra template args. |
|
| 152 | - */ |
|
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | - |
|
| 155 | - // Abort in case the parent invoice does not exist. |
|
| 156 | - $invoice = $subscription->get_parent_invoice(); |
|
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | - |
|
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 168 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | - $merge_tags = $email->get_merge_tags(); |
|
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | - $attachments = $email->get_attachments(); |
|
| 173 | - |
|
| 174 | - $result = $mailer->send( |
|
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | - $subject, |
|
| 177 | - $content, |
|
| 178 | - $attachments |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - // Maybe send a copy to the admin. |
|
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 183 | - $mailer->send( |
|
| 184 | - wpinv_get_admin_email(), |
|
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | - $content, |
|
| 187 | - $attachments |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ( $result ) { |
|
| 192 | - $invoice->add_system_note( |
|
| 193 | - sprintf( |
|
| 194 | - __( 'Successfully sent %1$s notification email to %2$s.', 'invoicing' ), |
|
| 195 | - sanitize_key( $type ), |
|
| 196 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 197 | - ) |
|
| 198 | - ); |
|
| 199 | - } else { |
|
| 200 | - $invoice->add_system_note( |
|
| 201 | - sprintf( |
|
| 202 | - __( 'Failed sending %1$s notification email to %2$s.', 'invoicing' ), |
|
| 203 | - sanitize_key( $type ), |
|
| 204 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 205 | - ) |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 210 | - |
|
| 211 | - } |
|
| 44 | + * Registers email hooks. |
|
| 45 | + */ |
|
| 46 | + public function init_hooks() { |
|
| 47 | + |
|
| 48 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | + foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | + |
|
| 51 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | + |
|
| 53 | + if ( ! $email->is_active() ) { |
|
| 54 | + continue; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( method_exists( $this, $email_type ) ) { |
|
| 58 | + add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 212 | 67 | |
| 213 | 68 | /** |
| 214 | - * Sends a new trial notification. |
|
| 215 | - * |
|
| 216 | - * @param WPInv_Subscription $subscription |
|
| 217 | - */ |
|
| 218 | - public function subscription_trial( $subscription ) { |
|
| 69 | + * Filters subscription merge tags. |
|
| 70 | + * |
|
| 71 | + * @param array $merge_tags |
|
| 72 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | + */ |
|
| 74 | + public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 219 | 75 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 76 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | + $merge_tags = array_merge( |
|
| 78 | + $merge_tags, |
|
| 79 | + $this->get_subscription_merge_tags( $object ) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 222 | 82 | |
| 223 | - } |
|
| 83 | + return $merge_tags; |
|
| 224 | 84 | |
| 225 | - /** |
|
| 226 | - * Sends a cancelled subscription notification. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Subscription $subscription |
|
| 229 | - */ |
|
| 230 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 231 | 86 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 87 | + /** |
|
| 88 | + * Generates subscription merge tags. |
|
| 89 | + * |
|
| 90 | + * @param WPInv_Subscription $subscription |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | + |
|
| 95 | + // Abort if it does not exist. |
|
| 96 | + if ( ! $subscription->get_id() ) { |
|
| 97 | + return array(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | + return array( |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | + '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | + '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | + '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | + '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | + '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | + ); |
|
| 234 | 113 | |
| 235 | - } |
|
| 114 | + } |
|
| 236 | 115 | |
| 237 | - /** |
|
| 238 | - * Sends a subscription expired notification. |
|
| 239 | - * |
|
| 240 | - * @param WPInv_Subscription $subscription |
|
| 241 | - */ |
|
| 242 | - public function subscription_expired( $subscription ) { |
|
| 116 | + /** |
|
| 117 | + * Checks if we should send a notification for a subscription. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function should_send_notification( $invoice ) { |
|
| 123 | + return 0 != $invoice->get_id(); |
|
| 124 | + } |
|
| 243 | 125 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 126 | + /** |
|
| 127 | + * Returns notification recipients. |
|
| 128 | + * |
|
| 129 | + * @param WPInv_Invoice $invoice |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + public function get_recipients( $invoice ) { |
|
| 133 | + $recipients = array( $invoice->get_email() ); |
|
| 246 | 134 | |
| 247 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 248 | 136 | |
| 249 | - /** |
|
| 250 | - * Sends a completed subscription notification. |
|
| 251 | - * |
|
| 252 | - * @param WPInv_Subscription $subscription |
|
| 253 | - */ |
|
| 254 | - public function subscription_complete( $subscription ) { |
|
| 137 | + if ( ! empty( $cc ) ) { |
|
| 138 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | + } |
|
| 255 | 141 | |
| 256 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 257 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 258 | 144 | |
| 259 | - } |
|
| 145 | + /** |
|
| 146 | + * Helper function to send an email. |
|
| 147 | + * |
|
| 148 | + * @param WPInv_Subscription $subscription |
|
| 149 | + * @param GetPaid_Notification_Email $email |
|
| 150 | + * @param string $type |
|
| 151 | + * @param array $extra_args Extra template args. |
|
| 152 | + */ |
|
| 153 | + public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | + |
|
| 155 | + // Abort in case the parent invoice does not exist. |
|
| 156 | + $invoice = $subscription->get_parent_invoice(); |
|
| 157 | + if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | + |
|
| 167 | + $recipients = $this->get_recipients( $invoice ); |
|
| 168 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | + $merge_tags = $email->get_merge_tags(); |
|
| 170 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | + $attachments = $email->get_attachments(); |
|
| 173 | + |
|
| 174 | + $result = $mailer->send( |
|
| 175 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | + $subject, |
|
| 177 | + $content, |
|
| 178 | + $attachments |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + // Maybe send a copy to the admin. |
|
| 182 | + if ( $email->include_admin_bcc() ) { |
|
| 183 | + $mailer->send( |
|
| 184 | + wpinv_get_admin_email(), |
|
| 185 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | + $content, |
|
| 187 | + $attachments |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ( $result ) { |
|
| 192 | + $invoice->add_system_note( |
|
| 193 | + sprintf( |
|
| 194 | + __( 'Successfully sent %1$s notification email to %2$s.', 'invoicing' ), |
|
| 195 | + sanitize_key( $type ), |
|
| 196 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 197 | + ) |
|
| 198 | + ); |
|
| 199 | + } else { |
|
| 200 | + $invoice->add_system_note( |
|
| 201 | + sprintf( |
|
| 202 | + __( 'Failed sending %1$s notification email to %2$s.', 'invoicing' ), |
|
| 203 | + sanitize_key( $type ), |
|
| 204 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 205 | + ) |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 260 | 210 | |
| 261 | - /** |
|
| 262 | - * Sends a subscription renewal reminder notification. |
|
| 263 | - * |
|
| 264 | - */ |
|
| 265 | - public function renewal_reminder() { |
|
| 211 | + } |
|
| 266 | 212 | |
| 267 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 213 | + /** |
|
| 214 | + * Sends a new trial notification. |
|
| 215 | + * |
|
| 216 | + * @param WPInv_Subscription $subscription |
|
| 217 | + */ |
|
| 218 | + public function subscription_trial( $subscription ) { |
|
| 268 | 219 | |
| 269 | - // Fetch reminder days. |
|
| 270 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 220 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 271 | 222 | |
| 272 | - // Abort if non is set. |
|
| 273 | - if ( empty( $reminder_days ) ) { |
|
| 274 | - return; |
|
| 275 | - } |
|
| 223 | + } |
|
| 276 | 224 | |
| 277 | - // Fetch matching subscriptions. |
|
| 225 | + /** |
|
| 226 | + * Sends a cancelled subscription notification. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Subscription $subscription |
|
| 229 | + */ |
|
| 230 | + public function subscription_cancelled( $subscription ) { |
|
| 231 | + |
|
| 232 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Sends a subscription expired notification. |
|
| 239 | + * |
|
| 240 | + * @param WPInv_Subscription $subscription |
|
| 241 | + */ |
|
| 242 | + public function subscription_expired( $subscription ) { |
|
| 243 | + |
|
| 244 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Sends a completed subscription notification. |
|
| 251 | + * |
|
| 252 | + * @param WPInv_Subscription $subscription |
|
| 253 | + */ |
|
| 254 | + public function subscription_complete( $subscription ) { |
|
| 255 | + |
|
| 256 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 257 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Sends a subscription renewal reminder notification. |
|
| 263 | + * |
|
| 264 | + */ |
|
| 265 | + public function renewal_reminder() { |
|
| 266 | + |
|
| 267 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 268 | + |
|
| 269 | + // Fetch reminder days. |
|
| 270 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 271 | + |
|
| 272 | + // Abort if non is set. |
|
| 273 | + if ( empty( $reminder_days ) ) { |
|
| 274 | + return; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Fetch matching subscriptions. |
|
| 278 | 278 | $args = array( |
| 279 | 279 | 'number' => -1, |
| 280 | - 'count_total' => false, |
|
| 281 | - 'status' => 'trialling active', |
|
| 280 | + 'count_total' => false, |
|
| 281 | + 'status' => 'trialling active', |
|
| 282 | 282 | 'date_expires_query' => array( |
| 283 | - 'relation' => 'OR', |
|
| 283 | + 'relation' => 'OR', |
|
| 284 | 284 | ), |
| 285 | - ); |
|
| 285 | + ); |
|
| 286 | 286 | |
| 287 | - foreach ( $reminder_days as $days ) { |
|
| 288 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 287 | + foreach ( $reminder_days as $days ) { |
|
| 288 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 289 | 289 | |
| 290 | - $args['date_expires_query'][] = array( |
|
| 291 | - 'year' => $date['year'], |
|
| 292 | - 'month' => $date['month'], |
|
| 293 | - 'day' => $date['day'], |
|
| 294 | - ); |
|
| 290 | + $args['date_expires_query'][] = array( |
|
| 291 | + 'year' => $date['year'], |
|
| 292 | + 'month' => $date['month'], |
|
| 293 | + 'day' => $date['day'], |
|
| 294 | + ); |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 298 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 299 | 299 | |
| 300 | 300 | foreach ( $subscriptions as $subscription ) { |
| 301 | 301 | |
| 302 | - // Skip packages. |
|
| 303 | - if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) { |
|
| 304 | - $email->object = $subscription; |
|
| 305 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 306 | - } |
|
| 302 | + // Skip packages. |
|
| 303 | + if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) { |
|
| 304 | + $email->object = $subscription; |
|
| 305 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 306 | + } |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | - } |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | 311 | } |
@@ -57,16 +57,16 @@ discard block |
||
| 57 | 57 | $args = wp_parse_args( |
| 58 | 58 | $args, |
| 59 | 59 | array( |
| 60 | - 'status' => array( 'publish' ), |
|
| 61 | - 'limit' => get_option( 'posts_per_page' ), |
|
| 62 | - 'page' => 1, |
|
| 63 | - 'exclude' => array(), |
|
| 64 | - 'orderby' => 'date', |
|
| 65 | - 'order' => 'DESC', |
|
| 66 | - 'type' => wpinv_item_types(), |
|
| 67 | - 'meta_query' => array(), |
|
| 68 | - 'return' => 'objects', |
|
| 69 | - 'paginate' => false, |
|
| 60 | + 'status' => array( 'publish' ), |
|
| 61 | + 'limit' => get_option( 'posts_per_page' ), |
|
| 62 | + 'page' => 1, |
|
| 63 | + 'exclude' => array(), |
|
| 64 | + 'orderby' => 'date', |
|
| 65 | + 'order' => 'DESC', |
|
| 66 | + 'type' => wpinv_item_types(), |
|
| 67 | + 'meta_query' => array(), |
|
| 68 | + 'return' => 'objects', |
|
| 69 | + 'paginate' => false, |
|
| 70 | 70 | ) |
| 71 | 71 | ); |
| 72 | 72 | |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | |
| 207 | 207 | function wpinv_get_item_types() { |
| 208 | 208 | $item_types = array( |
| 209 | - 'custom' => __( 'Standard', 'invoicing' ), |
|
| 210 | - 'fee' => __( 'Fee', 'invoicing' ), |
|
| 211 | - ); |
|
| 209 | + 'custom' => __( 'Standard', 'invoicing' ), |
|
| 210 | + 'fee' => __( 'Fee', 'invoicing' ), |
|
| 211 | + ); |
|
| 212 | 212 | return apply_filters( 'wpinv_get_item_types', $item_types ); |
| 213 | 213 | } |
| 214 | 214 | |
@@ -249,17 +249,17 @@ discard block |
||
| 249 | 249 | function wpinv_get_random_items( $num = 3, $post_ids = true ) { |
| 250 | 250 | if ( $post_ids ) { |
| 251 | 251 | $args = array( |
| 252 | - 'post_type' => 'wpi_item', |
|
| 253 | - 'orderby' => 'rand', |
|
| 254 | - 'post_count' => $num, |
|
| 255 | - 'fields' => 'ids', |
|
| 256 | - ); |
|
| 252 | + 'post_type' => 'wpi_item', |
|
| 253 | + 'orderby' => 'rand', |
|
| 254 | + 'post_count' => $num, |
|
| 255 | + 'fields' => 'ids', |
|
| 256 | + ); |
|
| 257 | 257 | } else { |
| 258 | 258 | $args = array( |
| 259 | - 'post_type' => 'wpi_item', |
|
| 260 | - 'orderby' => 'rand', |
|
| 261 | - 'post_count' => $num, |
|
| 262 | - ); |
|
| 259 | + 'post_type' => 'wpi_item', |
|
| 260 | + 'orderby' => 'rand', |
|
| 261 | + 'post_count' => $num, |
|
| 262 | + ); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | $args = apply_filters( 'wpinv_get_random_items', $args ); |
@@ -426,9 +426,9 @@ discard block |
||
| 426 | 426 | $bill_times = $item->get_recurring_limit(); |
| 427 | 427 | |
| 428 | 428 | if ( ! empty( $bill_times ) ) { |
| 429 | - $bill_times = $item->get_recurring_interval() * $bill_times; |
|
| 430 | - $bill_times = getpaid_get_subscription_period_label( $item->get_recurring_period(), $bill_times ); |
|
| 431 | - } |
|
| 429 | + $bill_times = $item->get_recurring_interval() * $bill_times; |
|
| 430 | + $bill_times = getpaid_get_subscription_period_label( $item->get_recurring_period(), $bill_times ); |
|
| 431 | + } |
|
| 432 | 432 | |
| 433 | 433 | if ( $item instanceof GetPaid_Form_Item && false === $_initial_price ) { |
| 434 | 434 | $initial_price = wpinv_price( $item->get_sub_total(), $currency ); |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -10,199 +10,199 @@ discard block |
||
| 10 | 10 | class GetPaid_Payment_Form_Submission { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Submission ID |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - public $id = null; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The raw submission data. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $data = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Submission totals |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $totals = array( |
|
| 32 | - |
|
| 33 | - 'subtotal' => array( |
|
| 34 | - 'initial' => 0, |
|
| 35 | - 'recurring' => 0, |
|
| 36 | - ), |
|
| 37 | - |
|
| 38 | - 'discount' => array( |
|
| 39 | - 'initial' => 0, |
|
| 40 | - 'recurring' => 0, |
|
| 41 | - ), |
|
| 42 | - |
|
| 43 | - 'fees' => array( |
|
| 44 | - 'initial' => 0, |
|
| 45 | - 'recurring' => 0, |
|
| 46 | - ), |
|
| 47 | - |
|
| 48 | - 'taxes' => array( |
|
| 49 | - 'initial' => 0, |
|
| 50 | - 'recurring' => 0, |
|
| 51 | - ), |
|
| 52 | - |
|
| 53 | - 'shipping' => array( |
|
| 54 | - 'initial' => 0, |
|
| 55 | - 'recurring' => 0, |
|
| 56 | - ), |
|
| 57 | - |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Sets the associated payment form. |
|
| 62 | - * |
|
| 63 | - * @var GetPaid_Payment_Form |
|
| 64 | - */ |
|
| 13 | + * Submission ID |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + public $id = null; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The raw submission data. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $data = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Submission totals |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $totals = array( |
|
| 32 | + |
|
| 33 | + 'subtotal' => array( |
|
| 34 | + 'initial' => 0, |
|
| 35 | + 'recurring' => 0, |
|
| 36 | + ), |
|
| 37 | + |
|
| 38 | + 'discount' => array( |
|
| 39 | + 'initial' => 0, |
|
| 40 | + 'recurring' => 0, |
|
| 41 | + ), |
|
| 42 | + |
|
| 43 | + 'fees' => array( |
|
| 44 | + 'initial' => 0, |
|
| 45 | + 'recurring' => 0, |
|
| 46 | + ), |
|
| 47 | + |
|
| 48 | + 'taxes' => array( |
|
| 49 | + 'initial' => 0, |
|
| 50 | + 'recurring' => 0, |
|
| 51 | + ), |
|
| 52 | + |
|
| 53 | + 'shipping' => array( |
|
| 54 | + 'initial' => 0, |
|
| 55 | + 'recurring' => 0, |
|
| 56 | + ), |
|
| 57 | + |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Sets the associated payment form. |
|
| 62 | + * |
|
| 63 | + * @var GetPaid_Payment_Form |
|
| 64 | + */ |
|
| 65 | 65 | protected $payment_form = null; |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | - * The country for the submission. |
|
| 69 | - * |
|
| 70 | - * @var string |
|
| 71 | - */ |
|
| 72 | - public $country = null; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * The state for the submission. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var string |
|
| 79 | - */ |
|
| 80 | - public $state = null; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * The invoice associated with the submission. |
|
| 84 | - * |
|
| 85 | - * @var WPInv_Invoice |
|
| 86 | - */ |
|
| 87 | - protected $invoice = null; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The recurring item for the submission. |
|
| 91 | - * |
|
| 92 | - * @var int |
|
| 93 | - */ |
|
| 94 | - public $has_recurring = 0; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * An array of fees for the submission. |
|
| 98 | - * |
|
| 99 | - * @var array |
|
| 100 | - */ |
|
| 101 | - protected $fees = array(); |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * An array of discounts for the submission. |
|
| 105 | - * |
|
| 106 | - * @var array |
|
| 107 | - */ |
|
| 108 | - protected $discounts = array(); |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * An array of taxes for the submission. |
|
| 112 | - * |
|
| 113 | - * @var array |
|
| 114 | - */ |
|
| 115 | - protected $taxes = array(); |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * An array of items for the submission. |
|
| 119 | - * |
|
| 120 | - * @var GetPaid_Form_Item[] |
|
| 121 | - */ |
|
| 122 | - protected $items = array(); |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * The last error. |
|
| 126 | - * |
|
| 127 | - * @var string |
|
| 128 | - */ |
|
| 129 | - public $last_error = null; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * The last error code. |
|
| 133 | - * |
|
| 134 | - * @var string |
|
| 135 | - */ |
|
| 136 | - public $last_error_code = null; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Class constructor. |
|
| 140 | - * |
|
| 141 | - */ |
|
| 142 | - public function __construct() { |
|
| 143 | - |
|
| 144 | - // Set the state and country to the default state and country. |
|
| 145 | - $this->country = wpinv_default_billing_country(); |
|
| 146 | - $this->state = wpinv_get_default_state(); |
|
| 147 | - |
|
| 148 | - // Do we have an actual submission? |
|
| 149 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 150 | - $this->load_data( wp_kses_post_deep( wp_unslash( $_POST ) ) ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Loads submission data. |
|
| 157 | - * |
|
| 158 | - * @param array $data |
|
| 159 | - */ |
|
| 160 | - public function load_data( $data ) { |
|
| 161 | - |
|
| 162 | - // Allow plugins to filter the data. |
|
| 163 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 164 | - |
|
| 165 | - // Cache it... |
|
| 166 | - $this->data = $data; |
|
| 167 | - |
|
| 168 | - // Then generate a unique id from the data. |
|
| 169 | - $this->id = md5( wp_json_encode( $data ) ); |
|
| 170 | - |
|
| 171 | - // Finally, process the submission. |
|
| 172 | - try { |
|
| 173 | - |
|
| 174 | - // Each process is passed an instance of the class (with reference) |
|
| 175 | - // and should throw an Exception whenever it encounters one. |
|
| 176 | - $processors = apply_filters( |
|
| 177 | - 'getpaid_payment_form_submission_processors', |
|
| 178 | - array( |
|
| 179 | - array( $this, 'process_payment_form' ), |
|
| 180 | - array( $this, 'process_invoice' ), |
|
| 181 | - array( $this, 'process_fees' ), |
|
| 182 | - array( $this, 'process_items' ), |
|
| 183 | - array( $this, 'process_discount' ), |
|
| 184 | - array( $this, 'process_taxes' ), |
|
| 185 | - ), |
|
| 186 | - $this |
|
| 187 | - ); |
|
| 188 | - |
|
| 189 | - foreach ( $processors as $processor ) { |
|
| 190 | - call_user_func_array( $processor, array( &$this ) ); |
|
| 191 | - } |
|
| 68 | + * The country for the submission. |
|
| 69 | + * |
|
| 70 | + * @var string |
|
| 71 | + */ |
|
| 72 | + public $country = null; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * The state for the submission. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var string |
|
| 79 | + */ |
|
| 80 | + public $state = null; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * The invoice associated with the submission. |
|
| 84 | + * |
|
| 85 | + * @var WPInv_Invoice |
|
| 86 | + */ |
|
| 87 | + protected $invoice = null; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The recurring item for the submission. |
|
| 91 | + * |
|
| 92 | + * @var int |
|
| 93 | + */ |
|
| 94 | + public $has_recurring = 0; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * An array of fees for the submission. |
|
| 98 | + * |
|
| 99 | + * @var array |
|
| 100 | + */ |
|
| 101 | + protected $fees = array(); |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * An array of discounts for the submission. |
|
| 105 | + * |
|
| 106 | + * @var array |
|
| 107 | + */ |
|
| 108 | + protected $discounts = array(); |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * An array of taxes for the submission. |
|
| 112 | + * |
|
| 113 | + * @var array |
|
| 114 | + */ |
|
| 115 | + protected $taxes = array(); |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * An array of items for the submission. |
|
| 119 | + * |
|
| 120 | + * @var GetPaid_Form_Item[] |
|
| 121 | + */ |
|
| 122 | + protected $items = array(); |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * The last error. |
|
| 126 | + * |
|
| 127 | + * @var string |
|
| 128 | + */ |
|
| 129 | + public $last_error = null; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * The last error code. |
|
| 133 | + * |
|
| 134 | + * @var string |
|
| 135 | + */ |
|
| 136 | + public $last_error_code = null; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Class constructor. |
|
| 140 | + * |
|
| 141 | + */ |
|
| 142 | + public function __construct() { |
|
| 143 | + |
|
| 144 | + // Set the state and country to the default state and country. |
|
| 145 | + $this->country = wpinv_default_billing_country(); |
|
| 146 | + $this->state = wpinv_get_default_state(); |
|
| 147 | + |
|
| 148 | + // Do we have an actual submission? |
|
| 149 | + if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 150 | + $this->load_data( wp_kses_post_deep( wp_unslash( $_POST ) ) ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Loads submission data. |
|
| 157 | + * |
|
| 158 | + * @param array $data |
|
| 159 | + */ |
|
| 160 | + public function load_data( $data ) { |
|
| 161 | + |
|
| 162 | + // Allow plugins to filter the data. |
|
| 163 | + $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 164 | + |
|
| 165 | + // Cache it... |
|
| 166 | + $this->data = $data; |
|
| 167 | + |
|
| 168 | + // Then generate a unique id from the data. |
|
| 169 | + $this->id = md5( wp_json_encode( $data ) ); |
|
| 170 | + |
|
| 171 | + // Finally, process the submission. |
|
| 172 | + try { |
|
| 173 | + |
|
| 174 | + // Each process is passed an instance of the class (with reference) |
|
| 175 | + // and should throw an Exception whenever it encounters one. |
|
| 176 | + $processors = apply_filters( |
|
| 177 | + 'getpaid_payment_form_submission_processors', |
|
| 178 | + array( |
|
| 179 | + array( $this, 'process_payment_form' ), |
|
| 180 | + array( $this, 'process_invoice' ), |
|
| 181 | + array( $this, 'process_fees' ), |
|
| 182 | + array( $this, 'process_items' ), |
|
| 183 | + array( $this, 'process_discount' ), |
|
| 184 | + array( $this, 'process_taxes' ), |
|
| 185 | + ), |
|
| 186 | + $this |
|
| 187 | + ); |
|
| 188 | + |
|
| 189 | + foreach ( $processors as $processor ) { |
|
| 190 | + call_user_func_array( $processor, array( &$this ) ); |
|
| 191 | + } |
|
| 192 | 192 | } catch ( GetPaid_Payment_Exception $e ) { |
| 193 | - $this->last_error = $e->getMessage(); |
|
| 194 | - $this->last_error_code = $e->getErrorCode(); |
|
| 195 | - } catch ( Exception $e ) { |
|
| 196 | - $this->last_error = $e->getMessage(); |
|
| 197 | - $this->last_error_code = $e->getCode(); |
|
| 198 | - } |
|
| 193 | + $this->last_error = $e->getMessage(); |
|
| 194 | + $this->last_error_code = $e->getErrorCode(); |
|
| 195 | + } catch ( Exception $e ) { |
|
| 196 | + $this->last_error = $e->getMessage(); |
|
| 197 | + $this->last_error_code = $e->getCode(); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - // Fired when we are done processing a submission. |
|
| 201 | - do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 200 | + // Fired when we are done processing a submission. |
|
| 201 | + do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 202 | 202 | |
| 203 | - } |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - /* |
|
| 205 | + /* |
|
| 206 | 206 | |-------------------------------------------------------------------------- |
| 207 | 207 | | Payment Forms. |
| 208 | 208 | |-------------------------------------------------------------------------- |
@@ -211,39 +211,39 @@ discard block |
||
| 211 | 211 | | submission has an active payment form etc. |
| 212 | 212 | */ |
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Prepares the submission's payment form. |
|
| 216 | - * |
|
| 217 | - * @since 1.0.19 |
|
| 218 | - */ |
|
| 219 | - public function process_payment_form() { |
|
| 214 | + /** |
|
| 215 | + * Prepares the submission's payment form. |
|
| 216 | + * |
|
| 217 | + * @since 1.0.19 |
|
| 218 | + */ |
|
| 219 | + public function process_payment_form() { |
|
| 220 | 220 | |
| 221 | - // Every submission needs an active payment form. |
|
| 222 | - if ( empty( $this->data['form_id'] ) ) { |
|
| 223 | - throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 224 | - } |
|
| 221 | + // Every submission needs an active payment form. |
|
| 222 | + if ( empty( $this->data['form_id'] ) ) { |
|
| 223 | + throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - // Fetch the payment form. |
|
| 227 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 226 | + // Fetch the payment form. |
|
| 227 | + $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 228 | 228 | |
| 229 | - if ( ! $this->payment_form->is_active() ) { |
|
| 230 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 231 | - } |
|
| 229 | + if ( ! $this->payment_form->is_active() ) { |
|
| 230 | + throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 234 | - } |
|
| 233 | + do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | 236 | /** |
| 237 | - * Returns the payment form. |
|
| 238 | - * |
|
| 239 | - * @since 1.0.19 |
|
| 240 | - * @return GetPaid_Payment_Form |
|
| 241 | - */ |
|
| 242 | - public function get_payment_form() { |
|
| 243 | - return $this->payment_form; |
|
| 244 | - } |
|
| 237 | + * Returns the payment form. |
|
| 238 | + * |
|
| 239 | + * @since 1.0.19 |
|
| 240 | + * @return GetPaid_Payment_Form |
|
| 241 | + */ |
|
| 242 | + public function get_payment_form() { |
|
| 243 | + return $this->payment_form; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /* |
|
| 246 | + /* |
|
| 247 | 247 | |-------------------------------------------------------------------------- |
| 248 | 248 | | Invoices. |
| 249 | 249 | |-------------------------------------------------------------------------- |
@@ -252,84 +252,84 @@ discard block |
||
| 252 | 252 | | might be for an existing invoice. |
| 253 | 253 | */ |
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Prepares the submission's invoice. |
|
| 257 | - * |
|
| 258 | - * @since 1.0.19 |
|
| 259 | - */ |
|
| 260 | - public function process_invoice() { |
|
| 255 | + /** |
|
| 256 | + * Prepares the submission's invoice. |
|
| 257 | + * |
|
| 258 | + * @since 1.0.19 |
|
| 259 | + */ |
|
| 260 | + public function process_invoice() { |
|
| 261 | 261 | |
| 262 | - // Abort if there is no invoice. |
|
| 263 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
| 264 | - return; |
|
| 265 | - } |
|
| 262 | + // Abort if there is no invoice. |
|
| 263 | + if ( empty( $this->data['invoice_id'] ) ) { |
|
| 264 | + return; |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - // If the submission is for an existing invoice, ensure that it exists |
|
| 268 | - // and that it is not paid for. |
|
| 269 | - $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 267 | + // If the submission is for an existing invoice, ensure that it exists |
|
| 268 | + // and that it is not paid for. |
|
| 269 | + $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 270 | 270 | |
| 271 | 271 | if ( empty( $invoice ) ) { |
| 272 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - if ( $invoice->is_paid() ) { |
|
| 276 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - $this->payment_form->invoice = $invoice; |
|
| 280 | - if ( ! $this->payment_form->is_default() ) { |
|
| 281 | - |
|
| 282 | - $items = array(); |
|
| 283 | - $item_ids = array(); |
|
| 284 | - |
|
| 285 | - foreach ( $invoice->get_items() as $item ) { |
|
| 286 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 287 | - $item_ids[] = $item->get_id(); |
|
| 288 | - $items[] = $item; |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
| 293 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 294 | - $item_ids[] = $item->get_id(); |
|
| 295 | - $items[] = $item; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - $this->payment_form->set_items( $items ); |
|
| 300 | - |
|
| 301 | - } else { |
|
| 302 | - $this->payment_form->set_items( $invoice->get_items() ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $this->country = $invoice->get_country(); |
|
| 306 | - $this->state = $invoice->get_state(); |
|
| 307 | - $this->invoice = $invoice; |
|
| 308 | - |
|
| 309 | - do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Returns the associated invoice. |
|
| 314 | - * |
|
| 315 | - * @since 1.0.19 |
|
| 316 | - * @return WPInv_Invoice |
|
| 317 | - */ |
|
| 318 | - public function get_invoice() { |
|
| 319 | - return $this->invoice; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Checks whether there is an invoice associated with this submission. |
|
| 324 | - * |
|
| 325 | - * @since 1.0.19 |
|
| 326 | - * @return bool |
|
| 327 | - */ |
|
| 328 | - public function has_invoice() { |
|
| 329 | - return ! empty( $this->invoice ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /* |
|
| 272 | + throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + if ( $invoice->is_paid() ) { |
|
| 276 | + throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + $this->payment_form->invoice = $invoice; |
|
| 280 | + if ( ! $this->payment_form->is_default() ) { |
|
| 281 | + |
|
| 282 | + $items = array(); |
|
| 283 | + $item_ids = array(); |
|
| 284 | + |
|
| 285 | + foreach ( $invoice->get_items() as $item ) { |
|
| 286 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 287 | + $item_ids[] = $item->get_id(); |
|
| 288 | + $items[] = $item; |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + foreach ( $this->payment_form->get_items() as $item ) { |
|
| 293 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 294 | + $item_ids[] = $item->get_id(); |
|
| 295 | + $items[] = $item; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + $this->payment_form->set_items( $items ); |
|
| 300 | + |
|
| 301 | + } else { |
|
| 302 | + $this->payment_form->set_items( $invoice->get_items() ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $this->country = $invoice->get_country(); |
|
| 306 | + $this->state = $invoice->get_state(); |
|
| 307 | + $this->invoice = $invoice; |
|
| 308 | + |
|
| 309 | + do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Returns the associated invoice. |
|
| 314 | + * |
|
| 315 | + * @since 1.0.19 |
|
| 316 | + * @return WPInv_Invoice |
|
| 317 | + */ |
|
| 318 | + public function get_invoice() { |
|
| 319 | + return $this->invoice; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Checks whether there is an invoice associated with this submission. |
|
| 324 | + * |
|
| 325 | + * @since 1.0.19 |
|
| 326 | + * @return bool |
|
| 327 | + */ |
|
| 328 | + public function has_invoice() { |
|
| 329 | + return ! empty( $this->invoice ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /* |
|
| 333 | 333 | |-------------------------------------------------------------------------- |
| 334 | 334 | | Items. |
| 335 | 335 | |-------------------------------------------------------------------------- |
@@ -338,129 +338,129 @@ discard block |
||
| 338 | 338 | | recurring item. But can have an unlimited number of non-recurring items. |
| 339 | 339 | */ |
| 340 | 340 | |
| 341 | - /** |
|
| 342 | - * Prepares the submission's items. |
|
| 343 | - * |
|
| 344 | - * @since 1.0.19 |
|
| 345 | - */ |
|
| 346 | - public function process_items() { |
|
| 347 | - |
|
| 348 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 349 | - |
|
| 350 | - foreach ( $processor->items as $item ) { |
|
| 351 | - $this->add_item( $item ); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Adds an item to the submission. |
|
| 359 | - * |
|
| 360 | - * @since 1.0.19 |
|
| 361 | - * @param GetPaid_Form_Item $item |
|
| 362 | - */ |
|
| 363 | - public function add_item( $item ) { |
|
| 364 | - |
|
| 365 | - // Make sure that it is available for purchase. |
|
| 366 | - if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 367 | - return; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - // Each submission can only contain one recurring item. |
|
| 371 | - if ( $item->is_recurring() ) { |
|
| 372 | - $this->has_recurring = $item->get_id(); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - // Update the items and totals. |
|
| 376 | - $this->items[ $item->get_id() ] = $item; |
|
| 377 | - $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
| 378 | - $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
| 379 | - |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Removes a specific item. |
|
| 384 | - * |
|
| 385 | - * You should not call this method after the discounts and taxes |
|
| 386 | - * have been calculated. |
|
| 387 | - * |
|
| 388 | - * @since 1.0.19 |
|
| 389 | - */ |
|
| 390 | - public function remove_item( $item_id ) { |
|
| 391 | - |
|
| 392 | - if ( isset( $this->items[ $item_id ] ) ) { |
|
| 393 | - $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 394 | - $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 395 | - |
|
| 396 | - if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 397 | - $this->has_recurring = 0; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - unset( $this->items[ $item_id ] ); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Returns the subtotal. |
|
| 407 | - * |
|
| 408 | - * @since 1.0.19 |
|
| 409 | - */ |
|
| 410 | - public function get_subtotal() { |
|
| 411 | - |
|
| 412 | - if ( wpinv_prices_include_tax() ) { |
|
| 413 | - return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - return $this->totals['subtotal']['initial']; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Returns the recurring subtotal. |
|
| 421 | - * |
|
| 422 | - * @since 1.0.19 |
|
| 423 | - */ |
|
| 424 | - public function get_recurring_subtotal() { |
|
| 425 | - |
|
| 426 | - if ( wpinv_prices_include_tax() ) { |
|
| 427 | - return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - return $this->totals['subtotal']['recurring']; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Returns all items. |
|
| 435 | - * |
|
| 436 | - * @since 1.0.19 |
|
| 437 | - * @return GetPaid_Form_Item[] |
|
| 438 | - */ |
|
| 439 | - public function get_items() { |
|
| 440 | - return $this->items; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Checks if there's a single subscription group in the submission. |
|
| 445 | - * |
|
| 446 | - * @since 2.3.0 |
|
| 447 | - * @return bool |
|
| 448 | - */ |
|
| 449 | - public function has_subscription_group() { |
|
| 450 | - return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Checks if there are multipe subscription groups in the submission. |
|
| 455 | - * |
|
| 456 | - * @since 2.3.0 |
|
| 457 | - * @return bool |
|
| 458 | - */ |
|
| 459 | - public function has_multiple_subscription_groups() { |
|
| 460 | - return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /* |
|
| 341 | + /** |
|
| 342 | + * Prepares the submission's items. |
|
| 343 | + * |
|
| 344 | + * @since 1.0.19 |
|
| 345 | + */ |
|
| 346 | + public function process_items() { |
|
| 347 | + |
|
| 348 | + $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 349 | + |
|
| 350 | + foreach ( $processor->items as $item ) { |
|
| 351 | + $this->add_item( $item ); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Adds an item to the submission. |
|
| 359 | + * |
|
| 360 | + * @since 1.0.19 |
|
| 361 | + * @param GetPaid_Form_Item $item |
|
| 362 | + */ |
|
| 363 | + public function add_item( $item ) { |
|
| 364 | + |
|
| 365 | + // Make sure that it is available for purchase. |
|
| 366 | + if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 367 | + return; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + // Each submission can only contain one recurring item. |
|
| 371 | + if ( $item->is_recurring() ) { |
|
| 372 | + $this->has_recurring = $item->get_id(); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + // Update the items and totals. |
|
| 376 | + $this->items[ $item->get_id() ] = $item; |
|
| 377 | + $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
| 378 | + $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
| 379 | + |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Removes a specific item. |
|
| 384 | + * |
|
| 385 | + * You should not call this method after the discounts and taxes |
|
| 386 | + * have been calculated. |
|
| 387 | + * |
|
| 388 | + * @since 1.0.19 |
|
| 389 | + */ |
|
| 390 | + public function remove_item( $item_id ) { |
|
| 391 | + |
|
| 392 | + if ( isset( $this->items[ $item_id ] ) ) { |
|
| 393 | + $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 394 | + $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 395 | + |
|
| 396 | + if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 397 | + $this->has_recurring = 0; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + unset( $this->items[ $item_id ] ); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Returns the subtotal. |
|
| 407 | + * |
|
| 408 | + * @since 1.0.19 |
|
| 409 | + */ |
|
| 410 | + public function get_subtotal() { |
|
| 411 | + |
|
| 412 | + if ( wpinv_prices_include_tax() ) { |
|
| 413 | + return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + return $this->totals['subtotal']['initial']; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Returns the recurring subtotal. |
|
| 421 | + * |
|
| 422 | + * @since 1.0.19 |
|
| 423 | + */ |
|
| 424 | + public function get_recurring_subtotal() { |
|
| 425 | + |
|
| 426 | + if ( wpinv_prices_include_tax() ) { |
|
| 427 | + return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + return $this->totals['subtotal']['recurring']; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Returns all items. |
|
| 435 | + * |
|
| 436 | + * @since 1.0.19 |
|
| 437 | + * @return GetPaid_Form_Item[] |
|
| 438 | + */ |
|
| 439 | + public function get_items() { |
|
| 440 | + return $this->items; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Checks if there's a single subscription group in the submission. |
|
| 445 | + * |
|
| 446 | + * @since 2.3.0 |
|
| 447 | + * @return bool |
|
| 448 | + */ |
|
| 449 | + public function has_subscription_group() { |
|
| 450 | + return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Checks if there are multipe subscription groups in the submission. |
|
| 455 | + * |
|
| 456 | + * @since 2.3.0 |
|
| 457 | + * @return bool |
|
| 458 | + */ |
|
| 459 | + public function has_multiple_subscription_groups() { |
|
| 460 | + return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /* |
|
| 464 | 464 | |-------------------------------------------------------------------------- |
| 465 | 465 | | Taxes |
| 466 | 466 | |-------------------------------------------------------------------------- |
@@ -469,128 +469,128 @@ discard block |
||
| 469 | 469 | | or only one-time. |
| 470 | 470 | */ |
| 471 | 471 | |
| 472 | - /** |
|
| 473 | - * Prepares the submission's taxes. |
|
| 474 | - * |
|
| 475 | - * @since 1.0.19 |
|
| 476 | - */ |
|
| 477 | - public function process_taxes() { |
|
| 478 | - |
|
| 479 | - // Abort if we're not using taxes. |
|
| 480 | - if ( ! $this->use_taxes() ) { |
|
| 481 | - return; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // If a custom country && state has been passed in, use it to calculate taxes. |
|
| 485 | - $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 486 | - if ( ! empty( $country ) ) { |
|
| 487 | - $this->country = $country; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 491 | - if ( ! empty( $state ) ) { |
|
| 492 | - $this->state = $state; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - // Confirm if the provided country and the ip country are similar. |
|
| 496 | - $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 497 | - if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 498 | - throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - // Abort if the country is not taxable. |
|
| 502 | - if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 503 | - return; |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 507 | - |
|
| 508 | - foreach ( $processor->taxes as $tax ) { |
|
| 509 | - $this->add_tax( $tax ); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Adds a tax to the submission. |
|
| 517 | - * |
|
| 518 | - * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
| 519 | - * @since 1.0.19 |
|
| 520 | - */ |
|
| 521 | - public function add_tax( $tax ) { |
|
| 522 | - |
|
| 523 | - if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 524 | - $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 525 | - $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - $this->taxes[ $tax['name'] ] = $tax; |
|
| 529 | - $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 530 | - $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 531 | - |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Removes a specific tax. |
|
| 536 | - * |
|
| 537 | - * @since 1.0.19 |
|
| 538 | - */ |
|
| 539 | - public function remove_tax( $tax_name ) { |
|
| 540 | - |
|
| 541 | - if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 542 | - $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 543 | - $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 544 | - unset( $this->taxes[ $tax_name ] ); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Whether or not we'll use taxes for the submission. |
|
| 551 | - * |
|
| 552 | - * @since 1.0.19 |
|
| 553 | - */ |
|
| 554 | - public function use_taxes() { |
|
| 555 | - |
|
| 556 | - $use_taxes = wpinv_use_taxes(); |
|
| 557 | - |
|
| 558 | - if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 559 | - $use_taxes = false; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 563 | - |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Returns the tax. |
|
| 568 | - * |
|
| 569 | - * @since 1.0.19 |
|
| 570 | - */ |
|
| 571 | - public function get_tax() { |
|
| 572 | - return $this->totals['taxes']['initial']; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Returns the recurring tax. |
|
| 577 | - * |
|
| 578 | - * @since 1.0.19 |
|
| 579 | - */ |
|
| 580 | - public function get_recurring_tax() { |
|
| 581 | - return $this->totals['taxes']['recurring']; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Returns all taxes. |
|
| 586 | - * |
|
| 587 | - * @since 1.0.19 |
|
| 588 | - */ |
|
| 589 | - public function get_taxes() { |
|
| 590 | - return $this->taxes; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /* |
|
| 472 | + /** |
|
| 473 | + * Prepares the submission's taxes. |
|
| 474 | + * |
|
| 475 | + * @since 1.0.19 |
|
| 476 | + */ |
|
| 477 | + public function process_taxes() { |
|
| 478 | + |
|
| 479 | + // Abort if we're not using taxes. |
|
| 480 | + if ( ! $this->use_taxes() ) { |
|
| 481 | + return; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // If a custom country && state has been passed in, use it to calculate taxes. |
|
| 485 | + $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 486 | + if ( ! empty( $country ) ) { |
|
| 487 | + $this->country = $country; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 491 | + if ( ! empty( $state ) ) { |
|
| 492 | + $this->state = $state; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + // Confirm if the provided country and the ip country are similar. |
|
| 496 | + $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 497 | + if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 498 | + throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + // Abort if the country is not taxable. |
|
| 502 | + if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 503 | + return; |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 507 | + |
|
| 508 | + foreach ( $processor->taxes as $tax ) { |
|
| 509 | + $this->add_tax( $tax ); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Adds a tax to the submission. |
|
| 517 | + * |
|
| 518 | + * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
| 519 | + * @since 1.0.19 |
|
| 520 | + */ |
|
| 521 | + public function add_tax( $tax ) { |
|
| 522 | + |
|
| 523 | + if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 524 | + $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 525 | + $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + $this->taxes[ $tax['name'] ] = $tax; |
|
| 529 | + $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 530 | + $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 531 | + |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Removes a specific tax. |
|
| 536 | + * |
|
| 537 | + * @since 1.0.19 |
|
| 538 | + */ |
|
| 539 | + public function remove_tax( $tax_name ) { |
|
| 540 | + |
|
| 541 | + if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 542 | + $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 543 | + $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 544 | + unset( $this->taxes[ $tax_name ] ); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Whether or not we'll use taxes for the submission. |
|
| 551 | + * |
|
| 552 | + * @since 1.0.19 |
|
| 553 | + */ |
|
| 554 | + public function use_taxes() { |
|
| 555 | + |
|
| 556 | + $use_taxes = wpinv_use_taxes(); |
|
| 557 | + |
|
| 558 | + if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 559 | + $use_taxes = false; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 563 | + |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Returns the tax. |
|
| 568 | + * |
|
| 569 | + * @since 1.0.19 |
|
| 570 | + */ |
|
| 571 | + public function get_tax() { |
|
| 572 | + return $this->totals['taxes']['initial']; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Returns the recurring tax. |
|
| 577 | + * |
|
| 578 | + * @since 1.0.19 |
|
| 579 | + */ |
|
| 580 | + public function get_recurring_tax() { |
|
| 581 | + return $this->totals['taxes']['recurring']; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Returns all taxes. |
|
| 586 | + * |
|
| 587 | + * @since 1.0.19 |
|
| 588 | + */ |
|
| 589 | + public function get_taxes() { |
|
| 590 | + return $this->taxes; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /* |
|
| 594 | 594 | |-------------------------------------------------------------------------- |
| 595 | 595 | | Discounts |
| 596 | 596 | |-------------------------------------------------------------------------- |
@@ -599,99 +599,99 @@ discard block |
||
| 599 | 599 | | or only one-time. They also do not have to come from a discount code. |
| 600 | 600 | */ |
| 601 | 601 | |
| 602 | - /** |
|
| 603 | - * Prepares the submission's discount. |
|
| 604 | - * |
|
| 605 | - * @since 1.0.19 |
|
| 606 | - */ |
|
| 607 | - public function process_discount() { |
|
| 608 | - |
|
| 609 | - $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
| 610 | - $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
| 611 | - $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 612 | - |
|
| 613 | - foreach ( $processor->discounts as $discount ) { |
|
| 614 | - $this->add_discount( $discount ); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Adds a discount to the submission. |
|
| 622 | - * |
|
| 623 | - * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
| 624 | - * @since 1.0.19 |
|
| 625 | - */ |
|
| 626 | - public function add_discount( $discount ) { |
|
| 627 | - $this->discounts[ $discount['name'] ] = $discount; |
|
| 628 | - $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 629 | - $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Removes a discount from the submission. |
|
| 634 | - * |
|
| 635 | - * @since 1.0.19 |
|
| 636 | - */ |
|
| 637 | - public function remove_discount( $name ) { |
|
| 638 | - |
|
| 639 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 640 | - $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 641 | - $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 642 | - unset( $this->discounts[ $name ] ); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * Checks whether there is a discount code associated with this submission. |
|
| 649 | - * |
|
| 650 | - * @since 1.0.19 |
|
| 651 | - * @return bool |
|
| 652 | - */ |
|
| 653 | - public function has_discount_code() { |
|
| 654 | - return ! empty( $this->discounts['discount_code'] ); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Returns the discount code. |
|
| 659 | - * |
|
| 660 | - * @since 1.0.19 |
|
| 661 | - * @return string |
|
| 662 | - */ |
|
| 663 | - public function get_discount_code() { |
|
| 664 | - return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * Returns the discount. |
|
| 669 | - * |
|
| 670 | - * @since 1.0.19 |
|
| 671 | - */ |
|
| 672 | - public function get_discount() { |
|
| 673 | - return $this->totals['discount']['initial']; |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - /** |
|
| 677 | - * Returns the recurring discount. |
|
| 678 | - * |
|
| 679 | - * @since 1.0.19 |
|
| 680 | - */ |
|
| 681 | - public function get_recurring_discount() { |
|
| 682 | - return $this->totals['discount']['recurring']; |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * Returns all discounts. |
|
| 687 | - * |
|
| 688 | - * @since 1.0.19 |
|
| 689 | - */ |
|
| 690 | - public function get_discounts() { |
|
| 691 | - return $this->discounts; |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /* |
|
| 602 | + /** |
|
| 603 | + * Prepares the submission's discount. |
|
| 604 | + * |
|
| 605 | + * @since 1.0.19 |
|
| 606 | + */ |
|
| 607 | + public function process_discount() { |
|
| 608 | + |
|
| 609 | + $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
| 610 | + $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
| 611 | + $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 612 | + |
|
| 613 | + foreach ( $processor->discounts as $discount ) { |
|
| 614 | + $this->add_discount( $discount ); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Adds a discount to the submission. |
|
| 622 | + * |
|
| 623 | + * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
| 624 | + * @since 1.0.19 |
|
| 625 | + */ |
|
| 626 | + public function add_discount( $discount ) { |
|
| 627 | + $this->discounts[ $discount['name'] ] = $discount; |
|
| 628 | + $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 629 | + $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Removes a discount from the submission. |
|
| 634 | + * |
|
| 635 | + * @since 1.0.19 |
|
| 636 | + */ |
|
| 637 | + public function remove_discount( $name ) { |
|
| 638 | + |
|
| 639 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
| 640 | + $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 641 | + $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 642 | + unset( $this->discounts[ $name ] ); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * Checks whether there is a discount code associated with this submission. |
|
| 649 | + * |
|
| 650 | + * @since 1.0.19 |
|
| 651 | + * @return bool |
|
| 652 | + */ |
|
| 653 | + public function has_discount_code() { |
|
| 654 | + return ! empty( $this->discounts['discount_code'] ); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Returns the discount code. |
|
| 659 | + * |
|
| 660 | + * @since 1.0.19 |
|
| 661 | + * @return string |
|
| 662 | + */ |
|
| 663 | + public function get_discount_code() { |
|
| 664 | + return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * Returns the discount. |
|
| 669 | + * |
|
| 670 | + * @since 1.0.19 |
|
| 671 | + */ |
|
| 672 | + public function get_discount() { |
|
| 673 | + return $this->totals['discount']['initial']; |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + /** |
|
| 677 | + * Returns the recurring discount. |
|
| 678 | + * |
|
| 679 | + * @since 1.0.19 |
|
| 680 | + */ |
|
| 681 | + public function get_recurring_discount() { |
|
| 682 | + return $this->totals['discount']['recurring']; |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * Returns all discounts. |
|
| 687 | + * |
|
| 688 | + * @since 1.0.19 |
|
| 689 | + */ |
|
| 690 | + public function get_discounts() { |
|
| 691 | + return $this->discounts; |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /* |
|
| 695 | 695 | |-------------------------------------------------------------------------- |
| 696 | 696 | | Fees |
| 697 | 697 | |-------------------------------------------------------------------------- |
@@ -701,100 +701,100 @@ discard block |
||
| 701 | 701 | | fees. |
| 702 | 702 | */ |
| 703 | 703 | |
| 704 | - /** |
|
| 705 | - * Prepares the submission's fees. |
|
| 706 | - * |
|
| 707 | - * @since 1.0.19 |
|
| 708 | - */ |
|
| 709 | - public function process_fees() { |
|
| 710 | - |
|
| 711 | - $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 712 | - |
|
| 713 | - foreach ( $fees_processor->fees as $fee ) { |
|
| 714 | - $this->add_fee( $fee ); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * Adds a fee to the submission. |
|
| 722 | - * |
|
| 723 | - * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
| 724 | - * @since 1.0.19 |
|
| 725 | - */ |
|
| 726 | - public function add_fee( $fee ) { |
|
| 727 | - |
|
| 728 | - if ( $fee['name'] == 'shipping' ) { |
|
| 729 | - $this->totals['shipping']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 730 | - $this->totals['shipping']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 731 | - return; |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - $this->fees[ $fee['name'] ] = $fee; |
|
| 735 | - $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 736 | - $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 737 | - |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * Removes a fee from the submission. |
|
| 742 | - * |
|
| 743 | - * @since 1.0.19 |
|
| 744 | - */ |
|
| 745 | - public function remove_fee( $name ) { |
|
| 746 | - |
|
| 747 | - if ( isset( $this->fees[ $name ] ) ) { |
|
| 748 | - $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 749 | - $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 750 | - unset( $this->fees[ $name ] ); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - if ( 'shipping' == $name ) { |
|
| 754 | - $this->totals['shipping']['initial'] = 0; |
|
| 755 | - $this->totals['shipping']['recurring'] = 0; |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - /** |
|
| 761 | - * Returns the fees. |
|
| 762 | - * |
|
| 763 | - * @since 1.0.19 |
|
| 764 | - */ |
|
| 765 | - public function get_fee() { |
|
| 766 | - return $this->totals['fees']['initial']; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - /** |
|
| 770 | - * Returns the recurring fees. |
|
| 771 | - * |
|
| 772 | - * @since 1.0.19 |
|
| 773 | - */ |
|
| 774 | - public function get_recurring_fee() { |
|
| 775 | - return $this->totals['fees']['recurring']; |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - /** |
|
| 779 | - * Returns all fees. |
|
| 780 | - * |
|
| 781 | - * @since 1.0.19 |
|
| 782 | - */ |
|
| 783 | - public function get_fees() { |
|
| 784 | - return $this->fees; |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Checks if there are any fees for the form. |
|
| 789 | - * |
|
| 790 | - * @return bool |
|
| 791 | - * @since 1.0.19 |
|
| 792 | - */ |
|
| 793 | - public function has_fees() { |
|
| 794 | - return count( $this->fees ) !== 0; |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - /* |
|
| 704 | + /** |
|
| 705 | + * Prepares the submission's fees. |
|
| 706 | + * |
|
| 707 | + * @since 1.0.19 |
|
| 708 | + */ |
|
| 709 | + public function process_fees() { |
|
| 710 | + |
|
| 711 | + $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 712 | + |
|
| 713 | + foreach ( $fees_processor->fees as $fee ) { |
|
| 714 | + $this->add_fee( $fee ); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * Adds a fee to the submission. |
|
| 722 | + * |
|
| 723 | + * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
| 724 | + * @since 1.0.19 |
|
| 725 | + */ |
|
| 726 | + public function add_fee( $fee ) { |
|
| 727 | + |
|
| 728 | + if ( $fee['name'] == 'shipping' ) { |
|
| 729 | + $this->totals['shipping']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 730 | + $this->totals['shipping']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 731 | + return; |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + $this->fees[ $fee['name'] ] = $fee; |
|
| 735 | + $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 736 | + $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 737 | + |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * Removes a fee from the submission. |
|
| 742 | + * |
|
| 743 | + * @since 1.0.19 |
|
| 744 | + */ |
|
| 745 | + public function remove_fee( $name ) { |
|
| 746 | + |
|
| 747 | + if ( isset( $this->fees[ $name ] ) ) { |
|
| 748 | + $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 749 | + $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 750 | + unset( $this->fees[ $name ] ); |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + if ( 'shipping' == $name ) { |
|
| 754 | + $this->totals['shipping']['initial'] = 0; |
|
| 755 | + $this->totals['shipping']['recurring'] = 0; |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + /** |
|
| 761 | + * Returns the fees. |
|
| 762 | + * |
|
| 763 | + * @since 1.0.19 |
|
| 764 | + */ |
|
| 765 | + public function get_fee() { |
|
| 766 | + return $this->totals['fees']['initial']; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + /** |
|
| 770 | + * Returns the recurring fees. |
|
| 771 | + * |
|
| 772 | + * @since 1.0.19 |
|
| 773 | + */ |
|
| 774 | + public function get_recurring_fee() { |
|
| 775 | + return $this->totals['fees']['recurring']; |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + /** |
|
| 779 | + * Returns all fees. |
|
| 780 | + * |
|
| 781 | + * @since 1.0.19 |
|
| 782 | + */ |
|
| 783 | + public function get_fees() { |
|
| 784 | + return $this->fees; |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Checks if there are any fees for the form. |
|
| 789 | + * |
|
| 790 | + * @return bool |
|
| 791 | + * @since 1.0.19 |
|
| 792 | + */ |
|
| 793 | + public function has_fees() { |
|
| 794 | + return count( $this->fees ) !== 0; |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + /* |
|
| 798 | 798 | |-------------------------------------------------------------------------- |
| 799 | 799 | | MISC |
| 800 | 800 | |-------------------------------------------------------------------------- |
@@ -802,147 +802,147 @@ discard block |
||
| 802 | 802 | | Extra submission functions. |
| 803 | 803 | */ |
| 804 | 804 | |
| 805 | - /** |
|
| 806 | - * Returns the shipping amount. |
|
| 807 | - * |
|
| 808 | - * @since 1.0.19 |
|
| 809 | - */ |
|
| 810 | - public function get_shipping() { |
|
| 811 | - return $this->totals['shipping']['initial']; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * Returns the recurring shipping. |
|
| 816 | - * |
|
| 817 | - * @since 1.0.19 |
|
| 818 | - */ |
|
| 819 | - public function get_recurring_shipping() { |
|
| 820 | - return $this->totals['shipping']['recurring']; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Checks if there are any shipping fees for the form. |
|
| 825 | - * |
|
| 826 | - * @return bool |
|
| 827 | - * @since 1.0.19 |
|
| 828 | - */ |
|
| 829 | - public function has_shipping() { |
|
| 830 | - return apply_filters( 'getpaid_payment_form_has_shipping', false, $this ); |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - /** |
|
| 834 | - * Checks if this is the initial fetch. |
|
| 835 | - * |
|
| 836 | - * @return bool |
|
| 837 | - * @since 1.0.19 |
|
| 838 | - */ |
|
| 839 | - public function is_initial_fetch() { |
|
| 840 | - return empty( $this->data['initial_state'] ); |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - /** |
|
| 844 | - * Returns the total amount to collect for this submission. |
|
| 845 | - * |
|
| 846 | - * @since 1.0.19 |
|
| 847 | - */ |
|
| 848 | - public function get_total() { |
|
| 849 | - $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() + $this->get_shipping() - $this->get_discount(); |
|
| 850 | - return max( $total, 0 ); |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - /** |
|
| 854 | - * Returns the recurring total amount to collect for this submission. |
|
| 855 | - * |
|
| 856 | - * @since 1.0.19 |
|
| 857 | - */ |
|
| 858 | - public function get_recurring_total() { |
|
| 859 | - $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() + $this->get_recurring_shipping() - $this->get_recurring_discount(); |
|
| 860 | - return max( $total, 0 ); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Whether payment details should be collected for this submission. |
|
| 865 | - * |
|
| 866 | - * @since 1.0.19 |
|
| 867 | - */ |
|
| 868 | - public function should_collect_payment_details() { |
|
| 869 | - $initial = $this->get_total(); |
|
| 870 | - $recurring = $this->get_recurring_total(); |
|
| 871 | - |
|
| 872 | - if ( $this->has_recurring == 0 ) { |
|
| 873 | - $recurring = 0; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - $collect = $initial > 0 || $recurring > 0; |
|
| 877 | - return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - /** |
|
| 881 | - * Returns the billing email of the user. |
|
| 882 | - * |
|
| 883 | - * @since 1.0.19 |
|
| 884 | - */ |
|
| 885 | - public function get_billing_email() { |
|
| 886 | - return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - /** |
|
| 890 | - * Checks if the submitter has a billing email. |
|
| 891 | - * |
|
| 892 | - * @since 1.0.19 |
|
| 893 | - */ |
|
| 894 | - public function has_billing_email() { |
|
| 895 | - $billing_email = $this->get_billing_email(); |
|
| 896 | - return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - /** |
|
| 900 | - * Returns the appropriate currency for the submission. |
|
| 901 | - * |
|
| 902 | - * @since 1.0.19 |
|
| 903 | - * @return string |
|
| 904 | - */ |
|
| 905 | - public function get_currency() { |
|
| 906 | - return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - /** |
|
| 910 | - * Returns the raw submission data. |
|
| 911 | - * |
|
| 912 | - * @since 1.0.19 |
|
| 913 | - * @return array |
|
| 914 | - */ |
|
| 915 | - public function get_data() { |
|
| 916 | - return $this->data; |
|
| 917 | - } |
|
| 918 | - |
|
| 919 | - /** |
|
| 920 | - * Returns a field from the submission data |
|
| 921 | - * |
|
| 922 | - * @param string $field |
|
| 923 | - * @since 1.0.19 |
|
| 924 | - * @return mixed|null |
|
| 925 | - */ |
|
| 926 | - public function get_field( $field, $sub_array_key = null ) { |
|
| 927 | - return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 928 | - } |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * Checks if a required field is set. |
|
| 932 | - * |
|
| 933 | - * @since 1.0.19 |
|
| 934 | - */ |
|
| 935 | - public function is_required_field_set( $field ) { |
|
| 936 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - /** |
|
| 940 | - * Formats an amount |
|
| 941 | - * |
|
| 942 | - * @since 1.0.19 |
|
| 943 | - */ |
|
| 944 | - public function format_amount( $amount ) { |
|
| 945 | - return wpinv_price( $amount, $this->get_currency() ); |
|
| 946 | - } |
|
| 805 | + /** |
|
| 806 | + * Returns the shipping amount. |
|
| 807 | + * |
|
| 808 | + * @since 1.0.19 |
|
| 809 | + */ |
|
| 810 | + public function get_shipping() { |
|
| 811 | + return $this->totals['shipping']['initial']; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * Returns the recurring shipping. |
|
| 816 | + * |
|
| 817 | + * @since 1.0.19 |
|
| 818 | + */ |
|
| 819 | + public function get_recurring_shipping() { |
|
| 820 | + return $this->totals['shipping']['recurring']; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Checks if there are any shipping fees for the form. |
|
| 825 | + * |
|
| 826 | + * @return bool |
|
| 827 | + * @since 1.0.19 |
|
| 828 | + */ |
|
| 829 | + public function has_shipping() { |
|
| 830 | + return apply_filters( 'getpaid_payment_form_has_shipping', false, $this ); |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + /** |
|
| 834 | + * Checks if this is the initial fetch. |
|
| 835 | + * |
|
| 836 | + * @return bool |
|
| 837 | + * @since 1.0.19 |
|
| 838 | + */ |
|
| 839 | + public function is_initial_fetch() { |
|
| 840 | + return empty( $this->data['initial_state'] ); |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + /** |
|
| 844 | + * Returns the total amount to collect for this submission. |
|
| 845 | + * |
|
| 846 | + * @since 1.0.19 |
|
| 847 | + */ |
|
| 848 | + public function get_total() { |
|
| 849 | + $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() + $this->get_shipping() - $this->get_discount(); |
|
| 850 | + return max( $total, 0 ); |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + /** |
|
| 854 | + * Returns the recurring total amount to collect for this submission. |
|
| 855 | + * |
|
| 856 | + * @since 1.0.19 |
|
| 857 | + */ |
|
| 858 | + public function get_recurring_total() { |
|
| 859 | + $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() + $this->get_recurring_shipping() - $this->get_recurring_discount(); |
|
| 860 | + return max( $total, 0 ); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Whether payment details should be collected for this submission. |
|
| 865 | + * |
|
| 866 | + * @since 1.0.19 |
|
| 867 | + */ |
|
| 868 | + public function should_collect_payment_details() { |
|
| 869 | + $initial = $this->get_total(); |
|
| 870 | + $recurring = $this->get_recurring_total(); |
|
| 871 | + |
|
| 872 | + if ( $this->has_recurring == 0 ) { |
|
| 873 | + $recurring = 0; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + $collect = $initial > 0 || $recurring > 0; |
|
| 877 | + return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + /** |
|
| 881 | + * Returns the billing email of the user. |
|
| 882 | + * |
|
| 883 | + * @since 1.0.19 |
|
| 884 | + */ |
|
| 885 | + public function get_billing_email() { |
|
| 886 | + return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + /** |
|
| 890 | + * Checks if the submitter has a billing email. |
|
| 891 | + * |
|
| 892 | + * @since 1.0.19 |
|
| 893 | + */ |
|
| 894 | + public function has_billing_email() { |
|
| 895 | + $billing_email = $this->get_billing_email(); |
|
| 896 | + return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + /** |
|
| 900 | + * Returns the appropriate currency for the submission. |
|
| 901 | + * |
|
| 902 | + * @since 1.0.19 |
|
| 903 | + * @return string |
|
| 904 | + */ |
|
| 905 | + public function get_currency() { |
|
| 906 | + return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + /** |
|
| 910 | + * Returns the raw submission data. |
|
| 911 | + * |
|
| 912 | + * @since 1.0.19 |
|
| 913 | + * @return array |
|
| 914 | + */ |
|
| 915 | + public function get_data() { |
|
| 916 | + return $this->data; |
|
| 917 | + } |
|
| 918 | + |
|
| 919 | + /** |
|
| 920 | + * Returns a field from the submission data |
|
| 921 | + * |
|
| 922 | + * @param string $field |
|
| 923 | + * @since 1.0.19 |
|
| 924 | + * @return mixed|null |
|
| 925 | + */ |
|
| 926 | + public function get_field( $field, $sub_array_key = null ) { |
|
| 927 | + return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 928 | + } |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * Checks if a required field is set. |
|
| 932 | + * |
|
| 933 | + * @since 1.0.19 |
|
| 934 | + */ |
|
| 935 | + public function is_required_field_set( $field ) { |
|
| 936 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + /** |
|
| 940 | + * Formats an amount |
|
| 941 | + * |
|
| 942 | + * @since 1.0.19 |
|
| 943 | + */ |
|
| 944 | + public function format_amount( $amount ) { |
|
| 945 | + return wpinv_price( $amount, $this->get_currency() ); |
|
| 946 | + } |
|
| 947 | 947 | |
| 948 | 948 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -10,55 +10,55 @@ discard block |
||
| 10 | 10 | class GetPaid_Payment_Form extends GetPaid_Data { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Which data store to load. |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 13 | + * Which data store to load. |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | 17 | protected $data_store_name = 'payment_form'; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * This is the name of this object type. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $object_type = 'payment_form'; |
|
| 20 | + * This is the name of this object type. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $object_type = 'payment_form'; |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | - * Form Data array. This is the core form data exposed in APIs. |
|
| 28 | - * |
|
| 29 | - * @since 1.0.19 |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $data = array( |
|
| 33 | - 'status' => 'draft', |
|
| 34 | - 'version' => '', |
|
| 35 | - 'date_created' => null, |
|
| 27 | + * Form Data array. This is the core form data exposed in APIs. |
|
| 28 | + * |
|
| 29 | + * @since 1.0.19 |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $data = array( |
|
| 33 | + 'status' => 'draft', |
|
| 34 | + 'version' => '', |
|
| 35 | + 'date_created' => null, |
|
| 36 | 36 | 'date_modified' => null, |
| 37 | 37 | 'name' => '', |
| 38 | 38 | 'author' => 1, |
| 39 | 39 | 'elements' => null, |
| 40 | - 'items' => null, |
|
| 41 | - 'earned' => 0, |
|
| 42 | - 'refunded' => 0, |
|
| 43 | - 'cancelled' => 0, |
|
| 44 | - 'failed' => 0, |
|
| 45 | - ); |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Stores meta in cache for future reads. |
|
| 49 | - * |
|
| 50 | - * A group must be set to to enable caching. |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $cache_group = 'getpaid_forms'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Stores a reference to the invoice if the form is for an invoice.. |
|
| 58 | - * |
|
| 59 | - * @var WPInv_Invoice |
|
| 60 | - */ |
|
| 61 | - public $invoice = 0; |
|
| 40 | + 'items' => null, |
|
| 41 | + 'earned' => 0, |
|
| 42 | + 'refunded' => 0, |
|
| 43 | + 'cancelled' => 0, |
|
| 44 | + 'failed' => 0, |
|
| 45 | + ); |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Stores meta in cache for future reads. |
|
| 49 | + * |
|
| 50 | + * A group must be set to to enable caching. |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $cache_group = 'getpaid_forms'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Stores a reference to the invoice if the form is for an invoice.. |
|
| 58 | + * |
|
| 59 | + * @var WPInv_Invoice |
|
| 60 | + */ |
|
| 61 | + public $invoice = 0; |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Stores a reference to the original WP_Post object |
@@ -68,35 +68,35 @@ discard block |
||
| 68 | 68 | protected $post = null; |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | - * Get the form if ID is passed, otherwise the form is new and empty. |
|
| 72 | - * |
|
| 73 | - * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
| 74 | - */ |
|
| 75 | - public function __construct( $form = 0 ) { |
|
| 76 | - parent::__construct( $form ); |
|
| 71 | + * Get the form if ID is passed, otherwise the form is new and empty. |
|
| 72 | + * |
|
| 73 | + * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
| 74 | + */ |
|
| 75 | + public function __construct( $form = 0 ) { |
|
| 76 | + parent::__construct( $form ); |
|
| 77 | 77 | |
| 78 | - if ( is_numeric( $form ) && $form > 0 ) { |
|
| 79 | - $this->set_id( $form ); |
|
| 80 | - } elseif ( $form instanceof self ) { |
|
| 78 | + if ( is_numeric( $form ) && $form > 0 ) { |
|
| 79 | + $this->set_id( $form ); |
|
| 80 | + } elseif ( $form instanceof self ) { |
|
| 81 | 81 | |
| 82 | - $this->set_id( $form->get_id() ); |
|
| 83 | - $this->invoice = $form->invoice; |
|
| 82 | + $this->set_id( $form->get_id() ); |
|
| 83 | + $this->invoice = $form->invoice; |
|
| 84 | 84 | |
| 85 | - } elseif ( ! empty( $form->ID ) ) { |
|
| 86 | - $this->set_id( $form->ID ); |
|
| 87 | - } else { |
|
| 88 | - $this->set_object_read( true ); |
|
| 89 | - } |
|
| 85 | + } elseif ( ! empty( $form->ID ) ) { |
|
| 86 | + $this->set_id( $form->ID ); |
|
| 87 | + } else { |
|
| 88 | + $this->set_object_read( true ); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | 91 | // Load the datastore. |
| 92 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 92 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 93 | 93 | |
| 94 | - if ( $this->get_id() > 0 ) { |
|
| 94 | + if ( $this->get_id() > 0 ) { |
|
| 95 | 95 | $this->post = get_post( $this->get_id() ); |
| 96 | - $this->data_store->read( $this ); |
|
| 96 | + $this->data_store->read( $this ); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | /* |
| 102 | 102 | |-------------------------------------------------------------------------- |
@@ -114,356 +114,356 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | |
| 116 | 116 | /** |
| 117 | - * Get plugin version when the form was created. |
|
| 118 | - * |
|
| 119 | - * @since 1.0.19 |
|
| 120 | - * @param string $context View or edit context. |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function get_version( $context = 'view' ) { |
|
| 124 | - return $this->get_prop( 'version', $context ); |
|
| 117 | + * Get plugin version when the form was created. |
|
| 118 | + * |
|
| 119 | + * @since 1.0.19 |
|
| 120 | + * @param string $context View or edit context. |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function get_version( $context = 'view' ) { |
|
| 124 | + return $this->get_prop( 'version', $context ); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | - * Get date when the form was created. |
|
| 129 | - * |
|
| 130 | - * @since 1.0.19 |
|
| 131 | - * @param string $context View or edit context. |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function get_date_created( $context = 'view' ) { |
|
| 135 | - return $this->get_prop( 'date_created', $context ); |
|
| 128 | + * Get date when the form was created. |
|
| 129 | + * |
|
| 130 | + * @since 1.0.19 |
|
| 131 | + * @param string $context View or edit context. |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function get_date_created( $context = 'view' ) { |
|
| 135 | + return $this->get_prop( 'date_created', $context ); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
| 139 | - * Get GMT date when the form was created. |
|
| 140 | - * |
|
| 141 | - * @since 1.0.19 |
|
| 142 | - * @param string $context View or edit context. |
|
| 143 | - * @return string |
|
| 144 | - */ |
|
| 145 | - public function get_date_created_gmt( $context = 'view' ) { |
|
| 139 | + * Get GMT date when the form was created. |
|
| 140 | + * |
|
| 141 | + * @since 1.0.19 |
|
| 142 | + * @param string $context View or edit context. |
|
| 143 | + * @return string |
|
| 144 | + */ |
|
| 145 | + public function get_date_created_gmt( $context = 'view' ) { |
|
| 146 | 146 | $date = $this->get_date_created( $context ); |
| 147 | 147 | |
| 148 | 148 | if ( $date ) { |
| 149 | 149 | $date = get_gmt_from_date( $date ); |
| 150 | 150 | } |
| 151 | - return $date; |
|
| 151 | + return $date; |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | - * Get date when the form was last modified. |
|
| 156 | - * |
|
| 157 | - * @since 1.0.19 |
|
| 158 | - * @param string $context View or edit context. |
|
| 159 | - * @return string |
|
| 160 | - */ |
|
| 161 | - public function get_date_modified( $context = 'view' ) { |
|
| 162 | - return $this->get_prop( 'date_modified', $context ); |
|
| 155 | + * Get date when the form was last modified. |
|
| 156 | + * |
|
| 157 | + * @since 1.0.19 |
|
| 158 | + * @param string $context View or edit context. |
|
| 159 | + * @return string |
|
| 160 | + */ |
|
| 161 | + public function get_date_modified( $context = 'view' ) { |
|
| 162 | + return $this->get_prop( 'date_modified', $context ); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | - * Get GMT date when the form was last modified. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.19 |
|
| 169 | - * @param string $context View or edit context. |
|
| 170 | - * @return string |
|
| 171 | - */ |
|
| 172 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
| 166 | + * Get GMT date when the form was last modified. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.19 |
|
| 169 | + * @param string $context View or edit context. |
|
| 170 | + * @return string |
|
| 171 | + */ |
|
| 172 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
| 173 | 173 | $date = $this->get_date_modified( $context ); |
| 174 | 174 | |
| 175 | 175 | if ( $date ) { |
| 176 | 176 | $date = get_gmt_from_date( $date ); |
| 177 | 177 | } |
| 178 | - return $date; |
|
| 178 | + return $date; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
| 182 | - * Get the form name. |
|
| 183 | - * |
|
| 184 | - * @since 1.0.19 |
|
| 185 | - * @param string $context View or edit context. |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - public function get_name( $context = 'view' ) { |
|
| 189 | - return $this->get_prop( 'name', $context ); |
|
| 182 | + * Get the form name. |
|
| 183 | + * |
|
| 184 | + * @since 1.0.19 |
|
| 185 | + * @param string $context View or edit context. |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + public function get_name( $context = 'view' ) { |
|
| 189 | + return $this->get_prop( 'name', $context ); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** |
| 193 | - * Alias of self::get_name(). |
|
| 194 | - * |
|
| 195 | - * @since 1.0.19 |
|
| 196 | - * @param string $context View or edit context. |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - public function get_title( $context = 'view' ) { |
|
| 200 | - return $this->get_name( $context ); |
|
| 201 | - } |
|
| 193 | + * Alias of self::get_name(). |
|
| 194 | + * |
|
| 195 | + * @since 1.0.19 |
|
| 196 | + * @param string $context View or edit context. |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + public function get_title( $context = 'view' ) { |
|
| 200 | + return $this->get_name( $context ); |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | 203 | /** |
| 204 | - * Get the owner of the form. |
|
| 205 | - * |
|
| 206 | - * @since 1.0.19 |
|
| 207 | - * @param string $context View or edit context. |
|
| 208 | - * @return int |
|
| 209 | - */ |
|
| 210 | - public function get_author( $context = 'view' ) { |
|
| 211 | - return (int) $this->get_prop( 'author', $context ); |
|
| 204 | + * Get the owner of the form. |
|
| 205 | + * |
|
| 206 | + * @since 1.0.19 |
|
| 207 | + * @param string $context View or edit context. |
|
| 208 | + * @return int |
|
| 209 | + */ |
|
| 210 | + public function get_author( $context = 'view' ) { |
|
| 211 | + return (int) $this->get_prop( 'author', $context ); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | /** |
| 215 | - * Get the elements that make up the form. |
|
| 216 | - * |
|
| 217 | - * @since 1.0.19 |
|
| 218 | - * @param string $context View or edit context. |
|
| 219 | - * @return array |
|
| 220 | - */ |
|
| 221 | - public function get_elements( $context = 'view' ) { |
|
| 222 | - $elements = $this->get_prop( 'elements', $context ); |
|
| 215 | + * Get the elements that make up the form. |
|
| 216 | + * |
|
| 217 | + * @since 1.0.19 |
|
| 218 | + * @param string $context View or edit context. |
|
| 219 | + * @return array |
|
| 220 | + */ |
|
| 221 | + public function get_elements( $context = 'view' ) { |
|
| 222 | + $elements = $this->get_prop( 'elements', $context ); |
|
| 223 | 223 | |
| 224 | - if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
| 224 | + if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
| 225 | 225 | return wpinv_get_data( 'sample-payment-form' ); |
| 226 | - } |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - // Ensure that all required elements exist. |
|
| 229 | - $_elements = array(); |
|
| 230 | - foreach ( $elements as $element ) { |
|
| 228 | + // Ensure that all required elements exist. |
|
| 229 | + $_elements = array(); |
|
| 230 | + foreach ( $elements as $element ) { |
|
| 231 | 231 | |
| 232 | - if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
| 232 | + if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
| 233 | 233 | |
| 234 | - $_elements[] = array( |
|
| 235 | - 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
| 236 | - 'id' => 'gtscicd', |
|
| 237 | - 'name' => 'gtscicd', |
|
| 238 | - 'type' => 'gateway_select', |
|
| 239 | - 'premade' => true, |
|
| 234 | + $_elements[] = array( |
|
| 235 | + 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
| 236 | + 'id' => 'gtscicd', |
|
| 237 | + 'name' => 'gtscicd', |
|
| 238 | + 'type' => 'gateway_select', |
|
| 239 | + 'premade' => true, |
|
| 240 | 240 | |
| 241 | - ); |
|
| 241 | + ); |
|
| 242 | 242 | |
| 243 | - } |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - $_elements[] = $element; |
|
| 245 | + $_elements[] = $element; |
|
| 246 | 246 | |
| 247 | - } |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | 249 | return $_elements; |
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get the items sold via the form. |
|
| 254 | - * |
|
| 255 | - * @since 1.0.19 |
|
| 256 | - * @param string $context View or edit context. |
|
| 257 | - * @param string $return objects or arrays. |
|
| 258 | - * @return GetPaid_Form_Item[] |
|
| 259 | - */ |
|
| 260 | - public function get_items( $context = 'view', $return = 'objects' ) { |
|
| 261 | - $items = $this->get_prop( 'items', $context ); |
|
| 262 | - |
|
| 263 | - if ( empty( $items ) || ! is_array( $items ) ) { |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get the items sold via the form. |
|
| 254 | + * |
|
| 255 | + * @since 1.0.19 |
|
| 256 | + * @param string $context View or edit context. |
|
| 257 | + * @param string $return objects or arrays. |
|
| 258 | + * @return GetPaid_Form_Item[] |
|
| 259 | + */ |
|
| 260 | + public function get_items( $context = 'view', $return = 'objects' ) { |
|
| 261 | + $items = $this->get_prop( 'items', $context ); |
|
| 262 | + |
|
| 263 | + if ( empty( $items ) || ! is_array( $items ) ) { |
|
| 264 | 264 | $items = wpinv_get_data( 'sample-payment-form-items' ); |
| 265 | - } |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - // Convert the items. |
|
| 268 | - $prepared = array(); |
|
| 267 | + // Convert the items. |
|
| 268 | + $prepared = array(); |
|
| 269 | 269 | |
| 270 | - foreach ( $items as $key => $value ) { |
|
| 270 | + foreach ( $items as $key => $value ) { |
|
| 271 | 271 | |
| 272 | - // Form items. |
|
| 273 | - if ( $value instanceof GetPaid_Form_Item ) { |
|
| 272 | + // Form items. |
|
| 273 | + if ( $value instanceof GetPaid_Form_Item ) { |
|
| 274 | 274 | |
| 275 | - if ( $value->can_purchase() ) { |
|
| 276 | - $prepared[] = $value; |
|
| 277 | - } |
|
| 275 | + if ( $value->can_purchase() ) { |
|
| 276 | + $prepared[] = $value; |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - continue; |
|
| 279 | + continue; |
|
| 280 | 280 | |
| 281 | - } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - // $item_id => $quantity (buy buttons) |
|
| 284 | - if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
| 285 | - $item = new GetPaid_Form_Item( $key ); |
|
| 283 | + // $item_id => $quantity (buy buttons) |
|
| 284 | + if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
| 285 | + $item = new GetPaid_Form_Item( $key ); |
|
| 286 | 286 | |
| 287 | - if ( $item->can_purchase() ) { |
|
| 287 | + if ( $item->can_purchase() ) { |
|
| 288 | 288 | |
| 289 | - $value = (float) $value; |
|
| 290 | - $item->set_quantity( $value ); |
|
| 291 | - if ( 0 == $value ) { |
|
| 292 | - $item->set_quantity( 1 ); |
|
| 293 | - $item->set_allow_quantities( true ); |
|
| 294 | - } |
|
| 289 | + $value = (float) $value; |
|
| 290 | + $item->set_quantity( $value ); |
|
| 291 | + if ( 0 == $value ) { |
|
| 292 | + $item->set_quantity( 1 ); |
|
| 293 | + $item->set_allow_quantities( true ); |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - $prepared[] = $item; |
|
| 297 | - } |
|
| 296 | + $prepared[] = $item; |
|
| 297 | + } |
|
| 298 | 298 | |
| 299 | - continue; |
|
| 300 | - } |
|
| 299 | + continue; |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - // Items saved via payment forms editor. |
|
| 303 | - if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
| 302 | + // Items saved via payment forms editor. |
|
| 303 | + if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
| 304 | 304 | |
| 305 | - $item = new GetPaid_Form_Item( $value['id'] ); |
|
| 305 | + $item = new GetPaid_Form_Item( $value['id'] ); |
|
| 306 | 306 | |
| 307 | - if ( ! $item->can_purchase() ) { |
|
| 308 | - continue; |
|
| 309 | - } |
|
| 307 | + if ( ! $item->can_purchase() ) { |
|
| 308 | + continue; |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - // Sub-total (Cart items). |
|
| 312 | - if ( isset( $value['subtotal'] ) ) { |
|
| 313 | - $item->set_price( $value['subtotal'] ); |
|
| 314 | - } |
|
| 311 | + // Sub-total (Cart items). |
|
| 312 | + if ( isset( $value['subtotal'] ) ) { |
|
| 313 | + $item->set_price( $value['subtotal'] ); |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - if ( isset( $value['quantity'] ) ) { |
|
| 317 | - $item->set_quantity( $value['quantity'] ); |
|
| 318 | - } |
|
| 316 | + if ( isset( $value['quantity'] ) ) { |
|
| 317 | + $item->set_quantity( $value['quantity'] ); |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - if ( isset( $value['allow_quantities'] ) ) { |
|
| 321 | - $item->set_allow_quantities( $value['allow_quantities'] ); |
|
| 322 | - } |
|
| 320 | + if ( isset( $value['allow_quantities'] ) ) { |
|
| 321 | + $item->set_allow_quantities( $value['allow_quantities'] ); |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - if ( isset( $value['required'] ) ) { |
|
| 325 | - $item->set_is_required( $value['required'] ); |
|
| 326 | - } |
|
| 324 | + if ( isset( $value['required'] ) ) { |
|
| 325 | + $item->set_is_required( $value['required'] ); |
|
| 326 | + } |
|
| 327 | 327 | |
| 328 | - if ( isset( $value['description'] ) ) { |
|
| 329 | - $item->set_custom_description( $value['description'] ); |
|
| 330 | - } |
|
| 328 | + if ( isset( $value['description'] ) ) { |
|
| 329 | + $item->set_custom_description( $value['description'] ); |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - $prepared[] = $item; |
|
| 333 | - continue; |
|
| 332 | + $prepared[] = $item; |
|
| 333 | + continue; |
|
| 334 | 334 | |
| 335 | - } |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - // $item_id => array( 'price' => 10 ) (item variations) |
|
| 338 | - if ( is_numeric( $key ) && is_array( $value ) ) { |
|
| 339 | - $item = new GetPaid_Form_Item( $key ); |
|
| 337 | + // $item_id => array( 'price' => 10 ) (item variations) |
|
| 338 | + if ( is_numeric( $key ) && is_array( $value ) ) { |
|
| 339 | + $item = new GetPaid_Form_Item( $key ); |
|
| 340 | 340 | |
| 341 | - if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
| 342 | - $item->set_price( $value['price'] ); |
|
| 343 | - } |
|
| 341 | + if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
| 342 | + $item->set_price( $value['price'] ); |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - if ( $item->can_purchase() ) { |
|
| 346 | - $prepared[] = $item; |
|
| 347 | - } |
|
| 345 | + if ( $item->can_purchase() ) { |
|
| 346 | + $prepared[] = $item; |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - continue; |
|
| 350 | - } |
|
| 349 | + continue; |
|
| 350 | + } |
|
| 351 | 351 | } |
| 352 | 352 | |
| 353 | - if ( 'objects' == $return && 'view' == $context ) { |
|
| 354 | - return $prepared; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - $items = array(); |
|
| 358 | - foreach ( $prepared as $item ) { |
|
| 359 | - $items[] = $item->prepare_data_for_use(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - return $items; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Get a single item belonging to the form. |
|
| 367 | - * |
|
| 368 | - * @since 1.0.19 |
|
| 369 | - * @param int $item_id The item id to return. |
|
| 370 | - * @return GetPaid_Form_Item|bool |
|
| 371 | - */ |
|
| 372 | - public function get_item( $item_id ) { |
|
| 373 | - |
|
| 374 | - if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
| 375 | - return false; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - foreach ( $this->get_items() as $item ) { |
|
| 379 | - if ( $item->get_id() == (int) $item_id ) { |
|
| 380 | - return $item; |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - return false; |
|
| 385 | - |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Gets a single element. |
|
| 390 | - * |
|
| 391 | - * @since 1.0.19 |
|
| 392 | - * @param string $element_type The element type to return. |
|
| 393 | - * @return array|bool |
|
| 394 | - */ |
|
| 395 | - public function get_element_type( $element_type ) { |
|
| 396 | - |
|
| 397 | - if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
| 402 | - |
|
| 403 | - if ( $element['type'] == $element_type ) { |
|
| 404 | - return $element; |
|
| 405 | - } |
|
| 353 | + if ( 'objects' == $return && 'view' == $context ) { |
|
| 354 | + return $prepared; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + $items = array(); |
|
| 358 | + foreach ( $prepared as $item ) { |
|
| 359 | + $items[] = $item->prepare_data_for_use(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + return $items; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Get a single item belonging to the form. |
|
| 367 | + * |
|
| 368 | + * @since 1.0.19 |
|
| 369 | + * @param int $item_id The item id to return. |
|
| 370 | + * @return GetPaid_Form_Item|bool |
|
| 371 | + */ |
|
| 372 | + public function get_item( $item_id ) { |
|
| 373 | + |
|
| 374 | + if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
| 375 | + return false; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + foreach ( $this->get_items() as $item ) { |
|
| 379 | + if ( $item->get_id() == (int) $item_id ) { |
|
| 380 | + return $item; |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + return false; |
|
| 385 | + |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Gets a single element. |
|
| 390 | + * |
|
| 391 | + * @since 1.0.19 |
|
| 392 | + * @param string $element_type The element type to return. |
|
| 393 | + * @return array|bool |
|
| 394 | + */ |
|
| 395 | + public function get_element_type( $element_type ) { |
|
| 396 | + |
|
| 397 | + if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
| 402 | + |
|
| 403 | + if ( $element['type'] == $element_type ) { |
|
| 404 | + return $element; |
|
| 405 | + } |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | - return false; |
|
| 409 | - |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get the total amount earned via this form. |
|
| 414 | - * |
|
| 415 | - * @since 1.0.19 |
|
| 416 | - * @param string $context View or edit context. |
|
| 417 | - * @return float |
|
| 418 | - */ |
|
| 419 | - public function get_earned( $context = 'view' ) { |
|
| 420 | - return $this->get_prop( 'earned', $context ); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Get the total amount refunded via this form. |
|
| 425 | - * |
|
| 426 | - * @since 1.0.19 |
|
| 427 | - * @param string $context View or edit context. |
|
| 428 | - * @return float |
|
| 429 | - */ |
|
| 430 | - public function get_refunded( $context = 'view' ) { |
|
| 431 | - return $this->get_prop( 'refunded', $context ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Get the total amount cancelled via this form. |
|
| 436 | - * |
|
| 437 | - * @since 1.0.19 |
|
| 438 | - * @param string $context View or edit context. |
|
| 439 | - * @return float |
|
| 440 | - */ |
|
| 441 | - public function get_cancelled( $context = 'view' ) { |
|
| 442 | - return $this->get_prop( 'cancelled', $context ); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Get the total amount failed via this form. |
|
| 447 | - * |
|
| 448 | - * @since 1.0.19 |
|
| 449 | - * @param string $context View or edit context. |
|
| 450 | - * @return float |
|
| 451 | - */ |
|
| 452 | - public function get_failed( $context = 'view' ) { |
|
| 453 | - return $this->get_prop( 'failed', $context ); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Get the currency. |
|
| 458 | - * |
|
| 459 | - * @since 1.0.19 |
|
| 460 | - * @param string $context View or edit context. |
|
| 461 | - * @return string |
|
| 462 | - */ |
|
| 463 | - public function get_currency() { |
|
| 464 | - $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
| 465 | - return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
| 466 | - } |
|
| 408 | + return false; |
|
| 409 | + |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get the total amount earned via this form. |
|
| 414 | + * |
|
| 415 | + * @since 1.0.19 |
|
| 416 | + * @param string $context View or edit context. |
|
| 417 | + * @return float |
|
| 418 | + */ |
|
| 419 | + public function get_earned( $context = 'view' ) { |
|
| 420 | + return $this->get_prop( 'earned', $context ); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Get the total amount refunded via this form. |
|
| 425 | + * |
|
| 426 | + * @since 1.0.19 |
|
| 427 | + * @param string $context View or edit context. |
|
| 428 | + * @return float |
|
| 429 | + */ |
|
| 430 | + public function get_refunded( $context = 'view' ) { |
|
| 431 | + return $this->get_prop( 'refunded', $context ); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Get the total amount cancelled via this form. |
|
| 436 | + * |
|
| 437 | + * @since 1.0.19 |
|
| 438 | + * @param string $context View or edit context. |
|
| 439 | + * @return float |
|
| 440 | + */ |
|
| 441 | + public function get_cancelled( $context = 'view' ) { |
|
| 442 | + return $this->get_prop( 'cancelled', $context ); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Get the total amount failed via this form. |
|
| 447 | + * |
|
| 448 | + * @since 1.0.19 |
|
| 449 | + * @param string $context View or edit context. |
|
| 450 | + * @return float |
|
| 451 | + */ |
|
| 452 | + public function get_failed( $context = 'view' ) { |
|
| 453 | + return $this->get_prop( 'failed', $context ); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Get the currency. |
|
| 458 | + * |
|
| 459 | + * @since 1.0.19 |
|
| 460 | + * @param string $context View or edit context. |
|
| 461 | + * @return string |
|
| 462 | + */ |
|
| 463 | + public function get_currency() { |
|
| 464 | + $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
| 465 | + return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | 468 | /* |
| 469 | 469 | |-------------------------------------------------------------------------- |
@@ -476,22 +476,22 @@ discard block |
||
| 476 | 476 | */ |
| 477 | 477 | |
| 478 | 478 | /** |
| 479 | - * Set plugin version when the item was created. |
|
| 480 | - * |
|
| 481 | - * @since 1.0.19 |
|
| 482 | - */ |
|
| 483 | - public function set_version( $value ) { |
|
| 484 | - $this->set_prop( 'version', $value ); |
|
| 479 | + * Set plugin version when the item was created. |
|
| 480 | + * |
|
| 481 | + * @since 1.0.19 |
|
| 482 | + */ |
|
| 483 | + public function set_version( $value ) { |
|
| 484 | + $this->set_prop( 'version', $value ); |
|
| 485 | 485 | } |
| 486 | 486 | |
| 487 | 487 | /** |
| 488 | - * Set date when the item was created. |
|
| 489 | - * |
|
| 490 | - * @since 1.0.19 |
|
| 491 | - * @param string $value Value to set. |
|
| 488 | + * Set date when the item was created. |
|
| 489 | + * |
|
| 490 | + * @since 1.0.19 |
|
| 491 | + * @param string $value Value to set. |
|
| 492 | 492 | * @return bool Whether or not the date was set. |
| 493 | - */ |
|
| 494 | - public function set_date_created( $value ) { |
|
| 493 | + */ |
|
| 494 | + public function set_date_created( $value ) { |
|
| 495 | 495 | $date = strtotime( $value ); |
| 496 | 496 | |
| 497 | 497 | if ( $date ) { |
@@ -504,13 +504,13 @@ discard block |
||
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | /** |
| 507 | - * Set date when the item was last modified. |
|
| 508 | - * |
|
| 509 | - * @since 1.0.19 |
|
| 510 | - * @param string $value Value to set. |
|
| 507 | + * Set date when the item was last modified. |
|
| 508 | + * |
|
| 509 | + * @since 1.0.19 |
|
| 510 | + * @param string $value Value to set. |
|
| 511 | 511 | * @return bool Whether or not the date was set. |
| 512 | - */ |
|
| 513 | - public function set_date_modified( $value ) { |
|
| 512 | + */ |
|
| 513 | + public function set_date_modified( $value ) { |
|
| 514 | 514 | $date = strtotime( $value ); |
| 515 | 515 | |
| 516 | 516 | if ( $date ) { |
@@ -523,164 +523,164 @@ discard block |
||
| 523 | 523 | } |
| 524 | 524 | |
| 525 | 525 | /** |
| 526 | - * Set the item name. |
|
| 527 | - * |
|
| 528 | - * @since 1.0.19 |
|
| 529 | - * @param string $value New name. |
|
| 530 | - */ |
|
| 531 | - public function set_name( $value ) { |
|
| 532 | - $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * Alias of self::set_name(). |
|
| 537 | - * |
|
| 538 | - * @since 1.0.19 |
|
| 539 | - * @param string $value New name. |
|
| 540 | - */ |
|
| 541 | - public function set_title( $value ) { |
|
| 542 | - $this->set_name( $value ); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Set the owner of the item. |
|
| 547 | - * |
|
| 548 | - * @since 1.0.19 |
|
| 549 | - * @param int $value New author. |
|
| 550 | - */ |
|
| 551 | - public function set_author( $value ) { |
|
| 552 | - $this->set_prop( 'author', (int) $value ); |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * Set the form elements. |
|
| 557 | - * |
|
| 558 | - * @since 1.0.19 |
|
| 559 | - * @sinve 2.3.4 Array values sanitized. |
|
| 560 | - * @param array $value Form elements. |
|
| 561 | - */ |
|
| 562 | - public function set_elements( $value ) { |
|
| 563 | - if ( is_array( $value ) ) { |
|
| 564 | - $this->set_prop( 'elements', wp_kses_post_deep( $value ) ); |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * Sanitize array values. |
|
| 570 | - * |
|
| 571 | - * @param $value |
|
| 572 | - * |
|
| 573 | - * @return mixed |
|
| 574 | - */ |
|
| 575 | - public function sanitize_array_values( $value ) { |
|
| 576 | - |
|
| 577 | - // sanitize |
|
| 578 | - if ( ! empty( $value ) ) { |
|
| 579 | - |
|
| 580 | - foreach ( $value as $key => $val_arr ) { |
|
| 581 | - |
|
| 582 | - if ( is_array( $val_arr ) ) { |
|
| 583 | - // check if we have sub array items. |
|
| 584 | - $sub_arr = array(); |
|
| 585 | - foreach ( $val_arr as $key2 => $val2 ) { |
|
| 586 | - if ( is_array( $val2 ) ) { |
|
| 587 | - $sub_arr[ $key2 ] = $this->sanitize_array_values( $val2 ); |
|
| 588 | - unset( $val_arr[ $key ][ $key2 ] ); |
|
| 589 | - } |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - // we allow some html in description so we sanitize it separately. |
|
| 593 | - $help_text = ! empty( $val_arr['description'] ) ? wp_kses_post( $val_arr['description'] ) : ''; |
|
| 594 | - |
|
| 595 | - // sanitize array elements |
|
| 596 | - $value[ $key ] = array_map( 'sanitize_text_field', $val_arr ); |
|
| 597 | - |
|
| 598 | - // add back the description if set |
|
| 599 | - if ( isset( $val_arr['description'] ) ) { |
|
| 526 | + * Set the item name. |
|
| 527 | + * |
|
| 528 | + * @since 1.0.19 |
|
| 529 | + * @param string $value New name. |
|
| 530 | + */ |
|
| 531 | + public function set_name( $value ) { |
|
| 532 | + $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * Alias of self::set_name(). |
|
| 537 | + * |
|
| 538 | + * @since 1.0.19 |
|
| 539 | + * @param string $value New name. |
|
| 540 | + */ |
|
| 541 | + public function set_title( $value ) { |
|
| 542 | + $this->set_name( $value ); |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Set the owner of the item. |
|
| 547 | + * |
|
| 548 | + * @since 1.0.19 |
|
| 549 | + * @param int $value New author. |
|
| 550 | + */ |
|
| 551 | + public function set_author( $value ) { |
|
| 552 | + $this->set_prop( 'author', (int) $value ); |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * Set the form elements. |
|
| 557 | + * |
|
| 558 | + * @since 1.0.19 |
|
| 559 | + * @sinve 2.3.4 Array values sanitized. |
|
| 560 | + * @param array $value Form elements. |
|
| 561 | + */ |
|
| 562 | + public function set_elements( $value ) { |
|
| 563 | + if ( is_array( $value ) ) { |
|
| 564 | + $this->set_prop( 'elements', wp_kses_post_deep( $value ) ); |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * Sanitize array values. |
|
| 570 | + * |
|
| 571 | + * @param $value |
|
| 572 | + * |
|
| 573 | + * @return mixed |
|
| 574 | + */ |
|
| 575 | + public function sanitize_array_values( $value ) { |
|
| 576 | + |
|
| 577 | + // sanitize |
|
| 578 | + if ( ! empty( $value ) ) { |
|
| 579 | + |
|
| 580 | + foreach ( $value as $key => $val_arr ) { |
|
| 581 | + |
|
| 582 | + if ( is_array( $val_arr ) ) { |
|
| 583 | + // check if we have sub array items. |
|
| 584 | + $sub_arr = array(); |
|
| 585 | + foreach ( $val_arr as $key2 => $val2 ) { |
|
| 586 | + if ( is_array( $val2 ) ) { |
|
| 587 | + $sub_arr[ $key2 ] = $this->sanitize_array_values( $val2 ); |
|
| 588 | + unset( $val_arr[ $key ][ $key2 ] ); |
|
| 589 | + } |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + // we allow some html in description so we sanitize it separately. |
|
| 593 | + $help_text = ! empty( $val_arr['description'] ) ? wp_kses_post( $val_arr['description'] ) : ''; |
|
| 594 | + |
|
| 595 | + // sanitize array elements |
|
| 596 | + $value[ $key ] = array_map( 'sanitize_text_field', $val_arr ); |
|
| 597 | + |
|
| 598 | + // add back the description if set |
|
| 599 | + if ( isset( $val_arr['description'] ) ) { |
|
| 600 | 600 | $value[ $key ]['description'] = $help_text;} |
| 601 | 601 | |
| 602 | - // add back sub array items after its been sanitized. |
|
| 603 | - if ( ! empty( $sub_arr ) ) { |
|
| 604 | - $value[ $key ] = array_merge( $value[ $key ], $sub_arr ); |
|
| 605 | - } |
|
| 606 | - } |
|
| 602 | + // add back sub array items after its been sanitized. |
|
| 603 | + if ( ! empty( $sub_arr ) ) { |
|
| 604 | + $value[ $key ] = array_merge( $value[ $key ], $sub_arr ); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | 607 | } |
| 608 | 608 | } |
| 609 | 609 | |
| 610 | - return $value; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * Set the form items. |
|
| 615 | - * |
|
| 616 | - * @since 1.0.19 |
|
| 617 | - * @param array $value Form elements. |
|
| 618 | - */ |
|
| 619 | - public function set_items( $value ) { |
|
| 620 | - if ( is_array( $value ) ) { |
|
| 621 | - $this->set_prop( 'items', $value ); |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Set the total amount earned via this form. |
|
| 627 | - * |
|
| 628 | - * @since 1.0.19 |
|
| 629 | - * @param float $value Amount earned. |
|
| 630 | - */ |
|
| 631 | - public function set_earned( $value ) { |
|
| 632 | - $value = max( (float) $value, 0 ); |
|
| 633 | - $this->set_prop( 'earned', $value ); |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Set the total amount refunded via this form. |
|
| 638 | - * |
|
| 639 | - * @since 1.0.19 |
|
| 640 | - * @param float $value Amount refunded. |
|
| 641 | - */ |
|
| 642 | - public function set_refunded( $value ) { |
|
| 643 | - $value = max( (float) $value, 0 ); |
|
| 644 | - $this->set_prop( 'refunded', $value ); |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * Set the total amount cancelled via this form. |
|
| 649 | - * |
|
| 650 | - * @since 1.0.19 |
|
| 651 | - * @param float $value Amount cancelled. |
|
| 652 | - */ |
|
| 653 | - public function set_cancelled( $value ) { |
|
| 654 | - $value = max( (float) $value, 0 ); |
|
| 655 | - $this->set_prop( 'cancelled', $value ); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Set the total amount failed via this form. |
|
| 660 | - * |
|
| 661 | - * @since 1.0.19 |
|
| 662 | - * @param float $value Amount cancelled. |
|
| 663 | - */ |
|
| 664 | - public function set_failed( $value ) { |
|
| 665 | - $value = max( (float) $value, 0 ); |
|
| 666 | - $this->set_prop( 'failed', $value ); |
|
| 667 | - } |
|
| 610 | + return $value; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * Set the form items. |
|
| 615 | + * |
|
| 616 | + * @since 1.0.19 |
|
| 617 | + * @param array $value Form elements. |
|
| 618 | + */ |
|
| 619 | + public function set_items( $value ) { |
|
| 620 | + if ( is_array( $value ) ) { |
|
| 621 | + $this->set_prop( 'items', $value ); |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Set the total amount earned via this form. |
|
| 627 | + * |
|
| 628 | + * @since 1.0.19 |
|
| 629 | + * @param float $value Amount earned. |
|
| 630 | + */ |
|
| 631 | + public function set_earned( $value ) { |
|
| 632 | + $value = max( (float) $value, 0 ); |
|
| 633 | + $this->set_prop( 'earned', $value ); |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Set the total amount refunded via this form. |
|
| 638 | + * |
|
| 639 | + * @since 1.0.19 |
|
| 640 | + * @param float $value Amount refunded. |
|
| 641 | + */ |
|
| 642 | + public function set_refunded( $value ) { |
|
| 643 | + $value = max( (float) $value, 0 ); |
|
| 644 | + $this->set_prop( 'refunded', $value ); |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * Set the total amount cancelled via this form. |
|
| 649 | + * |
|
| 650 | + * @since 1.0.19 |
|
| 651 | + * @param float $value Amount cancelled. |
|
| 652 | + */ |
|
| 653 | + public function set_cancelled( $value ) { |
|
| 654 | + $value = max( (float) $value, 0 ); |
|
| 655 | + $this->set_prop( 'cancelled', $value ); |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + /** |
|
| 659 | + * Set the total amount failed via this form. |
|
| 660 | + * |
|
| 661 | + * @since 1.0.19 |
|
| 662 | + * @param float $value Amount cancelled. |
|
| 663 | + */ |
|
| 664 | + public function set_failed( $value ) { |
|
| 665 | + $value = max( (float) $value, 0 ); |
|
| 666 | + $this->set_prop( 'failed', $value ); |
|
| 667 | + } |
|
| 668 | 668 | |
| 669 | 669 | /** |
| 670 | 670 | * Create an item. For backwards compatibilty. |
| 671 | 671 | * |
| 672 | 672 | * @deprecated |
| 673 | - * @return int item id |
|
| 673 | + * @return int item id |
|
| 674 | 674 | */ |
| 675 | 675 | public function create( $data = array() ) { |
| 676 | 676 | |
| 677 | - // Set the properties. |
|
| 678 | - if ( is_array( $data ) ) { |
|
| 679 | - $this->set_props( $data ); |
|
| 680 | - } |
|
| 677 | + // Set the properties. |
|
| 678 | + if ( is_array( $data ) ) { |
|
| 679 | + $this->set_props( $data ); |
|
| 680 | + } |
|
| 681 | 681 | |
| 682 | - // Save the item. |
|
| 683 | - return $this->save(); |
|
| 682 | + // Save the item. |
|
| 683 | + return $this->save(); |
|
| 684 | 684 | |
| 685 | 685 | } |
| 686 | 686 | |
@@ -688,7 +688,7 @@ discard block |
||
| 688 | 688 | * Updates an item. For backwards compatibilty. |
| 689 | 689 | * |
| 690 | 690 | * @deprecated |
| 691 | - * @return int item id |
|
| 691 | + * @return int item id |
|
| 692 | 692 | */ |
| 693 | 693 | public function update( $data = array() ) { |
| 694 | 694 | return $this->create( $data ); |
@@ -704,22 +704,22 @@ discard block |
||
| 704 | 704 | */ |
| 705 | 705 | |
| 706 | 706 | /** |
| 707 | - * Checks whether this is the default payment form. |
|
| 708 | - * |
|
| 709 | - * @since 1.0.19 |
|
| 710 | - * @return bool |
|
| 711 | - */ |
|
| 707 | + * Checks whether this is the default payment form. |
|
| 708 | + * |
|
| 709 | + * @since 1.0.19 |
|
| 710 | + * @return bool |
|
| 711 | + */ |
|
| 712 | 712 | public function is_default() { |
| 713 | 713 | $is_default = $this->get_id() == wpinv_get_default_payment_form(); |
| 714 | 714 | return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this ); |
| 715 | - } |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | 717 | /** |
| 718 | - * Checks whether the form is active. |
|
| 719 | - * |
|
| 720 | - * @since 1.0.19 |
|
| 721 | - * @return bool |
|
| 722 | - */ |
|
| 718 | + * Checks whether the form is active. |
|
| 719 | + * |
|
| 720 | + * @since 1.0.19 |
|
| 721 | + * @return bool |
|
| 722 | + */ |
|
| 723 | 723 | public function is_active() { |
| 724 | 724 | $is_active = 0 !== (int) $this->get_id(); |
| 725 | 725 | |
@@ -728,75 +728,75 @@ discard block |
||
| 728 | 728 | } |
| 729 | 729 | |
| 730 | 730 | return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this ); |
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * Checks whether the form has a given item. |
|
| 735 | - * |
|
| 736 | - * @since 1.0.19 |
|
| 737 | - * @return bool |
|
| 738 | - */ |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * Checks whether the form has a given item. |
|
| 735 | + * |
|
| 736 | + * @since 1.0.19 |
|
| 737 | + * @return bool |
|
| 738 | + */ |
|
| 739 | 739 | public function has_item( $item_id ) { |
| 740 | 740 | return false !== $this->get_item( $item_id ); |
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Checks whether the form has a given element. |
|
| 745 | - * |
|
| 746 | - * @since 1.0.19 |
|
| 747 | - * @return bool |
|
| 748 | - */ |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Checks whether the form has a given element. |
|
| 745 | + * |
|
| 746 | + * @since 1.0.19 |
|
| 747 | + * @return bool |
|
| 748 | + */ |
|
| 749 | 749 | public function has_element_type( $element_type ) { |
| 750 | 750 | return false !== $this->get_element_type( $element_type ); |
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Checks whether this form is recurring or not. |
|
| 755 | - * |
|
| 756 | - * @since 1.0.19 |
|
| 757 | - * @return bool |
|
| 758 | - */ |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Checks whether this form is recurring or not. |
|
| 755 | + * |
|
| 756 | + * @since 1.0.19 |
|
| 757 | + * @return bool |
|
| 758 | + */ |
|
| 759 | 759 | public function is_recurring() { |
| 760 | 760 | |
| 761 | - if ( ! empty( $this->invoice ) ) { |
|
| 762 | - return $this->invoice->is_recurring(); |
|
| 763 | - } |
|
| 761 | + if ( ! empty( $this->invoice ) ) { |
|
| 762 | + return $this->invoice->is_recurring(); |
|
| 763 | + } |
|
| 764 | 764 | |
| 765 | - foreach ( $this->get_items() as $item ) { |
|
| 765 | + foreach ( $this->get_items() as $item ) { |
|
| 766 | 766 | |
| 767 | - if ( $item->is_recurring() ) { |
|
| 768 | - return true; |
|
| 769 | - } |
|
| 767 | + if ( $item->is_recurring() ) { |
|
| 768 | + return true; |
|
| 769 | + } |
|
| 770 | 770 | } |
| 771 | 771 | |
| 772 | 772 | return false; |
| 773 | - } |
|
| 773 | + } |
|
| 774 | 774 | |
| 775 | - /** |
|
| 776 | - * Retrieves the form's html. |
|
| 777 | - * |
|
| 778 | - * @since 1.0.19 |
|
| 779 | - */ |
|
| 775 | + /** |
|
| 776 | + * Retrieves the form's html. |
|
| 777 | + * |
|
| 778 | + * @since 1.0.19 |
|
| 779 | + */ |
|
| 780 | 780 | public function get_html( $extra_markup = '' ) { |
| 781 | 781 | |
| 782 | - // Return the HTML. |
|
| 783 | - return wpinv_get_template_html( |
|
| 784 | - 'payment-forms/form.php', |
|
| 785 | - array( |
|
| 786 | - 'form' => $this, |
|
| 787 | - 'extra_markup' => $extra_markup, |
|
| 788 | - ) |
|
| 789 | - ); |
|
| 790 | - |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - /** |
|
| 794 | - * Displays the payment form. |
|
| 795 | - * |
|
| 796 | - * @since 1.0.19 |
|
| 797 | - */ |
|
| 782 | + // Return the HTML. |
|
| 783 | + return wpinv_get_template_html( |
|
| 784 | + 'payment-forms/form.php', |
|
| 785 | + array( |
|
| 786 | + 'form' => $this, |
|
| 787 | + 'extra_markup' => $extra_markup, |
|
| 788 | + ) |
|
| 789 | + ); |
|
| 790 | + |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + /** |
|
| 794 | + * Displays the payment form. |
|
| 795 | + * |
|
| 796 | + * @since 1.0.19 |
|
| 797 | + */ |
|
| 798 | 798 | public function display( $extra_markup = '' ) { |
| 799 | - echo $this->get_html( $extra_markup ); |
|
| 799 | + echo $this->get_html( $extra_markup ); |
|
| 800 | 800 | } |
| 801 | 801 | |
| 802 | 802 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -10,67 +10,67 @@ discard block |
||
| 10 | 10 | class GetPaid_Form_Item extends WPInv_Item { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Stores a custom description for the item. |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - protected $custom_description = null; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Stores the item quantity. |
|
| 21 | - * |
|
| 22 | - * @var float |
|
| 23 | - */ |
|
| 24 | - protected $quantity = 1; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Stores the item meta. |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $meta = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Is this item required? |
|
| 35 | - * |
|
| 36 | - * @var int |
|
| 37 | - */ |
|
| 38 | - protected $is_required = true; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Are quantities allowed? |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - protected $allow_quantities = false; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Associated invoice. |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - public $invoice_id = 0; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Item discount. |
|
| 56 | - * |
|
| 57 | - * @var float |
|
| 58 | - */ |
|
| 59 | - public $item_discount = 0; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Recurring item discount. |
|
| 63 | - * |
|
| 64 | - * @var float |
|
| 65 | - */ |
|
| 66 | - public $recurring_item_discount = 0; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Item tax. |
|
| 70 | - * |
|
| 71 | - * @var float |
|
| 72 | - */ |
|
| 73 | - public $item_tax = 0; |
|
| 13 | + * Stores a custom description for the item. |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + protected $custom_description = null; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Stores the item quantity. |
|
| 21 | + * |
|
| 22 | + * @var float |
|
| 23 | + */ |
|
| 24 | + protected $quantity = 1; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Stores the item meta. |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $meta = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Is this item required? |
|
| 35 | + * |
|
| 36 | + * @var int |
|
| 37 | + */ |
|
| 38 | + protected $is_required = true; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Are quantities allowed? |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + protected $allow_quantities = false; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Associated invoice. |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + public $invoice_id = 0; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Item discount. |
|
| 56 | + * |
|
| 57 | + * @var float |
|
| 58 | + */ |
|
| 59 | + public $item_discount = 0; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Recurring item discount. |
|
| 63 | + * |
|
| 64 | + * @var float |
|
| 65 | + */ |
|
| 66 | + public $recurring_item_discount = 0; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Item tax. |
|
| 70 | + * |
|
| 71 | + * @var float |
|
| 72 | + */ |
|
| 73 | + public $item_tax = 0; |
|
| 74 | 74 | |
| 75 | 75 | /* |
| 76 | 76 | |-------------------------------------------------------------------------- |
@@ -88,230 +88,230 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * Get the item name. |
|
| 92 | - * |
|
| 93 | - * @since 1.0.19 |
|
| 94 | - * @param string $context View or edit context. |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function get_name( $context = 'view' ) { |
|
| 98 | - $name = parent::get_name( $context ); |
|
| 99 | - return $name . wpinv_get_item_suffix( $this ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the item name without a suffix. |
|
| 104 | - * |
|
| 105 | - * @since 1.0.19 |
|
| 106 | - * @param string $context View or edit context. |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function get_raw_name( $context = 'view' ) { |
|
| 110 | - return parent::get_name( $context ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the item description. |
|
| 115 | - * |
|
| 116 | - * @since 1.0.19 |
|
| 117 | - * @param string $context View or edit context. |
|
| 118 | - * @return string |
|
| 119 | - */ |
|
| 120 | - public function get_description( $context = 'view' ) { |
|
| 121 | - |
|
| 122 | - if ( isset( $this->custom_description ) ) { |
|
| 123 | - return $this->custom_description; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return parent::get_description( $context ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Returns the sub total. |
|
| 131 | - * |
|
| 132 | - * @since 1.0.19 |
|
| 133 | - * @param string $context View or edit context. |
|
| 134 | - * @return float |
|
| 135 | - */ |
|
| 136 | - public function get_sub_total( $context = 'view' ) { |
|
| 137 | - return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Returns the recurring sub total. |
|
| 142 | - * |
|
| 143 | - * @since 1.0.19 |
|
| 144 | - * @param string $context View or edit context. |
|
| 145 | - * @return float |
|
| 146 | - */ |
|
| 147 | - public function get_recurring_sub_total( $context = 'view' ) { |
|
| 148 | - |
|
| 149 | - if ( $this->is_recurring() ) { |
|
| 150 | - return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return 0; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @deprecated |
|
| 158 | - */ |
|
| 159 | - public function get_qantity( $context = 'view' ) { |
|
| 160 | - return $this->get_quantity( $context ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Get the item quantity. |
|
| 165 | - * |
|
| 166 | - * @since 1.0.19 |
|
| 167 | - * @param string $context View or edit context. |
|
| 168 | - * @return float |
|
| 169 | - */ |
|
| 170 | - public function get_quantity( $context = 'view' ) { |
|
| 171 | - $quantity = (float) $this->quantity; |
|
| 172 | - |
|
| 173 | - if ( 'view' == $context ) { |
|
| 174 | - return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $quantity; |
|
| 178 | - |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the item meta data. |
|
| 183 | - * |
|
| 184 | - * @since 1.0.19 |
|
| 185 | - * @param string $context View or edit context. |
|
| 186 | - * @return meta |
|
| 187 | - */ |
|
| 188 | - public function get_item_meta( $context = 'view' ) { |
|
| 189 | - $meta = $this->meta; |
|
| 190 | - |
|
| 191 | - if ( 'view' == $context ) { |
|
| 192 | - return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $meta; |
|
| 196 | - |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Returns whether or not customers can update the item quantity. |
|
| 201 | - * |
|
| 202 | - * @since 1.0.19 |
|
| 203 | - * @param string $context View or edit context. |
|
| 204 | - * @return bool |
|
| 205 | - */ |
|
| 206 | - public function get_allow_quantities( $context = 'view' ) { |
|
| 207 | - $allow_quantities = (bool) $this->allow_quantities; |
|
| 208 | - |
|
| 209 | - if ( 'view' == $context ) { |
|
| 210 | - return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $allow_quantities; |
|
| 214 | - |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Returns whether or not the item is required. |
|
| 219 | - * |
|
| 220 | - * @since 1.0.19 |
|
| 221 | - * @param string $context View or edit context. |
|
| 222 | - * @return bool |
|
| 223 | - */ |
|
| 224 | - public function get_is_required( $context = 'view' ) { |
|
| 225 | - $is_required = (bool) $this->is_required; |
|
| 226 | - |
|
| 227 | - if ( 'view' == $context ) { |
|
| 228 | - return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return $is_required; |
|
| 232 | - |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Prepares form data for use. |
|
| 237 | - * |
|
| 238 | - * @since 1.0.19 |
|
| 239 | - * @return array |
|
| 240 | - */ |
|
| 241 | - public function prepare_data_for_use( $required = null ) { |
|
| 242 | - |
|
| 243 | - $required = is_null( $required ) ? $this->is_required() : $required; |
|
| 244 | - return array( |
|
| 245 | - 'title' => strip_tags( $this->get_name() ), |
|
| 246 | - 'id' => $this->get_id(), |
|
| 247 | - 'price' => $this->get_price(), |
|
| 248 | - 'recurring' => $this->is_recurring(), |
|
| 249 | - 'description' => $this->get_description(), |
|
| 250 | - 'allow_quantities' => $this->allows_quantities(), |
|
| 251 | - 'required' => $required, |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Prepares form data for ajax use. |
|
| 258 | - * |
|
| 259 | - * @since 1.0.19 |
|
| 260 | - * @return array |
|
| 261 | - */ |
|
| 262 | - public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) { |
|
| 263 | - |
|
| 264 | - $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
| 265 | - |
|
| 266 | - if ( $description ) { |
|
| 267 | - $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $price = ! $is_renewal ? $this->get_price() : $this->get_recurring_price(); |
|
| 271 | - $subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total(); |
|
| 272 | - return array( |
|
| 273 | - 'id' => $this->get_id(), |
|
| 274 | - 'texts' => array( |
|
| 275 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
| 276 | - 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
| 277 | - 'item-quantity' => floatval( $this->get_quantity() ), |
|
| 278 | - 'item-price' => wpinv_price( $price, $currency ), |
|
| 279 | - 'item-total' => wpinv_price( $subtotal, $currency ), |
|
| 280 | - ), |
|
| 281 | - 'inputs' => array( |
|
| 282 | - 'item-id' => $this->get_id(), |
|
| 283 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
| 284 | - 'item-description' => wp_kses_post( $this->get_description() ), |
|
| 285 | - 'item-quantity' => floatval( $this->get_quantity() ), |
|
| 286 | - 'item-price' => $price, |
|
| 287 | - ), |
|
| 288 | - ); |
|
| 289 | - |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Prepares form data for saving (cart_details). |
|
| 294 | - * |
|
| 295 | - * @since 1.0.19 |
|
| 296 | - * @return array |
|
| 297 | - */ |
|
| 298 | - public function prepare_data_for_saving() { |
|
| 299 | - |
|
| 300 | - return array( |
|
| 301 | - 'post_id' => $this->invoice_id, |
|
| 302 | - 'item_id' => $this->get_id(), |
|
| 303 | - 'item_name' => sanitize_text_field( $this->get_raw_name( 'edit' ) ), |
|
| 304 | - 'item_description' => $this->get_description( 'edit' ), |
|
| 305 | - 'tax' => $this->item_tax, |
|
| 306 | - 'item_price' => $this->get_price( 'edit' ), |
|
| 307 | - 'quantity' => (float) $this->get_quantity( 'edit' ), |
|
| 308 | - 'discount' => $this->item_discount, |
|
| 309 | - 'subtotal' => $this->get_sub_total( 'edit' ), |
|
| 310 | - 'price' => $this->get_sub_total( 'edit' ) + $this->item_tax - $this->item_discount, |
|
| 311 | - 'meta' => $this->get_item_meta( 'edit' ), |
|
| 312 | - ); |
|
| 313 | - |
|
| 314 | - } |
|
| 91 | + * Get the item name. |
|
| 92 | + * |
|
| 93 | + * @since 1.0.19 |
|
| 94 | + * @param string $context View or edit context. |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function get_name( $context = 'view' ) { |
|
| 98 | + $name = parent::get_name( $context ); |
|
| 99 | + return $name . wpinv_get_item_suffix( $this ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the item name without a suffix. |
|
| 104 | + * |
|
| 105 | + * @since 1.0.19 |
|
| 106 | + * @param string $context View or edit context. |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function get_raw_name( $context = 'view' ) { |
|
| 110 | + return parent::get_name( $context ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the item description. |
|
| 115 | + * |
|
| 116 | + * @since 1.0.19 |
|
| 117 | + * @param string $context View or edit context. |
|
| 118 | + * @return string |
|
| 119 | + */ |
|
| 120 | + public function get_description( $context = 'view' ) { |
|
| 121 | + |
|
| 122 | + if ( isset( $this->custom_description ) ) { |
|
| 123 | + return $this->custom_description; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return parent::get_description( $context ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Returns the sub total. |
|
| 131 | + * |
|
| 132 | + * @since 1.0.19 |
|
| 133 | + * @param string $context View or edit context. |
|
| 134 | + * @return float |
|
| 135 | + */ |
|
| 136 | + public function get_sub_total( $context = 'view' ) { |
|
| 137 | + return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Returns the recurring sub total. |
|
| 142 | + * |
|
| 143 | + * @since 1.0.19 |
|
| 144 | + * @param string $context View or edit context. |
|
| 145 | + * @return float |
|
| 146 | + */ |
|
| 147 | + public function get_recurring_sub_total( $context = 'view' ) { |
|
| 148 | + |
|
| 149 | + if ( $this->is_recurring() ) { |
|
| 150 | + return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return 0; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @deprecated |
|
| 158 | + */ |
|
| 159 | + public function get_qantity( $context = 'view' ) { |
|
| 160 | + return $this->get_quantity( $context ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Get the item quantity. |
|
| 165 | + * |
|
| 166 | + * @since 1.0.19 |
|
| 167 | + * @param string $context View or edit context. |
|
| 168 | + * @return float |
|
| 169 | + */ |
|
| 170 | + public function get_quantity( $context = 'view' ) { |
|
| 171 | + $quantity = (float) $this->quantity; |
|
| 172 | + |
|
| 173 | + if ( 'view' == $context ) { |
|
| 174 | + return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $quantity; |
|
| 178 | + |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the item meta data. |
|
| 183 | + * |
|
| 184 | + * @since 1.0.19 |
|
| 185 | + * @param string $context View or edit context. |
|
| 186 | + * @return meta |
|
| 187 | + */ |
|
| 188 | + public function get_item_meta( $context = 'view' ) { |
|
| 189 | + $meta = $this->meta; |
|
| 190 | + |
|
| 191 | + if ( 'view' == $context ) { |
|
| 192 | + return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $meta; |
|
| 196 | + |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Returns whether or not customers can update the item quantity. |
|
| 201 | + * |
|
| 202 | + * @since 1.0.19 |
|
| 203 | + * @param string $context View or edit context. |
|
| 204 | + * @return bool |
|
| 205 | + */ |
|
| 206 | + public function get_allow_quantities( $context = 'view' ) { |
|
| 207 | + $allow_quantities = (bool) $this->allow_quantities; |
|
| 208 | + |
|
| 209 | + if ( 'view' == $context ) { |
|
| 210 | + return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $allow_quantities; |
|
| 214 | + |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Returns whether or not the item is required. |
|
| 219 | + * |
|
| 220 | + * @since 1.0.19 |
|
| 221 | + * @param string $context View or edit context. |
|
| 222 | + * @return bool |
|
| 223 | + */ |
|
| 224 | + public function get_is_required( $context = 'view' ) { |
|
| 225 | + $is_required = (bool) $this->is_required; |
|
| 226 | + |
|
| 227 | + if ( 'view' == $context ) { |
|
| 228 | + return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return $is_required; |
|
| 232 | + |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Prepares form data for use. |
|
| 237 | + * |
|
| 238 | + * @since 1.0.19 |
|
| 239 | + * @return array |
|
| 240 | + */ |
|
| 241 | + public function prepare_data_for_use( $required = null ) { |
|
| 242 | + |
|
| 243 | + $required = is_null( $required ) ? $this->is_required() : $required; |
|
| 244 | + return array( |
|
| 245 | + 'title' => strip_tags( $this->get_name() ), |
|
| 246 | + 'id' => $this->get_id(), |
|
| 247 | + 'price' => $this->get_price(), |
|
| 248 | + 'recurring' => $this->is_recurring(), |
|
| 249 | + 'description' => $this->get_description(), |
|
| 250 | + 'allow_quantities' => $this->allows_quantities(), |
|
| 251 | + 'required' => $required, |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Prepares form data for ajax use. |
|
| 258 | + * |
|
| 259 | + * @since 1.0.19 |
|
| 260 | + * @return array |
|
| 261 | + */ |
|
| 262 | + public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) { |
|
| 263 | + |
|
| 264 | + $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
| 265 | + |
|
| 266 | + if ( $description ) { |
|
| 267 | + $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $price = ! $is_renewal ? $this->get_price() : $this->get_recurring_price(); |
|
| 271 | + $subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total(); |
|
| 272 | + return array( |
|
| 273 | + 'id' => $this->get_id(), |
|
| 274 | + 'texts' => array( |
|
| 275 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
| 276 | + 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
| 277 | + 'item-quantity' => floatval( $this->get_quantity() ), |
|
| 278 | + 'item-price' => wpinv_price( $price, $currency ), |
|
| 279 | + 'item-total' => wpinv_price( $subtotal, $currency ), |
|
| 280 | + ), |
|
| 281 | + 'inputs' => array( |
|
| 282 | + 'item-id' => $this->get_id(), |
|
| 283 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
| 284 | + 'item-description' => wp_kses_post( $this->get_description() ), |
|
| 285 | + 'item-quantity' => floatval( $this->get_quantity() ), |
|
| 286 | + 'item-price' => $price, |
|
| 287 | + ), |
|
| 288 | + ); |
|
| 289 | + |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Prepares form data for saving (cart_details). |
|
| 294 | + * |
|
| 295 | + * @since 1.0.19 |
|
| 296 | + * @return array |
|
| 297 | + */ |
|
| 298 | + public function prepare_data_for_saving() { |
|
| 299 | + |
|
| 300 | + return array( |
|
| 301 | + 'post_id' => $this->invoice_id, |
|
| 302 | + 'item_id' => $this->get_id(), |
|
| 303 | + 'item_name' => sanitize_text_field( $this->get_raw_name( 'edit' ) ), |
|
| 304 | + 'item_description' => $this->get_description( 'edit' ), |
|
| 305 | + 'tax' => $this->item_tax, |
|
| 306 | + 'item_price' => $this->get_price( 'edit' ), |
|
| 307 | + 'quantity' => (float) $this->get_quantity( 'edit' ), |
|
| 308 | + 'discount' => $this->item_discount, |
|
| 309 | + 'subtotal' => $this->get_sub_total( 'edit' ), |
|
| 310 | + 'price' => $this->get_sub_total( 'edit' ) + $this->item_tax - $this->item_discount, |
|
| 311 | + 'meta' => $this->get_item_meta( 'edit' ), |
|
| 312 | + ); |
|
| 313 | + |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | 316 | /* |
| 317 | 317 | |-------------------------------------------------------------------------- |
@@ -323,70 +323,70 @@ discard block |
||
| 323 | 323 | | object. |
| 324 | 324 | */ |
| 325 | 325 | |
| 326 | - /** |
|
| 327 | - * Set the item qantity. |
|
| 328 | - * |
|
| 329 | - * @since 1.0.19 |
|
| 330 | - * @param float $quantity The item quantity. |
|
| 331 | - */ |
|
| 332 | - public function set_quantity( $quantity ) { |
|
| 333 | - |
|
| 334 | - if ( ! is_numeric( $quantity ) ) { |
|
| 335 | - $quantity = 1; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $this->quantity = (float) $quantity; |
|
| 339 | - |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Set the item meta data. |
|
| 344 | - * |
|
| 345 | - * @since 1.0.19 |
|
| 346 | - * @param array $meta The item meta data. |
|
| 347 | - */ |
|
| 348 | - public function set_item_meta( $meta ) { |
|
| 349 | - $this->meta = maybe_unserialize( $meta ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Set whether or not the quantities are allowed. |
|
| 354 | - * |
|
| 355 | - * @since 1.0.19 |
|
| 356 | - * @param bool $allow_quantities |
|
| 357 | - */ |
|
| 358 | - public function set_allow_quantities( $allow_quantities ) { |
|
| 359 | - $this->allow_quantities = (bool) $allow_quantities; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Set whether or not the item is required. |
|
| 364 | - * |
|
| 365 | - * @since 1.0.19 |
|
| 366 | - * @param bool $is_required |
|
| 367 | - */ |
|
| 368 | - public function set_is_required( $is_required ) { |
|
| 369 | - $this->is_required = (bool) $is_required; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Sets the custom item description. |
|
| 374 | - * |
|
| 375 | - * @since 1.0.19 |
|
| 376 | - * @param string $description |
|
| 377 | - */ |
|
| 378 | - public function set_custom_description( $description ) { |
|
| 379 | - $this->custom_description = $description; |
|
| 380 | - } |
|
| 326 | + /** |
|
| 327 | + * Set the item qantity. |
|
| 328 | + * |
|
| 329 | + * @since 1.0.19 |
|
| 330 | + * @param float $quantity The item quantity. |
|
| 331 | + */ |
|
| 332 | + public function set_quantity( $quantity ) { |
|
| 333 | + |
|
| 334 | + if ( ! is_numeric( $quantity ) ) { |
|
| 335 | + $quantity = 1; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + $this->quantity = (float) $quantity; |
|
| 339 | + |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Set the item meta data. |
|
| 344 | + * |
|
| 345 | + * @since 1.0.19 |
|
| 346 | + * @param array $meta The item meta data. |
|
| 347 | + */ |
|
| 348 | + public function set_item_meta( $meta ) { |
|
| 349 | + $this->meta = maybe_unserialize( $meta ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Set whether or not the quantities are allowed. |
|
| 354 | + * |
|
| 355 | + * @since 1.0.19 |
|
| 356 | + * @param bool $allow_quantities |
|
| 357 | + */ |
|
| 358 | + public function set_allow_quantities( $allow_quantities ) { |
|
| 359 | + $this->allow_quantities = (bool) $allow_quantities; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Set whether or not the item is required. |
|
| 364 | + * |
|
| 365 | + * @since 1.0.19 |
|
| 366 | + * @param bool $is_required |
|
| 367 | + */ |
|
| 368 | + public function set_is_required( $is_required ) { |
|
| 369 | + $this->is_required = (bool) $is_required; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Sets the custom item description. |
|
| 374 | + * |
|
| 375 | + * @since 1.0.19 |
|
| 376 | + * @param string $description |
|
| 377 | + */ |
|
| 378 | + public function set_custom_description( $description ) { |
|
| 379 | + $this->custom_description = $description; |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | 382 | /** |
| 383 | 383 | * We do not want to save items to the database. |
| 384 | 384 | * |
| 385 | - * @return int item id |
|
| 385 | + * @return int item id |
|
| 386 | 386 | */ |
| 387 | 387 | public function save( $data = array() ) { |
| 388 | 388 | return $this->get_id(); |
| 389 | - } |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | 391 | /* |
| 392 | 392 | |-------------------------------------------------------------------------- |
@@ -398,23 +398,23 @@ discard block |
||
| 398 | 398 | */ |
| 399 | 399 | |
| 400 | 400 | /** |
| 401 | - * Checks whether the item has enabled dynamic pricing. |
|
| 402 | - * |
|
| 403 | - * @since 1.0.19 |
|
| 404 | - * @return bool |
|
| 405 | - */ |
|
| 406 | - public function is_required() { |
|
| 401 | + * Checks whether the item has enabled dynamic pricing. |
|
| 402 | + * |
|
| 403 | + * @since 1.0.19 |
|
| 404 | + * @return bool |
|
| 405 | + */ |
|
| 406 | + public function is_required() { |
|
| 407 | 407 | return (bool) $this->get_is_required(); |
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Checks whether users can edit the quantities. |
|
| 412 | - * |
|
| 413 | - * @since 1.0.19 |
|
| 414 | - * @return bool |
|
| 415 | - */ |
|
| 416 | - public function allows_quantities() { |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Checks whether users can edit the quantities. |
|
| 412 | + * |
|
| 413 | + * @since 1.0.19 |
|
| 414 | + * @return bool |
|
| 415 | + */ |
|
| 416 | + public function allows_quantities() { |
|
| 417 | 417 | return (bool) $this->get_allow_quantities(); |
| 418 | - } |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | 420 | } |
@@ -12,167 +12,167 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Discount { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Submission discounts. |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $discounts = array(); |
|
| 15 | + /** |
|
| 16 | + * Submission discounts. |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $discounts = array(); |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Class constructor |
|
| 23 | + * |
|
| 24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | + * @param float $initial_total |
|
| 26 | + * @param float $recurring_total |
|
| 27 | + */ |
|
| 28 | + public function __construct( $submission, $initial_total, $recurring_total ) { |
|
| 29 | + |
|
| 30 | + // Process any existing invoice discounts. |
|
| 31 | + if ( $submission->has_invoice() ) { |
|
| 32 | + $this->discounts = $submission->get_invoice()->get_discounts(); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + // Do we have a discount? |
|
| 36 | + $discount = $submission->get_field( 'discount' ); |
|
| 37 | + |
|
| 38 | + if ( empty( $discount ) ) { |
|
| 39 | + |
|
| 40 | + if ( isset( $this->discounts['discount_code'] ) ) { |
|
| 41 | + unset( $this->discounts['discount_code'] ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + return; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + // Processes the discount code. |
|
| 48 | + $amount = max( $initial_total, $recurring_total ); |
|
| 49 | + $this->process_discount( $submission, $discount, $amount ); |
|
| 50 | + |
|
| 51 | + } |
|
| 20 | 52 | |
| 21 | 53 | /** |
| 22 | - * Class constructor |
|
| 23 | - * |
|
| 24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | - * @param float $initial_total |
|
| 26 | - * @param float $recurring_total |
|
| 27 | - */ |
|
| 28 | - public function __construct( $submission, $initial_total, $recurring_total ) { |
|
| 29 | - |
|
| 30 | - // Process any existing invoice discounts. |
|
| 31 | - if ( $submission->has_invoice() ) { |
|
| 32 | - $this->discounts = $submission->get_invoice()->get_discounts(); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - // Do we have a discount? |
|
| 36 | - $discount = $submission->get_field( 'discount' ); |
|
| 37 | - |
|
| 38 | - if ( empty( $discount ) ) { |
|
| 39 | - |
|
| 40 | - if ( isset( $this->discounts['discount_code'] ) ) { |
|
| 41 | - unset( $this->discounts['discount_code'] ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - return; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - // Processes the discount code. |
|
| 48 | - $amount = max( $initial_total, $recurring_total ); |
|
| 49 | - $this->process_discount( $submission, $discount, $amount ); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Processes a submission discount. |
|
| 55 | - * |
|
| 56 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 57 | - * @param string $discount |
|
| 58 | - * @param float $amount |
|
| 59 | - */ |
|
| 60 | - public function process_discount( $submission, $discount, $amount ) { |
|
| 61 | - |
|
| 62 | - // Fetch the discount. |
|
| 63 | - $discount = new WPInv_Discount( $discount ); |
|
| 64 | - |
|
| 65 | - // Ensure it is active. |
|
| 54 | + * Processes a submission discount. |
|
| 55 | + * |
|
| 56 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 57 | + * @param string $discount |
|
| 58 | + * @param float $amount |
|
| 59 | + */ |
|
| 60 | + public function process_discount( $submission, $discount, $amount ) { |
|
| 61 | + |
|
| 62 | + // Fetch the discount. |
|
| 63 | + $discount = new WPInv_Discount( $discount ); |
|
| 64 | + |
|
| 65 | + // Ensure it is active. |
|
| 66 | 66 | if ( ! $this->is_discount_active( $discount ) ) { |
| 67 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // Required items. |
|
| 71 | - if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) { |
|
| 72 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Exceeded limit. |
|
| 76 | - if ( $discount->has_exceeded_limit() ) { |
|
| 77 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // Validate usages. |
|
| 81 | - $this->validate_single_use_discount( $submission, $discount ); |
|
| 82 | - |
|
| 83 | - // Validate amount. |
|
| 84 | - $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 85 | - |
|
| 86 | - // Save the discount. |
|
| 87 | - $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Validates a single use discount. |
|
| 92 | - * |
|
| 93 | - * @param WPInv_Discount $discount |
|
| 94 | - * @return bool |
|
| 95 | - */ |
|
| 96 | - public function is_discount_active( $discount ) { |
|
| 97 | - return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Returns a user's id or email. |
|
| 102 | - * |
|
| 103 | - * @param string $email |
|
| 104 | - * @return int|string|false |
|
| 105 | - */ |
|
| 106 | - public function get_user_id_or_email( $email ) { |
|
| 107 | - |
|
| 108 | - if ( is_user_logged_in() ) { |
|
| 109 | - return get_current_user_id(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return empty( $email ) ? false : sanitize_email( $email ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Validates a single use discount. |
|
| 117 | - * |
|
| 118 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 119 | - * @param WPInv_Discount $discount |
|
| 120 | - */ |
|
| 121 | - public function validate_single_use_discount( $submission, $discount ) { |
|
| 122 | - |
|
| 123 | - // Abort if it is not a single use discount. |
|
| 124 | - if ( ! $discount->is_single_use() ) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Ensure there is a valid billing email. |
|
| 129 | - $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 130 | - |
|
| 131 | - if ( empty( $user ) ) { |
|
| 132 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Has the user used this discount code before? |
|
| 136 | - if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 137 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Validates the discount's amount. |
|
| 144 | - * |
|
| 145 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 146 | - * @param WPInv_Discount $discount |
|
| 147 | - * @param float $amount |
|
| 148 | - */ |
|
| 149 | - public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 150 | - |
|
| 151 | - // Validate minimum amount. |
|
| 152 | - if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 153 | - $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 154 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Validate the maximum amount. |
|
| 158 | - if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 159 | - $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 160 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Calculates the discount code's amount. |
|
| 167 | - * |
|
| 168 | - * Ensure that the discount exists and has been validated before calling this method. |
|
| 169 | - * |
|
| 170 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 171 | - * @param WPInv_Discount $discount |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - public function calculate_discount( $submission, $discount ) { |
|
| 175 | - return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
| 176 | - } |
|
| 67 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // Required items. |
|
| 71 | + if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) { |
|
| 72 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Exceeded limit. |
|
| 76 | + if ( $discount->has_exceeded_limit() ) { |
|
| 77 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // Validate usages. |
|
| 81 | + $this->validate_single_use_discount( $submission, $discount ); |
|
| 82 | + |
|
| 83 | + // Validate amount. |
|
| 84 | + $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 85 | + |
|
| 86 | + // Save the discount. |
|
| 87 | + $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Validates a single use discount. |
|
| 92 | + * |
|
| 93 | + * @param WPInv_Discount $discount |
|
| 94 | + * @return bool |
|
| 95 | + */ |
|
| 96 | + public function is_discount_active( $discount ) { |
|
| 97 | + return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Returns a user's id or email. |
|
| 102 | + * |
|
| 103 | + * @param string $email |
|
| 104 | + * @return int|string|false |
|
| 105 | + */ |
|
| 106 | + public function get_user_id_or_email( $email ) { |
|
| 107 | + |
|
| 108 | + if ( is_user_logged_in() ) { |
|
| 109 | + return get_current_user_id(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return empty( $email ) ? false : sanitize_email( $email ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Validates a single use discount. |
|
| 117 | + * |
|
| 118 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 119 | + * @param WPInv_Discount $discount |
|
| 120 | + */ |
|
| 121 | + public function validate_single_use_discount( $submission, $discount ) { |
|
| 122 | + |
|
| 123 | + // Abort if it is not a single use discount. |
|
| 124 | + if ( ! $discount->is_single_use() ) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Ensure there is a valid billing email. |
|
| 129 | + $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 130 | + |
|
| 131 | + if ( empty( $user ) ) { |
|
| 132 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Has the user used this discount code before? |
|
| 136 | + if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 137 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Validates the discount's amount. |
|
| 144 | + * |
|
| 145 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 146 | + * @param WPInv_Discount $discount |
|
| 147 | + * @param float $amount |
|
| 148 | + */ |
|
| 149 | + public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 150 | + |
|
| 151 | + // Validate minimum amount. |
|
| 152 | + if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 153 | + $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 154 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Validate the maximum amount. |
|
| 158 | + if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 159 | + $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 160 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Calculates the discount code's amount. |
|
| 167 | + * |
|
| 168 | + * Ensure that the discount exists and has been validated before calling this method. |
|
| 169 | + * |
|
| 170 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 171 | + * @param WPInv_Discount $discount |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + public function calculate_discount( $submission, $discount ) { |
|
| 175 | + return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | 178 | } |