# MobilePay での支払い

デンマークとフィンランドで一般的な支払い方法として使われている MobilePay を受け付ける方法をご紹介します。

# ダイレクト API


MobilePay は、デンマークとフィンランドで使われている [1 回限りの使用](https://docs.stripe.com/payments/payment-methods.md#usage)のカードウォレット決済手段です。顧客は MobilePay アプリを使用して、支払いを[認証して承認](https://docs.stripe.com/payments/payment-methods.md#customer-actions)できます。

顧客が MobilePay で支払う場合、Stripe は MobilePay から受け取ったカードデータを使用してカード取引を実行します。カード取引の処理は実装には表示されず、Stripe は支払いの成功または失敗を[直ちに通知](https://docs.stripe.com/payments/payment-methods.md#payment-notification)します。

## Stripe を設定する [サーバー側]

まず、Stripe アカウントが必要です。[今すぐ登録してください](https://dashboard.stripe.com/register)。

アプリケーションから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。

#### Ruby

```bash
# Available as a gem
sudo gem install stripe
```

```ruby
# If you use bundler, you can add this line to your Gemfile
gem 'stripe'
```

## PaymentIntent を作成する [サーバー側]

[PaymentIntent](https://docs.stripe.com/api/payment_intents/object.md) は、顧客から支払いを回収する意図を表すオブジェクトであり、各段階を通じて支払いプロセスのライフサイクルを追跡します。サーバーで `PaymentIntent` を作成し、回収する金額とサポートされている通貨 (`eur`、`dkk`、`sek`、または `nok`) を指定します。[動的な決済手段](https://docs.stripe.com/payments/payment-methods/dynamic-payment-methods.md)を使用すると、対象となる決済手段が自動的に表示されます。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=dkk \
  -d "automatic_payment_methods[enabled]=true" \
  -d "payment_method_data[type]=mobilepay"
```

### レスポンスの例

```json
{
  "id": "pi_12345",
  "amount": 1099,
  "client_secret": "pi_12345_secret_abcdef",
  "currency": "dkk",
  "payment_method": "pm_12345",
  "payment_method_types": [
     "mobilepay"
  ],
  "status": "requires_confirmation"
}
```

### client secret を取得する

PaymentIntent には、*client secret* (The client secret is a unique key returned from Stripe as part of a PaymentIntent. This key lets the client access important fields from the PaymentIntent (status, amount, currency) while hiding sensitive ones (metadata, customer)) が含まれています。これは、支払いプロセスを安全に完了するためにクライアント側で使用されます。client secret をクライアント側に渡す際は、いくつかの方法を使用できます。

#### 1 ページのアプリケーション

ブラウザーの `fetch` 関数を使用して、サーバーのエンドポイントから client secret を取得します。この方法は、クライアント側が 1 ページのアプリケーションで、特に React などの最新のフロントエンドフレームワークで構築されている場合に最適です。client secret を処理するサーバーのエンドポイントを作成します。

#### Ruby

```ruby
get '/secret' do
  intent = # ... Create or retrieve the PaymentIntent
  {client_secret: intent.client_secret}.to_json
end
```

その後、クライアント側で JavaScript を使用して client secret を取得します。

```javascript
(async () => {
  const response = await fetch('/secret');
  const {client_secret: clientSecret} = await response.json();
  // Render the form using the clientSecret
})();
```

#### サーバ側のレンダリング

サーバーからクライアントに client secret を渡します。この方法は、アプリケーションがブラウザーへの送信前に静的なコンテンツをサーバーで生成する場合に最適です。

決済フォームに [client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) を追加します。サーバー側のコードで、PaymentIntent から client secret を取得します。

#### Ruby

```erb
<form id="payment-form" data-secret="<%= @intent.client_secret %>">
  <button id="submit">Submit</button>
</form>
```

```ruby
get '/checkout' do
  @intent = # ... Fetch or create the PaymentIntent
  erb :checkout
end
```

## PaymentIntentを確定する

[ステップ 2](https://docs.stripe.com/payments/mobilepay/accept-a-payment.md#create-payment-intent) の PaymentIntent [ID](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-id) を使用して、PaymentIntent を*確定* (Confirming an intent indicates that the customer intends to use the current or provided payment method. Upon confirmation, the intent attempts to initiate the portions of the flow that have real-world side effects)します。これは、指定された *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) を使用して支払うことを宣言するものです。Stripe は PaymentIntent が確定されると、支払いを開始します。[return_url](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-return_url) は、支払い完了後に顧客をリダイレクトする場所を指定します。

```curl
curl https://api.stripe.com/v1/payment_intents/{{PAYMENTINTENT_ID}}/confirm \
  -u "<<YOUR_SECRET_KEY>>:" \
  --data-urlencode "return_url=https://example.com/checkout/complete"
```

### レスポンスの例

```json
{
  "id": "pi_12345",
  "amount": 1099,
  "currency": "dkk",
  "payment_method": "pm_12345",
  "next_action": {
    "redirect_to_url": {
      "return_url": "https://example.com/checkout/complete",
      "url": "https://pm-redirects.stripe.com/authorize/acct_123/pa_nonce_abc"
    },
    "type": "redirect_to_url"
  },
  "payment_method_types": [
     "mobilepay"
  ],
  "status": "requires_action"
}
```

支払いを承認するには、[next_action[redirect_to_url][url]](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-next_action-redirect_to_url-url) フィールドの URL に顧客をリダイレクトします。

- デスクトップでは、URL によって MobilePay スタートページが開き、顧客はそこで、MobilePay アカウントを識別する電話番号を入力します。その後、顧客は MobilePay スマートフォンアプリを使用して支払いの認証を進めることができます。
- モバイルデバイスでは、デスクトップでのプロセスと同様に、この URL によって、MobilePay アプリケーションが直接開くか (存在する場合)、MobilePay スタートページに移動します。

顧客は 5 分以内にリダイレクト URL を開き、MobilePay アプリで支払いを承認できます。基のカードによる支払いが失敗した場合、顧客は別のカードを選択して、MobilePay アプリで再試行できます。支払いが 5 分以内に承認されない場合、支払いは失敗し、PaymentIntent のステータスは `requires_payment_method` に移行します。

## 支払い後のイベントを処理する

支払いが完了すると、Stripe は [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) イベントを送信します。ダッシュボード、カスタム *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests)、またはパートナーソリューションを使用してこれらのイベントを受信し、また、顧客への注文確認メールの送信、データベースでの売上の記録、配送ワークフローの開始などのアクションを実行します。

クライアントからのコールバックを待つのではなく、これらのイベントをリッスンします。クライアント側では、コールバックが実行される前に顧客がブラウザーのウィンドウを閉じたり、アプリを終了したりする可能性があります。また、悪意を持つクライアントがレスポンスを不正操作する恐れもあります。非同期型のイベントをリッスンするよう構築済みのシステムを設定することで、これ以降はより多くの決済手段を簡単に受け付けられるようになります。[サポートされているすべての決済手段の違い](https://stripe.com/payments/payment-methods-guide)をご確認ください。

- **ダッシュボードでイベントを手動で処理する**

  ダッシュボードでは、[支払いの表示](https://dashboard.stripe.com/payments)、メールの領収書の送信、入金の処理、失敗した支払いの再試行を行えます。

- **Custom Webhook を構築する**

  [カスタム webhook](https://docs.stripe.com/webhooks/handling-payment-events.md#build-your-own-webhook) ハンドラを構築して、イベントをリッスンし、カスタムの非同期決済フローを構築できます。Stripe CLI を使用すると、ローカルで webhook 連携をテストしてデバッグできます。

- **構築済みアプリを導入する**

  パートナーアプリケーションを統合することで、[自動化](https://stripe.partners/?f_category=automation)や[マーケティング/セールス](https://stripe.partners/?f_category=marketing-and-sales)などの一般的なビジネスイベントを処理します。

## 組み込みをテストする

[テスト API キー](https://docs.stripe.com/keys.md#test-live-modes)を使用して、PaymentIntent を作成します。PaymentIntent を確定したら、`next_action` リダイレクト URL に従い、支払いをオーソリまたは失敗させるオプションのあるテストページをテストします。

- **Authorize test payment (テスト支払いをオーソリする)** をクリックして、支払い成功のケースをテストします。 PaymentIntent は `requires_action` から `succeeded` に移行します。
- **Fail test payment (テスト支払いを失敗させる)** をクリックして、顧客の認証失敗のケースをテストします。PaymentIntent が `requires_action` から `requires_payment_method` に移行します。

## Optional: 支払いをオーソリし、後でキャプチャーする

MobilePay は[オーソリとキャプチャの分離](https://docs.stripe.com/payments/place-a-hold-on-a-payment-method.md)に対応しています。

### オーソリのみを行うよう Stripe に指示する

オーソリとキャプチャーを分離することを指定するには、PaymentIntent の作成時に、[capture_method](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-capture_method) を `manual` に設定します。このパラメーターは、MobilePay に関連付けられている顧客のカードの金額のみをオーソリするよう Stripe に指示します。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount=1099 \
  -d currency=dkk \
  -d capture_method=manual \
  -d "automatic_payment_methods[enabled]=true" \
  -d "payment_method_data[type]=mobilepay"
```

オーソリが成功すると、Stripe は [payment_intent.amount_capturable_updated](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.amount_capturable_updated) イベントを送信します。詳細については、[イベント](https://docs.stripe.com/api/events.md)をご覧ください。

### 売上をキャプチャーする

オーソリが成功すると、PaymentIntent の[ステータス](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-status)は `requires_capture` に移行し、オーソリされた資金を[確定](https://docs.stripe.com/api/payment_intents/capture.md)できます。オーソリされた全額またはその一部のみを確定できます。

```curl
curl https://api.stripe.com/v1/payment_intents/{{PAYMENTINTENT_ID}}/capture \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d amount_to_capture=1099
```

## Optional: キャンセル

MobilePay 決済に関連付けられた[PaymentIntent をキャンセル](https://docs.stripe.com/api/payment_intents/cancel.md)することにより、MobilePay 決済を期限切れ前にキャンセルできます。

## 失敗した支払い

基になったカード取引が拒否されると、MobilePay 取引が失敗することがあります。詳しくは、[カードの支払い拒否](https://docs.stripe.com/declines/card.md)をご覧ください。この場合、PaymentMethod は解除され、PaymentIntent のステータスは自動的に `requires_payment_method` に移行します。

PaymentIntent のステータスが `requires_action` の場合、顧客は 5 分以内に支払いを認証する必要があります。5 分経過してもアクションが実行されない場合、PaymentMethod は解除され、PaymentIntent のステータスは自動的に `requires_payment_method` に移行します。

## 返金と不審請求の申請

Stripe は、MobilePay 取引の一環として標準のカードネットワークを使用してカード取引を実行します。[返金](https://docs.stripe.com/refunds.md) と[不審請求の申し立て](https://docs.stripe.com/disputes/how-disputes-work.md)には、Visa と Mastercard のネットワークのルールが適用されます。

