# Accept a PayNow payment

Accept online payments with PayNow, a funds transfer service popular in Singapore.

# Checkout

> This is a Checkout for when payment-ui is checkout. View the full page at https://docs.stripe.com/payments/paynow/accept-a-payment?payment-ui=checkout.

> Stripe 可以通过评估货币、付款方式限制和其他参数，自动为客户展示相关支付方式。
> 
> - 请遵循[接受付款](https://docs.stripe.com/payments/accept-a-payment.md?payment-ui=checkout&ui=stripe-hosted)指南，构建一个使用[动态支付方式](https://docs.stripe.com/payments/payment-methods/dynamic-payment-methods.md)的 Checkout 集成。
- 如果您不想使用动态支付方式，请按照以下步骤在 Checkout 集成中手动配置支付方式。

PayNow is a [single-use](https://docs.stripe.com/payments/payment-methods.md#usage) payment method. Customers pay with PayNow by scanning the QR code that they see during Checkout. Completing the payment redirects customers back to your website.

This guide describes using Paynow in your online checkout flow. For in-person payments with Stripe Terminal, see [Additional payment methods](https://docs.stripe.com/terminal/payments/additional-payment-methods.md).

## 确定兼容性

**支持的商家所在地**：SG

**支持的货币**：`sgd`

**展示货币**：`sgd`

**支付模式**：Yes

**设置模式**：No

**订阅模式**：No

A Checkout Session must satisfy all of the following conditions to support PayNow payments:

- *Prices* (Prices define how much and how often to charge for products. This includes how much the product costs, what currency to use, and the interval if the price is for subscriptions) for all line items must be in the same currency, and must be in `sgd`.
- You can only use one-time line items (PayNow Checkout Sessions don’t support recurring *subscription* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis) plans).

## 收款

> 该指南建立于基本的[接受付款](https://docs.stripe.com/payments/accept-a-payment.md?integration=checkout) Checkout 集成的基础之上。

This guides you through enabling PayNow and shows the differences between accepting payments using dynamic payment methods and manually configuring payment methods.

### Enable PayNow as a payment method

创建新的 [Checkout Session](https://docs.stripe.com/api/checkout/sessions.md) 时，您需要：

1. Add `paynow` to the list of `payment_method_types`.
1. 确保您的所有 `line_items` 都使用相同的货币。

#### Stripe 托管页面

```curl
curl https://api.stripe.com/v1/checkout/sessions \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d "line_items[0][price_data][currency]=sgd" \
  -d "line_items[0][price_data][product_data][name]=T-shirt" \
  -d "line_items[0][price_data][unit_amount]=2000" \
  -d "line_items[0][quantity]=1" \
  -d mode=payment \
  -d "payment_method_types[0]=card" \
  -d "payment_method_types[1]=paynow" \
  --data-urlencode "success_url=https://example.com/success"
```

#### 嵌入式表单

```curl
curl https://api.stripe.com/v1/checkout/sessions \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d "line_items[0][price_data][currency]=sgd" \
  -d "line_items[0][price_data][product_data][name]=T-shirt" \
  -d "line_items[0][price_data][unit_amount]=2000" \
  -d "line_items[0][quantity]=1" \
  -d mode=payment \
  -d "payment_method_types[0]=card" \
  -d "payment_method_types[1]=paynow" \
  --data-urlencode "return_url=https://example.com/return" \
  -d ui_mode=embedded_page
```

### 履行订单

接受付款后，学习如何[履行订单](https://docs.stripe.com/checkout/fulfillment.md)。

## Test your integration 

When testing your Checkout integration, select PayNow as the payment method and click the **Generate QR code** button, which creates and renders a QR code.

While testing, you can scan the QR code with a QR code scanning application on your mobile device. The QR code payload contains a URL which brings you to a Stripe-hosted PayNow test payment page where you can either authorize or fail the test payment.

In live mode, you’ll be able to scan the QR code using a preferred banking app or payment app that supports PayNow.

## See also

- [Checkout fulfillment](https://docs.stripe.com/checkout/fulfillment.md)
- [Customizing Checkout](https://docs.stripe.com/payments/checkout/customization.md)


# 直接收款 API

> This is a 直接收款 API for when payment-ui is direct-api. View the full page at https://docs.stripe.com/payments/paynow/accept-a-payment?payment-ui=direct-api.

PayNow is a [single-use](https://docs.stripe.com/payments/payment-methods.md#usage) payment method. Customers use their preferred app from participating banks and participating non-bank financial institutions to scan the QR code presented to them in the checkout flow and complete the payment.

This guide describes using Paynow in your online checkout flow. For in-person payments with Stripe Terminal, see [Additional payment methods](https://docs.stripe.com/terminal/payments/additional-payment-methods.md).

## 设置 Stripe

首先，您需要有 Stripe 账户。[立即注册](https://dashboard.stripe.com/register)。

用我们的官方库从您的应用程序访问 Stripe API：

#### 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 [服务器端]

A [PaymentIntent](https://docs.stripe.com/api/payment_intents/object.md) is an object that represents your intent to collect payment from a customer and tracks the lifecycle of the payment process through each stage. First, create a PaymentIntent on your server and specify the amount to collect and the currency. If you already have an integration using the [Payment Intents API](https://docs.stripe.com/payments/payment-intents.md), add `paynow` to the list of [payment method types](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_types) for your `PaymentIntent`.

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d "payment_method_types[]=paynow" \
  -d "payment_method_data[type]=paynow" \
  -d amount=1099 \
  -d currency=sgd
```

### 检索客户端私钥

PaymentIntent 中包含的是一个*客户端私钥* (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))，用于在客户端安全地完成支付流程。有不同的方法可以将客户端私钥传递到客户端。

#### 单页应用

使用浏览器的 `fetch` 功能，从您的服务器上的端点获取客户端私钥。如果您的客户端是单页应用，特别是用现代的前端框架（例如 React）搭建的情况，则该方法最为合适。创建服务于客户端私钥的服务器端点：

#### Ruby

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

然后在客户端用 JavaScript 获取客户端私钥：

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

#### 服务端呈现

从服务器将客户端私钥传递到客户端。如果在将静态内容发送到浏览器之前，您的应用程序会在服务器上生成静态内容，则此种方法最有效。

在您的结账表单中添加 [client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret)。在您的服务器代码中，从 PaymentIntent 检索客户端密钥：

#### 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
```

## Display the PayNow QR Code [客户端]

In this step, you’ll complete PayNow payments on the client with [Stripe.js](https://docs.stripe.com/payments/elements.md). Include the Stripe.js script on your checkout page by adding it to the `head` of your HTML file.

#### HTML

```html
<head>
  <title>Checkout</title>
  <script src="https://js.stripe.com/dahlia/stripe.js"></script>
</head>
```

用下面的 JavaScript 在您的结账页面创建一个 Stripe.js 实例。

#### JavaScript

```js
// Set your publishable key. Remember to change this to your live publishable key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
var stripe = Stripe('<<YOUR_PUBLISHABLE_KEY>>');
```

Use `stripe.confirmPayNowPayment` to confirm the payment on the client side.

#### JavaScript

```js
var form = document.getElementById('payment-form');

form.addEventListener('submit', function(event) {
  event.preventDefault();

  // Set the clientSecret here you got in Step 2
  stripe.confirmPayNowPayment(
    clientSecret,
  ).then((res) => {
    if(res.paymentIntent.status === 'succeeded') {
      // The user scanned the QR code
    } else {
      // The user closed the modal, cancelling payment
    }
  });
});
```

After you call `confirmPayNowPayment`, the webpage displays a QR code. Your customers can scan the QR code and authenticate the payment using their preferred banking app or payment app. You should remain on the page with the QR code until Stripe [fulfills the order](https://docs.stripe.com/payments/paynow/accept-a-payment.md#fulfill-order) and you know the outcome of the payment.

## 履行订单 [服务器端]

Use a method such as [webhooks](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) to handle order *fulfillment* (Fulfillment is the process of providing the goods or services purchased by a customer, typically after payment is collected), instead of relying on your customer to return to the payment status page. When a customer completes payment, the `PaymentIntent` transitions to `succeeded` and emits the [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) event.

## Test your integration 

While testing, you can scan the QR code with a QR code scanning application on your mobile device. The QR code payload contains a URL that redirects you to a Stripe-hosted PayNow test payment page where you can either authorize or fail the test payment.

In live mode, you can scan the QR code using an app from participating banks and participating non-bank financial institutions.

