Bod has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to update a subscription in Stripe.
This involves calling an API with an authorisation header using POST. If the payload is empty, the API returns a JSON object representing the existing subscription. If there is subscription data in the payload, Stripe attempts to update the subscription and returns the complete subscription object. Pretty straightforward and it all works fine. Until I need to read the existing subscription object and then update it.
I have hit a problem and I can't think of how to debug it further!
Here is the minimum code to demonstrate the problem:
With the code as it is, I get an error from Stripe that I have not supplied an API key. The key is in the $headers variable. If I take out the first call to Stripe, the one with the empty payload, then the second one succeeds * so the API key is working fine in this case. But as soon as I make two calls, it fails.#!/usr/bin/perl -T use CGI::Carp qw(fatalsToBrowser); use FindBin qw($RealBin); my $safepath; BEGIN { if ($RealBin =~ m!^(/home/...path.../(test|uk)/www)!) { $safepath = "$1/../lib"; } else { die "Illegal use of software - visit www.way-finder.uk to use +this site"; } } use lib "$safepath"; use Site::Variables; use HTTP::Tiny; use JSON; use Data::Dumper; use strict; use warnings; my $http = HTTP::Tiny->new; my $headers = { 'headers' => { 'Authorization' => 'Bearer ' . $Site::Variables::stripe_secret +, }, 'agent' => 'Wayfinder/v3.0', }; my $sub_id = 'sub_xxxxxxxxxxx'; # This line is the culprit... my $res = $http->post_form("https://api.stripe.com/v1/subscriptions/$s +ub_id", {}, $headers); my $payload = decode_json($res->{'content'}); my $subscription = { 'items[0][id]' => 'x', 'items[0][price]' => 'some price', }; my $response = $http->post_form("https://api.stripe.com/v1/subscriptio +ns/$sub_id", $subscription, $headers); print "Content-type: text/plain\n\n"; print Dumper $response;
Things I've tried but haven't helped:
- Turning off taint mode
- Creating two instances of HTTP::Tiny and making each call with a different instance
- Creating a copy of $headers to use in the second call
- Adding a 5 second delay between calls to Stripe
It is as if HTTP::Tiny doesn't like making consecutive POSTs but I cannot find anything in the documentation about this.
* - Without the first call, the second call to Stripe gives an error because I haven't got the parameters right. But it doesn't complain about there not not being an API key
|
---|