+}
+
+=item payment_gateway [ OPTION => VALUE, ... ]
+
+Returns a payment gateway object (see L<FS::payment_gateway>) for this agent.
+
+Currently available options are I<nofatal>, I<method>, I<thirdparty> and I<conf>.
+
+If I<nofatal> is set, and no gateway is available, then the empty string
+will be returned instead of throwing a fatal exception.
+
+The I<method> option can be used to influence the choice
+as well. Presently only CHEK/ECHECK and PAYPAL methods are meaningful.
+
+If I<method> is CHEK/ECHECK and the default gateway is being returned,
+the business-onlinepayment-ach gateway will be returned if available.
+
+If I<thirdparty> is set and the I<method> is PAYPAL, the defined paypal
+gateway will be returned.
+
+Exisisting I<$conf> may be passed for efficiency.
+
+=cut
+
+# opts invnum/payinfo for cardtype/taxclass overrides no longer supported
+# any future overrides added here need to be reconciled with the tokenization process
+
+sub payment_gateway {
+ my ( $self, %options ) = @_;
+
+ $options{'conf'} ||= new FS::Conf;
+ my $conf = $options{'conf'};
+
+ if ( $options{thirdparty} ) {
+
+ # allows PayPal to coexist with credit card gateways
+ my $is_paypal = { op => '!=', value => 'PayPal' };
+ if ( uc($options{method}) eq 'PAYPAL' ) {
+ $is_paypal = 'PayPal';
+ }
+
+ my $gateway = qsearchs({
+ table => 'payment_gateway',
+ addl_from => ' JOIN agent_payment_gateway USING (gatewaynum) ',
+ hashref => {
+ gateway_namespace => 'Business::OnlineThirdPartyPayment',
+ gateway_module => $is_paypal,
+ disabled => '',
+ },
+ extra_sql => ' AND agentnum = '.$self->agentnum,
+ });
+
+ if ( $gateway ) {
+ return $gateway;
+ } elsif ( $options{'nofatal'} ) {
+ return '';
+ } else {
+ die "no third-party gateway configured\n";
+ }
+ }
+
+ my $cardtype_search = "AND ( cardtype IS NULL OR cardtype <> 'ACH')";
+ $cardtype_search = "AND ( cardtype IS NULL OR cardtype = 'ACH' )" if $options{method} eq 'ECHECK';
+
+ my $override =
+ qsearchs({
+ "table" => 'agent_payment_gateway',
+ "hashref" => { agentnum => $self->agentnum, },
+ "extra_sql" => $cardtype_search,
+ });
+
+ my $payment_gateway = FS::payment_gateway->by_key_or_default(
+ gatewaynum => $override ? $override->gatewaynum : '',
+ %options,
+ );
+
+ $payment_gateway;
+}
+
+=item invoice_modes
+
+Returns all L<FS::invoice_mode> objects that are valid for this agent (i.e.
+those with this agentnum or null agentnum).
+
+=cut
+
+sub invoice_modes {
+ my $self = shift;
+ qsearch( {
+ table => 'invoice_mode',
+ hashref => { agentnum => $self->agentnum },
+ extra_sql => ' OR agentnum IS NULL',
+ order_by => ' ORDER BY modename',
+ } );
+}