Skip to content

Instantly share code, notes, and snippets.

Avatar

Fabian Zitter Ninigi

  • Tokyo, Japan
View GitHub Profile
@Ninigi
Ninigi / hide_and_show.js
Last active Mar 23, 2020
How to hide and show payment gateways in shopify checkout (COD)
View hide_and_show.js
// The Payment Gateway / Shippnig Method can be internationalized,
// if the shop does not use this internationalization, use ["Cash on Delivery (COD)"]
var CODGatewayNames = ["代金引換", "Cash on Delivery (COD)"],
CODShippingNames = ["代金引換", "Cash on Delivery"],
radioButtonSelector = ".radio__input .input-radio";
function toggleCODPayment(shippingElement) {
var isCOD = function(el) {
var label = el.querySelectorAll(".radio__label__primary")[0];
var labelText = label ? label.textContent.trim().toLowerCase() : "";
View hide_and_show_paypal.js
function toggleCountryPayment() {
var isPayPal = function(el) {
return el.querySelectorAll(".radio__label__primary")[0] && el.querySelectorAll(".radio__label__primary")[0].textContent.trim() === "PayPal";
}
var shipToElement = document.querySelector("address.address") && document.querySelector("address.address").firstChild;
if (shipToElement && shipToElement.data.indexOf("Malaysia") > -1) {
var els = document.querySelectorAll(".section--payment-method .radio-wrapper.content-box__row"),
radioButtons = [];
View embedded_app_plug.ex
defmodule Useless.Plugs.EmbeddedAppPlug do
@moduledoc """
Loads the shop from database, puts a private shop field in conn and sets x-frame-options.
Will halt if no shop found
"""
alias Plug.Conn
alias Useless.ShopifyApp
View shop_controller.ex
defmodule UselessWeb.ShopController do
# ...
def new(conn, %{"hmac" => _, "shop" => shopify_domain}) do
shop = ShopifyApp.find_shop_by(%{shopify_domain: shopify_domain})
conn
|> put_layout({UselessWeb.EmbeddedAppView, "layout.html"})
|> render("show.html", shop: shop)
end
View shop_controller.ex
defmodule UselessWeb.ShopController do
# ...
def new(conn, %{"shop" => shop_name}) when is_bitstring(shop_name),
do: signup(conn, %{"shop" => %{"shopify_domain" => shop_name}})
def new(conn, _params) do
changeset = ShopifyApp.change_shop(%Shop{})
render(conn, "new.html", changeset: changeset)
end
View auth_controller.ex
defmodule UselessWeb.AuthController do
use UselessWeb, :controller
alias Useless.ShopifyApp
alias Useless.ShopifyApp.Shop
def shopify(conn, %{"shop" => shopify_domain, "code" => shopify_token}) do
session = shopify_domain |> Shopify.session()
with {:ok, %{data: oauth}} <- Shopify.OAuth.request_token(session, shopify_token),
View shop_controller.ex
defmodule UselessWeb.ShopController do
use UselessWeb, :controller
alias Useless.ShopifyApp
alias Useless.ShopifyApp.Shop
def new(conn, _params) do
changeset = ShopifyApp.change_shop(%Shop{})
render(conn, "new.html", changeset: changeset)
end
View shopify_app.ex
@doc """
Creates a new shop if no shop with the given shopify_domain exists, or updates
the existing shop
## Examples
iex> create_or_update_shop(%{shopify_domain: "shop@my_shopify.com", shopify_token: "a-token"})
{:ok, %Shop{}}
iex> create_or_update_shop(%{shopify_domain: nil, shopify_token: "a-token"})
View shopify_app_test.exs
test "create_or_update_shop/1 creates a shop if it doesn't already exist" do
assert {:ok, %Shop{} = shop} = ShopifyApp.create_or_update_shop(@valid_attrs)
assert shop.shopify_domain == @valid_attrs.shopify_domain
assert shop.shopify_token == @valid_attrs.shopify_token
end
test "create_or_update_shop/1 updates a shop's token if it already exists" do
shop_fixture()
params = %{
shopify_domain: @valid_attrs.shopify_domain,
View new.html.eex
<h2>New Shop</h2>
<%= render "signup_form.html", Map.put(assigns, :action, shop_path(@conn, :signup)) %>
You can’t perform that action at this time.