Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 213 lines (200 sloc) 6.565 kb
#!/usr/bin/env perl
use 5.14.1;
use Dancer qw/:syntax/;
use Dancer::Plugin::Email;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Biopay::Daemon;
use Biopay::Member;
use Biopay::Receipt;
use Biopay::EmailReceipt qw/parse_email/;
use Biopay::Transaction;
use Biopay::PaymentProcessor;
use Biopay::Util qw/email_admin email_board host/;
use Try::Tiny;
my $daemon = Biopay::Daemon->new(
name => 'job-runner',
want_stream => 1,
job_handlers => {
'send-email' => sub {
my $args = shift->args->{msg};
print " (Sending '$args->{subject}' email to $args->{to}) ";
for my $to (parse_email(delete $args->{to})) {
email {
from => config->{email_from},
to => $to,
%$args,
};
}
},
'email-all-members' => sub {
my ($job, $member) = @_;
email_all_members($job->args);
},
'send-receipt' => sub {
my ($job, $member) = @_;
print " (Receipt to @{[$member->id]} - @{[$member->name]}) ";
# Shell out so we can avoid AE-style programming of the
# receipt code
run('send-receipt', $job->_id);
},
'register-member' => sub {
my ($job, $member) = @_;
register_member($member, $job->args->{PIN});
},
'send-protest' => sub {
my ($job, $member) = @_;
my $cc_member = $job->args->{cc_member} ? 1 : 0;
print " (Protest for @{[$member->id]}) ";
# Shell out so we can avoid AE-style programming of the
# receipt code
run('send-protest', $member->id, ($cc_member ? '--cc' : ()));
},
},
CVs => [
AnyEvent->timer(
after => 2.0, interval => 60,
cb => \&check_for_unpaid_txns,
),
],
update_handlers => {
qr/^(member|txn):/ =>\&check_for_unpaid_txns,
}
);
$daemon->run;
exit;
sub email_all_members {
my $args = shift;
my $subj = $args->{subj} || die "send_email: subj is not defined!";
my $body = $args->{body} || die "send_email: body is not defined!";
Biopay::Member->All(
sub {
my $members = shift;
my %emails = map { $_ => 1 }
grep { length } grep { defined }
map { parse_email($_->email) }
grep { $_->active and !$_->email_optout }
@$members;
print " (Sending '$subj' to: ";
for my $email (keys %emails) {
email {
to => $email,
from => config->{email_from},
subject => $subj,
type => 'html',
message => $body,
};
print "$email ";
}
print ") ";
}
);
print "Emailing the board. ";
email {
to => config->{board_email},
from => config->{email_from},
subject => $subj,
type => 'html',
message => $body,
};
}
sub register_member {
my $member = shift;
my $PIN = shift;
my $email = $member->email;
my $signup_price = $daemon->prices->signup_price;
try {
print " (Register: $email) ";
# Process sign-up fee
my $order_num = 'signup:' . $member->id;
my ($ok, $msg) = Biopay::PaymentProcessor->new->process(
order_num => $order_num,
amount => $signup_price,
hash => $member->payment_hash,
);
if (!$ok) {
die "Failed to process registration payment for " .
$member->id . ": $msg\n";
}
Biopay::Receipt->Create(
member_id => $member->id,
order_num => $order_num,
amount => $signup_price,
items => [
{
amount => $signup_price, type => 'signup',
desc => 'Signup'
}
]
);
# Activate Cardlock
Biopay::Command->Create(
command => 'change_PIN',
member_id => $member->id,
new_PIN => $PIN,
on_error => sub {
email_admin("Failed to create change_PIN job",
"Member_id: @{[$member->id]} PIN:$PIN\n\nError: $_");
}
);
$member->send_welcome_email($PIN);
email_board("New member - " . $member->id,
"Hello Board, we just had a new member join the co-op: "
. $member->name . ".\n\n"
. "Details: " . host() . "/members/" . $member->id . "\n");
}
catch {
my $err = "Failed to register new member: $_";
debug $err;
email_admin("Error registering new member $email", "Error: $err");
};
}
sub check_for_unpaid_txns {
state $last_check = 0;
my $time_since_last_check = time() - $last_check;
if ($time_since_last_check < 15) {
return;
}
$last_check = time();
print 'U';
Biopay::Transaction->All_unpaid(
sub {
my $txns = shift;
my %member;
my %member_total;
for my $txn (@$txns) {
push @{ $member{$txn->member_id} }, $txn;
$member_total{$txn->member_id} += $txn->price;
}
for my $mid (keys %member) {
if ($member_total{$mid} < 5) {
# print " (Member $mid has < \$5, skipping) ";
next;
}
my $txns = $member{$mid};
Biopay::Member->By_id($mid, sub {
my $member = shift;
unless ($member) {
print " (No member $mid found) ";
return;
}
return if $member->billing_error;
if (my $hash = $member->payment_hash) {
print 'P';
run("process-transactions", $member->id);
}
else {
print 'u';
}
},
);
}
}
);
}
sub run {
my $cmd = shift;
warn "Running '$cmd @_'\n";
system($^X, "$FindBin::Bin/$cmd", @_)
and die "Failed to run: $cmd";
}
Jump to Line
Something went wrong with that request. Please try again.