• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP error communicating with Stripe. (Network error [errno 55]: Failed sending HTTP POST request) error with stripe php

Alien10

New Coder
I am getting an error in php when using stripe, Here is the error:

Fatal error: Uncaught Unexpected error communicating with Stripe. If this problem persists, let us know at [email protected]. (Network error [errno 55]: Failed sending HTTP POST request) thrown in C:\xampp\htdocs\subscription-use-cases\medium\vendor\stripe\stripe-php\lib\HttpClient\CurlClient.php on line 386

I have attached the files for the script. Please tell me what is wrong
 

Attachments

  • medium.zip
    400.1 KB · Views: 1

Alien10

New Coder
Hi @Alien10,

Try doing a test, https://github.com/stripe/stripe-reachability See if any errors come up.

And just make sure to share the code rather than sharing a .zip file. By sharing the code directly we can simple copy and paste it into our editors.
Here is the output of https://github.com/stripe/stripe-reachability/blob/master/bin/test.sh :

Code:
========================================

Checking os...

WARNING: not tested on MINGW32_NT-10.0-19041

ERROR: os check failed

========================================

Checking ip...

+ curl -4 --write-out \n ifconfig.co/json

{"ip":"101.191.147.186","ip_decimal":1707054010,"country":"Australia","country_i

so":"AU","country_eu":false,"region_name":"New South Wales","region_code":"NSW",

"zip_code":"2000","city":"Sydney","latitude":-33.8591,"longitude":151.2002,"time

_zone":"Australia/Sydney","asn":"AS1221","asn_org":"Telstra Corporation Ltd","ho

stname":"cpe-101-191-147-186.nb04.nsw.asp.telstra.net","user_agent":{"product":"

curl","version":"7.70.0","raw_value":"curl/7.70.0"}}

OK: ip check

========================================

Checking route...

OK: route check

========================================

Checking curl_https...

+ curl -Iv https://api.stripe.com/healthcheck

*   Trying 34.241.202.139:443...

* Connected to api.stripe.com (34.241.202.139) port 443 (#0)

* ALPN, offering h2

* ALPN, offering http/1.1

* successfully set certificate verify locations:

*   CAfile: C:/Users/Lisa Emanuel/scoop/apps/git/2.27.0.windows.1/Git/mingw32/ss

l/certs/ca-bundle.crt

  CApath: none

* TLSv1.3 (OUT), TLS handshake, Client hello (1):

* TLSv1.3 (IN), TLS handshake, Server hello (2):

* TLSv1.2 (IN), TLS handshake, Certificate (11):

* TLSv1.2 (IN), TLS handshake, Server key exchange (12):

* TLSv1.2 (IN), TLS handshake, Server finished (14):

* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):

* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):

* TLSv1.2 (OUT), TLS handshake, Finished (20):

* TLSv1.2 (IN), TLS handshake, Finished (20):

* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256

* ALPN, server accepted to use h2

* Server certificate:

*  subject: businessCategory=Private Organization; jurisdictionC=US; jurisdictio

nST=Delaware; serialNumber=4675506; C=US; ST=California; L=San Francisco; O=Stri

pe, Inc; CN=api.stripe.com

*  start date: May 14 00:00:00 2020 GMT

*  expire date: Oct  7 12:00:00 2020 GMT

*  subjectAltName: host "api.stripe.com" matched cert's "api.stripe.com"

*  issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 Extended

Validation Server CA

*  SSL certificate verify ok.

* Using HTTP2, server supports multi-use

* Connection state changed (HTTP/2 confirmed)

* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0

* Using Stream ID: 1 (easy handle 0x17ec1d0)

> HEAD /healthcheck HTTP/2

> Host: api.stripe.com

> user-agent: curl/7.70.0

> accept: */*

>

* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!

< HTTP/2 200

HTTP/2 200

< server: nginx

server: nginx

< date: Sun, 02 Aug 2020 02:58:17 GMT

date: Sun, 02 Aug 2020 02:58:17 GMT

< content-type: text/plain; charset=utf-8

content-type: text/plain; charset=utf-8

< content-length: 96

content-length: 96

< access-control-allow-origin: *

access-control-allow-origin: *

< strict-transport-security: max-age=31556926; includeSubDomains; preload

strict-transport-security: max-age=31556926; includeSubDomains; preload


<

* Connection #0 to host api.stripe.com left intact

OK: curl_https check
I got the code from https://medium.com/@Keithweaver_/using-stripe-with-php-c341fcc6b68b

I have the stripe composer package installed and here is the HTML:


HTML:
<html>

    <head>

        <!-- The Styling File -->

        <link rel="stylesheet" href="style.css"/>

    </head>

    <body>

    <form action="./charge.php" method="post" id="payment-form">

        <div class="form-row">

            <label for="card-element">Credit or debit card</label>

            <div id="card-element">

            <!-- a Stripe Element will be inserted here. -->

            </div>

            <!-- Used to display form errors -->

            <div id="card-errors"></div>

        </div>

        <button>Submit Payment</button>

    </form>

    <!-- The needed JS files -->

    <!-- JQUERY File -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <!-- Stripe JS -->

    <script src="https://js.stripe.com/v3/"></script>

    <!-- Your JS File -->

    <script src="charge.js"></script>

    </body>

</html>
and the css:

CSS:
.StripeElement {

    background-color: white;

    padding: 8px 12px;

    border-radius: 4px;

    border: 1px solid transparent;

    box-shadow: 0 1px 3px 0 #e6ebf1;

    -webkit-transition: box-shadow 150ms ease;

    transition: box-shadow 150ms ease;

}

.StripeElement--focus {

    box-shadow: 0 1px 3px 0 #cfd7df;

}

.StripeElement--invalid {

    border-color: #fa755a;

}

.StripeElement--webkit-autofill {

    background-color: #fefde5 !important;

}
and the javascript:


JavaScript:
// Stripe API Key

var stripe = Stripe('stripe key');

var elements = stripe.elements();

// Custom Styling

var style = {

    base: {

        color: '#32325d',

        lineHeight: '24px',

        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',

        fontSmoothing: 'antialiased',

        fontSize: '16px',

        '::placeholder': {

            color: '#aab7c4'

        }

    },

    invalid: {

        color: '#fa755a',

        iconColor: '#fa755a'

    }

};

// Create an instance of the card Element

var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>

card.mount('#card-element');

// Handle real-time validation errors from the card Element.

card.addEventListener('change', function(event) {

    var displayError = document.getElementById('card-errors');

if (event.error) {

        displayError.textContent = event.error.message;

    } else {

        displayError.textContent = '';

    }

});

// Handle form submission

var form = document.getElementById('payment-form');

form.addEventListener('submit', function(event) {

    event.preventDefault();

stripe.createToken(card).then(function(result) {

        if (result.error) {

            // Inform the user if there was an error

            var errorElement = document.getElementById('card-errors');

            errorElement.textContent = result.error.message;

        } else {

            stripeTokenHandler(result.token);

        }

    });

});

// Send Stripe Token to Server

function stripeTokenHandler(token) {

    // Insert the token ID into the form so it gets submitted to the server

    var form = document.getElementById('payment-form');

// Add Stripe Token to hidden input

    var hiddenInput = document.createElement('input');

    hiddenInput.setAttribute('type', 'hidden');

    hiddenInput.setAttribute('name', 'stripeToken');

    hiddenInput.setAttribute('value', token.id);

    form.appendChild(hiddenInput);

// Submit form

    form.submit();

}
and the PHP:


PHP:
<?php

    require_once('vendor/autoload.php');

    \Stripe\Stripe::setVerifySslCerts(false);

    \Stripe\Stripe::setApiKey('stripe key');

    $token = $_POST['stripeToken'];

    // This is a $20.00 charge in AU Dollar.

    $charge = \Stripe\Charge::create(

        array(

            'amount' => 2000,

            'currency' => 'aud',

            'source' => $token

        )

    );

    \Stripe\Stripe::setMaxNetworkRetries(4);

    print_r($charge);
There is my code and my responses from https://github.com/stripe/stripe-reachability/blob/master/bin/test.sh
 

Master Yoda

Administrator
Administrator
Verified
Thank you for testing that out! So I'd recommend searching the OS Error: It looks promising. (I'm not an expert on this but I believe this may answer your question)
Checking os...

WARNING: not tested on MINGW32_NT-10.0-19041

ERROR: os check failed
Hey @Ghost, @swift, @Krusty the Senile, @Tealk and @joe, do any of you guys have an answer to this?

It could be because you are doing this on localhost and from what I read on StackOverFlow :sick:. Stripe requires the use of SSL: https://stackoverflow.com/questions/20513126/unexpected-error-communicating-with-stripe

\Stripe\Stripe::setVerifySslCerts(false);
 

Alien10

New Coder
Thank you for testing that out! So I'd recommend searching the OS Error: It looks promising. (I'm not an expert on this but I believe this may answer your question)

Hey @Ghost, @swift, @Krusty the Senile, @Tealk and @joe, do any of you guys have an answer to this?

It could be because you are doing this on localhost and from what I read on StackOverFlow :sick:. Stripe requires the use of SSL: https://stackoverflow.com/questions/20513126/unexpected-error-communicating-with-stripe
Thank you. It helped me a lot because I have been trying to fix this problem for a week now. I thought I have tried this but the spelling must have been incorrect.
 

swift

Coder
I use Stripe with PHP a lot and have never encountered this issue. I think as @Master Yoda suggested, it could be something to do with your environment so take a look through that thread on StackOverflow.

I do notice that you're running windows and xampp - is it possible that you just don't have the right extensions installed? I don't use windows for development so I'm just speculating here. I haven't used xampp in years but it might be worth taking a look at docker or vagrant or something similar where you're running your development environment inside of Linux.
 
Top Bottom