1
votes

I am trying to set up a Perl script that will process credit card charges through Stripe using Net::Stripe.

I am able to successfully request a token, but when I use that token to post the charge, I get this error:

Error: invalid_request_error - Invalid string: {"exp_month"=>"12", "exp_year"=>"2021", "object"=>"card"} at Stripe.pm line 637

Here is my code

$API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

my $stripe = Net::Stripe->new(
    api_key       => $API_KEY,
    debug         => '1',
    debug_network => '1'
);

my $card_token = $stripe->post_token(
    card => {
        number    => $FORM{'Credit Card #'},
        exp_month => $FORM{'Expiration Month'},
        exp_year  => $FORM{'Expiration Year'},
        cvc       => $FORM{'Credit Card Security Code'}
    }
);

print Dumper( $card_token );

try {
    my $charge = $stripe->post_charge(
        amount      => $amount_of_charge_in_cents,
        currency    => 'usd',
        card        => $card_token,
        description => 'ABC Charge',
        customer    => $FORM{'Name'}
    );
}
catch {
    warn "caught error: $_";    # not $@
}

Should I be using something other than $card_token as the card value?

2
Is the token an object or does it contain an id? Ultimately, to make a charge, you only care about the id (eg. tok_xxx).korben

2 Answers

0
votes

The documentation for the Stripe API call Create a card token (which is wrapped by post_token) says that the following fields are required

exp_month
exp_year
number
currency
cvc

But you are supplying only four of those

0
votes

Thanks to korben I successfully modified my code to work by using just the returned token id:

    $API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

my $stripe = Net::Stripe->new(
    api_key       => $API_KEY,
    debug         => '1',
    debug_network => '1'
);

my $card_token = $stripe->post_token(
    card => {
        number    => $FORM{'Credit Card #'},
        exp_month => $FORM{'Expiration Month'},
        exp_year  => $FORM{'Expiration Year'},
        cvc       => $FORM{'Credit Card Security Code'},
        name      => $FORM{'Name on Card'},
    }
);
print Dumper($card_token);

$token_id =  $card_token->{id};

try {
    my $charge = $stripe->post_charge(
         amount      => $amount_of_charge_in_cents,
         currency    => 'usd',
         card    => $token_id,
         description => 'ABC Charge'
    );
} catch {
    warn "caught error: $_"; # not $@
}

I also removed the "customer" value, as that is the ID of an existing customer, and added "name" to the card token code.