@@ -13,36 +13,36 @@ discard block |
||
| 13 | 13 | class GetPaid_Notification_Email { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Contains the type of this notification email. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Contains the type of this notification email. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Contains any object to use in filters. |
|
| 24 | - * |
|
| 25 | - * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
| 26 | - */ |
|
| 23 | + * Contains any object to use in filters. |
|
| 24 | + * |
|
| 25 | + * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
| 26 | + */ |
|
| 27 | 27 | public $object; |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Class constructor. |
|
| 31 | - * |
|
| 30 | + * Class constructor. |
|
| 31 | + * |
|
| 32 | 32 | * @param string $id Email Type. |
| 33 | 33 | * @param mixed $object Optional. Associated object. |
| 34 | - */ |
|
| 35 | - public function __construct( $id, $object = false ) { |
|
| 34 | + */ |
|
| 35 | + public function __construct( $id, $object = false ) { |
|
| 36 | 36 | $this->id = $id; |
| 37 | 37 | $this->object = $object; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | - * Retrieves an option |
|
| 42 | - * |
|
| 41 | + * Retrieves an option |
|
| 42 | + * |
|
| 43 | 43 | * @return mixed |
| 44 | - */ |
|
| 45 | - public function get_option( $key ) { |
|
| 44 | + */ |
|
| 45 | + public function get_option( $key ) { |
|
| 46 | 46 | |
| 47 | 47 | $key = "email_{$this->id}_$key"; |
| 48 | 48 | $value = wpinv_get_option( $key, null ); |
@@ -60,80 +60,80 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | - * Retrieves the email body. |
|
| 64 | - * |
|
| 63 | + * Retrieves the email body. |
|
| 64 | + * |
|
| 65 | 65 | * @return string |
| 66 | - */ |
|
| 67 | - public function get_body() { |
|
| 66 | + */ |
|
| 67 | + public function get_body() { |
|
| 68 | 68 | $body = $this->get_option( 'body' ); |
| 69 | 69 | return apply_filters( 'getpaid_get_email_body', $body, $this->id, $this->object ); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | - * Retrieves the email subject. |
|
| 74 | - * |
|
| 73 | + * Retrieves the email subject. |
|
| 74 | + * |
|
| 75 | 75 | * @return string |
| 76 | - */ |
|
| 77 | - public function get_subject() { |
|
| 76 | + */ |
|
| 77 | + public function get_subject() { |
|
| 78 | 78 | $subject = $this->get_option( 'subject' ); |
| 79 | 79 | return apply_filters( 'getpaid_get_email_subject', $subject, $this->id, $this->object ); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | - * Retrieves the email heading. |
|
| 84 | - * |
|
| 83 | + * Retrieves the email heading. |
|
| 84 | + * |
|
| 85 | 85 | * @return string |
| 86 | - */ |
|
| 87 | - public function get_heading() { |
|
| 86 | + */ |
|
| 87 | + public function get_heading() { |
|
| 88 | 88 | $heading = $this->get_option( 'heading' ); |
| 89 | 89 | return apply_filters( 'getpaid_get_email_heading', $heading, $this->id, $this->object ); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | - * Checks if an email is active. |
|
| 94 | - * |
|
| 93 | + * Checks if an email is active. |
|
| 94 | + * |
|
| 95 | 95 | * @return bool |
| 96 | - */ |
|
| 97 | - public function is_active() { |
|
| 96 | + */ |
|
| 97 | + public function is_active() { |
|
| 98 | 98 | $is_active = ! empty( $this->get_option( 'active' ) ); |
| 99 | 99 | return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object ); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | - * Checks if the site's admin should receive email notifications. |
|
| 104 | - * |
|
| 103 | + * Checks if the site's admin should receive email notifications. |
|
| 104 | + * |
|
| 105 | 105 | * @return bool |
| 106 | - */ |
|
| 107 | - public function include_admin_bcc() { |
|
| 106 | + */ |
|
| 107 | + public function include_admin_bcc() { |
|
| 108 | 108 | $include_admin_bcc = ! empty( $this->get_option( 'admin_bcc' ) ); |
| 109 | 109 | return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object ); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | - * Checks whether this email should be sent to the customer or admin. |
|
| 114 | - * |
|
| 113 | + * Checks whether this email should be sent to the customer or admin. |
|
| 114 | + * |
|
| 115 | 115 | * @return bool |
| 116 | - */ |
|
| 117 | - public function is_admin_email() { |
|
| 116 | + */ |
|
| 117 | + public function is_admin_email() { |
|
| 118 | 118 | $is_admin_email = in_array( $this->id, array( 'new_invoice', 'cancelled_invoice', 'failed_invoice' ) ); |
| 119 | 119 | return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object ); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | - * Returns email attachments. |
|
| 124 | - * |
|
| 123 | + * Returns email attachments. |
|
| 124 | + * |
|
| 125 | 125 | * @return array |
| 126 | - */ |
|
| 127 | - public function get_attachments() { |
|
| 126 | + */ |
|
| 127 | + public function get_attachments() { |
|
| 128 | 128 | return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object ); |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | - * Returns an array of merge tags. |
|
| 133 | - * |
|
| 132 | + * Returns an array of merge tags. |
|
| 133 | + * |
|
| 134 | 134 | * @return array |
| 135 | - */ |
|
| 136 | - public function get_merge_tags() { |
|
| 135 | + */ |
|
| 136 | + public function get_merge_tags() { |
|
| 137 | 137 | |
| 138 | 138 | $merge_tags = array( |
| 139 | 139 | '{site_title}' => wpinv_get_blogname(), |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | - * Adds merge tags to a text. |
|
| 148 | - * |
|
| 147 | + * Adds merge tags to a text. |
|
| 148 | + * |
|
| 149 | 149 | * @param string string $text |
| 150 | 150 | * @param array $merge_tags |
| 151 | 151 | * @return string |
| 152 | - */ |
|
| 153 | - public function add_merge_tags( $text, $merge_tags = array() ) { |
|
| 152 | + */ |
|
| 153 | + public function add_merge_tags( $text, $merge_tags = array() ) { |
|
| 154 | 154 | |
| 155 | 155 | foreach ( $merge_tags as $key => $value ) { |
| 156 | 156 | $text = str_replace( $key, $value, $text ); |
@@ -160,13 +160,13 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
| 163 | - * Returns the email content |
|
| 164 | - * |
|
| 163 | + * Returns the email content |
|
| 164 | + * |
|
| 165 | 165 | * @param array $merge_tags |
| 166 | 166 | * @param array $extra_args Extra template args |
| 167 | 167 | * @return string |
| 168 | - */ |
|
| 169 | - public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
| 168 | + */ |
|
| 169 | + public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
| 170 | 170 | |
| 171 | 171 | $content = wpinv_get_template_html( |
| 172 | 172 | "emails/wpinv-email-{$this->id}.php", |
@@ -15,31 +15,31 @@ discard block |
||
| 15 | 15 | abstract class GetPaid_Authorize_Net_Legacy_Gateway extends GetPaid_Payment_Gateway { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Class constructor. |
|
| 19 | - */ |
|
| 20 | - public function __construct() { |
|
| 18 | + * Class constructor. |
|
| 19 | + */ |
|
| 20 | + public function __construct() { |
|
| 21 | 21 | parent::__construct(); |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | - * Returns the API URL. |
|
| 26 | - * |
|
| 27 | - * |
|
| 28 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 29 | - * @return string |
|
| 30 | - */ |
|
| 31 | - public function get_api_url( $invoice ) { |
|
| 25 | + * Returns the API URL. |
|
| 26 | + * |
|
| 27 | + * |
|
| 28 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 29 | + * @return string |
|
| 30 | + */ |
|
| 31 | + public function get_api_url( $invoice ) { |
|
| 32 | 32 | return $this->is_sandbox( $invoice ) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * Communicates with authorize.net |
|
| 37 | - * |
|
| 38 | - * |
|
| 39 | - * @param array $post Data to post. |
|
| 36 | + * Communicates with authorize.net |
|
| 37 | + * |
|
| 38 | + * |
|
| 39 | + * @param array $post Data to post. |
|
| 40 | 40 | * @param WPInv_Invoice $invoice Invoice. |
| 41 | - * @return stdClass|WP_Error |
|
| 42 | - */ |
|
| 41 | + * @return stdClass|WP_Error |
|
| 42 | + */ |
|
| 43 | 43 | public function post( $post, $invoice ){ |
| 44 | 44 | |
| 45 | 45 | $url = $this->get_api_url( $invoice ); |
@@ -81,12 +81,12 @@ discard block |
||
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
| 84 | - * Returns the API authentication params. |
|
| 85 | - * |
|
| 86 | - * |
|
| 87 | - * @return array |
|
| 88 | - */ |
|
| 89 | - public function get_auth_params() { |
|
| 84 | + * Returns the API authentication params. |
|
| 85 | + * |
|
| 86 | + * |
|
| 87 | + * @return array |
|
| 88 | + */ |
|
| 89 | + public function get_auth_params() { |
|
| 90 | 90 | |
| 91 | 91 | return array( |
| 92 | 92 | 'name' => $this->get_option( 'login_id' ), |
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | - * Cancels a subscription remotely |
|
| 100 | - * |
|
| 101 | - * |
|
| 102 | - * @param WPInv_Subscription $subscription Subscription. |
|
| 99 | + * Cancels a subscription remotely |
|
| 100 | + * |
|
| 101 | + * |
|
| 102 | + * @param WPInv_Subscription $subscription Subscription. |
|
| 103 | 103 | * @param WPInv_Invoice $invoice Invoice. |
| 104 | - */ |
|
| 105 | - public function cancel_subscription( $subscription, $invoice ) { |
|
| 104 | + */ |
|
| 105 | + public function cancel_subscription( $subscription, $invoice ) { |
|
| 106 | 106 | |
| 107 | 107 | // Backwards compatibility. New version do not use authorize.net subscriptions. |
| 108 | 108 | $this->post( |
@@ -118,17 +118,17 @@ discard block |
||
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | - * Processes ipns. |
|
| 122 | - * |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function verify_ipn() { |
|
| 121 | + * Processes ipns. |
|
| 122 | + * |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function verify_ipn() { |
|
| 126 | 126 | |
| 127 | 127 | $this->maybe_process_old_ipn(); |
| 128 | 128 | |
| 129 | 129 | // Validate the IPN. |
| 130 | 130 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
| 131 | - wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) ); |
|
| 131 | + wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) ); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | // Event type. |
@@ -167,24 +167,24 @@ discard block |
||
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
| 170 | - * Validates IPN invoices. |
|
| 171 | - * |
|
| 170 | + * Validates IPN invoices. |
|
| 171 | + * |
|
| 172 | 172 | * @param WPInv_Invoice $invoice |
| 173 | 173 | * @param object $payload |
| 174 | - * @return void |
|
| 175 | - */ |
|
| 176 | - public function validate_ipn_invoice( $invoice, $payload ) { |
|
| 174 | + * @return void |
|
| 175 | + */ |
|
| 176 | + public function validate_ipn_invoice( $invoice, $payload ) { |
|
| 177 | 177 | if ( ! $invoice->exists() || $payload->id != $invoice->get_transaction_id() ) { |
| 178 | 178 | exit; |
| 179 | 179 | } |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
| 183 | - * Process subscriptio IPNS. |
|
| 184 | - * |
|
| 185 | - * @return void |
|
| 186 | - */ |
|
| 187 | - public function maybe_process_old_ipn() { |
|
| 183 | + * Process subscriptio IPNS. |
|
| 184 | + * |
|
| 185 | + * @return void |
|
| 186 | + */ |
|
| 187 | + public function maybe_process_old_ipn() { |
|
| 188 | 188 | |
| 189 | 189 | $data = wp_unslash( $_POST ); |
| 190 | 190 | |
@@ -226,11 +226,11 @@ discard block |
||
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | /** |
| 229 | - * Validates the old IPN signature. |
|
| 229 | + * Validates the old IPN signature. |
|
| 230 | 230 | * |
| 231 | 231 | * @param array $posted |
| 232 | - */ |
|
| 233 | - public function validate_old_ipn_signature( $posted ) { |
|
| 232 | + */ |
|
| 233 | + public function validate_old_ipn_signature( $posted ) { |
|
| 234 | 234 | |
| 235 | 235 | $signature = $this->get_option( 'signature_key' ); |
| 236 | 236 | if ( ! empty( $signature ) ) { |
@@ -249,9 +249,9 @@ discard block |
||
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | /** |
| 252 | - * Check Authorize.NET IPN validity. |
|
| 253 | - */ |
|
| 254 | - public function validate_ipn() { |
|
| 252 | + * Check Authorize.NET IPN validity. |
|
| 253 | + */ |
|
| 254 | + public function validate_ipn() { |
|
| 255 | 255 | |
| 256 | 256 | wpinv_error_log( 'Validating Authorize.NET IPN response' ); |
| 257 | 257 | |
@@ -110,8 +110,8 @@ discard block |
||
| 110 | 110 | $css = getpaid_get_email_css(); |
| 111 | 111 | |
| 112 | 112 | // include css inliner |
| 113 | - if ( ! class_exists( 'Emogrifier' ) ) { |
|
| 114 | - include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' ); |
|
| 113 | + if ( ! class_exists( 'Emogrifier' ) ) { |
|
| 114 | + include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' ); |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Inline the css. |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | $message = wpinv_email_style_body( $message ); |
| 191 | 191 | $to = array_merge( wpinv_parse_list( $to ), wpinv_parse_list( $cc ) ); |
| 192 | 192 | |
| 193 | - return $mailer->send( |
|
| 193 | + return $mailer->send( |
|
| 194 | 194 | $to, |
| 195 | 195 | $subject, |
| 196 | 196 | $message, |
@@ -117,14 +117,14 @@ |
||
| 117 | 117 | */ |
| 118 | 118 | function getpaid_doing_it_wrong( $function, $message, $version ) { |
| 119 | 119 | |
| 120 | - $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
|
| 121 | - |
|
| 122 | - if ( wp_doing_ajax() || defined( 'REST_REQUEST' ) ) { |
|
| 123 | - do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
| 124 | - error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
| 125 | - } else { |
|
| 126 | - _doing_it_wrong( $function, $message, $version ); |
|
| 127 | - } |
|
| 120 | + $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
|
| 121 | + |
|
| 122 | + if ( wp_doing_ajax() || defined( 'REST_REQUEST' ) ) { |
|
| 123 | + do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
| 124 | + error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
| 125 | + } else { |
|
| 126 | + _doing_it_wrong( $function, $message, $version ); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -13,128 +13,128 @@ |
||
| 13 | 13 | class GetPaid_Payment_Forms { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Class constructor |
|
| 17 | - * |
|
| 18 | - */ |
|
| 19 | - public function __construct() { |
|
| 20 | - |
|
| 21 | - // Update a payment form's revenue whenever an invoice is paid for or refunded. |
|
| 22 | - add_action( 'getpaid_invoice_payment_status_changed', array( $this, 'increment_form_revenue' ) ); |
|
| 23 | - add_action( 'getpaid_invoice_payment_status_reversed', array( $this, 'decrease_form_revenue' ) ); |
|
| 24 | - |
|
| 25 | - // Sync form amount whenever invoice statuses change. |
|
| 26 | - add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_failed_amount' ), 10, 3 ); |
|
| 27 | - add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_refunded_amount' ), 10, 3 ); |
|
| 28 | - add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_cancelled_amount' ), 10, 3 ); |
|
| 29 | - |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Increments a form's revenue whenever there is a payment. |
|
| 34 | - * |
|
| 35 | - * @param WPInv_Invoice $invoice |
|
| 36 | - */ |
|
| 37 | - public function increment_form_revenue( $invoice ) { |
|
| 38 | - |
|
| 39 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 40 | - if ( $form->get_id() ) { |
|
| 41 | - $form->set_earned( $form->get_earned() + $invoice->get_total() ); |
|
| 42 | - $form->save(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Decreases form revenue whenever invoice payment changes. |
|
| 49 | - * |
|
| 50 | - * @param WPInv_Invoice $invoice |
|
| 51 | - */ |
|
| 52 | - public function decrease_form_revenue( $invoice ) { |
|
| 53 | - |
|
| 54 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 55 | - if ( $form->get_id() ) { |
|
| 56 | - $form->set_earned( $form->get_earned() - $invoice->get_total() ); |
|
| 57 | - $form->save(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Updates a form's failed amount. |
|
| 64 | - * |
|
| 65 | - * @param WPInv_Invoice $invoice |
|
| 66 | - * @param string $from |
|
| 67 | - * @param string $to |
|
| 68 | - */ |
|
| 69 | - public function update_form_failed_amount( $invoice, $from, $to ) { |
|
| 70 | - |
|
| 71 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 72 | - if ( $form->get_id() ) { |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ( 'wpi-failed' == $from ) { |
|
| 77 | - $form->set_failed( $form->get_failed() - $invoice->get_total() ); |
|
| 78 | - $form->save(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ( 'wpi-failed' == $to ) { |
|
| 82 | - $form->set_failed( $form->get_failed() + $invoice->get_total() ); |
|
| 83 | - $form->save(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Updates a form's refunded amount. |
|
| 90 | - * |
|
| 91 | - * @param WPInv_Invoice $invoice |
|
| 92 | - * @param string $from |
|
| 93 | - * @param string $to |
|
| 94 | - */ |
|
| 95 | - public function update_form_refunded_amount( $invoice, $from, $to ) { |
|
| 96 | - |
|
| 97 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 98 | - if ( $form->get_id() ) { |
|
| 99 | - return; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if ( 'wpi-refunded' == $from ) { |
|
| 103 | - $form->set_refunded( $form->get_refunded() - $invoice->get_total() ); |
|
| 104 | - $form->save(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ( 'wpi-refunded' == $to ) { |
|
| 108 | - $form->set_refunded( $form->get_refunded() + $invoice->get_total() ); |
|
| 109 | - $form->save(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Updates a form's cancelled amount. |
|
| 116 | - * |
|
| 117 | - * @param WPInv_Invoice $invoice |
|
| 118 | - * @param string $from |
|
| 119 | - * @param string $to |
|
| 120 | - */ |
|
| 121 | - public function update_form_cancelled_amount( $invoice, $from, $to ) { |
|
| 122 | - |
|
| 123 | - $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 124 | - if ( $form->get_id() ) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if ( 'wpi-cancelled' == $from ) { |
|
| 129 | - $form->set_cancelled( $form->get_cancelled() - $invoice->get_total() ); |
|
| 130 | - $form->save(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if ( 'wpi-cancelled' == $to ) { |
|
| 134 | - $form->set_cancelled( $form->get_cancelled() + $invoice->get_total() ); |
|
| 135 | - $form->save(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - } |
|
| 16 | + * Class constructor |
|
| 17 | + * |
|
| 18 | + */ |
|
| 19 | + public function __construct() { |
|
| 20 | + |
|
| 21 | + // Update a payment form's revenue whenever an invoice is paid for or refunded. |
|
| 22 | + add_action( 'getpaid_invoice_payment_status_changed', array( $this, 'increment_form_revenue' ) ); |
|
| 23 | + add_action( 'getpaid_invoice_payment_status_reversed', array( $this, 'decrease_form_revenue' ) ); |
|
| 24 | + |
|
| 25 | + // Sync form amount whenever invoice statuses change. |
|
| 26 | + add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_failed_amount' ), 10, 3 ); |
|
| 27 | + add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_refunded_amount' ), 10, 3 ); |
|
| 28 | + add_action( 'getpaid_invoice_status_changed', array( $this, 'update_form_cancelled_amount' ), 10, 3 ); |
|
| 29 | + |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Increments a form's revenue whenever there is a payment. |
|
| 34 | + * |
|
| 35 | + * @param WPInv_Invoice $invoice |
|
| 36 | + */ |
|
| 37 | + public function increment_form_revenue( $invoice ) { |
|
| 38 | + |
|
| 39 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 40 | + if ( $form->get_id() ) { |
|
| 41 | + $form->set_earned( $form->get_earned() + $invoice->get_total() ); |
|
| 42 | + $form->save(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Decreases form revenue whenever invoice payment changes. |
|
| 49 | + * |
|
| 50 | + * @param WPInv_Invoice $invoice |
|
| 51 | + */ |
|
| 52 | + public function decrease_form_revenue( $invoice ) { |
|
| 53 | + |
|
| 54 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 55 | + if ( $form->get_id() ) { |
|
| 56 | + $form->set_earned( $form->get_earned() - $invoice->get_total() ); |
|
| 57 | + $form->save(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Updates a form's failed amount. |
|
| 64 | + * |
|
| 65 | + * @param WPInv_Invoice $invoice |
|
| 66 | + * @param string $from |
|
| 67 | + * @param string $to |
|
| 68 | + */ |
|
| 69 | + public function update_form_failed_amount( $invoice, $from, $to ) { |
|
| 70 | + |
|
| 71 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 72 | + if ( $form->get_id() ) { |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ( 'wpi-failed' == $from ) { |
|
| 77 | + $form->set_failed( $form->get_failed() - $invoice->get_total() ); |
|
| 78 | + $form->save(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ( 'wpi-failed' == $to ) { |
|
| 82 | + $form->set_failed( $form->get_failed() + $invoice->get_total() ); |
|
| 83 | + $form->save(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Updates a form's refunded amount. |
|
| 90 | + * |
|
| 91 | + * @param WPInv_Invoice $invoice |
|
| 92 | + * @param string $from |
|
| 93 | + * @param string $to |
|
| 94 | + */ |
|
| 95 | + public function update_form_refunded_amount( $invoice, $from, $to ) { |
|
| 96 | + |
|
| 97 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 98 | + if ( $form->get_id() ) { |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if ( 'wpi-refunded' == $from ) { |
|
| 103 | + $form->set_refunded( $form->get_refunded() - $invoice->get_total() ); |
|
| 104 | + $form->save(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ( 'wpi-refunded' == $to ) { |
|
| 108 | + $form->set_refunded( $form->get_refunded() + $invoice->get_total() ); |
|
| 109 | + $form->save(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Updates a form's cancelled amount. |
|
| 116 | + * |
|
| 117 | + * @param WPInv_Invoice $invoice |
|
| 118 | + * @param string $from |
|
| 119 | + * @param string $to |
|
| 120 | + */ |
|
| 121 | + public function update_form_cancelled_amount( $invoice, $from, $to ) { |
|
| 122 | + |
|
| 123 | + $form = new GetPaid_Payment_Form( $invoice->get_payment_form() ); |
|
| 124 | + if ( $form->get_id() ) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if ( 'wpi-cancelled' == $from ) { |
|
| 129 | + $form->set_cancelled( $form->get_cancelled() - $invoice->get_total() ); |
|
| 130 | + $form->save(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if ( 'wpi-cancelled' == $to ) { |
|
| 134 | + $form->set_cancelled( $form->get_cancelled() + $invoice->get_total() ); |
|
| 135 | + $form->save(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | } |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | - exit; // Exit if accessed directly |
|
| 10 | + exit; // Exit if accessed directly |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | /** |
@@ -16,10 +16,10 @@ discard block |
||
| 16 | 16 | class GetPaid_Meta_Box_Payment_Form { |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | - * Output the metabox. |
|
| 20 | - * |
|
| 21 | - * @param WP_Post $post |
|
| 22 | - */ |
|
| 19 | + * Output the metabox. |
|
| 20 | + * |
|
| 21 | + * @param WP_Post $post |
|
| 22 | + */ |
|
| 23 | 23 | public static function output( $post ) { |
| 24 | 24 | ?> |
| 25 | 25 | <div id="wpinv-form-builder" class="bsui"> |
@@ -86,11 +86,11 @@ discard block |
||
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | - * Save meta box data. |
|
| 90 | - * |
|
| 91 | - * @param int $post_id |
|
| 92 | - */ |
|
| 93 | - public static function save( $post_id ) { |
|
| 89 | + * Save meta box data. |
|
| 90 | + * |
|
| 91 | + * @param int $post_id |
|
| 92 | + */ |
|
| 93 | + public static function save( $post_id ) { |
|
| 94 | 94 | |
| 95 | 95 | // Prepare the form. |
| 96 | 96 | $form = new GetPaid_Payment_Form( $post_id ); |
@@ -121,11 +121,11 @@ discard block |
||
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
| 124 | - * Converts an array fo form items to objects. |
|
| 125 | - * |
|
| 126 | - * @param array $items |
|
| 127 | - */ |
|
| 128 | - public static function item_to_objects( $items ) { |
|
| 124 | + * Converts an array fo form items to objects. |
|
| 125 | + * |
|
| 126 | + * @param array $items |
|
| 127 | + */ |
|
| 128 | + public static function item_to_objects( $items ) { |
|
| 129 | 129 | |
| 130 | 130 | $objects = array(); |
| 131 | 131 | |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | - exit; // Exit if accessed directly |
|
| 11 | + exit; // Exit if accessed directly |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
@@ -17,10 +17,10 @@ discard block |
||
| 17 | 17 | class GetPaid_Meta_Box_Discount_Details { |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Output the metabox. |
|
| 21 | - * |
|
| 22 | - * @param WP_Post $post |
|
| 23 | - */ |
|
| 20 | + * Output the metabox. |
|
| 21 | + * |
|
| 22 | + * @param WP_Post $post |
|
| 23 | + */ |
|
| 24 | 24 | public static function output( $post ) { |
| 25 | 25 | |
| 26 | 26 | // Prepare the discount. |
@@ -368,34 +368,34 @@ discard block |
||
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
| 371 | - * Save meta box data. |
|
| 372 | - * |
|
| 373 | - * @param int $post_id |
|
| 374 | - */ |
|
| 375 | - public static function save( $post_id ) { |
|
| 371 | + * Save meta box data. |
|
| 372 | + * |
|
| 373 | + * @param int $post_id |
|
| 374 | + */ |
|
| 375 | + public static function save( $post_id ) { |
|
| 376 | 376 | |
| 377 | 377 | // Prepare the discount. |
| 378 | 378 | $discount = new WPInv_Discount( $post_id ); |
| 379 | 379 | |
| 380 | 380 | // Load new data. |
| 381 | 381 | $discount->set_props( |
| 382 | - array( |
|
| 383 | - 'code' => isset( $_POST['wpinv_discount_code'] ) ? $_POST['wpinv_discount_code'] : null, |
|
| 384 | - 'amount' => isset( $_POST['wpinv_discount_amount'] ) ? $_POST['wpinv_discount_amount'] : null, |
|
| 385 | - 'start' => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null, |
|
| 386 | - 'expiration' => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null, |
|
| 387 | - 'is_single_use' => isset( $_POST['wpinv_discount_single_use'] ), |
|
| 382 | + array( |
|
| 383 | + 'code' => isset( $_POST['wpinv_discount_code'] ) ? $_POST['wpinv_discount_code'] : null, |
|
| 384 | + 'amount' => isset( $_POST['wpinv_discount_amount'] ) ? $_POST['wpinv_discount_amount'] : null, |
|
| 385 | + 'start' => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null, |
|
| 386 | + 'expiration' => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null, |
|
| 387 | + 'is_single_use' => isset( $_POST['wpinv_discount_single_use'] ), |
|
| 388 | 388 | 'type' => isset( $_POST['wpinv_discount_type'] ) ? $_POST['wpinv_discount_type'] : null, |
| 389 | - 'is_recurring' => isset( $_POST['wpinv_discount_recurring'] ), |
|
| 390 | - 'items' => isset( $_POST['wpinv_discount_items'] ) ? $_POST['wpinv_discount_items'] : array(), |
|
| 391 | - 'excluded_items' => isset( $_POST['wpinv_discount_excluded_items'] ) ? $_POST['wpinv_discount_excluded_items'] : array(), |
|
| 392 | - 'max_uses' => isset( $_POST['wpinv_discount_max_uses'] ) ? $_POST['wpinv_discount_max_uses'] : null, |
|
| 393 | - 'min_total' => isset( $_POST['wpinv_discount_min_total'] ) ? $_POST['wpinv_discount_min_total'] : null, |
|
| 394 | - 'max_total' => isset( $_POST['wpinv_discount_max_total'] ) ? $_POST['wpinv_discount_max_total'] : null, |
|
| 395 | - ) |
|
| 389 | + 'is_recurring' => isset( $_POST['wpinv_discount_recurring'] ), |
|
| 390 | + 'items' => isset( $_POST['wpinv_discount_items'] ) ? $_POST['wpinv_discount_items'] : array(), |
|
| 391 | + 'excluded_items' => isset( $_POST['wpinv_discount_excluded_items'] ) ? $_POST['wpinv_discount_excluded_items'] : array(), |
|
| 392 | + 'max_uses' => isset( $_POST['wpinv_discount_max_uses'] ) ? $_POST['wpinv_discount_max_uses'] : null, |
|
| 393 | + 'min_total' => isset( $_POST['wpinv_discount_min_total'] ) ? $_POST['wpinv_discount_min_total'] : null, |
|
| 394 | + 'max_total' => isset( $_POST['wpinv_discount_max_total'] ) ? $_POST['wpinv_discount_max_total'] : null, |
|
| 395 | + ) |
|
| 396 | 396 | ); |
| 397 | 397 | |
| 398 | - $discount->save(); |
|
| 399 | - do_action( 'getpaid_discount_metabox_save', $post_id, $discount ); |
|
| 400 | - } |
|
| 398 | + $discount->save(); |
|
| 399 | + do_action( 'getpaid_discount_metabox_save', $post_id, $discount ); |
|
| 400 | + } |
|
| 401 | 401 | } |
@@ -12,108 +12,108 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Fees { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The fee validation error. |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - public $fee_error; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Submission fees. |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $fees = array(); |
|
| 15 | + /** |
|
| 16 | + * The fee validation error. |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + public $fee_error; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Submission fees. |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $fees = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Class constructor |
|
| 29 | + * |
|
| 30 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 31 | + */ |
|
| 32 | + public function __construct( $submission ) { |
|
| 33 | + |
|
| 34 | + // Process any existing invoice fees. |
|
| 35 | + if ( $submission->has_invoice() ) { |
|
| 36 | + $this->fees = $submission->get_invoice()->get_fees(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + // Process price fields. |
|
| 40 | + $data = $submission->get_data(); |
|
| 41 | + $payment_form = $submission->get_payment_form(); |
|
| 42 | + |
|
| 43 | + foreach ( $payment_form->get_elements() as $element ) { |
|
| 44 | + |
|
| 45 | + if ( 'price_input' == $element['type'] ) { |
|
| 46 | + $this->process_price_input( $element, $data ); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + if ( 'price_select' == $element['type'] ) { |
|
| 50 | + $this->process_price_select( $element, $data ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Process a price input field. |
|
| 59 | + * |
|
| 60 | + * @param array $element |
|
| 61 | + * @param array $data |
|
| 62 | + */ |
|
| 63 | + public function process_price_input( $element, $data ) { |
|
| 64 | + |
|
| 65 | + // Abort if not passed. |
|
| 66 | + if ( empty( $data[ $element['id'] ] ) ) { |
|
| 67 | + return; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
| 71 | + $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
| 72 | + |
|
| 73 | + if ( $amount < $minimum ) { |
|
| 74 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), $minimum ) ); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $this->fees[ $element['label'] ] = array( |
|
| 78 | + 'name' => $element['label'], |
|
| 79 | + 'initial_fee' => $amount, |
|
| 80 | + 'recurring_fee' => 0, |
|
| 81 | + ); |
|
| 82 | + |
|
| 83 | + } |
|
| 26 | 84 | |
| 27 | 85 | /** |
| 28 | - * Class constructor |
|
| 29 | - * |
|
| 30 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 31 | - */ |
|
| 32 | - public function __construct( $submission ) { |
|
| 33 | - |
|
| 34 | - // Process any existing invoice fees. |
|
| 35 | - if ( $submission->has_invoice() ) { |
|
| 36 | - $this->fees = $submission->get_invoice()->get_fees(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - // Process price fields. |
|
| 40 | - $data = $submission->get_data(); |
|
| 41 | - $payment_form = $submission->get_payment_form(); |
|
| 42 | - |
|
| 43 | - foreach ( $payment_form->get_elements() as $element ) { |
|
| 44 | - |
|
| 45 | - if ( 'price_input' == $element['type'] ) { |
|
| 46 | - $this->process_price_input( $element, $data ); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - if ( 'price_select' == $element['type'] ) { |
|
| 50 | - $this->process_price_select( $element, $data ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Process a price input field. |
|
| 59 | - * |
|
| 60 | - * @param array $element |
|
| 61 | - * @param array $data |
|
| 62 | - */ |
|
| 63 | - public function process_price_input( $element, $data ) { |
|
| 64 | - |
|
| 65 | - // Abort if not passed. |
|
| 66 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
| 67 | - return; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
| 71 | - $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
| 72 | - |
|
| 73 | - if ( $amount < $minimum ) { |
|
| 74 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), $minimum ) ); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $this->fees[ $element['label'] ] = array( |
|
| 78 | - 'name' => $element['label'], |
|
| 79 | - 'initial_fee' => $amount, |
|
| 80 | - 'recurring_fee' => 0, |
|
| 81 | - ); |
|
| 82 | - |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Process a price select field. |
|
| 87 | - * |
|
| 88 | - * @param array $element |
|
| 89 | - * @param array $data |
|
| 90 | - */ |
|
| 91 | - public function process_price_select( $element, $data ) { |
|
| 92 | - |
|
| 93 | - // Abort if not passed. |
|
| 94 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
| 95 | - return; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
| 99 | - $selected = wpinv_parse_list( $data[ $element['id'] ] ); |
|
| 100 | - $total = 0; |
|
| 101 | - |
|
| 102 | - foreach ( $selected as $price ) { |
|
| 103 | - |
|
| 104 | - if ( ! isset( $options[ $price ] ) ) { |
|
| 105 | - throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $total += (float) wpinv_sanitize_amount( $price ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $this->fees[ $element['label'] ] = array( |
|
| 112 | - 'name' => $element['label'], |
|
| 113 | - 'initial_fee' => $total, |
|
| 114 | - 'recurring_fee' => 0, |
|
| 115 | - ); |
|
| 116 | - |
|
| 117 | - } |
|
| 86 | + * Process a price select field. |
|
| 87 | + * |
|
| 88 | + * @param array $element |
|
| 89 | + * @param array $data |
|
| 90 | + */ |
|
| 91 | + public function process_price_select( $element, $data ) { |
|
| 92 | + |
|
| 93 | + // Abort if not passed. |
|
| 94 | + if ( empty( $data[ $element['id'] ] ) ) { |
|
| 95 | + return; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
| 99 | + $selected = wpinv_parse_list( $data[ $element['id'] ] ); |
|
| 100 | + $total = 0; |
|
| 101 | + |
|
| 102 | + foreach ( $selected as $price ) { |
|
| 103 | + |
|
| 104 | + if ( ! isset( $options[ $price ] ) ) { |
|
| 105 | + throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $total += (float) wpinv_sanitize_amount( $price ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $this->fees[ $element['label'] ] = array( |
|
| 112 | + 'name' => $element['label'], |
|
| 113 | + 'initial_fee' => $total, |
|
| 114 | + 'recurring_fee' => 0, |
|
| 115 | + ); |
|
| 116 | + |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | } |
@@ -12,189 +12,189 @@ |
||
| 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 Exception( __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // Exceeded limit. |
|
| 71 | - if ( $discount->has_exceeded_limit() ) { |
|
| 72 | - throw new Exception( __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Validate usages. |
|
| 76 | - $this->validate_single_use_discount( $submission, $discount ); |
|
| 77 | - |
|
| 78 | - // Validate amount. |
|
| 79 | - $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 80 | - |
|
| 81 | - // Save the discount. |
|
| 82 | - $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Validates a single use discount. |
|
| 87 | - * |
|
| 88 | - * @param WPInv_Discount $discount |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - public function is_discount_active( $discount ) { |
|
| 92 | - return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns a user's id or email. |
|
| 97 | - * |
|
| 98 | - * @param string $email |
|
| 99 | - * @return int|string|false |
|
| 100 | - */ |
|
| 101 | - public function get_user_id_or_email( $email ) { |
|
| 102 | - |
|
| 103 | - if ( is_user_logged_in() ) { |
|
| 104 | - return get_current_user_id(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return empty( $email ) ? false : sanitize_email( $email ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Validates a single use discount. |
|
| 112 | - * |
|
| 113 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 114 | - * @param WPInv_Discount $discount |
|
| 115 | - */ |
|
| 116 | - public function validate_single_use_discount( $submission, $discount ) { |
|
| 117 | - |
|
| 118 | - // Abort if it is not a single use discount. |
|
| 119 | - if ( ! $discount->is_single_use() ) { |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // Ensure there is a valid billing email. |
|
| 124 | - $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 125 | - |
|
| 126 | - if ( empty( $user ) ) { |
|
| 127 | - throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // Has the user used this discount code before? |
|
| 131 | - if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 132 | - throw new Exception( __( 'You have already used this discount', 'invoicing' ) ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Validates the discount's amount. |
|
| 139 | - * |
|
| 140 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 141 | - * @param WPInv_Discount $discount |
|
| 142 | - * @param float $amount |
|
| 143 | - */ |
|
| 144 | - public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 145 | - |
|
| 146 | - // Validate minimum amount. |
|
| 147 | - if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 148 | - $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 149 | - throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Validate the maximum amount. |
|
| 153 | - if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 154 | - $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 155 | - throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Calculates the discount code's amount. |
|
| 162 | - * |
|
| 163 | - * Ensure that the discount exists and has been validated before calling this method. |
|
| 164 | - * |
|
| 165 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 166 | - * @param WPInv_Discount $discount |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function calculate_discount( $submission, $discount ) { |
|
| 170 | - |
|
| 171 | - $initial_discount = 0; |
|
| 172 | - $recurring_discount = 0; |
|
| 173 | - |
|
| 174 | - foreach ( $submission->get_items() as $item ) { |
|
| 175 | - |
|
| 176 | - // Abort if it is not valid for this item. |
|
| 177 | - if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 178 | - continue; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Calculate the initial amount... |
|
| 182 | - $initial_discount += $discount->get_discounted_amount( $item->get_sub_total() ); |
|
| 183 | - |
|
| 184 | - // ... and maybe the recurring amount. |
|
| 185 | - if ( $item->is_recurring() && $discount->is_recurring() ) { |
|
| 186 | - $recurring_discount += $discount->get_discounted_amount( $item->get_recurring_sub_total() ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return array( |
|
| 192 | - 'name' => 'discount_code', |
|
| 193 | - 'discount_code' => $discount->get_code(), |
|
| 194 | - 'initial_discount' => $initial_discount, |
|
| 195 | - 'recurring_discount' => $recurring_discount, |
|
| 196 | - ); |
|
| 197 | - |
|
| 198 | - } |
|
| 67 | + throw new Exception( __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // Exceeded limit. |
|
| 71 | + if ( $discount->has_exceeded_limit() ) { |
|
| 72 | + throw new Exception( __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Validate usages. |
|
| 76 | + $this->validate_single_use_discount( $submission, $discount ); |
|
| 77 | + |
|
| 78 | + // Validate amount. |
|
| 79 | + $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 80 | + |
|
| 81 | + // Save the discount. |
|
| 82 | + $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Validates a single use discount. |
|
| 87 | + * |
|
| 88 | + * @param WPInv_Discount $discount |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + public function is_discount_active( $discount ) { |
|
| 92 | + return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns a user's id or email. |
|
| 97 | + * |
|
| 98 | + * @param string $email |
|
| 99 | + * @return int|string|false |
|
| 100 | + */ |
|
| 101 | + public function get_user_id_or_email( $email ) { |
|
| 102 | + |
|
| 103 | + if ( is_user_logged_in() ) { |
|
| 104 | + return get_current_user_id(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return empty( $email ) ? false : sanitize_email( $email ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Validates a single use discount. |
|
| 112 | + * |
|
| 113 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 114 | + * @param WPInv_Discount $discount |
|
| 115 | + */ |
|
| 116 | + public function validate_single_use_discount( $submission, $discount ) { |
|
| 117 | + |
|
| 118 | + // Abort if it is not a single use discount. |
|
| 119 | + if ( ! $discount->is_single_use() ) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // Ensure there is a valid billing email. |
|
| 124 | + $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 125 | + |
|
| 126 | + if ( empty( $user ) ) { |
|
| 127 | + throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // Has the user used this discount code before? |
|
| 131 | + if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 132 | + throw new Exception( __( 'You have already used this discount', 'invoicing' ) ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Validates the discount's amount. |
|
| 139 | + * |
|
| 140 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 141 | + * @param WPInv_Discount $discount |
|
| 142 | + * @param float $amount |
|
| 143 | + */ |
|
| 144 | + public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 145 | + |
|
| 146 | + // Validate minimum amount. |
|
| 147 | + if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 148 | + $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 149 | + throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Validate the maximum amount. |
|
| 153 | + if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 154 | + $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 155 | + throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Calculates the discount code's amount. |
|
| 162 | + * |
|
| 163 | + * Ensure that the discount exists and has been validated before calling this method. |
|
| 164 | + * |
|
| 165 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 166 | + * @param WPInv_Discount $discount |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function calculate_discount( $submission, $discount ) { |
|
| 170 | + |
|
| 171 | + $initial_discount = 0; |
|
| 172 | + $recurring_discount = 0; |
|
| 173 | + |
|
| 174 | + foreach ( $submission->get_items() as $item ) { |
|
| 175 | + |
|
| 176 | + // Abort if it is not valid for this item. |
|
| 177 | + if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 178 | + continue; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Calculate the initial amount... |
|
| 182 | + $initial_discount += $discount->get_discounted_amount( $item->get_sub_total() ); |
|
| 183 | + |
|
| 184 | + // ... and maybe the recurring amount. |
|
| 185 | + if ( $item->is_recurring() && $discount->is_recurring() ) { |
|
| 186 | + $recurring_discount += $discount->get_discounted_amount( $item->get_recurring_sub_total() ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return array( |
|
| 192 | + 'name' => 'discount_code', |
|
| 193 | + 'discount_code' => $discount->get_code(), |
|
| 194 | + 'initial_discount' => $initial_discount, |
|
| 195 | + 'recurring_discount' => $recurring_discount, |
|
| 196 | + ); |
|
| 197 | + |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | } |