
import PaymentIntentResponseFields from "../../../src/reference/subbly-sdk/types/PaymentIntentResponse.mdx";

<SdkPage group="payment-intents" />

<Method id="get-payment-intent" signature="subbly.paymentIntents.getPaymentIntent(paymentIntentId)">

Gets a payment intent, including the client secret Stripe needs to run 3-D
Secure. Call it when `subbly.checkout.purchase` rejects with
`code: 'payment_requires_action'`; the rejection carries the ID to pass here.
Needs an authenticated customer.

<Params>
  <Param name="paymentIntentId" required type="string">
    ID of the payment intent. Read it from `err.paymentIntentId` on the purchase
    error.
  </Param>
</Params>

<Returns type="Promise<PaymentIntentResponse>">
The payment intent.
<Properties label="payment intent" collapsed>
  <PaymentIntentResponseFields />
</Properties>
</Returns>

<Example>

```js title="Get the client secret"
const { token } = await subbly.paymentIntents.getPaymentIntent(err.paymentIntentId)

const { paymentIntent } = await stripe.confirmCardPayment(token)
```

</Example>

</Method>

<Method id="confirm" signature="subbly.paymentIntents.confirm(paymentIntentId, externalPaymentId)">

Tells Subbly that Stripe finished verifying the payment intent. Call
`subbly.checkout.purchase` again afterwards to finish the order. Needs an
authenticated customer.

<Params>
  <Param name="paymentIntentId" required type="string">
    ID of the Subbly payment intent, from `err.paymentIntentId`.
  </Param>

  <Param name="externalPaymentId" required type="string">
    ID Stripe gave back: the `paymentIntent.id` from `stripe.confirmCardPayment`.
  </Param>
</Params>

<Returns type="Promise<PaymentIntentResponse>">
The payment intent, with its new status.
<Properties label="payment intent" collapsed>
  <PaymentIntentResponseFields />
</Properties>
</Returns>

<Example>

```js title="Finish a 3-D Secure purchase"
try {
  await subbly.checkout.purchase(subbly.cart.id)
} catch (err) {
  if (err.code !== 'payment_requires_action') throw err

  const { token } = await subbly.paymentIntents.getPaymentIntent(err.paymentIntentId)
  const { paymentIntent } = await stripe.confirmCardPayment(token)

  await subbly.paymentIntents.confirm(err.paymentIntentId, paymentIntent.id)
  await subbly.checkout.purchase(subbly.cart.id)
}
```

</Example>

</Method>
