[interchange-cvs] interchange - kwalsh modified lib/Vend/Payment/TestPayment.pm

interchange-core@icdevgroup.org interchange-core@icdevgroup.org
Sun Sep 8 11:41:01 2002


User:      kwalsh
Date:      2002-09-08 15:40:08 GMT
Added:     lib/Vend/Payment TestPayment.pm
Log:
	* The Vend::Payment::TestPayment module allows developers to test
	  their site's interaction with the Interchange Payment system,
	  without actually being connected to a real Payment Services
	  Provider.

	  In theory, once the site's payment interaction is set up and
	  working to the developer's satisfaction, the test Payment module
	  can be transparently swapped for a real one.

	  The module will perform one of three actions:

		- If the card number is 4111111111111111 then the
		  transaction will be approved.

		- If the card number is 4111111111111129 then the
		  transaction will be declined.

		- Any other card number will raise an error and the
		  transaction will be declined.

	  More test cases can be added later, depending upon what other
	  developers need.

Revision  Changes    Path
1.1                  interchange/lib/Vend/Payment/TestPayment.pm


rev 1.1, prev_rev 1.0
Index: TestPayment.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
# Vend::Payment::TestPayment - Interchange payment test module
#
# $Id: TestPayment.pm,v 1.1 2002/09/08 15:40:07 kwalsh Exp $
#
# Copyright (C) 2002 Cursor Software Limited.
# All Rights Reserved.
#
# Author: Kevin Walsh <kevin@cursor.biz>
# Based on original code by Mike Heins <mheins@perusion.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.
#
package Vend::Payment::TestPayment;

=3Dhead1 Interchange payment test module

Vend::Payment::TestPayment $Revision: 1.1 $

=3Dhead1 SYNOPSIS

    &charge=3Dtestpayment
=20
	or
=20
    [charge mode=3Dtestpayment param1=3Dvalue1 param2=3Dvalue2]

=3Dhead1 PREREQUISITES

None.

=3Dhead1 DESCRIPTION

The Vend::Payment::TestPayment module implements the testpayment() routine
for use with Interchange.  It's compatible on a call level with the other
Interchange payment modules -- in theory (and even usually in practice) you
could switch from TestPayment to another payment module with a few
configuration file changes.

The module will perform one of three actions:

=3Dover 4

=3Ditem *

If the card number is 4111111111111111 then the transaction will be approve=
d.

=3Ditem *

If the card number is 4111111111111129 then the transaction will be decline=
d.

=3Ditem *

Any other card number will raise an error and the transaction will be decli=
ned.

=3Dback

To enable this module, place this directive in C<interchange.cfg>:

    Require module Vend::Payment::TestPayment

This I<must> be in interchange.cfg or a file included from it.

Make sure CreditCardAuto is off (default in Interchange demos).

The mode can be named anything, but the C<gateway> parameter must be set
to C<testpayment>.  To make it the default payment gateway for all credit
card transactions in a specific catalog, you can set in C<catalog.cfg>:

    Variable   MV_PAYMENT_MODE  testpayment

It uses several of the standard settings from Interchange payment.  Any time
we speak of a setting, it is obtained either first from the tag/call option=
s,
then from an Interchange order Route named for the mode, then finally a
default global payment variable, For example, the C<id> parameter would
be specified by:

    [charge mode=3Dtestpayment id=3Dtestid]

or

    Route testpayment id testid

or=20

    Variable MV_PAYMENT_ID      testid

The active settings are:

=3Dover 4

=3Ditem id

A test account ID, which can be any value you like.
Global parameter is MV_PAYMENT_ID.

=3Ditem secret

A test account password, which can be any value you like.
Global parameter is MV_PAYMENT_SECRET.  This is not needed for test
charges, using this module, but you may as well set it up anyway.

=3Ditem transaction

The type of transaction to be run.  Valid values are:

    auth
    return
    reverse
    sale
    settle
    void

Actually, the transaction type is ignored in this version, but you may as
well set it anyway.

=3Ditem remap=20

This remaps the form variable names to the ones needed by TestPayment.  See
the C<Payment Settings> heading in the Interchange documentation for use.

=3Dback

=3Dhead2 Troubleshooting

=3Dover 4

=3Ditem *

Make sure you "Require"d the module in interchange.cfg:

    Require module Vend::Payment::TestPayment

=3Ditem *

Check the error logs, both catalog and global.

=3Ditem *

Make sure you set your payment parameters properly.=20=20

=3Ditem *

Try an order, then put this code in a page:

    <XMP>
    [calc]
        my $string =3D $Tag->uneval( { ref =3D> $Session->{payment_result} =
});
        $string =3D~ s/{/{\n/;
        $string =3D~ s/,/,\n/g;
        return $string;
    [/calc]
    </XMP>

That should show what happened.

=3Ditem *

If all else fails, Cursor Software and other consultants are available to h=
elp
with integration for a fee.

=3Dback

=3Dhead1 BUGS

There is actually nothing *in* Vend::Payment::TestPayment.  It changes pack=
ages
to Vend::Payment and places things there.

=3Dhead1 AUTHORS

Kevin Walsh <kevin@cursor.biz>
Based on original code by Mike Heins <mheins@perusion.com>

=3Dcut

BEGIN {
    ::logGlobal("%s payment module loaded",__PACKAGE__)
	unless $Vend::Quiet;
}

$VERSION =3D substr(q$Revision: 1.1 $,10);

package Vend::Payment;

sub testpayment {
    my ($user,$amount) =3D @_;

    my $opt;
    if (ref $user){
	$opt =3D $user;
	$user =3D $opt->{id} || undef;
	$secret =3D $opt->{secret} || undef;
    }
    else{
	$opt =3D {};
    }
=09
    my $actual;
    if ($opt->{actual}){
	$actual =3D $opt->{actual};
    }
    else{
	my (%actual) =3D map_actual();
	$actual =3D \%actual;
    }

#::logDebug("actual map result: " . ::uneval($actual));
    unless ($user){
	$user =3D charge_param('id') or return (
	    MStatus =3D> 'failure-hard',
	    MErrMsg =3D> errmsg('No account id'),
	);
    }
=09
    $secret ||=3D charge_param('secret');

    my $precision =3D $opt->{precision} || 2;

    $actual->{mv_credit_card_exp_month} =3D~ s/\D//g;
    $actual->{mv_credit_card_exp_year} =3D~ s/\D//g;
    $actual->{mv_credit_card_number} =3D~ s/\D//g;

    my $exp =3D sprintf('%02d%02d',
	$actual->{mv_credit_card_exp_month},
	$actual->{mv_credit_card_exp_year},
    );

    $opt->{transaction} ||=3D 'sale';

    $amount =3D $opt->{total_cost} if $opt->{total_cost};
    unless ($amount){
        $amount =3D Vend::Interpolate::total_cost();
        $amount =3D Vend::Util::round_to_frac_digits($amount,$precision);
    }

    $order_id =3D gen_order_id($opt);

    my %result;

    if ($actual->{mv_credit_card_number} eq '4111111111111111'){
    	$result{'pop.status'} =3D 'success';
	$result{'pop.order-id'} =3D $opt->{order_id};
	$result{'pop.auth-code'} =3D 'test_auth_code';
    }
    elsif ($actual->{mv_credit_card_number} eq '4111111111111129'){
    	$result{'pop.status'} =3D 'failure';
	my $msg =3D $opt->{message_declined} ||
	    'TestPayment error: %s.  Please call in your order or try again.';
	$result{'pop.error-message'} =3D errmsg($msg,'Payment declined by the card=
 issuer');
    }
    else{
    	$result{'pop.status'} =3D 'failure';
	delete $result{'pop.order-id'};
	delete $result{'order-id'};
	my $msg =3D $opt->{message_declined} ||
	    'TestPayment error: %s.  Please call in your order or try again.';
	$result{'pop.error-message'} =3D errmsg($msg,'Invalid test card number');
    }

    $result{MStatus} =3D $result{'pop.status'};
    $result{MErrMsg} =3D $result{'pop.error-message'} if $result{'pop.error=
-message'};
    $result{'order-id'} =3D $result{'pop.order-id'} if $result{'pop.order-i=
d'};

    return %result;
}

package Vend::Payment::TestPayment;

1;