{"id":1670,"date":"2021-08-24T21:51:08","date_gmt":"2021-08-24T12:51:08","guid":{"rendered":"http:\/\/www.agile-software.site\/?p=1670"},"modified":"2021-08-27T15:31:37","modified_gmt":"2021-08-27T06:31:37","slug":"stripedjango","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=1670","title":{"rendered":"stripe+Django"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>pip install stripe<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">settings.py<\/h2>\n\n\n\n<p>stripe\u306e\u958b\u767a&gt;API\u304b\u3089\u516c\u958b\u53ef\u80fd\u30ad\u30fc\u3068\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30ad\u30fc\u3092\u8cbc\u308a\u4ed8\u3051\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>\nSTRIPE_PUBLIC_KEY = &#39;&#39;\nSTRIPE_SECRET_KEY = &#39;&#39;<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">checkout\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u4f5c\u6210<\/h2>\n\n\n\n<p>\u4fa1\u683c\u3084\u5728\u5eab\u72b6\u6cc1\u306a\u3069\u3001\u5546\u54c1\u5728\u5eab\u306b\u95a2\u3059\u308b\u6a5f\u5bc6\u60c5\u5831\u306f\u5e38\u306b\u30b5\u30fc\u30d0\u30fc\u5074\u306b\u7f6e\u304d\u3001\u9867\u5ba2\u304c\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u304b\u3089\u64cd\u4f5c\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3059\u3002&nbsp;<code>price_data<\/code>&nbsp;\u307e\u305f\u306f&nbsp;<a href=\"https:\/\/stripe.com\/docs\/payments\/accept-a-payment?platform=web&amp;ui=checkout#create-product-prices-upfront\" target=\"_blank\" rel=\"noreferrer noopener\">\u4e8b\u524d\u5b9a\u7fa9\u3055\u308c\u305f\u4fa1\u683c<\/a>\u3092\u4f7f\u7528\u3057\u3066\u3001Checkout \u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u5546\u54c1\u60c5\u5831\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u306e ID \u3092\u6e21\u3057\u307e\u3059\u3002<br>\u4eca\u56de\u306f\u6708\u984d\u306e\u30b5\u30d6\u30b9\u30af\u30ea\u30d7\u30b7\u30e7\u30f3<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>           line_items=[\n               {\n                   &#39;price_data&#39;: {\n                       &#39;currency&#39;: &#39;usd&#39;,\n                       &#39;unit_amount&#39;: 2000,\n                       &#39;product_data&#39;: {\n                           &#39;name&#39;: &#39;Stubborn Attachments&#39;,\n                           &#39;images&#39;: [&#39;https:\/\/i.imgur.com\/EHyR2nP.png&#39;],\n                       },\n                   },\n                   &#39;quantity&#39;: 1,\n               },\n           ],<\/code><\/pre><\/div>\n\n\n\n<p>mode\u304cpaiment\u306e\u5834\u5408\u3002subscription\u306b\u3059\u308b\u3068\u6708\u984d<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>mode=&#39;subscription&#39;,<\/code><\/pre><\/div>\n\n\n\n<p>\u6c7a\u6e08\u304c\u6210\u529f\u3057\u305f\u5834\u5408\u3068\u30ad\u30e3\u30f3\u30bb\u30eb\u30da\u30fc\u30b8\u306e URL \u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br>success_url\u3068cancel_url\u306f\u3001Django\u306e\u95a2\u6570(build_absolute_uri\u3001reverse)\u3092\u5229\u7528\u3057\u3066\u3001full path\u3092\u751f\u6210<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>success_url=request.build_absolute_uri(reverse(&#39;success.html&#39;)),\ncancel_url=request.build_absolute_uri(reverse(&#39;cancel.html&#39;)),<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"views.py\"><code>import stripe\nfrom django.views import generic\nfrom django.http import JsonResponse\nfrom django.urls import reverse\n\n\ndef create_checkout_session(request):\n   stripe.api_key = settings.STRIPE_SECRET_KEY\n\n   try:\n       checkout_session = stripe.checkout.Session.create(\n           payment_method_types=[&#39;card&#39;],\n           line_items=[\n               {\n                   &#39;price_data&#39;: {\n                       &#39;currency&#39;: &#39;usd&#39;,\n                       &#39;unit_amount&#39;: 2000,\n                       &#39;product_data&#39;: {\n                           &#39;name&#39;: &#39;Stubborn Attachments&#39;,\n                           &#39;images&#39;: [&#39;https:\/\/i.imgur.com\/EHyR2nP.png&#39;],\n                       },\n                   },\n                   &#39;quantity&#39;: 1,\n               },\n           ],\n           mode=&#39;payment&#39;,\n           success_url=request.build_absolute_uri(reverse(&#39;payment:success&#39;)),\n           cancel_url=request.build_absolute_uri(reverse(&#39;payment:cancel&#39;)),\n       )\n       return JsonResponse({&#39;id&#39;: checkout_session.id})\n   except Exception as e:\n       return JsonResponse({&#39;error&#39;:str(e)})<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"> cource.html,success.html\u3068cancel.html\u3092\u4f5c\u6210<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>{% extends &quot;api\/base.html&quot; %}\n\n{% block content %}\n{% if user.is_authenticated %}\n&lt;form action=&quot;\/create-checkout-session&quot; method=&quot;POST&quot;&gt;\n  &lt;section&gt;\n    &lt;button type=&quot;button&quot; id=&quot;checkout-button&quot;&gt;1\u304b\u6708\u7121\u6599\u4f53\u9a13\u7533\u3057\u8fbc\u307f&lt;\/button&gt;\n  &lt;\/section&gt;\n&lt;\/form&gt;\n\n&lt;script type=&quot;text\/javascript&quot;&gt;\n  \/\/ Create an instance of the Stripe object with your publishable API key\n  var stripe = Stripe(&quot;pk_live_51JQu8WBHrrkrpmkS3FAPZsOxnpSSKXFwsnGdfqfGr1FRlgoRdvZaN64KqiLPSe0Bzzjd2o1hjEc3IIaAGFPAGIW200HmTQ2qAk&quot;);\n  var checkoutButton = document.getElementById(&quot;checkout-button&quot;);\n\n  checkoutButton.addEventListener(&quot;click&quot;, function () {\n    fetch(&quot;\/create_checkout_session\/&quot;, {\n      method: &quot;POST&quot;,\n      headers: {\n          &#39;Accept&#39;: &#39;application\/json&#39;,\n          &#39;Content-Type&#39;: &#39;application\/json; charset=UTF-8&#39;,\n          &#39;X-CSRFToken&#39;: &#39;{{ csrf_token }}&#39;\n      },\n    })\n      .then(function (response) {\n        return response.json();\n      })\n      .then(function (session) {\n        return stripe.redirectToCheckout({ sessionId: session.id });\n      })\n      .then(function (result) {\n        \/\/ If redirectToCheckout fails due to a browser or network\n        \/\/ error, you should display the localized error message to your\n        \/\/ customer using error.message.\n        if (result.error) {\n          alert(result.error.message);\n        }\n      })\n      .catch(function (error) {\n        console.error(&quot;Error:&quot;, error);\n      });\n  });\n&lt;\/script&gt;\n{% endif %}\n\n{% endblock %}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"cancel.html\"><code>&lt;html&gt;\n&lt;head&gt;\n &lt;title&gt;\u8cfc\u5165\u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u3057\u305f&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n &lt;section&gt;\n   &lt;h1&gt;\u8cfc\u5165\u304c\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u3057\u305f&lt;\/h1&gt;\n   &lt;a href=&quot;{% url &#39;cource&#39; %}&quot;&gt;\u5546\u54c1\u30da\u30fc\u30b8\u306b\u3082\u3069\u308b&lt;\/a&gt;\n &lt;\/section&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"success.html\"><code>&lt;html&gt;\n&lt;head&gt;\n &lt;title&gt;\u8cfc\u5165\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\uff01&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n &lt;section&gt;\n   &lt;h1&gt;\u8cfc\u5165\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\uff01&lt;\/h1&gt;\n   &lt;a href=&quot;{% url &#39;ebay_circle&#39; %}&quot;&gt;\u30de\u30a4\u30da\u30fc\u30b8\u3078&lt;\/a&gt;\n &lt;\/section&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">URL<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"urls.py\"><code>urlpatterns = [   \n    path(&#39;cource\/&#39;, views.CourceView.as_view(), name=&#39;cource&#39;),\n \u3000path(&#39;create_checkout_session\/&#39;, views.create_checkout_session, name=&#39;checkout_session&#39;),\n    path(&#39;success\/&#39;, views.PaymentSuccessView.as_view(), name=&#39;success&#39;),\n    path(&#39;cancel\/&#39;, views.PaymentCancelView.as_view(), name=&#39;cancel&#39;),\n]<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb settings.py stripe\u306e\u958b\u767agt;API\u304b\u3089\u516c\u958b\u53ef\u80fd\u30ad\u30fc\u3068\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30ad\u30fc\u3092\u8cbc\u308a\u4ed8\u3051\u307e\u3059\u3002 checkout\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u4f5c\u6210 \u4fa1\u683c\u3084\u5728\u5eab\u72b6\u6cc1\u306a\u3069\u3001\u5546\u54c1\u5728\u5eab\u306b\u95a2\u3059\u308b\u6a5f\u5bc6\u60c5\u5831\u306f\u5e38\u306b\u30b5\u30fc\u30d0\u30fc\u5074\u306b\u7f6e<\/p>\n","protected":false},"author":1,"featured_media":1683,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jin_ogp_image_url":"","_jin_last_featured_id":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django"],"_links":{"self":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1670"}],"version-history":[{"count":2,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1670\/revisions"}],"predecessor-version":[{"id":1684,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1670\/revisions\/1684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/media\/1683"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}