diff --git a/app/Services/Client/PaymentMethod.php b/app/Services/Client/PaymentMethod.php new file mode 100644 index 000000000000..35fe8de2f735 --- /dev/null +++ b/app/Services/Client/PaymentMethod.php @@ -0,0 +1,240 @@ +client = $client; + $this->amount = $amount; + } + + public function run() :Credit + { + + $this->getGateways() + ->getMethods() + ->buildUrls(); + } + + public function getPaymentUrls() + { + return $this->payment_urls; + } + + public function getPaymentMethods() + { + return $this->payment_methods; + } + + private function getGateways() + { + + $company_gateways = $this->client->getSetting('company_gateway_ids'); + + //we need to check for "0" here as we disable a payment gateway for a client with the number "0" + if ($company_gateways || $company_gateways == '0') { + + $transformed_ids = $this->transformKeys(explode(',', $company_gateways)); + + $this->gateways = $this->client + ->company + ->company_gateways + ->whereIn('id', $transformed_ids) + ->where('gateway_key', '!=', '54faab2ab6e3223dbe848b1686490baa') + ->sortby(function ($model) use ($transformed_ids) { //company gateways are sorted in order of priority + return array_search($model->id, $transformed_ids);// this closure sorts for us + }); + + } else { + + $this->gateways = $this->client + ->company + ->company_gateways + ->where('gateway_key', '!=', '54faab2ab6e3223dbe848b1686490baa') + ->where('is_deleted', false); + + } + + + return $this; + } + + + private function getCustomGateways() + { + + $company_gateways = $this->client->getSetting('company_gateway_ids'); + + //we need to check for "0" here as we disable a payment gateway for a client with the number "0" + if ($company_gateways || $company_gateways == '0') { + + $transformed_ids = $this->transformKeys(explode(',', $company_gateways)); + + $this->gateways = $this->client + ->company + ->company_gateways + ->whereIn('id', $transformed_ids) + ->where('gateway_key', '=', '54faab2ab6e3223dbe848b1686490baa') + ->sortby(function ($model) use ($transformed_ids) { //company gateways are sorted in order of priority + return array_search($model->id, $transformed_ids);// this closure sorts for us + }); + + } else { + + $this->gateways = $this->client + ->company + ->company_gateways + ->where('gateway_key', '=', '54faab2ab6e3223dbe848b1686490baa') + ->where('is_deleted', false); + + } + + + return $this; + } + + private function getMethods() + { + // we should prefilter $gateway->driver($this)->gatewayTypes() + // and only include the enabled payment methods on the gateway + $this->$this->payment_methods = []; + + foreach ($this->gateways as $gateway) { + foreach ($gateway->driver($this)->gatewayTypes() as $type) { + if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) { + if ($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $amount)) { + $this->payment_methods[] = [$gateway->id => $type]; + } + } else { + $this->payment_methods[] = [$gateway->id => $type]; + } + } + } + + //transform from Array to Collection + $payment_methods_collections = collect($this->payment_methods); + + //** Plucks the remaining keys into its own collection + $this->payment_methods = $payment_methods_collections->intersectByKeys($payment_methods_collections->flatten(1)->unique()); + + /* Loop through custom gateways if any exist and append them to the methods collection*/ + $this->getCustomGateways(); + + //note we have to use GatewayType::CREDIT_CARD as alias for CUSTOM + foreach ($this->gateways as $gateway) { + foreach ($gateway->driver($this)->gatewayTypes() as $type) { + if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) { + if ($this->validGatewayForAmount($gateway->fees_and_limits->{GatewayType::CREDIT_CARD}, $amount)) { + $this->payment_methods->push([$gateway->id => $type]); + } + } else { + $this->payment_methods->push([$gateway->id => NULL]); + } + } + } + + } + + private function buildUrls() + { + + foreach ($payment_methods_intersect as $key => $child_array) { + foreach ($child_array as $gateway_id => $gateway_type_id) { + $gateway = CompanyGateway::find($gateway_id); + + $fee_label = $gateway->calcGatewayFeeLabel($amount, $this); + + if(!$gateway_type_id){ + + $this->payment_urls[] = [ + 'label' => $gateway->getConfigField('name') . $fee_label, + 'company_gateway_id' => $gateway_id, + 'gateway_type_id' => GatewayType::CREDIT_CARD, + ]; + } + else + { + $this->payment_urls[] = [ + 'label' => $gateway->getTypeAlias($gateway_type_id) . $fee_label, + 'company_gateway_id' => $gateway_id, + 'gateway_type_id' => $gateway_type_id, + ]; + } + } + } + + if (($this->client->getSetting('use_credits_payment') == 'option' || $this->client->getSetting('use_credits_payment') == 'always') && $this->client->service()->getCreditBalance() > 0) { + + // Show credits as only payment option if both statements are true. + if ( + $this->client->service()->getCreditBalance() > $amount + && $this->client->getSetting('use_credits_payment') == 'always') { + $payment_urls = []; + } + + $payment_urls[] = [ + 'label' => ctrans('texts.apply_credit'), + 'company_gateway_id' => CompanyGateway::GATEWAY_CREDIT, + 'gateway_type_id' => GatewayType::CREDIT, + ]; + } + + return $this; + + } + + private function validGatewayForAmount($fees_and_limits_for_payment_type, $amount) :bool + { + if (isset($fees_and_limits_for_payment_type)) { + $fees_and_limits = $fees_and_limits_for_payment_type; + } else { + return true; + } + + if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && $amount < $fees_and_limits->min_limit) { + return false; + } + + if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $fees_and_limits->max_limit != -1 && $amount > $fees_and_limits->max_limit) { + return false; + } + + return true; + } +} + +