093891e93807186b49ed18a27a96ebd00868755c
[freeside.git] / FS / FS / payinfo_transaction_Mixin.pm
1 package FS::payinfo_transaction_Mixin;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::payby;
6 use FS::payinfo_Mixin;
7 use FS::Record qw(qsearchs);
8 use FS::cust_main;
9 use FS::payment_gateway;
10
11 @ISA = qw( FS::payinfo_Mixin );
12
13 =head1 NAME
14
15 FS::payinfo_transaction_Mixin - Mixin class for records in tables that represent transactions.
16
17 =head1 SYNOPSIS
18
19 package FS::some_table;
20 use vars qw(@ISA);
21 @ISA = qw( FS::payinfo_transaction_Mixin FS::Record );
22
23 =head1 DESCRIPTION
24
25 This is a mixin class for records that represent transactions: that contain
26 payinfo and realtime result fields (gatewaynum, processor, authorization,
27 order_number).  Currently FS::cust_pay, FS::cust_refund, and FS::cust_pay_void.
28
29 =head1 METHODS
30
31 =over 4
32
33 =item cust_main
34
35 Returns the parent customer object (see L<FS::cust_main>).
36
37 =cut
38
39 sub cust_main {
40   my $self = shift;
41   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
42 }
43
44 =item payby_name
45
46 Returns a name for the payby field.
47
48 =cut
49
50 sub payby_name {
51   my $self = shift;
52   if ( $self->payby eq 'BILL' ) { #kludge
53     'Check';
54   } else {
55     FS::payby->shortname( $self->payby );
56   }
57 }
58
59 # We keep _parse_paybatch just because the upgrade needs it.
60
61 sub _parse_paybatch {
62   my $self = shift;
63
64   $self->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
65     or return {};
66               #"Can't parse paybatch for paynum $options{'paynum'}: ".
67               #  $cust_pay->paybatch;
68
69   my( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
70
71   if ( $gatewaynum ) { #gateway for the payment to be refunded
72
73     my $payment_gateway =
74       qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
75
76     die "payment gateway $gatewaynum not found" #?
77       unless $payment_gateway;
78
79     $processor = $payment_gateway->gateway_module;
80
81   }
82
83   {
84     'gatewaynum'    => $gatewaynum,
85     'processor'     => $processor,
86     'authorization' => $auth,
87     'order_number'  => $order_number,
88   };
89
90 }
91
92 # because we can't actually name the field 'authorization' (reserved word)
93 sub authorization {
94   my $self = shift;
95   $self->auth(@_);
96 }
97
98 =item payinfo_check
99
100 Checks the validity of the realtime payment fields (gatewaynum, processor,
101 auth, and order_number) as well as payby and payinfo
102
103 =cut
104
105 sub payinfo_check {
106   my $self = shift;
107
108   # All of these can be null, so in principle this could go in payinfo_Mixin.
109
110   $self->SUPER::payinfo_check()
111   || $self->ut_numbern('gatewaynum')
112   # not ut_foreign_keyn, it causes upgrades to fail
113   || $self->ut_alphan('processor')
114   || $self->ut_textn('auth')
115   || $self->ut_textn('order_number')
116   || '';
117 }
118
119 =back
120
121 =head1 SEE ALSO
122
123 L<FS::payinfo_Mixin>
124
125 =cut
126
127 1;