{"id":53702,"date":"2024-12-03T12:25:53","date_gmt":"2024-12-03T04:25:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53702\/"},"modified":"2024-12-03T12:25:53","modified_gmt":"2024-12-03T04:25:53","slug":"nodejs-%e4%b8%ad%e7%9a%84-stripe-%e8%ae%a2%e9%98%85%e9%9b%86%e6%88%90-%e7%bb%88%e6%9e%81%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53702\/","title":{"rendered":"Nodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Nodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aNodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241203\/1733231565674f03cd55b1c.jpg\" class=\"aligncenter\" title=\"Nodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]\u63d2\u56fe\" alt=\"Nodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]\u63d2\u56fe\" \/><\/p>\n<p>\u8ba9 stripe \u8ba2\u9605\u4e0e\u540e\u7aef\u670d\u52a1\u914d\u5408\u4f7f\u7528\u53ef\u80fd\u4f1a\u5f88\u68d8\u624b\uff0c\u5e76\u4e14\u5e38\u5e38\u4f1a\u5bfc\u81f4\u5f00\u53d1\u4eba\u5458\u6240\u8c13\u7684\u53ef\u6015\u7684\u201c\u5927\u8111\u5206\u88c2\u201d\u2014\u2014\u540c\u6b65\u7ba1\u7406 stripe \u7684\u903b\u8f91\u548c\u60a8\u81ea\u5df1\u7684\u540e\u7aef\u6570\u636e\u3002<\/p>\n<p>\u5728 vratix\uff0c\u6211\u4eec\u5728\u6784\u5efa\u5f00\u6e90 stripe \u8ba2\u9605 api \u6a21\u5757\u65f6\u6b63\u9762\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u6211\u4eec\u5982\u4f55\u5728 node.js \u4e2d\u5904\u7406 stripe \u8ba2\u9605\u8ba1\u8d39\uff0c\u4ee5\u4fdd\u6301\u4e8b\u60c5\u7b80\u5355\u3001\u53ef\u6269\u5c55\u4e14\u5bf9\u5f00\u53d1\u4eba\u5458\u53cb\u597d\u3002<\/p>\n<h3> \u6838\u5fc3\u539f\u5219\uff1a\u8ba9 stripe \u6210\u4e3a\u771f\u7406\u4e4b\u6e90 <\/h3>\n<p>\u5173\u952e\u662f<strong>\u5c06\u5c3d\u53ef\u80fd\u591a\u7684\u903b\u8f91\u8f6c\u79fb\u5230stripe<\/strong>\uff0c\u540c\u65f6\u4fdd\u6301\u6570\u636e\u5e93\u6700\u5c0f\u5316\u3002\u6211\u4eec\u53ea\u5b58\u50a8\uff1a<\/p>\n<ul>\n<li>\u5ba2\u6237 id<\/li>\n<li>\u8ba2\u9605id<\/li>\n<li>\u8ba1\u5212<\/li>\n<\/ul>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u907f\u514d\uff1a<\/p>\n<ul>\n<li>\u8fc7\u4e8e\u590d\u6742\u7684\u540e\u7aef\u903b\u8f91<\/li>\n<li>\u7528\u4e8e\u540c\u6b65\u4eea\u8868\u677f\u66f4\u6539\u7684\u5bb9\u6613\u51fa\u9519\u7684 webhook \u5b9e\u73b0<\/li>\n<li>\u6570\u636e\u5197\u4f59<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u4ecd\u7136\u62e5\u6709\u529f\u80fd\u9f50\u5168\u7684\u8ba2\u9605\u8ba1\u8d39\u7cfb\u7edf\uff0c\u540c\u65f6\u4f9d\u8d56 stripe <strong>\u4f5c\u4e3a\u552f\u4e00\u7684\u4e8b\u5b9e\u6765\u6e90<\/strong>\u3002<\/p>\n<h3> \u6211\u4eec\u5b9e\u65bd\u7684\u7279\u70b9 <\/h3>\n<p>\u5728\u672c\u6307\u5357\u7ed3\u675f\u65f6\uff0c\u60a8\u5c06\u62e5\u6709\u4e00\u4e2a\u57fa\u4e8e\u8ba2\u9605\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u652f\u6301\uff1a<\/p>\n<ul>\n<li>\u7528\u6237\u8ba2\u9605\u8ba1\u5212<\/li>\n<li>\u7ed3\u5e10\u4f1a\u8bae<\/li>\n<li>\u8ba2\u9605\u8ffd\u52a0\u9500\u552e<\/li>\n<li>\u53ef\u7528\u8ba1\u5212\u5217\u8868<\/li>\n<\/ul>\n<h4> \u6280\u672f\u5806\u6808 <\/h4>\n<ul>\n<li>postgresql<\/li>\n<li>node.js express.js<\/li>\n<li>\u6253\u5b57\u7a3f<\/li>\n<\/ul>\n<h3> \u7b2c1\u6b65\uff1a\u6570\u636e\u5e93\u8bbe\u8ba1 <\/h3>\n<p>\u6211\u4eec\u9996\u5148\u8bbe\u8ba1\u4e00\u4e2a\u5e72\u51c0\u3001\u6700\u5c0f\u7684\u6570\u636e\u5e93\u8868\uff1a<\/p>\n<pre>create table user_subscriptions (  \n    \"id\" serial primary key,  \n    \"plan\" varchar not null,  \n    \"user_id\" integer not null references users(id) on delete cascade,  \n    \"customer_id\" varchar,  \n    \"subscription_id\" varchar not null,  \n    \"is_owner\" boolean not null default true,  \n    \"created_at\" timestamp not null default now(),  \n    unique (user_id, subscription_id)  \n);\n<\/pre>\n<p>\u8981\u70b9\uff1a<\/p>\n<ul>\n<li> user_id\uff1a\u5f15\u7528\u60a8\u7684\u5185\u90e8\u7528\u6237\u8868<\/li>\n<li> plan\uff1a\u8ddf\u8e2a\u8ba2\u9605\u8ba1\u5212<\/li>\n<li> subscription_id\uff1astripe \u8ba2\u9605 id<\/li>\n<li> is_owner\uff1a\u6807\u8bb0\u4e3b\u8981\u8ba2\u9605\u6301\u6709\u8005<\/li>\n<\/ul>\n<h3> \u7b2c 2 \u6b65\uff1a\u63a7\u5236\u5668 <\/h3>\n<p>\u6211\u4eec\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u6765\u4fdd\u6301\u4e1a\u52a1\u903b\u8f91\u6a21\u5757\u5316\u548c\u53ef\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u6211\u4eec\u7684 stripe \u8ba2\u9605\u63a7\u5236\u5668\u7684\u7247\u6bb5\uff1a<\/p>\n<pre>async getsubscriptions() {  \n  const stripeprices = await stripe.prices.list({  \n    active: true,  \n    type: \"recurring\",  \n    expand: [\"data.product\"],  \n  });  \n\n  return stripeprices.data.map((price) =&gt; {  \n    const product = price.product as stripe.product;  \n    return {  \n      plan: price.lookup_key || product.name.tolowercase().replaceall(\" \", \"_\"),  \n      name: product.name,  \n      priceid: price.id,  \n      interval: price.recurring!.interval,  \n      price: { currency: price.currency, amount: price.unit_amount },  \n    };  \n  });  \n}  \n<\/pre>\n<p>\u4e3b\u8981\u4eae\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u81ea\u5b9a\u4e49\u8ba2\u9605\u5bc6\u94a5<\/strong>\uff1a\u6e90\u81ea\u4ea7\u54c1\u540d\u79f0\u6216\u7528\u4e8e\u6e05\u7406\u8ba1\u5212\u68c0\u67e5\u7684lookup_key\uff08user.plan === &#8216;pro_plan&#8217;\uff09\u3002<\/li>\n<li> <strong>stripe-first \u65b9\u6cd5<\/strong>\uff1a\u6211\u4eec\u76f4\u63a5\u4ece stripe \u83b7\u53d6\u8ba2\u9605\u6570\u636e\uff0c\u907f\u514d\u4e86\u201c\u8111\u88c2\u201d\u3002<\/li>\n<\/ul>\n<h3> \u7b2c 3 \u6b65\uff1a\u7b80\u5316 stripe \u7ed3\u8d26 <\/h3>\n<p>\u6211\u4eec\u7684 createcheckout \u51fd\u6570\u8bbe\u7f6e\u8ba2\u9605\u7ed3\u8d26\u4f1a\u8bdd\uff1a<\/p>\n<pre>const checkout = await stripe.checkout.sessions.create({  \n  line_items: [  \n    {  \n      price: priceid,  \n      adjustable_quantity: { enabled: true },  \n      quantity: seats || 1,  \n    },  \n  ],  \n  mode: \"subscription\",  \n  subscription_data: { metadata: { userid } },  \n  success_url: checkout_success_url,  \n  cancel_url: checkout_cancel_url,  \n});  \n\nreturn { url: checkout.url! };  \n<\/pre>\n<h3> \u60f3\u8df3\u8fc7\u8fd9\u4e00\u5207\u5417\uff1f <\/h3>\n<p>\u6211\u4eec\u5df2\u5c06\u6240\u6709\u5185\u5bb9\u6253\u5305\u5230\u4e00\u4e2a\u73b0\u6210\u7684\u5f00\u6e90\u6a21\u5757\u4e2d\u3002 <strong>\u4e0d\u5230 30 \u79d2<\/strong>\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\uff1a<\/p>\n<ul>\n<li>\u6761\u7eb9\u96c6\u6210<\/li>\n<li>\u8eab\u4efd\u9a8c\u8bc1<\/li>\n<li>\u6570\u636e\u5e93\u914d\u7f6e<\/li>\n<li>\u9884\u5efa\u8def\u7531\u548c sql \u67e5\u8be2<\/li>\n<\/ul>\n<p>\u8fd0\u884c\u8fd9\u4e2a\uff1a<\/p>\n<pre>npx vratix init  \n<\/pre>\n<p>\u67e5\u770b\u6211\u4eec\u7684 stripe \u8ba2\u9605\u6a21\u5757\u6587\u6863\u4ee5\u4e86\u89e3\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u5b8c\u6574\u4ee3\u7801\u53ef\u5728\u6211\u4eec\u7684 github \u5b58\u50a8\u5e93\u4e2d\u83b7\u53d6\u3002<\/p>\n<p>\u5728\u6b64\u5904\u89c2\u770b\u6f14\u793a\u89c6\u9891\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u5de5\u4f5c ui \u5b8c\u6210\u6240\u6709\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<p>\u6211\u5f88\u60f3\u542c\u542c\u60a8\u7684\u60f3\u6cd5 &#8211; \u8fd9\u662f\u5426\u4f1a\u8ba9\u6784\u5efa\u8ba2\u9605 api \u53d8\u5f97\u66f4\u5bb9\u6613\uff1f\u8ba9\u6211\u4eec\u77e5\u9053\u60a8\u63a5\u4e0b\u6765\u60f3\u770b\u5230\u54ea\u4e9b\u529f\u80fd\uff01 <\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aNodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5e2e\u52a9\u60a8\u63d0\u9ad8\u5b66\u4e60\u7f16\u7801\u6548\u7387\u7684\u4e60\u60ef\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Nodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357] \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7\u300aNodejs \u4e2d\u7684 Stripe \u8ba2\u9605\u96c6\u6210 [\u7ec8\u6781\u6307\u5357]\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01 \u8ba9 stripe \u8ba2\u9605\u4e0e\u540e\u7aef\u670d\u52a1\u914d\u5408\u4f7f\u7528\u53ef\u80fd\u4f1a\u5f88\u68d8\u624b\uff0c\u5e76\u4e14\u5e38\u5e38\u4f1a\u5bfc\u81f4\u5f00\u53d1\u4eba\u5458\u6240\u8c13\u7684\u53ef\u6015\u7684\u201c\u5927\u8111\u5206\u88c2\u201d\u2014\u2014\u540c\u6b65\u7ba1\u7406 stripe \u7684\u903b\u8f91\u548c\u60a8\u81ea\u5df1\u7684\u540e\u7aef\u6570\u636e\u3002 \u5728 vratix\uff0c\u6211\u4eec\u5728\u6784\u5efa\u5f00\u6e90 stripe \u8ba2\u9605 api \u6a21\u5757\u65f6\u6b63\u9762\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u6211\u4eec\u5982\u4f55\u5728 node.js \u4e2d\u5904\u7406 stripe \u8ba2\u9605\u8ba1\u8d39\uff0c\u4ee5\u4fdd\u6301\u4e8b\u60c5\u7b80\u5355\u3001\u53ef\u6269\u5c55\u4e14\u5bf9\u5f00\u53d1\u4eba\u5458\u53cb\u597d\u3002 \u6838\u5fc3\u539f\u5219\uff1a\u8ba9 stripe \u6210\u4e3a\u771f\u7406\u4e4b\u6e90 \u5173\u952e\u662f\u5c06\u5c3d\u53ef\u80fd\u591a\u7684\u903b\u8f91\u8f6c\u79fb\u5230stripe\uff0c\u540c\u65f6\u4fdd\u6301\u6570\u636e\u5e93\u6700\u5c0f\u5316\u3002\u6211\u4eec\u53ea\u5b58\u50a8\uff1a \u5ba2\u6237 id \u8ba2\u9605id \u8ba1\u5212 \u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u907f\u514d\uff1a \u8fc7\u4e8e\u590d\u6742\u7684\u540e\u7aef\u903b\u8f91 \u7528\u4e8e\u540c\u6b65\u4eea\u8868\u677f\u66f4\u6539\u7684\u5bb9\u6613\u51fa\u9519\u7684 webhook \u5b9e\u73b0 \u6570\u636e\u5197\u4f59 \u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u4ecd\u7136\u62e5\u6709\u529f\u80fd\u9f50\u5168\u7684\u8ba2\u9605\u8ba1\u8d39\u7cfb\u7edf\uff0c\u540c\u65f6\u4f9d\u8d56 stripe \u4f5c\u4e3a\u552f\u4e00\u7684\u4e8b\u5b9e\u6765\u6e90\u3002 \u6211\u4eec\u5b9e\u65bd\u7684\u7279\u70b9 \u5728\u672c\u6307\u5357\u7ed3\u675f\u65f6\uff0c\u60a8\u5c06\u62e5\u6709\u4e00\u4e2a\u57fa\u4e8e\u8ba2\u9605\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u652f\u6301\uff1a \u7528\u6237\u8ba2\u9605\u8ba1\u5212 \u7ed3\u5e10\u4f1a\u8bae \u8ba2\u9605\u8ffd\u52a0\u9500\u552e \u53ef\u7528\u8ba1\u5212\u5217\u8868 \u6280\u672f\u5806\u6808 postgresql node.js express.js \u6253\u5b57\u7a3f \u7b2c1\u6b65\uff1a\u6570\u636e\u5e93\u8bbe\u8ba1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53702","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53702"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53702\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}