Python 从stripe中检索具有嵌套价格的所有产品?

Python 从stripe中检索具有嵌套价格的所有产品?,python,stripe-payments,Python,Stripe Payments,我喜欢条带API的简单性,但我不确定是否可以检索嵌套对象。我希望能够检索所有产品和价格,并在我的网站上公开展示它们 我可以列出所有价格和产品,如下所示: stripe.Price.list(limit=3) stripe.Products.list(limit=3) 在前端,我必须写一些逻辑,将价格与产品联系起来,区分每月和每年的价格,等等。我很想把这个嵌套起来。这可能吗 我也不完全确定公开公开返回的信息是否安全(API密钥显然是隐藏的)。我想知道更多关于这个的信息 在前端,我必须写一些逻辑,

我喜欢条带API的简单性,但我不确定是否可以检索嵌套对象。我希望能够检索所有产品和价格,并在我的网站上公开展示它们

我可以列出所有价格和产品,如下所示:

stripe.Price.list(limit=3)
stripe.Products.list(limit=3)
在前端,我必须写一些逻辑,将价格与产品联系起来,区分每月和每年的价格,等等。我很想把这个嵌套起来。这可能吗

我也不完全确定公开公开返回的信息是否安全(API密钥显然是隐藏的)。我想知道更多关于这个的信息

在前端,我必须写一些逻辑,将价格与产品联系起来,区分每月和每年的价格,等等。我很想把这个嵌套起来。这可能吗

方法是首先从API中列出产品,然后迭代它们并检索与给定产品相关的所有价格:

  • 列出所有价格:
  • 指定产品ID时:
Product对象不包含其价格列表,因此实际上无法提出两个单独的请求:

我也不完全确定公开公开返回的信息是否安全(API密钥显然是隐藏的)。我想知道更多关于这个的信息

这是一个好问题,因为一般来说,只有使用可发布密钥检索的对象才可以安全地直接使用stripeapi的客户端。在这些情况下,API引用将对象上的属性标记为“可使用可发布密钥检索”,例如:

  • 检索PaymentIntent客户端:
  • 可检索的属性:

但是,只要您的服务器端端点不直接返回使用您的密钥检索的产品/价格对象,而是返回仅具有所需属性的过滤版本,您就可以了

反过来做;得到所有的价格,然后得到相应的产品

首先创建条带实例,然后从函数请求数据

// index.ts
import { getStripeProduct, getStripeProducts } from './products'
import * as express from 'express'
import Stripe from 'stripe'
const router = express.Router()
const stripe = (apiKey:string) => {
  return new Stripe(apiKey, {
    apiVersion: "2020-08-27",
    typescript: true
  });
}

// GET a product 
router.get('/product/:id', async (req: express.Request, res: express.Response) => {
  console.log(`API call to GET /public/product/${req.params.id}`)
  await getStripeProduct(stripe(STRIPE_SECRET_KEY)), req.params.id )
  .then((product) => {
    respond(res, {data: product})
  })
  .catch((error) => {
    respond(res, {code: error.statusCode, message: error.message})
  })
})

// GET a product list
router.get('/products', async (req: express.Request, res: express.Response) => {
  console.log('API call to GET /public/products')
  await getStripeProducts(stripe(STRIPE_SECRET_KEY))
  .then((products) => {
    respond(res, {data: products})
  })
  .catch((error) => {
    console.error(error)
    respond(res, {code: error.statusCode, message: error.message})
  })
})

注意:我有一个名为
respond
的实用函数,它将API响应标准化。你可以先阅读ttmarek的答案但是,请注意,如果您需要所有答案,页面似乎没有提供完整的示例。发件人:

数组中的每个条目都是一个单独的price对象。如果没有更多的价格 如果可用,则生成的数组将为空

正如它所说的那样,为了得到所有的数组,需要循环直到得到的数组为空

这里是一些代码,将得到你所有的产品和所有的价格

import stripe
stripe.api_key = stripe_test_private_key
def get_all_stripe_objects(stripe_listable):
    objects = []
    get_more = True
    starting_after = None
    while get_more:
        #stripe.Customer implements ListableAPIResource(APIResource):
        resp = stripe_listable.list(limit=100,starting_after=starting_after)
        objects.extend(resp['data'])
        get_more = resp['has_more']
        if len(resp['data'])>0:
            starting_after = resp['data'][-1]['id']
    return objects
all_stripe_products = get_all_stripe_objects(stripe.Product)
all_stripe_prices = get_all_stripe_objects(stripe.Price)
// Product Model
export interface ProductModel {
  id: string;
  name: string;
  caption?: string;
  description?: string;
  active?: boolean;
  images?: string[];
  price?: string | number | null;
  recurring?: {
    interval: string,
    interval_count: number
  },
  metadata?: object
}

export class ProductModel implements ProductModel {
  constructor(productData?: ProductModel) {
    if (productData) {
      Object.assign(this, productData);
    }
  }
}
import stripe
stripe.api_key = stripe_test_private_key
def get_all_stripe_objects(stripe_listable):
    objects = []
    get_more = True
    starting_after = None
    while get_more:
        #stripe.Customer implements ListableAPIResource(APIResource):
        resp = stripe_listable.list(limit=100,starting_after=starting_after)
        objects.extend(resp['data'])
        get_more = resp['has_more']
        if len(resp['data'])>0:
            starting_after = resp['data'][-1]['id']
    return objects
all_stripe_products = get_all_stripe_objects(stripe.Product)
all_stripe_prices = get_all_stripe_objects(stripe.Price)