mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-05-01 05:41 am

[dw-free] Payment system admin pages

[commit: http://hg.dwscoalition.org/dw-free/rev/ecaed4a16f04]

http://bugs.dwscoalition.org/show_bug.cgi?id=1063

Mostly from [personal profile] sophie, bare framework for viewing orders/carts,
we will be expanding this a lot.

Patch by [staff profile] mark.

Files modified:
  • htdocs/admin/pay/index.bml
  • htdocs/admin/pay/view.bml
--------------------------------------------------------------------------------
diff -r c3af97541795 -r ecaed4a16f04 htdocs/admin/pay/index.bml
--- a/htdocs/admin/pay/index.bml	Fri May 01 05:02:44 2009 +0000
+++ b/htdocs/admin/pay/index.bml	Fri May 01 05:41:45 2009 +0000
@@ -25,7 +25,6 @@
         return "User does not exist.\n"
             unless $u;
 
-        my @actions = ();
         my $ps = DW::Pay::get_paid_status( $u );
         if ( $ps ) {
             $body .= '<h2>Paid Status</h2>';
@@ -33,7 +32,6 @@
 
             if ( $ps->{permanent} ) {
                 $body .= '; <strong>Permanent Status</strong> will never expire.';
-                push @actions, '', 'Use the override section to change account type';
 
             } else {
                 if ( $ps->{expiresin} > 0 ) {
@@ -42,175 +40,68 @@
                     $exp =~ s/ ago//;
                     $body .= "; expires $expt (<strong>$exp</strong>).";
 
-                    if ( $ps->{typeid} == 1 ) {
-                        push @actions, 'set-premium', 'Change type to Premium Paid Account';
-                    } else {
-                        push @actions, 'set-basic', 'Change type to Basic Paid Account';
-                    }
-
-                    push @actions, 'extend-6', 'Extend existing type by 6 months',
-                                   'extend-12', 'Extend existing type by 12 months';
-
                 } else {
                     $body .= '; <strong>expired</strong>.';
 
-                    push @actions, '', 'Use the override section to give user a different account type';
                 }
             }
 
         } else {
             $body .= '<p>User has never had a paid account of any kind.</p>';
 
-            push @actions, '', 'Use the override section to give user a paid account';
         }
 
         $body .= qq|<p><a href="/admin/statushistory.bml?user=$u->{user}">View statushistory for user.</a></p>|;
 
-        $body .= "<h2>Edit Status</h2>";
+        $body .= "<h2>View Carts</h2>";
 
-        $body .= "<ul>";
-        while (my ($type, $name) = (shift @actions, shift @actions)) {
-            last unless $name;
+        my @carts = DW::Shop::Cart->get_all( $u );
 
-            if ( $type ne '' ) {
-                $body .= qq|<li><a href="/admin/pay/index.bml?edit=$u->{user}&act=$type">$name</a></li>|;
-            } else {
-                $body .= "<li>$name</li>";
+        if ( @carts ) {
+            $body .= "<table border='1'>";
+            $body .= "<tr><th>Cart Number</th><th>Date</th><th>Total</th>";
+            $body .= "<th>Payment Method</th><th>Status</th><th>Details</th>";
+            foreach my $cart ( @carts ) {
+                my $state = $cart->state;
+                my $paymentmethod = $cart->paymentmethod;
+                my $paystr = ( $paymentmethod ?
+                                   $ML{"/shop/receipt.bml.cart.paymentmethod.$paymentmethod"} :
+                                   "(not yet selected)" );
+                my $date = DateTime->from_epoch( epoch => $cart->starttime );
+
+                my $detailstext = "Details";
+                $detailstext .= " / Mark as Payment Received" if $state == $DW::Shop::STATE_PEND_PAID;
+
+                $body .= "<tr>";
+                $body .= "<td>" . $cart->id . "</td>";
+                $body .= "<td>" . $date->strftime( "%F %r %Z" ) . "</td>";
+                $body .= "<td>\$" . $cart->total . " USD</td>";
+                $body .= "<td>$paystr</td>";
+                $body .= "<td>$ML{\"/shop/receipt.bml.cart.status.$state\"}</td>";
+                $body .= "<td><a href='$LJ::SITEROOT/admin/pay/view?cartid=" . $cart->id . "'>$detailstext</a></td>";
+                $body .= "</tr>";
             }
+            $body .= "</table>";
         }
-        $body .= "</ul>";
-
-        $body .= "<h2>Hardcore Override</h2><p>These actions will change the paid status with no regard " .
-                 "for the existing paid status.  Their existing status will be thrown away!</p>";
-        $body .= "<ul>";
-
-        @actions = ('override-free', 'Override: reset to free account status',
-                    'override-basic-6', 'Override: 6 months Basic Paid status',
-                    'override-basic-12', 'Override: 12 months Basic Paid status',
-                    'override-basic-99', 'Override: permanent Basic Paid status',
-                    'override-premium-6', 'Override: 6 months Premium Paid status',
-                    'override-premium-12', 'Override: 12 months Premium Paid status',
-                    'override-premium-99', 'Override: permanent Premium Paid status',);
-        while (my ($type, $name) = (shift @actions, shift @actions)) {
-            last unless $type;
-
-            $body .= qq|<li><a href="/admin/pay/index.bml?edit=$u->{user}&act=$type">$name</a></li>|;
+        else {
+            $body .= "<p>This user has not made any orders.</p>";
         }
-
-        $body .= "</ul>";
-
-    } elsif ($GET{edit}) {
-        $body .= '<p>[ <a href="/admin/pay/index.bml">&lt;&lt; Back to Index</a> ]</p>';
-
-        # allow editing a user's paid status
-        my $u = LJ::load_user( $GET{edit} );
-        return "User does not exist.\n"
-            unless $u;
-
-        my $what = $GET{act};
-        return "No action.\n"
-            unless $what;
-
-        unless ( LJ::did_post() ) {
-            $body .= "<h2>CONFIRM</h2><p>You must confirm that you wish to take action <b>$what</b> on " . LJ::ljuser( $u ) . ".</p>";
-            $body .= "<form method='post' action='/admin/pay/index.bml?edit=$u->{user}&act=$what'>";
-            $body .= "<input type='submit' value='Confirmed!'>";
-            $body .= "</form>";
-            return $body;
-        }
-
-        my $note = sub {
-            LJ::statushistory_add( $u, $remote, 'paidstatus', sprintf( shift, @_ ) );
-        };
-
-        my $ps = DW::Pay::get_paid_status( $u );
-
-        if ( $what eq 'override-free' ) {
-            DW::Pay::update_paid_status( $u, permanent => 0, expiretime => 0 );
-            $note->( "Override: reverted to free." );
-        } elsif ( $what eq 'override-basic-6' ) {
-            DW::Pay::update_paid_status( $u, permanent => 0, typeid => 1, _set_months => 6 );
-            $note->( "Override: set to 6 months Basic." );
-        } elsif ( $what eq 'override-basic-12' ) {
-            DW::Pay::update_paid_status( $u, permanent => 0, typeid => 1, _set_months => 12 );
-            $note->( "Override: set to 12 months Basic." );
-        } elsif ( $what eq 'override-basic-99' ) {
-            DW::Pay::update_paid_status( $u, permanent => 1, typeid => 1 );
-            $note->( "Override: set to permanent Basic." );
-        } elsif ( $what eq 'override-premium-6' ) {
-            DW::Pay::update_paid_status( $u, permanent => 0, typeid => 2, _set_months => 6 );
-            $note->( "Override: set to 6 months Premium." );
-        } elsif ( $what eq 'override-premium-12' ) {
-            DW::Pay::update_paid_status( $u, permanent => 0, typeid => 2, _set_months => 12 );
-            $note->( "Override: set to 12 months Premium." );
-        } elsif ( $what eq 'override-premium-99' ) {
-            DW::Pay::update_paid_status( $u, permanent => 1, typeid => 2 );
-            $note->( "Override: set to permanent Premium." );
-        } elsif ( $what eq 'extend-6' ) {
-            DW::Pay::update_paid_status( $u, _add_months => 6 );
-            $note->( "Override: added 6 months to existing type." );
-        } elsif ( $what eq 'extend-12' ) {
-            DW::Pay::update_paid_status( $u, _add_months => 12 );
-            $note->( "Override: added 12 months to existing type." );
-        } else {
-            return "Invalid action.\n";
-        }
-
-        DW::Pay::sync_caps( $u );
-
-        BML::redirect( "$LJ::SITEROOT/admin/pay/index.bml?view=$u->{user}" );
-
-    } elsif ($GET{show} eq 'summary') {
-        $body .= '<p>[ <a href="/admin/pay/index.bml">&lt;&lt; Back to Index</a> ]</p>';
-
-        # show summary of payment information, i.e. how many accounts there are,
-        # of what types, and how many payments have been received broken down on a
-        # daily basis (last week) and monthly basis (forever)
-
-        my $dbr = DW::Pay::get_db_reader();
-
-        my $rows = $dbr->selectall_arrayref( q{
-                SELECT DATE_FORMAT(FROM_UNIXTIME(paydate), '%m-%d'), typeid, duration, COUNT(*), SUM(amount)
-                FROM dw_payments
-                WHERE paydate > UNIX_TIMESTAMP() - 86400*8
-                      AND status = 'paid-completed'
-                GROUP BY 1, 2, 3
-                ORDER BY 1 DESC, 2, 3
-            } );
-
-        $body .= '<h2>Past 7 Days</h2>';
-        $body .= '<table border="1"><tr><td>day</td><td>type</td><td>duration</td><td>sold</td><td>revenue</td></tr>';
-        foreach my $row ( @{ $rows || [] } ) {
-            $body .= "<tr><td>$row->[0]</td><td>" . DW::Pay::type_name( $row->[1] ) . "</td><td>$row->[2]</td>" .
-                     "<td>$row->[3]</td><td>\$$row->[4].00</td></tr>";
-        }
-        $body .= '</table>';
-
-        my $rows = $dbr->selectall_arrayref( q{
-                SELECT DATE_FORMAT(FROM_UNIXTIME(paydate), '%m-%Y'), typeid, duration, COUNT(*), SUM(amount)
-                FROM dw_payments
-                WHERE status = 'paid-completed'
-                GROUP BY 1, 2, 3
-                ORDER BY 1 DESC, 2, 3
-            } );
-
-        $body .= '<h2>All Time</h2>';
-        $body .= '<table border="1"><tr><td>month</td><td>type</td><td>duration</td><td>sold</td><td>revenue</td></tr>';
-        foreach my $row ( @{ $rows || [] } ) {
-            $body .= "<tr><td>$row->[0]</td><td>" . DW::Pay::type_name( $row->[1] ) . "</td><td>$row->[2]</td>" .
-                     "<td>$row->[3]</td><td>\$$row->[4].00</td></tr>";
-        }
-        $body .= '</table>';
 
     } else {
         # print the basic form
         $body .= <<EOF;
 
-<form method="get" action="/admin/pay/index.bml">
-
 <ul>
-    <li><a href="/admin/pay/index.bml?show=summary">Complete payment summary</a></li>
-    <li>Edit user: <input type="text" name="view" /> <input type="submit" value="Go" /></li>
+    <li>Edit user:
+      <form method="get" action="/admin/pay/index">
+      <input type="text" name="view" /> <input type="submit" value="Go" />
+      </form>
+    </li>
+    <li>View cart/order ID:
+      <form method="get" action="/admin/pay/view">
+      <input type="text" name="cartid" /> <input type="submit" value="Go" />
+      </form>
+    </li>
 </ul>
 
 EOF
diff -r c3af97541795 -r ecaed4a16f04 htdocs/admin/pay/view.bml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/admin/pay/view.bml	Fri May 01 05:41:45 2009 +0000
@@ -0,0 +1,109 @@
+<html>
+<head><title>Payment Management</title></head>
+<body>
+
+<?_code
+{
+    use strict;
+    use vars qw(%GET %POST);
+
+    my $remote = LJ::get_remote();
+    my @displayprivs = ( "payments" );
+    my $numprivs = @displayprivs;
+
+    return "<?needlogin?>" unless $remote; 
+    return BML::ml( "admin.noprivserror", { numprivs => $numprivs, needprivs => "<b>" . join(", ", @displayprivs) . "</b>"} )
+        unless LJ::check_priv($remote, 'payments');
+
+    my $body = '<h1>Payment Manager - View Cart</h1>';
+    $body .= '<p>[ <a href="/admin/pay/index.bml">&lt;&lt; Back to Index</a> ]</p>';
+
+    return "Must provide cartid."
+        unless $GET{cartid} && $GET{cartid} > 0;
+
+    my $cartid = $GET{cartid} + 0;
+    my $cart = DW::Shop::Cart->get_from_cartid( $cartid )
+        or return "Sorry, invalid cart/cart not found in the database!";
+
+    $cartid = $cart->id;   # get a normalised value back
+    my $state = $cart->state;
+    my $u = LJ::load_userid( $cart->userid );
+    my $from = ( defined $u ? $u->ljuser_display : "Logged-out user with uniq: " . $cart->uniq );
+
+    my $paymentmethod = $cart->paymentmethod;
+    my $paystr = ( $paymentmethod ?
+                       $ML{"/shop/receipt.bml.cart.paymentmethod.$paymentmethod"} :
+                       "(not yet selected)" );
+
+    $body .= "<h2>Cart #$cartid</h2>";
+    $body .= "<table border='1'>";
+    $body .= "<tr><th>From:</th><td>$from</td></tr>";
+    $body .= "<tr><th>Date:</th><td>" . DateTime->from_epoch( epoch => $cart->starttime )->strftime( "%F %r %Z" ) . "</td></tr>";
+    $body .= "<tr><th>Total:</th><td>\$" . $cart->total . " USD</td></tr>";
+    $body .= "<tr><th>Payment Method:</th><td>$paystr</td></tr>";
+    $body .= "<th>Status:</th><td>$ML{\"/shop/receipt.bml.cart.status.$state\"}</td></tr>";
+    $body .= "</table>";
+
+    $body .= "<h3>Items</h3>";
+    if ( $cart->has_items ) {
+        $body .= LJ::Widget::ShopCart->render( receipt => 1, cart => $cart );
+    }
+    else {
+        $body .= "<p>This cart has no items in it.</p>";
+    }
+
+    # attempt to create an engine so we can get more info
+    my $classname = $DW::Shop::PAYMENTMETHODS{$paymentmethod}->{class};
+    my $engine = eval "DW::Shop::Engine::${classname}->new_from_cart( \$cart )";
+
+    if ( $classname eq 'PayPal' ) {
+        # show the payer's details.
+        $body .= "<h2>Payer Details</h2>";
+        $body .= "<table border='1'>";
+        $body .= "<tr><th>First Name:</th><td>" . $engine->firstname . "</td></tr>";
+        $body .= "<tr><th>Last Name:</th></td>" . $engine->lastname . "</td></tr>";
+        $body .= "<tr><th>PayPal Email Address:</th><td>" . $engine->email . "</td></tr>";
+        $body .= "<tr><th>User Email Address:</th><td>" . $cart->email . "</td></tr>";
+        $body .= "</table>";
+
+    } elsif ( $classname eq 'CheckMoneyOrder' ) {
+        # show the payer's details.
+        $body .= "<h2>Payer Details</h2>";
+        $body .= "<table border='1'>";
+        $body .= "<tr><th>Email Address:</th><td>" . $cart->email . "</td></tr>";
+        $body .= "</table>";
+    }
+
+    if ( $state == $DW::Shop::STATE_PEND_PAID ) {
+        $body .= <<HTML;
+<h2>Mark as Payment Received</h2>
+
+<form method="post" action="/admin/pay/mark-received">
+<p>
+<label for="paymentmethod">Payment method:</label>
+<select id="paymentmethod" name="paymentmethod">
+<option value="cash">Cash</option>
+<option value="check">Check</option>
+<option value="moneyorder">Money Order</option>
+<option value="other">Other</option>
+</select>
+</p>
+
+<p>
+<label for="notes">Payment notes (check no, address, etc):</label><br>
+<textarea rows="5" cols="40" name="notes" id="notes"></textarea>
+</p>
+
+<p>
+<input type="submit" value="Mark as Received">
+</p>
+</form>
+HTML
+    }
+
+    return $body;
+}
+_code?>
+
+</body>
+</html>
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org