package Lacuna::Web::Pay; use Moose; use utf8; no warnings qw(uninitialized); extends qw(Lacuna::Web); use Digest::MD5 qw(md5_hex); use Digest::HMAC_SHA1; use XML::Hash::LX; use Tie::IxHash; use LWP::UserAgent; sub www_default { my ($self, $request) = @_; my $session_id = $request->param('session_id'); my $session = $self->get_session($session_id); unless (defined $session) { confess [ 401, 'You must be logged in to purchase essentia.']; } if ($session->is_sitter) { confess [ 401, 'Sitters cannot purchase essentia.']; } my $empire = $session->empire; unless (defined $empire) { confess [401, 'Empire not found.']; } return [ $self->buy_currency_url($empire->id), { status => 302 } ]; #return $self->wrap('
'); } sub wrap { my ($self, $content) = @_; return $self->wrapper($content, { title => 'Purchase Essentia', logo => 1 }); } sub buy_currency_url { my ($self, $user_id) = @_; my $config = Lacuna->config->get(); return $config->{server_url}.'pay/buy/currency?user_id='.$user_id; } sub www_buy_currency { my ($self, $request) = @_; my $user_id = $request->param('user_id'); my $content = < function update_currency(el) { var text = el.options[el.selectedIndex].text; var value = text.split(" "); var newvalue = value[0]; var form = document.getElementById('cc_form'); var currency = form.premium_currency_amount; currency.value = newvalue; } function close_current() { try { window.setTimeout( function () { window.close() }, 10000); } catch (e) {}; }

Get More Essentia

Get More Essentia
Personal Information
Credit Card Information
EoHTML return $self->wrapper($content); } sub www_buy_currency_cc { my ($self, $request) = @_; confess [1009, 'Card number is required.'] unless $request->param('card_number'); confess [1009, 'Expiration month is required and must be 2 digits.'] unless ($request->param('expiration_month') && length($request->param('expiration_month')) == 2); confess [1009, 'Expiration year is required and must be 4 digits.'] unless ($request->param('expiration_year') && length($request->param('expiration_year')) == 4); confess [1009, 'CVV2 is required and must be 3 or 4 digits.'] unless ($request->param('cvv2') && length($request->param('cvv2')) >= 3 && length($request->param('cvv2')) <= 4); my $config = Lacuna->config->get('itransact'); my @name = split /\s+/, $request->param('name'); my %payload; tie %payload, 'Tie::IxHash'; %payload = ( AuthTransaction => { CustomerData => { Email => $request->param('email'), BillingAddress => { Address1 => $request->param('address1'), FirstName => shift @name, LastName => join(' ', @name), City => $request->param('city'), State => $request->param('state'), Zip => $request->param('postal_code'), Country => $request->param('country'), Phone => $request->param('phone_number'), }, CustId => $request->param('user_id'), }, Total => $request->param('total'), Description => 'Essentia Order', AccountInfo => { CardAccount => { AccountNumber => $request->param('card_number'), ExpirationMonth => $request->param('expiration_month'), ExpirationYear => $request->param('expiration_year'), CVVNumber => $request->param('cvv2'), }, }, TransactionControl => { SendCustomerEmail => $config->{SendCustomerEmail}, SendMerchantEmail => $config->{SendMerchantEmail}, TestMode => $config->{TestMode}, }, }, ); $payload{AuthTransaction}{CustomerData}{BillingAddress}{Address2} = $request->param('address2') if ( $request->param('address2') ); my $xml = hash2xml \%payload; $xml = (split(/\n/, $xml))[1]; # strip the tag before calculating the PayloadSignature my $hmac = Digest::HMAC_SHA1->new($config->{APIKey}); $hmac->add($xml); my %xml = ( GatewayInterface => { APICredentials => { Username => $config->{APIUsername}, TargetGateway => $config->{Gateway}, PayloadSignature => $hmac->b64digest . '=', }, %payload, } ); $xml = hash2xml \%xml; my $response = LWP::UserAgent->new->post( 'https://secure.itransact.com/cgi-bin/rc/xmltrans2.cgi', Content_Type => 'text/xml', Content => $xml, Accept => 'text/xml', ); if ($response->is_success) { my $result = xml2hash $response->decoded_content; $result = $result->{GatewayInterface}{TransactionResponse}{TransactionResult}; if($result->{Status} eq 'ok') { # get user account my $empire_id = $request->param('user_id'); unless ($empire_id) { return ['Not a valid user_id.', { status => 401 }]; } my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->find($empire_id); unless (defined $empire) { return ['Empire not found.', { status => 404 }]; } # make sure we haven't already processed this transaction my $transaction_id = $result->{AuthCode}; unless ($transaction_id) { return ['Not a valid iTransact AuthCode.', { status => 402 }]; } my $transaction = Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->search( { transaction_id => $transaction_id } )->first; if (defined $transaction) { return ['Already processed this transaction.', { status => 200 }]; } # add essentia and alert user my $amount = $request->param('premium_currency_amount'); $empire->add_essentia({ amount => $amount, reason => 'Purchased via iTransact', type => 'paid', transaction_id => $transaction_id, }); $empire->update; $empire->send_predefined_message( tags => ['Alert'], filename => 'purchase_essentia.txt', params => [$amount, $transaction_id], ); $self->_run_promotions({ amount => $amount, empire => $empire, cost => $request->param('total'), type => 'iTransact', transaction_id => $transaction_id }); my $script = " try { window.opener.YAHOO.lacuna.Essentia.paymentFinished(); window.setTimeout( function () { window.close() }, 5000); } catch (e) {} "; return $self->wrap('Thank you! The essentia will be added to your account momentarily.'); } else { return $self->wrap('Card was rejected: '.$result->{XID}); } } else { return $self->wrap('Could not connect to the credit card processor.'); } } sub _run_promotions { my ($self, $opts) = @_; my $rs = Lacuna->db->resultset("Promotion")->current_promotions_rs; while (my $promo = $rs->next()) { $promo->essentia_purchased($opts); } } no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0);