Skip to content

stripe ​

Stripe 支付。

概述 ​

Stripe 支付包含 Checkout 和 PaymentIntent 两种方式。使用前需要在站点 CMS 中配置 Stripe 密钥和 Webhook 签名密钥。

API说明
k.payment.stripeCheckoutStripe Checkout 托管支付页
k.payment.stripeStripe PaymentIntent 和卡管理

stripeCheckout.charge() ​

创建 Stripe Checkout 支付请求。

ts
k.api.get(() => {
    const result = k.payment.stripeCheckout.charge({
        name: 'T-shirt',
        description: 'Order payment',
        totalAmount: 1.5,
        currency: 'USD',
        order: 'order-id',
        returnUrl: '/payment/success?orderId=order-id',
        cancelUrl: '/payment/cancel?orderId=order-id',
        customer: 'customer@example.com',
        callbackCodeName: 'PaymentCallback'
    })

    k.response.renderView(result.nextAction.renderHtml)
})

参数: StripeCheckoutParams

参数类型必填说明
cancelUrlstring是Checkout 返回按钮或取消支付地址
returnUrlstring是支付成功后的返回地址
customerstring否客户标识。提供后可自动创建 Stripe Customer
totalAmountnumber是订单金额
namestring是订单名称
descriptionstring是订单描述
currencystring是货币,如 USD、EUR
orderstring否Commerce 订单 ID。提供后支付成功会更新订单支付状态
callbackCodeNamestring否支付完成后执行的回调代码名称

返回: ChargeResponse

stripe.charge() ​

创建 Stripe PaymentIntent 支付请求。

ts
k.api.post(() => {
    const result = k.payment.stripe.charge({
        name: 'T-shirt',
        description: 'Order payment',
        totalAmount: 1.5,
        currency: 'USD',
        order: 'order-id',
        returnUrl: '/payment/success?orderId=order-id',
        customer: 'customer-id',
        paymentMethodId: 'payment-method-id',
        card: {
            number: '4242424242424242',
            expMonth: '12',
            expYear: '2030',
            cvc: '123',
            name: 'John Doe'
        }
    })

    return { requestId: result.requestId, nextAction: result.nextAction }
})

参数: StripePaymentIntentParams

参数类型必填说明
returnUrlstring是支付完成后的返回地址
cardCard否卡信息
paymentMethodIdstring否已保存的 Stripe PaymentMethod ID
customerstring否Stripe Customer ID 或客户标识
totalAmountnumber是订单金额
namestring是订单名称
descriptionstring是订单描述
currencystring是货币,如 USD、EUR
orderstring否Commerce 订单 ID。提供后支付成功会更新订单支付状态
callbackCodeNamestring否支付完成后执行的回调代码名称

返回: ChargeResponse

checkStatus() ​

检查支付状态。

参数:

参数类型必填说明
requestIdstring是charge() 返回的 requestId。
ts
k.api.get(() => {
    const status = k.payment.stripeCheckout.checkStatus('request-id')
    return { paid: status.paid, failed: status.failed, status: status.status }
})

返回: PaymentStatusResponse

getCardList() ​

获取指定客户保存的卡列表。

ts
k.api.get(() => {
    return k.payment.stripe.getCardList('customer-id')
})

参数:

参数类型必填说明
customerstring是Stripe Customer ID

返回: any

createCard() ​

为客户创建卡。

参数:

参数类型必填说明
customerstring是Stripe Customer ID。
cardCard是要保存的卡信息。
ts
k.api.post(() => {
    return k.payment.stripe.createCard('customer-id', {
        number: '4242424242424242',
        expMonth: '12',
        expYear: '2030',
        cvc: '123',
        name: 'John Doe'
    })
})

返回: string

removeCard() ​

从客户上移除卡。

参数:

参数类型必填说明
cardIdstring是要移除的 Stripe 卡 ID。
ts
k.api.post(() => {
    k.payment.stripe.removeCard('card-id')
    return 'success'
})

返回: void

updateOrder() ​

处理 Stripe Checkout 回调并更新订单。

参数:

参数类型必填说明
contextobject是当前支付回调上下文,通常传入 k.payment.stripeCheckout.context。
ts
k.api.post(() => {
    const callback = k.payment.stripeCheckout.updateOrder(k.payment.stripeCheckout.context)
    return { paid: callback.paid, status: callback.status }
})

返回: PaymentCallback

Card ​

属性类型说明
numberstring卡号
cvcstringCVC
expMonthstring过期月份
expYearstring过期年份
namestring持卡人姓名

StripeSetting ​

属性类型说明
secretKeystringStripe Secret Key
webhookSigningSecretstringWebhook 签名密钥