Stripe PaymentIntent

請求〜決済

請求情報作成(php)

※laravel
use Stripe\PaymentIntent;

Stripe::setApiKey('XXXXXXXXXXXXX');
Stripe::setApiVersion('XXXXXXXXXXXXX');

$intent = PaymentIntent::create([
 'payment_method_types' => ['card'],
 'currency' => 'jpy',
 'amount' => 20000,
 顧客情報参照
 'customer' => $customer->id,
 この設定がないと後で決済情報を取得できない
 'setup_future_usage' => 'off_session',
 true=請求と同時に決済する / false=請求の作成のみで後に決済を行う(ホテルの予約方式)
 'capture' => true/false,
 請求先
 'transfer_data' => [
  'destination' => '{{CONNECTED_STRIPE_ACCOUNT_ID}}',
  'amount' => '{{金額}}',
 ],
]);

return view('stripe.create')->with([
 'data' => [
  'client_secret' => $intent->client_secret,
 ],
]);

決済処理(JavaScript)

※html+JavaScript
<script src="https://js.stripe.com/v3/"></script>

<form id="payment-form">
 <input id="name" placeholder="Jenny Rosen" value="Jenny Rosen" required />

 stripeライブラリを読み込んで自動的にカード情報の入力欄が作成される
 <div id="card-element">
 <!-- Elements will create input elements here -->
 </div>

 <button id="submit">Pay</button>
</form>

<script>
 document.addEventListener('DOMContentLoaded', async () => {
  let stripe = Stripe('XXXXXXXXXXXXXXXXXXXX');

  const clientSecret = "{{ $data['client_secret'] }}";

  const elements = stripe.elements();
  const card = elements.create('card');
  card.mount('#card-element');

  // When the form is submitted…
  const form = document.getElementById('payment-form');
  let submitted = false;
  form.addEventListener('submit', async (e) => {
   e.preventDefault();

   // Disable double submission of the form
   if(submitted) { return; }
   submitted = true;
   form.querySelector('button').disabled = true;

   const nameInput = document.querySelector('#name');

   stripeのAPIをcallして請求情報を作成
   const {error: stripeError, paymentIntent} = await stripe.confirmCardPayment(
    clientSecret,
    {
     payment_method: {
      card: card,
      billing_details: {
       name: nameInput.value,
      },
     },
    }
   );
  });
 });
</script>

決済情報取得(php)

$customer = Customer::retrieve('XXXXXXXXXXXXXXX');

JavaScript側での決済処理の戻り値paymentIntent.idを取得
$paymentIntentId = 〜;

PaymentIntent情報取得
$paymentIntent = PaymentIntent::retrieve($paymentIntentId);

PaymentMethod情報取得
$paymentMethod = PaymentMethod::retrieve($paymentIntent->payment_method);
$paymentMethod->attach([
 'customer' => $customer->id,
]);

決済情報取得
$paymentMethod = PaymentMethod::all([
 'customer' => $customer->id,
 'type' => 'card'
]);

顧客情報

作成

use Stripe\Customer;

Stripe::setApiKey('XXXXXXXXXXXXX');
Stripe::setApiVersion('XXXXXXXXXXXXX');

$customer = Customer::create([
 'name' => 'name',
 'description' => 'description',
]);

呼び出し

use Stripe\Customer;

Stripe::setApiKey('XXXXXXXXXXXXX');
Stripe::setApiVersion('XXXXXXXXXXXXX');

$customer = Customer::retrieve(CustomerID);

Follow me!