Business-PinPayment

 view release on metacpan or  search on metacpan

lib/Business/PinPayment.pm  view on Meta::CPAN

package Business::PinPayment;
use strict;
use warnings;
use Net::SSL;
use HTTP::Request;
use LWP::UserAgent;
use JSON;

our $VERSION = '0.04';

# build 4.1

sub new {
  my ($class, %args) = (@_);
  my $self = bless {}, $class;
  $args{config} ||= {};
  
  $self->{config} = {
    api_version => '1', # the '1' in the API endpoint host names, e.g. https://test-api.pin.net.au/1/charges
    api_key => undef, # Secret API Key
    api => 'charges', # 'customers', 'refunds'
    amount => '100', # 100 cents. Must be greater or equal to $1.00
    currency => 'AUD', # 'USD', 'NZD', or 'SGD'
    description => 'charges',
    email => 'tester@work.com.au',
    ip_address => undef,
    charge_token => undef, # for refunds API
    card_token => undef,
    customer_token => undef,
    card => {
      number => '5520000000000000',
      expiry_month => '05',
      expiry_year => '2014',
      cvc => '123',
      name => 'John Smith',
      address_line1 => '1 Test St',
      address_line2 => undef,
      address_city => 'Sydney',
      address_postcode => '2000',
      address_state => 'NSW',
      address_country => 'Australia'
    }
  };

  foreach my $key (qw(api_key api amount currency description email ip_address charge_token card_token customer_token)) {
    next unless defined $args{config}->{$key};
    $self->{config}->{$key} = $args{config}->{$key};
  }

  if ($self->{config}->{card_token} || $self->{config}->{customer_token}) {
    delete $self->{config}->{card};
  }
  else {
    foreach my $key (qw(number expiry_month expiry_year cvc name address_line1 address_line2 address_city address_postcode address_state address_country)) {
      next unless defined $args{config}->{card}->{$key};
      $self->{config}->{card}->{$key} = $args{config}->{card}->{$key};
    }
  }

  my $url;
  my $live = $args{live};

  my $api = delete ($self->{config}->{api});
  my $api_version = delete ($self->{config}->{api_version});
  my $api_key = delete ($self->{config}->{api_key});

  unless ($api_key) {
    $self->{error} = 'Missing Secret API Key';
    return $self;
  }

  if ($live) {
    $url = 'https://api.pin.net.au/' . $api_version;
  }
  else {
    $url = 'https://test-api.pin.net.au/' . $api_version
  }

  if ($api eq 'refunds' && $self->{config}->{charge_token}) {

lib/Business/PinPayment.pm  view on Meta::CPAN


__END__

=head1 NAME

Business::PinPayment - Interface for Pin Payment API

=head1 SYNOPSIS

  use Business::PinPayment;

  # Run a test charge of $1.00
  my $test = Business::PinPayment->new(config => {api_key => 'T3sTS3cret-ap1key'});
  if ($test->successful()) {
    print 'Test Successful';
  }
  else {
    print $test->error();
  }

  # Run a live one
  my $live = Business::PinPayment->new(
    live => 1,
    config => {
      api_key => 'L1veS3cret-ap1key',
      amount => '100',
      currency => 'AUD',
      description => 'charges',
      email => 'tester@work.com.au',
      card => {
        number => '5520000000000000',
        expiry_month => '05',
        expiry_year => '2014',
        cvc => '123',
        name => 'John Smith',
        address_line1 => '1 Test St',
        address_city => 'Sydney',
        address_postcode => '2000',
        address_state => 'NSW',
        address_country => 'Australia'
      }
    }
  );

=head1 DESCRIPTION

An interface to the Pin Payment API.

=head1 METHODS

=head2 C<new>

Instantiates a new PinPayment object. By default, it runs a test transaction with the given API key.

=over

=item C<config>

A hashref of parameters accepted by the PinPayment API . See L<https://pin.net.au/docs/api>. Default values:

  api_version => '1', # the '1' in the API endpoint host names, e.g. https://test-api.pin.net.au/1/charges
  api_key => undef, # Secret API Key
  api => 'charges', # 'customers', 'refunds'
  amount => '100', # 100 cents. Must be greater or equal to $1.00
  currency => 'AUD', # 'USD', 'NZD', or 'SGD'
  description => 'charges',
  email => 'tester@work.com.au',
  ip_address => undef,
  charge_token => undef, # for refunds API
  card_token => undef,
  customer_token => undef,
  card => {
    number => '5520000000000000',
    expiry_month => '05',
    expiry_year => '2014',
    cvc => '123',
    name => 'John Smith',
    address_line1 => '1 Test St',
    address_line2 => undef,
    address_city => 'Sydney',
    address_postcode => '2000',
    address_state => 'NSW',
    address_country => 'Australia',
  }


The following script performs a test charge and then refunds it. It then creates a test customer, charges the created customer via the customer token (ID) and card token.

  # You may need to disable SSL host name verification for testing
  $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

  my $test_api_key = 'L1veS3cret-ap1key';

  # Test charge for $1.00
  my $charge = Business::PinPayment->new(
    config => {
      api_key => $test_api_key,
      card => {
        number => '5520000000000000',
        expiry_month => '05',
        expiry_year => '2014',
        cvc => '123',
        name => 'John Smith',
        address_line1 => '1 Test St',
        address_city => 'Sydney',
        address_postcode => '2000',
        address_state => 'NSW',
        address_country => 'Australia'
      }
    }
  );

  if ($charge->successful()) {
    print 'Charge Token: ' . $charge->id() . "\n";

    # Refund the charge
    my $refund = Business::PinPayment->new(
      config => {
        api => 'refunds',
        api_key => $test_api_key,
        charge_token => $charge->id()



( run in 0.339 second using v1.01-cache-2.11-cpan-9581c071862 )