8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelでStripeを導入しようとしたらStripe\Stripeが無いよとなる件

Last updated at Posted at 2019-01-06

環境

Laravel5.5
PHP7.0.26
Cloud9

コマンド

$ sudo apt-get install php7.0-curl
$ composer require stripe/stripe-php

▼Stripeをcomposerでインストールする手順は以下のURLが公式ページを参照
https://stripe.com/docs/libraries#php

ビュー(blade側)の記述

▼参考URL
https://stripe.com/docs/checkout#integration-simple

resources/views/stripe.blade.html
<form action="/payment" method="POST">
    {!! csrf_field() !!}
    <script
        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="pk_test_************************"
        data-amount="12000"
        data-name="TakayukiNJ"
        data-email="g181tg2061@dhw.ac.jp"
        data-description="description"
        data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
        data-locale="auto"
        data-currency="jpy"
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        })
    >
    </script>
</form>

コントローラー側(web.php)の記述

▼参考URL
https://stripe.com/docs/quickstart

routes/web.php
Route::post('/payment', function () {

    //ここにstripeアカウントのAPIキーをコピペ
\Stripe\Stripe::setApiKey("sk_test_*******************");

    // Token is created using Checkout or Elements!
    // Get the payment token ID submitted by the form:
    $token = $_POST['stripeToken'];
    
    // Create a charge: this will charge the user's card
    try {
        $charge = \Stripe\Charge::create([
            'amount' => 12000,
            'currency' => 'jpy',
            'description' => 'Example charge',
            'source' => $token,
        ]);

    } catch(\Stripe\Error\Card $e) {
      // The card has been declined
    }

    // サンクスメール送る...

    return view('home');
});

今回の闇(エラー内容)

stripe/stripe-phpを使いたくて
composer update もしくは composer require stripe/stripe-phpを実行しても
下記のエラーが出てしまう。

- stripe/stripe-php v6.28.0 requires ext-curl * -> the requested PHP extension curl is missing from your system.

ブラウザでテスト実行すると、当然下記のweb.phpの該当箇所で怒られる。

web.php
FatalThrowableError in web.php line ***:
Class 'Stripe\Stripe' not found

闇は深かったが、解決しました。

ターミナルで順番にやるべきこと

$ sudo apt-get install php7.0-curl
$ cp /usr/lib/php/20151012/curl.so ~/workspace/ext/curl.so
$ sudo service apache2 restart
$ composer require stripe/stripe-php

考察

Cloud9でLaravelを動作するために、PHPのバージョンを5から7にした為、本来extファイルにcurl.soが入る必要があったのだが、別のファイルにcurl.soが保存されてしまったと考えられる。その為、コピーしたら、composerが無事に動いてくれた。
もっと違うやり方があるのかもしれないが、一旦動いたので、他にも同じことで困っている方がいたらお試しあれ!

Special Thanks

今回、G's Academyの先輩でありデジタルハリウッド大学院の同期である酒井氏(@sakateu)に、多大なるご協力をいただきました。ありがとう!!

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?