# ==================================================================
# Gossamer Links - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,071,086,086,085      
#   Revision : $Id: 2CheckOut.pm,v 1.13 2006/08/22 23:07:53 brewt Exp $
# 
# Copyright (c) 2003 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited.  Please
# see LICENSE file for full details.
# ==================================================================
#
# Glue between Gossamer Links and 2CheckOut payment interface

package Links::Payment::Remote::2CheckOut;
use strict;

# Make sure the payment module is available
use GT::Payment::Remote::2CheckOut;
use Links qw/:objects/;
use Links::Payment qw/:status :log/;
use Links::SiteHTML;
use vars qw/%INVALID %EMPTY/;

sub required {
# -----------------------------------------------------------------------------
# Returns a list of required field names.  Each field name will be looked for
# in the language file, prefixed with 'PAYMENT_REMOTE_2CheckOut_', for the
# title of the field, and 'PAYMENT_REMOTE_DESC_2CheckOut_' for a description of
# the field's contents.
# Note that these are just required SETUP fields, so things like credit card
# number, billing name, etc. are NOT included.

    return
        seller_id => { type => 'TEXT', valid => '^\d{1,10}$' },
        secret_word => { type => 'TEXT', valid => '^(?!tango$).+$' };
}

sub optional {
# -----------------------------------------------------------------------------
    return
        demo => { type => 'YESNO' };
}

sub payment_info {
# -----------------------------------------------------------------------------
# Returns a hashref of payment hints
#
    my @fields = qw/seller_id secret_word demo/;
    my $ret = {
        fields => \@fields
    };
    if (my $info = $CFG->{payment}->{remote}->{used}->{'2CheckOut'}) {
        for (@fields) {
            $ret->{$_} = $info->{$_};
        }
    }
    return $ret;
}

sub verify {
# -----------------------------------------------------------------------------
# Checks that $IN, combined with the saved admin settings, makes up all of the
# required information.  Returns 1 on success, or an array ref of invalid keys
# on failure.  For Remote payment methods, this has no real effect.
    return 1;
}

sub postback {
# -----------------------------------------------------------------------------

    my $pay = $DB->table('Payments');
    my $log = $DB->table('PaymentLogs');

    my $unique = $IN->param('cart_order_id');
    my $payment = $pay->select({ payments_id => $unique })->fetchrow_hashref
        or return; # Whatever it is, we didn't create it.

    GT::Payment::Remote::2CheckOut::process(
        param => $IN,
        sellerid => $CFG->{payment}->{remote}->{used}->{'2CheckOut'}->{seller_id},
        password => $CFG->{payment}->{remote}->{used}->{'2CheckOut'}->{secret_word},
        demo => $CFG->{payment}->{remote}->{used}->{'2CheckOut'}->{demo},
        on_valid => sub {
            return unless $IN->param('total') >= $payment->{payments_amount};

            return if $payment->{payments_status} == COMPLETED;

            my $cond = GT::SQL::Condition->new();
            $cond->add(paylogs_payments_id => '=' => $unique);
            $cond->add(paylogs_type => '=' => LOG_ACCEPTED);
            $cond->add(paylogs_text => LIKE => "%\n2CheckOut order number: " . $IN->param('order_number') . "%\n");
            my $found = $log->count($cond);
            return if $found;

            $pay->update(
                { payments_status => COMPLETED, payments_last => time },
                { payments_id => $payment->{payments_id} }
            );

            $log->insert({
                paylogs_payments_id => $payment->{payments_id},
                paylogs_type => LOG_ACCEPTED,
                paylogs_time => time,
                paylogs_text => (
                    sprintf(Links::language('PAYMENT_REMOTE_APPROVED') => '2CheckOut') . "\n" .
                    "2CheckOut order number: " . $IN->param('order_number') . "\n" .
                    "Amount: $payment->{payments_amount}\n"
                )
            });

            Links::Payment::process_payment($payment->{payments_linkid}, $payment->{payments_term});
        }
    );

    print $IN->header;
    print Links::SiteHTML::display('payment_success');
    1;
}

1;
