Business-GoCardless

 view release on metacpan or  search on metacpan

lib/Business/GoCardless/Payment.pm  view on Meta::CPAN

    amount_refunded
    charge_date
    created_at
    currency
    description
    faster_ach
    fx
    id
    links
    metadata
    reference
    retry_if_possible
    scheme
    status
/ ] => (
    is => 'rw',
);

=head1 Operations on a payment

    retry
    cancel
    update
    refund

    $Payment->retry if $Payment->failed;

retry and cancel optionally take metadata:

    $Payment->cancel( metadata => { ... } );

=head2 update

Update the payment's metadata and/or retry_if_possible, returns the updated
Payment object:

    $Payment->update( metadata => { ... },retry_if_possible => 1 );

=head2 refund

Create a refund against the payment, returns a L<Business::GoCardless::Refund>
object. Note that refunds are restricted by default on GoCardless accounts and
must be enabled by GoCardless support before this will work.

    my $Refund = $Payment->refund(
        amount                    => 100,           # required
        total_amount_confirmation => 100,           # defaults to amount_refunded + amount
        reference                 => 'REFUND001',   # optional
        metadata                  => { ... },       # optional
    );

total_amount_confirmation is a safety check used by GoCardless to prevent
accidental duplicate refunds - it is the total amount refunded on the payment
once this refund is applied. If the payment object is stale (i.e. another refund
has since been made) then GoCardless will reject the refund.

=cut

sub retry  { shift->_action( 'retry',@_ ); }
sub cancel { shift->_action( 'cancel',@_ ); }
sub update { shift->_update( @_ ); }

sub refund {
    my ( $self,%params ) = @_;

    # no default for the amount, we don't want to refund anything by accident
    Business::GoCardless::Exception->throw({
        message => "amount is required for ->refund",
    }) if ! defined( $params{amount} );

    my $already_refunded = $self->amount_refunded // 0;
    my $amount = $params{amount};

    my $data = $self->client->api_post(
        '/refunds',
        {
            refunds => {
                %params,
                amount                    => $amount,
                total_amount_confirmation => $params{total_amount_confirmation}
                    // ( $already_refunded + $amount ),
                links                     => {
                    %{ $params{links} // {} },
                    payment => $self->id,
                },
            },
        },
    );

    return Business::GoCardless::Refund->new(
        client => $self->client,
        %{ $data->{refunds} },
    );
}

=head1 Status checks on a payment

    pending
    paid
    failed
    chargedback
    cancelled
    withdrawn
    refunded
    submitted
    confirmed

    if ( $Payment->failed ) {
        ...
    }

=cut

sub pending     { return shift->status =~ /pending/ }
sub paid        { return shift->status eq 'paid_out' }
sub failed      { return shift->status eq 'failed' }
sub chargedback { return shift->status eq 'charged_back' }
sub cancelled   { return shift->status eq 'cancelled' }
sub withdrawn   { return shift->status eq 'customer_approval_denied' }
sub refunded    { return shift->status eq 'refunded' }
sub submitted   { return shift->status eq 'submitted' }



( run in 0.656 second using v1.01-cache-2.11-cpan-145b269f2f3 )