Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors When Making a Stripe Charge using Custom cURL and PHP Code

After doing 3 days of solid research and trying my absoloute best to resolve the issue in hand, Ive become to the point of no success and have had to realise maybe this is something i should have not tried to attempt! On Failing with my own personal attempts i have had to turn to you experts in need of some advice and help along my beginner learning stage.

I have tried to use the PHP code supplied by stripe however due to the complex custom form we use in our website and also alot of our website traffic comes internally we need a way to send payments to stripe easiser than integrating a whole new php payment process that can handle the payments from internal clients as well so i thought i knew a little cURL due to using several cURL and PHP API Calls already within the website process.

The cURL i have tried for stripe is as follows:


    $headers = array(
    'Authorization: Bearer sk_test_my_test_key_here',
);
$ch = curl_init("https://api.stripe.com/v1/charges -H Content-Type=application/x-www-form-urlencoded -u sk_test_my_test_key: -d source[object]=card -d source[number]=4242424242424242 -d source[exp_month]=08 -d source[exp_year]=18 -d source[cvc]=123 -d amount=250 -d currency=gbp -d description=Testing ");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
curl_close($ch);

{ "error": { "type": "invalid_request_error", "message": "Unrecognized request URL (POST: /v1/charges%20-H%20Content-Type=application/x-www-form-urlencoded%20-u%20sk_test_keyP:%20-d%20source[object]=card%20-d%20source[number]=4242424242424242%20-d%20source[exp_month]=08%20-d%20source[exp_year]=18%20-d%20source[cvc]=123%20-d%20amount=250%20-d%20currency=gbp%20-d%20description=Testing). Please see https://stripe.com/docs or we can help at https://support.stripe.com/." } }


I have tried to read up on the documentation, but nothing explains about the errors i get, I think the biggest part of the problem is im not experienced enough to know exactly what to research and the key-phrases experts like you would know to look up.


i have tried with and without the -H Content-Type includedand still get the same issue however if i was to copy and paste the curl into the command line and execute it from a command using

curl.exe https://...................

then the message is successful and i get the response i want back to say the card has been charged.

also tried  `$string = rawurlencode($data) and then http_build_query` on the $string

Here is enough example i have tried:

<?php
$headers = array(
    'Authorization: Bearer sk_test_removed_for_stackoverflow',
);

// create curl resource 
$ch = curl_init(); 

// set url 
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/charges -u sk_test_removed: -d source[object]=card -d source[number]=4242424242424242 -d source[exp_month]=08 -d source[exp_year]=18 -d source[cvc]=123 -d amount=250 -d currency=gbp -d description=Test "); 

also tried it like this:

// set url 
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/charges -u sk_test_removed: -d source[object]=$card -d source[number]=$number -d source[exp_month]=$expdate -d source[exp_year]=$expmonth -d source[cvc]=$ccv -d amount=$amount -d currency=gbp -d description=$sale "); 

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// $output contains the output string 
$output = curl_exec($ch); 
print($output);

// close curl resource to free up system resources 
curl_close($ch);
?>

I am now honestly at a lost end, I would be extreamly greatfull for anyone to be able to assist me and help me on this issue, After 3 hard painfull days i really don't know what to do.

As a novice learner it is very hard knowing exactly what to search for when doing re-search on the issues. I have spent 3 days on google with 2 warning messages saying they think i was sending automated query's as a result of how many times if done research!! So any advice and help really would go a long way on this.

Thanks All.

like image 267
NewLearner Avatar asked Oct 24 '25 05:10

NewLearner


1 Answers

Is using the Stripe PHP bindings not a possible solution? It would be much simpler:

\Stripe\Stripe::setApiKey("sk_test_...");

// Token creation. In production this should be done client-side via Stripe.js or Checkout.
$token = \Stripe\Token::create([
  "card" => array(
    "number" => "4242424242424242",
    "exp_month" => 8,
    "exp_year" => 18,
    "cvc" => "123"
  )
]);

// Charge creation
try {
    $charge = \Stripe\Charge::create([
      "amount" => 250,
      "currency" => "gbp",
      "source" => $token->id,
      "description" => "Testing"
    ]);
} catch(...) {
    // Handle possible failures. See https://stripe.com/docs/api/php#errors
}

If you really insist on going with curl, this should work:

$apiKey = 'sk_test_...';
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => "https://api.stripe.com/v1/charges",
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . $apiKey
    ],
    CURLOPT_POSTFIELDS => http_build_query([
        "amount" => 250,
        "currency" => 'gbp',
        "source" => array(
            "object" => "card",
            "number" => "4242424242424242",
            "exp_month" => 8,
            "exp_year" => 18,
            "cvc" => "123"
        ),
        "description" => "Testing"
    ])
]);
$resp = curl_exec($curl);
curl_close($curl);

Notes:

  1. Providing the card details yourself, without client-side tokenization via Stripe.js or Checkout, raises the problem of PCI compliance, but I assume you're aware of that.

  2. Using the bindings makes it much easier to handle charge creation failures, via exceptions. See the documentation for more information.

like image 153
Ywain Avatar answered Oct 26 '25 19:10

Ywain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!