Skip to content

Commit

Permalink
Subscribe add monthly
Browse files Browse the repository at this point in the history
Add monthly and semiannual subs, fix admin stats email, and general
cleanup.
  • Loading branch information
paulej72 committed Feb 22, 2015
1 parent 1d373db commit 196a950
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 263 deletions.
67 changes: 3 additions & 64 deletions plugins/Subscribe/Static/Static.pm
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,6 @@ sub countCurrentSubs {
"subscriber_until >= $md_today");
}

sub countTotalRenewingSubs {
my($self) = @_;
my $dt_epoch = DateTime->new( year => 1970, month => 1, day => 1 );
my $md_epoch = DateTime::Format::MySQL->format_date($dt_epoch);
return scalar( @{
$self->sqlSelectColArrayref(
'users_info.uid, COUNT(*) AS c',
'users_info LEFT JOIN subscribe_payments ON users_info.uid = subscribe_payments.uid',
"users_info.subscriber_until > $md_epoch",
'GROUP BY users_info.uid HAVING c > 1'
)
} );
}

sub countCurrentRenewingSubs {
my($self) = @_;
my $dt_today = DateTime->today;
my $md_today = DateTime::Format::MySQL->format_date($dt_today);
return scalar( @{
$self->sqlSelectColArrayref(
'users_info.uid, COUNT(*) AS c',
'users_info LEFT JOIN subscribe_payments ON users_info.uid = subscribe_payments.uid',
"users_info.subscriber_until >= $md_today",
'GROUP BY users_info.uid HAVING c > 1'
)
} );
}

sub countTotalGiftSubs {
my($self) = @_;
my @gift_uids = $self->_getUidsForPaymentType("gift");
Expand All @@ -95,38 +67,6 @@ sub countCurrentGiftSubs {
'subscriber_until >= '.$md_today.' AND uid in('.join(',',@gift_uids).')');
}

sub countTotalRenewingGiftSubs {
my($self) = @_;
my @gift_uids = $self->_getUidsForPaymentType("gift");
my $dt_epoch = DateTime->new( year => 1970, month => 1, day => 1 );
my $md_epoch = DateTime::Format::MySQL->format_date($dt_epoch);
return 0 unless @gift_uids;
return scalar( @{
$self->sqlSelectColArrayref(
'users_info.uid, COUNT(*) AS c',
'users_info LEFT JOIN subscribe_payments ON users_info.uid = subscribe_payments.uid AND users_info.uid in('.join(',',@gift_uids).')',
"users_info.subscriber_until > $md_epoch",
'GROUP BY users_info.uid HAVING c > 1'
)
} );
}

sub countCurrentRenewingGiftSubs {
my($self) = @_;
my @gift_uids = $self->_getUidsForPaymentType("gift");
return 0 unless @gift_uids;
my $dt_today = DateTime->today;
my $md_today = DateTime::Format::MySQL->format_date($dt_today);
return scalar( @{
$self->sqlSelectColArrayref(
'users_info.uid, COUNT(*) AS c',
'users_info LEFT JOIN subscribe_payments ON users_info.uid = subscribe_payments.uid',
'users_info.subscriber_until >= '.$md_today.' AND users_info.uid in('.join(',',@gift_uids).')',
'GROUP BY users_info.uid HAVING c > 1'
)
} );
}

sub getLowRunningSubs {
my ($self) = @_;
my $low_val = getCurrentStatic('subscribe_low_val');
Expand Down Expand Up @@ -190,12 +130,11 @@ sub getSubscriberList {
method, transaction_id, data, memo,
nickname, realemail, seclev, author,
karma, upmods, downmods, created_at,
users_hits.hits as hits, subscriber_until, payment_type, puid, hide_subscription",
"subscribe_payments, users, users_info, users_hits",
subscriber_until, payment_type, puid, hide_subscription",
"subscribe_payments, users, users_info",
"ts BETWEEN '$start' AND '$end'
AND subscribe_payments.uid = users.uid
AND subscribe_payments.uid = users_info.uid
AND subscribe_payments.uid = users_hits.uid"
AND subscribe_payments.uid = users_info.uid"
);
}

Expand Down
11 changes: 1 addition & 10 deletions plugins/Subscribe/Subscribe.pm
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,6 @@ sub convertDaysToDollars {
}


sub convertToText{
my($self, $hashref) = @_;
use Data::Dumper;
my $dumped = Dumper($hashref);
$dumped =~ s/^\s+//mg; $dumped =~ s/^.VAR1 = {\n//g; $dumped =~ s/};\n//g;
return $dumped;
}


sub addDaysToSubscriber {
my($self, $uid, $days) = @_;
return 0 unless $uid;
Expand Down Expand Up @@ -502,7 +493,7 @@ sub ppAddLog {
my $data = {
transaction_id => $logthis->{txn_id},
transaction_type => $logthis->{txn_type},
raw_transaction => $self->convertToText($logthis)
raw_transaction => encode_json($logthis)
};

$data->{email} = $logthis->{payer_email} if $logthis->{payer_email};
Expand Down
10 changes: 3 additions & 7 deletions plugins/Subscribe/bitpay.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,18 @@ sub main {
sub bitpay {
my ($form, $slashdb, $user, $constants) = @_;

my $puid = $form->{puid};
my $uid = $form->{uid};
my $from = $form->{from};
my $isGift = $puid ne $uid ? 1 : 0;
$puid = '""' unless $isGift;


my $subscribe = getObject('Slash::Subscribe');
my $invoice = $subscribe->bpCreateInvoice(
price => $constants->{bitpay_amount},
price => $form->{amount},
currency => "USD",
notificationType => "json",
transactionSpeed => "high",
fullNotifications => "true",
redirectURL => $constants->{bitpay_return},
notificationURL => $constants->{bitpay_callback},
posData => "{ \"uid\" : $uid , \"gift\" : $isGift , \"puid\" : $puid , \"from\" : \"$from\" }",
posData => "{ \"uid\" : $form->{uid} , \"type\" : \"$form->{type}\" , \"puid\" : $form->{puid} , \"from\" : \"$form->{from}\", \"days\" : $form->{days} }",
);

redirect("$invoice->{url}", "302");
Expand Down
3 changes: 2 additions & 1 deletion plugins/Subscribe/mysql_dump
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ INSERT INTO vars (name, value, description) VALUES ('paypal_amount', '20.00', 'H
INSERT INTO vars (name, value, description) VALUES ('paypal_host','www.sandbox.paypal.com','Host part of url for PayPal. Change this to to use PayPal sandbox. ');
INSERT INTO vars (name, value, description) VALUES ('paypal_business', 'yourname@example.com', 'Email of Paypal business acct');
INSERT INTO vars (name, value, description) VALUES ('paypal_cancel_return', 'http://www.example.com/subscribe.pl', 'URL to go to if payment is cancelled before being made');
INSERT INTO vars (name, value, description) VALUES ('paypal_image_src', 'http://images.paypal.com/images/x-click-but02.gif', 'URL of button image to click on');
INSERT INTO vars (name, value, description) VALUES ('paypal_image_src', '//www.paypalobjects.com/en_US/i/btn/x-click-but01.gif', 'URL of buy now button image to click on');
INSERT INTO vars (name, value, description) VALUES ('paypal_sub_image_src', '//www.paypalobjects.com/en_US/i/btn/x-click-but20.gif', 'URL of subscribe button image to click on');
INSERT INTO vars (name, value, description) VALUES ('paypal_no_shipping', '1', '0=Shipping address reqd (more secure), 1=No shipping address reqd (more convenient)');
INSERT INTO vars (name, value, description) VALUES ('paypal_num_days','365','How many days your paypal_amount buys');
INSERT INTO vars (name, value, description) VALUES ('paypal_num_pages', '1000', 'How many pages your paypal_amount buys');
Expand Down
33 changes: 14 additions & 19 deletions plugins/Subscribe/subscribe.pl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DateTime;
use DateTime::Format::MySQL;
use Slash::Constants qw(:web :messages);
use JSON;

sub main {
my $user = getCurrentUser();
Expand Down Expand Up @@ -216,31 +217,22 @@ sub paypal {
my $pp_pdt = $subscribe->ppDoPDT($txid);
# use Data::Dumper; print STDERR Dumper($pp_pdt);

$pp_pdt->{custom} = decode_json($pp_pdt->{custom}) || "";

if (ref($pp_pdt) eq "HASH") {
my $days = $subscribe->convertDollarsToDays($pp_pdt->{payment_gross}, 'paypal');
my $payment_net = $pp_pdt->{payment_gross} - $pp_pdt->{payment_fee};

my ($puid, $payment_type, $from);
if ($pp_pdt->{custom}){
$puid = $pp_pdt->{custom};
$payment_type = 'gift';
$from = $pp_pdt->{option_selection1};
} else {
$puid = $pp_pdt->{item_number};
$payment_type = 'user';
}

my $payment = {
days => $days,
uid => $pp_pdt->{item_number},
days => $pp_pdt->{custom}{days},
uid => $pp_pdt->{custom}{uid},
payment_net => $payment_net,
payment_gross => $pp_pdt->{payment_gross},
payment_type => $payment_type,
payment_type => $pp_pdt->{custom}{type},
transaction_id => $pp_pdt->{txn_id},
method => 'paypal',
email => $from,
raw_transaction => $subscribe->convertToText($pp_pdt),
puid => $puid
email => $pp_pdt->{custom}{from},
raw_transaction => encode_json($pp_pdt),
puid => $pp_pdt->{custom}{puid}
};

if (!$subscribe->paymentExists($txid)){
Expand All @@ -249,9 +241,9 @@ sub paypal {
my ($rows, $result, $warning);
$rows = $subscribe->insertPayment($payment);
if ($rows && $rows == 1) {
$result = $subscribe->addDaysToSubscriber($payment->{uid}, $days);
$result = $subscribe->addDaysToSubscriber($payment->{uid}, $payment->{days});
if ($result && $result == 1){
send_gift_msg($payment->{uid}, $payment->{puid}, $payment->{days}, $from) if $payment->{payment_type} eq "gift";
send_gift_msg($payment->{uid}, $payment->{puid}, $payment->{days}, $payment->{email}) if $payment->{payment_type} eq "gift";
} else {
$warning = "DEBUG: Payment accepted but user subscription not updated!\n" . Dumper($payment);
print STDERR $warning;
Expand Down Expand Up @@ -335,6 +327,7 @@ sub confirm {
my($form, $slashdb, $user, $constants) = @_;

my $type = $form->{subscription_type};

my $days = $form->{subscription_days};
my $amount;
switch($days) {
Expand Down Expand Up @@ -366,6 +359,7 @@ sub confirm {

my $uid = $form->{uid};
my $sub_user = $slashdb->getUser($uid);
my $puid = sub_user->{uid};
my $title ="Confirm subscription and choose payment type";
my $prefs_titlebar = slashDisplay('prefs_titlebar', {
tab_selected => 'subscription',
Expand All @@ -378,6 +372,7 @@ sub confirm {
days => $days,
amount => $amount,
uid => $uid,
puid => $puid,
sub_user => $sub_user,
user => $user,
from => $form->{from}
Expand Down
Loading

0 comments on commit 196a950

Please sign in to comment.