Business-Stripe

 view release on metacpan or  search on metacpan

Stripe.pm  view on Meta::CPAN

     source         => 'tok_5EuIyKyCTc0f2V',
     description    => 'Ice cream'
 ) and return $stripe->success;

 say $stripe->error->{message};

=head1 DESCRIPTION

This module provides common bindings for the Stripe payment system.
Any API calls that do not have bindings can be accessed through the
generic C<api> method.

=head2 General Methods

=head3 new (I<%options>)

Creates a new Business::Stripe object for you to use. The only
B<< required argument >> is C<-api_key>, which was given to you
as part of your Stripe account to access the API.

Other (optional) arguments are:

=over 4

=item C<-version> Sets a Stripe API version to use, overriding your
account's default. You can use this to test if new versions of
the API work with your code. To support marketplaces, for instance, you
should use at least C<'2014-11-05'>.

=item C<-ua_args> Hashref of options that will be passed directly as
arguments to LWP::UserAgent. Example:

    my $stripe = Business::Stripe->new(
        -api_key => 'xxxxxxxxx',
        -ua_args => {
            timeout   => 10,
            env_proxy => 1,
            agent     => 'myApp',
            ssl_opts  => { verify_hostname => 0 },
        },
    );

=item C<-ua> Completely overrides the default user agent object (L<LWP::UserAgent>).
Note that your object I<must> accept HTTPS, and provide a C<request()> method
accepting L<HTTP::Request> objects and returning L<HTTP::Response>-compatible
objects. You can use this to have a common user agent make all requests in
your code. The example above works exactly like:

    my $ua = LWP::UserAgent->new(
        timeout   => 10,
        env_proxy => 1,
        agent     => 'myApp',
        ssl_opts  => { verify_hostname => 0 },
    );

    my $stripe = Business::Stripe->new(
        -api_key => 'xxxxxxxx',
        -ua      => $ua,
    );

=item C<-url> Overrides the default API endpoint (C<https://api.stripe.com/v1/>)

=item C<-stripe_account> If you use the
L<< OAauth authentication flow for managed accounts|https://stripe.com/docs/connect/authentication >>
You can use this argument to make operations on behalf of a managed account.

=back

=cut

sub new {
    my $class    = shift;
    my $self     = { @_ };

    bless $self, $class;
    $self->_init;
    return $self;
}

=head3 api (I<$method>, I<$path>, I<%params>)

Generic function that sends requests to Stripe.
Check the L<< Stripe API Reference|https://stripe.com/docs/api >>
for specific calls.

The first argument is the HTTP method: C<"post">, C<"get"> or C<"delete">.

The second is the target path, like "tokens", "plans", "customers"
or even complex paths like "customers/$id/subscriptions". Check the
Stripe API Reference for a list of all available paths.

Use the optional third argument to send a hash of data with your API call.
This is usually required on all C<"post"> calls to the API.

On success, it returns a true value. If the returned data structure contains
an C<id> field, this is the value returned. Otherwise, "1" is returned and
you should check L<< $stripe->success() | /success >> for the actual data
structure.

In case of failures, it returns false (0) and you should then check
L<< $stripe->error() | /error >> for the appropriate data structure.

Examples:

=over 4

=item get a credit card source token on the server side (without using Stripe.js)

    my $token_id = $stripe->api('post', 'tokens',
        'card[number]'    => '4242424242424242',
        'card[exp_month]' => 12,
        'card[exp_year]'  => 2022,
        'card[cvc]'       => 123
    ) or die $stripe->error->{message};

=item create a new customer (with the $token_id from above)

    my $customer = $stripe->api('post', 'customers',
        email       => 'myuser@example.com',
        name        => 'Jane S. Customer',
        description => 'Displayed alongside the customer on your dashboard',



( run in 1.109 second using v1.01-cache-2.11-cpan-0b5f733616e )