调至内容部分
创建账户
或
登录
Stripe 文档徽标
/
询问人工智能
创建账户
登录
开始
付款
财务自动化
平台和交易市场
资金管理
开发人员工具
开始
付款
财务自动化
开始
付款
财务自动化
平台和交易市场
资金管理
概览探索所有产品
开始构建
开始开发
项目示例
关于 API
    API 一览
    Payment Intents API
    Setup Intents API
    支付方式
    产品和价格
    Older API
      收款
        迁移到新的 API
        接受银行卡付款
        保存卡
        冻结银行卡内的资金
        用 Connect 收款
      来源
    发布阶段
Build with LLMs
在无代码的情况下使用 Stripe
设置 Stripe
创建账户
网页端管理平台
移动端管理平台
迁移到 Stripe
管理欺诈风险
了解欺诈
Radar 欺诈保护
管理争议
验证身份
首页开始About the APIsOlder APIsCharges

用 Stripe Elements 和 Charges API 接受付款Charges API

接受美国和加拿大客户的线上付款。

复制页面

旧版 API

The content of this section refers to a Legacy feature. Use the Payment Intents API instead.

Charges API 不支持以下功能,信用卡合规对其中很多都有要求:

  • 印度商家
  • Bank requests for card authentication
  • Strong Customer Authentication

用我们的预构建的银行卡 UI 组件创建支付表单,安全地收集银行卡详情,不需要处理敏感的数据。然后这些银行卡详情会被转化为一个代表性的 Token,可将它安全地发送到您的服务器。您的服务器用此令牌来创建收款。

注意

本指南中的这些步骤已在 GitHub 全面部署。克隆 repo 并按照说明运行演示应用。

设置 Stripe
客户端
服务器端

服务器端

该集成要求您的服务器上的端点与 Stripe API 通讯。请用我们的官方库从您的服务器访问 Stripe API:

Command Line
Ruby
# Available as a gem sudo gem install stripe
Gemfile
Ruby
# If you use bundler, you can add this line to your Gemfile gem 'stripe'

客户端

Stripe Android SDK 是开源的,且有完整的文档。

To install the SDK, add stripe-android to the dependencies block of your app/build.gradle file:

build.gradle.kts
Kotlin
plugins { id("com.android.application") } android { ... } dependencies { // ... // Stripe Android SDK implementation("com.stripe:stripe-android:21.13.0") // Include the financial connections SDK to support US bank account as a payment method implementation("com.stripe:financial-connections:21.13.0") }

注意

有关最新 SDK 发布及过往版本的详细信息,请查看 GitHub 上的发布页面。要想在新版本发布时接收通知,请查看仓库的发布情况。

用您的 Stripe 公钥配置 SDK,以便它可以向 Stripe API 发送请求,例如在您的 Application 子类中:

Kotlin
import com.stripe.android.PaymentConfiguration class MyApp : Application() { override fun onCreate() { super.onCreate() PaymentConfiguration.init( applicationContext,
"pk_test_TYooMQauvdEDq54NiTphI7jx"
) } }

注意

Use your test keys while you test and develop, and your live mode keys when you publish your app.

创建您的支付表单
客户端

用 CardInputWidget 在客户端安全地收集银行卡信息,这是 SDK 提供的一个临时 UI 组件,用于收集卡号、有效期、CVC 和邮编。

CardInputWidget 执行实时验证和格式化。

通过向结账页面的布局中添加以下模块,创建一个银行卡组件和Pay按钮实例:

content_checkout.xml
查看完整示例
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_checkout" tools:context=".CheckoutActivity"> <!-- ... --> <com.stripe.android.view.CardInputWidget android:id="@+id/cardInputWidget" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"/> <Button android:text="Pay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/payButton" android:layout_marginTop="20dp" app:layout_constraintTop_toBottomOf="@+id/cardInputWidget" app:layout_constraintStart_toStartOf="@+id/cardInputWidget" app:layout_constraintEnd_toEndOf="@+id/cardInputWidget"/> <!-- ... --> </androidx.constraintlayout.widget.ConstraintLayout>

运行您的应用程序,确保结账页面可以显示银行卡组件及支付按钮。

创建令牌
客户端

当客户点击支付按钮时,将 CardInputWidget 收集的银行卡信息转化成一个 Stripe 令牌。令牌化可以确保敏感的银行卡数据从不接触您的服务器,让您的集成永远保持 PCI 合规。

下面的代码显示了 Stripe#createToken() 的使用方式。该方法将 Card 实例作为了第一个参数。第二个参数是 ApiResultCallback<Token> 实例,它是客户端在成功或失败时调用的。成功完成令牌化操作后,将返回的令牌 ID 发送到您的服务器。

CheckoutActivityKotlin.kt
Kotlin
查看完整示例
// Hook up the pay button to the card widget and Stripe instance val payButton: Button = findViewById(R.id.payButton) val weakActivity = WeakReference<Activity>(this@CheckoutActivityKotlin) payButton.setOnClickListener { // Get the card details from the card widget val cardInputWidget = findViewById<CardInputWidget>(R.id.cardInputWidget) cardInputWidget.card?.let { card -> // Create a Stripe token from the card details stripe = Stripe(applicationContext, PaymentConfiguration.getInstance(applicationContext).publishableKey) stripe.createToken(card, object: ApiResultCallback<Token> { override fun onSuccess(result: Token) { val tokenID = result.id // Send the Token identifier to the server... } override fun onError(e: java.lang.Exception) { // Handle error } }) } }

从客户端将 Token id 发送到您的服务器。

用令牌创建收款
服务器端

Command Line
curl
curl https://api.stripe.com/v1/charges \ -u
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:
\ -d "amount"=999 \ -d "currency"="usd" \ -d "description"="Example charge" \ -d "source"="tok_visa"

创建收款后得到的响应要么是一个收款,要么是一个错误,并附有一个错误代码。如果响应成功,则您可以履行客户的订单,并向他们显示成功界面。否则,向他们显示错误。

测试您的集成

如果您现在可以顺利在银行卡组件内输入测试卡,提交至服务器,然后看到您的服务器成功创建收款,那就说明您的集成完工了。

您已创建好 Charges API 集成,可以开始接收银行卡付款了。此 API 不支持扩展到美国和加拿大以外的企业或客户。为获得更强大的全球支付功能,请学习用 Payment Intents API 接受付款。

另见

  • 接受 Google Pay
  • 用 Charges API 保存银行卡
此页面的内容有帮助吗?
是否
需要帮助?联系支持。
加入我们的早期使用计划。
查看我们的更改日志。
有问题?联系销售。
LLM? Read llms.txt.
Powered by Markdoc