Skip to content

Instantly share code, notes, and snippets.

@youmikoh
Last active May 23, 2022 11:17
Show Gist options
  • Save youmikoh/05ac32f6d7d90fea350a169a27e3b107 to your computer and use it in GitHub Desktop.
Save youmikoh/05ac32f6d7d90fea350a169a27e3b107 to your computer and use it in GitHub Desktop.
dev_store_test_orders .ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "dev_store_test_orders .ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOnzsbrFB2QewTyvmJNwC88",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/youmikoh/05ac32f6d7d90fea350a169a27e3b107/dev_store_test_orders.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Tools for creating valid test orders on a development store 🛠\n",
"\n"
],
"metadata": {
"id": "PnqfRB2nLiIj"
}
},
{
"cell_type": "markdown",
"source": [
"> This is a python implementation - to same can be achieved with tools like [Postman](https://postman.com/) or [Insomnia](https://insomnia.rest/). Please refer to [this practical guide using Postman]().\n",
"\n"
],
"metadata": {
"id": "pi0PBDr2hoZC"
}
},
{
"cell_type": "markdown",
"source": [
"### ✅ Requirements\n",
"\n",
"1. a [development store](https://shopify.dev/apps/tools/development-stores#create-a-development-store-to-test-your-app)\n",
"2. a [custom app from the Admin](https://help.shopify.com/en/manual/apps/custom-apps#create-and-install-a-custom-app) of your development store with access to write/edit orders\n",
"3. Admin API access token from your custom app - note: this can only be revealed once after installation"
],
"metadata": {
"id": "TsTUEdcvNHVH"
}
},
{
"cell_type": "code",
"source": [
"import requests\n",
"import time"
],
"metadata": {
"id": "_YSgyXh3URrD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"SHOP = \"{your-dev-store-name}\"\n",
"ACCESS_TOKEN = \"{your-admin-api-access-token}\""
],
"metadata": {
"id": "tFlByKQmhHJ0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"headers = {\"X-Shopify-Access-Token\": ACCESS_TOKEN}\n",
"\n",
"def rest_admin_api(x):\n",
" return f\"https://{SHOP}.myshopify.com/admin/api/2022-04/{x}\"\n",
" \n",
"graphql_admin_api = f\"https://{SHOP}.myshopify.com/admin/api/2022-04/graphql.json\""
],
"metadata": {
"id": "cYmoonO5mQcu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 🔓 Valid test orders that will NOT lock checkout via `REST`\n",
"\n",
"> 🗒 *reference docs:*\n",
"- [Test orders](https://shopify.dev/api/examples/order-testing)\n",
"- [POST Create an order](https://shopify.dev/api/admin-rest/2022-01/resources/order#post-orders)\n",
"- [GET Retrieves a specific transaction](https://shopify.dev/api/admin-rest/2022-01/resources/transaction#get-orders-order-id-transactions-transaction-id)\n",
"- [PUT Update an order](https://shopify.dev/api/admin-rest/2022-01/resources/order#put-orders-order-id)"
],
"metadata": {
"id": "-4mbmpjnVwwy"
}
},
{
"cell_type": "code",
"source": [
"def create_order():\n",
" order_body = {\n",
" \"order\": {\n",
" \"note\": \"order created by rest admin api\",\n",
" \"line_items\": line_items,\n",
" \"transactions\": [\n",
" {\n",
" \"kind\": \"authorization\",\n",
" \"status\": \"success\",\n",
" \"amount\": 200\n",
" }\n",
" ]\n",
" }\n",
" }\n",
" response = requests.post(\n",
" rest_admin_api(\"orders.json\"),\n",
" json=order_body,\n",
" headers=headers,\n",
" )\n",
" if response.ok:\n",
" order_id = response.json().get(\"order\").get(\"id\")\n",
" print(\"created order:\", order_id)\n",
" return order_id\n",
" else:\n",
" return Exception(f\"failed to create order: {response.reason}\")\n"
],
"metadata": {
"id": "jVgTzenTf1t3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def capture_test_order(order_id):\n",
" response = requests.get(\n",
" rest_admin_api(f\"orders/{order_id}/transactions.json\"),\n",
" headers=headers,\n",
" )\n",
" if response.ok:\n",
" transaction_id = response.json().get(\"transactions\").pop().get(\"id\")\n",
" print(\"retrieved order transaction:\", transaction_id)\n",
" else:\n",
" return Exception(f\"failed to retrieve order transaction: {response.reason}\")\n",
" \n",
" transaction_body = {\n",
" \"transaction\": {\n",
" \"kind\": \"capture\",\n",
" \"parent_id\": transaction_id,\n",
" \"test\": \"true\"\n",
" }\n",
" }\n",
" capture = requests.post(\n",
" rest_admin_api(f\"orders/{order_id}/transactions.json\"),\n",
" json=transaction_body,\n",
" headers=headers\n",
" )\n",
" if capture.ok:\n",
" print(\"captured test order:\", order_id)\n",
" else:\n",
" return Exception(f\"failed to capture test order: {capture.reason}\")"
],
"metadata": {
"id": "bvjGGP7C-_wS"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"line_items = [\n",
" {\n",
" \"title\": \"test order item\",\n",
" \"price\": \"200\",\n",
" \"quantity\": 1,\n",
" },\n",
"]\n",
"\n",
"order = create_order()\n",
"capture_test_order(order)"
],
"metadata": {
"id": "5Nejvuk0qdkV",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "09db1882-9955-4c8d-bb5a-d7a949cabe22"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"created order: 4796462792941\n",
"retrieved order transaction: 5602279817453\n",
"captured test order: 4796462792941\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"### WIP: 🔓 Valid test orders that will NOT lock checkout via [`shopify_python_api`](https://github.com/Shopify/shopify_python_api)."
],
"metadata": {
"id": "cOP5VsfIglhw"
}
},
{
"cell_type": "code",
"source": [
"!pip install --upgrade ShopifyAPI"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "IpsC0i9DgivE",
"outputId": "3046507b-7208-4d65-ddfd-242094669ce9"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: ShopifyAPI in /usr/local/lib/python3.7/dist-packages (11.0.0)\n",
"Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from ShopifyAPI) (3.13)\n",
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from ShopifyAPI) (1.15.0)\n",
"Requirement already satisfied: PyJWT>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from ShopifyAPI) (2.4.0)\n",
"Requirement already satisfied: pyactiveresource>=2.2.2 in /usr/local/lib/python3.7/dist-packages (from ShopifyAPI) (2.2.2)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "9YZc-285gy7G"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment