package LJ::SMS; use strict; use lib "$ENV{LJHOME}/cgi-bin"; sub push_yes_means { my ($u, $means) = @_; my $propval = $u->prop('sms_yes_means'); unless ($propval) { $u->set_prop('sms_yes_means', $means); return; } my @vals = split(',', $propval); # at any given point there can only be one outstanding 'buy-' request in # queue. the most recently added should always be what is active if ($means =~ /buy-/) { # remove existing buy requests from queue @vals = grep { $_ !~ /buy-/ } @vals; } # now add the requested value unless it is already in queue push @vals, $means unless grep { $_ eq $means } @vals; my $newval = join(',', @vals); # can't go over max userprop size, sorry return 0 if length $newval > 255; $u->set_prop('sms_yes_means', $newval); return 1; } sub yes_means { my $u = shift; my $propval = $u->prop('sms_yes_means') or return (); return split(',', $propval); } sub clear_yes_means { my $u = shift; $u->set_prop('sms_yes_means', ''); } sub set_yes_means { my $u = shift; $u->set_prop('sms_yes_means', join(',', @_)); } sub pop_yes_means { my $u = shift; my $propval = $u->prop('sms_yes_means'); return undef unless $propval; my @vals = split(',', $propval); my $curval = pop @vals; $u->set_prop('sms_yes_means', join(',', @vals)); return $curval; } sub shift_yes_means { my $u = shift; my $propval = $u->prop('sms_yes_means'); return undef unless $propval; my @vals = split(',', $propval); my $curval = shift @vals; $u->set_prop('sms_yes_means', join(',', @vals)); return $curval; } # get the next item in the queue but don't shift it sub peek_yes_means { my $u = shift; my $propval = $u->prop('sms_yes_means'); return undef unless $propval; my @vals = split(',', $propval); return shift @vals; } sub send_confirm_purchase { my ($u, $qty) = @_; # FIXME: need method to look up price for given $qty my $price = LJ::Pay::extra_sms_price($qty); die "invalid message quantity: $qty" unless defined $price; my $disp_price = sprintf("\$%.02f", $price); my $body_text = "You have chosen a one-time billing of $disp_price for $qty " . "messages to be billed to your phone. To confirm this purchase, " . "reply YES. Standard rates apply."; my $msg = LJ::SMS::Message->new ( owner => $u, body_text => $body_text, to => $u, class_key => 'Buy-Confirm', ); return $u->send_sms($msg, no_quota => 1); } # stop sms program for this user sub stop_all { my $u = shift; my $msg = shift; # stop everything and don't send anything to this user ever again $msg->respond ("You have discontinued $LJ::SMS_TITLE. You will no longer receive messages " . "or charges for this service. Questions contact support\@$LJ::SMS_DOMAIN", force => 1, no_quota => 1); $u->set_prop('sms_carrier', undef); $u->delete_sms_number; } # find out how much we have billed this user this month, make sure # we don't go over the sms billing limit sub can_purchase { my ($u, $amt) = @_; return 1 unless $amt; return 1 unless $LJ::SMS_MONTHLY_BILL_LIMIT; my ($month, $year) = (localtime())[4,5]; $month++; $year += 1900; my @msgs = LJ::SMS::Message->load($u, month => $month, year => $year); my @payids = grep { $_ } map { $_->meta('payid') } @msgs; return 1 unless scalar @payids; my $total_dolla = LJ::Pay::Payment::payment_sum(userid => $u->id, payids => \@payids, method => 'sms'); return 1 unless $total_dolla + $amt > $LJ::SMS_MONTHLY_BILL_LIMIT; return 0; } 1;