@@ -12,114 +12,114 @@ |
||
| 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, $submission ); |
|
| 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 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 63 | + */ |
|
| 64 | + public function process_price_input( $element, $data, $submission ) { |
|
| 65 | + |
|
| 66 | + // Abort if not passed. |
|
| 67 | + if ( empty( $data[ $element['id'] ] ) ) { |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
| 72 | + $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
| 73 | + |
|
| 74 | + if ( $amount < $minimum ) { |
|
| 75 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), wpinv_price( $minimum, $submission->get_currency() ) ) ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $this->fees[ $element['label'] ] = array( |
|
| 79 | + 'name' => $element['label'], |
|
| 80 | + 'initial_fee' => $amount, |
|
| 81 | + 'recurring_fee' => 0, |
|
| 82 | + ); |
|
| 83 | + |
|
| 84 | + } |
|
| 26 | 85 | |
| 27 | 86 | /** |
| 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, $submission ); |
|
| 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 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 63 | - */ |
|
| 64 | - public function process_price_input( $element, $data, $submission ) { |
|
| 65 | - |
|
| 66 | - // Abort if not passed. |
|
| 67 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
| 72 | - $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
| 73 | - |
|
| 74 | - if ( $amount < $minimum ) { |
|
| 75 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), wpinv_price( $minimum, $submission->get_currency() ) ) ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $this->fees[ $element['label'] ] = array( |
|
| 79 | - 'name' => $element['label'], |
|
| 80 | - 'initial_fee' => $amount, |
|
| 81 | - 'recurring_fee' => 0, |
|
| 82 | - ); |
|
| 83 | - |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Process a price select field. |
|
| 88 | - * |
|
| 89 | - * @param array $element |
|
| 90 | - * @param array $data |
|
| 91 | - */ |
|
| 92 | - public function process_price_select( $element, $data ) { |
|
| 93 | - |
|
| 94 | - // Abort if not passed. |
|
| 95 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
| 100 | - $selected = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) ); |
|
| 101 | - $total = 0; |
|
| 102 | - $sub_labels = array(); |
|
| 103 | - |
|
| 104 | - foreach ( $selected as $price ) { |
|
| 105 | - |
|
| 106 | - if ( ! isset( $options[ $price ] ) ) { |
|
| 107 | - throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $price = explode( '|', $price ); |
|
| 111 | - |
|
| 112 | - $sub_labels[] = $price[0]; |
|
| 113 | - $total += (float) wpinv_sanitize_amount( $price[1] ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $this->fees[ $element['label'] ] = array( |
|
| 117 | - 'name' => $element['label'], |
|
| 118 | - 'initial_fee' => $total, |
|
| 119 | - 'recurring_fee' => 0, |
|
| 120 | - 'description' => implode( ', ', $sub_labels ), |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - } |
|
| 87 | + * Process a price select field. |
|
| 88 | + * |
|
| 89 | + * @param array $element |
|
| 90 | + * @param array $data |
|
| 91 | + */ |
|
| 92 | + public function process_price_select( $element, $data ) { |
|
| 93 | + |
|
| 94 | + // Abort if not passed. |
|
| 95 | + if ( empty( $data[ $element['id'] ] ) ) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
| 100 | + $selected = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) ); |
|
| 101 | + $total = 0; |
|
| 102 | + $sub_labels = array(); |
|
| 103 | + |
|
| 104 | + foreach ( $selected as $price ) { |
|
| 105 | + |
|
| 106 | + if ( ! isset( $options[ $price ] ) ) { |
|
| 107 | + throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $price = explode( '|', $price ); |
|
| 111 | + |
|
| 112 | + $sub_labels[] = $price[0]; |
|
| 113 | + $total += (float) wpinv_sanitize_amount( $price[1] ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $this->fees[ $element['label'] ] = array( |
|
| 117 | + 'name' => $element['label'], |
|
| 118 | + 'initial_fee' => $total, |
|
| 119 | + 'recurring_fee' => 0, |
|
| 120 | + 'description' => implode( ', ', $sub_labels ), |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | } |