LoginSignup
19
18

More than 1 year has passed since last update.

PHPでStripe Checkout (new version)を使ったクレジットカード決済サンプル

Last updated at Posted at 2020-10-21

はじめに

サイトにクレジットカード決済を導入できる「Stripe Checkout」のバージョンが新しくなったので、1回限りのクレジットカード決済のサンプルを書いてみます。

サンプルコードの環境

CentOS 8
php 7.2
stripe-php 7.6

stripe-phpはcomposerでインストールします。

composer require stripe/stripe-php

支払いページサンプル

Stripeの支払いフォームに渡す情報として、金額、商品名、リダイレクト先URLなどを定義してセッションを作成します。
リダイレクト先URLのパラメーターに {CHECKOUT_SESSION_ID} をつけておくと、リダイレクト時に自動でsession_idに変換されます。

payment.php
<?php
require './vendor/autoload.php';

// ご自身のAPIキーを入力
$secretKey = 'sk_test_xxxxxxxx';
$publicKey = 'pk_test_xxxxxxxx';

$stripe = new \Stripe\StripeClient($secretKey);

$session = $stripe->checkout->sessions->create([
    'payment_method_types' => ['card'],
    'line_items' => [[
        'price_data' => [
            'currency' => 'JPY',
            'product_data' => [
                'name' => '商品名',
            ],
            'unit_amount' => 100,
        ],
        'quantity' => 1,
    ]],
    'mode' => 'payment',
    // ご自身のサイトURLを入力
    'success_url' => 'https://192.168.56.101/success.php?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => 'https://192.168.56.101/success.php?session_id={CHECKOUT_SESSION_ID}',
]);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>支払いページ</title>
<script src="https://js.stripe.com/v3/"></script>
</head>

<body>
<button id="checkout-button">支払う</button>
<script type="text/javascript">
  var stripe = Stripe('<?php echo $publicKey;?>');

  var checkoutButton = document.getElementById('checkout-button');
  checkoutButton.addEventListener('click', function() {
    stripe.redirectToCheckout({sessionId: "<?php echo $session->id;?>"})
    .then(function (result) {
      if (result.error) {
        // var displayError = document.getElementById('error-message');
        // displayError.textContent = result.error.message;
      }
    });
  });
</script>
</body>
</html>

支払いページを開いて支払うボタンを押すと、Stripeの支払いページに移動します。
card.PNG
クレジットカード情報を入力して支払うとsuccess_urlにリダイレクトされます。

完了ページサンプル

success_urlのsuccess.phpファイルです。GETで渡されたsession_idを使って、支払いが完了したか確認します。

success.php
<?php
require './vendor/autoload.php';

// ご自身のAPIキーを入力
$secretKey = 'sk_test_xxxxxxxx';

$stripe = new \Stripe\StripeClient($secretKey);

$session = $stripe->checkout->sessions->retrieve($_GET['session_id'], []);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>完了ページ</title>
<script src="https://js.stripe.com/v3/"></script>
</head>

<body>
<?php
if ($session->payment_status === 'paid') {
    echo '<p>支払いが完了しました</p>';
}
if ($session->payment_status === 'unpaid') {
    echo '<p>支払いが完了していません</p>';
}
?>
</body>
</html>

支払いが完了していると「支払いが完了しました」と表示されます。支払いをキャンセルした場合は「支払いが完了していません」と表示されます。

Stripeのダッシュボードで、支払いが完了していることをご確認ください。

レガシーバージョンと比べて変わったところ

顧客(Customer)が必ず作られるようになった

支払いが完了すると、顧客(Customer)が必ず作られるようになりました。カード情報を保持するのでダッシュボードから追加請求などができるようになります。顧客の削除はダッシュボードからできます。

Apple Pay、Google Payに対応

ダッシュボードの設定でApple Pay、Google Payのオンオフができます。

3Dセキュアに対応

本人認証の3Dセキュアに対応しているようです。

ポップアップがページ遷移になった

レガシーバージョンの時は支払いボタンを押すとポップアップウィンドウが開いていたのですが、ページ遷移で支払いができるようになりました。

支払いフォームの色を変更できるようになった

自身のサイトに溶け込んだデザインにすることができます。

など。
詳しくはStripe Checkoutのドキュメントでご確認ください。

以上で終わります。

19
18
3

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
19
18