Ví dụ về đoạn mã tùy chỉnh thanh toán
Đoạn mã tùy chỉnh thanh toán tương tác với cổng thanh toán, đồng thời có thể thay đổi tiêu đề, trạng thái hiển thị và vị trí hiển thị của cổng. Các đoạn mã này chạy mỗi khi khách hàng truy cập vào trang phương thức thanh toán lúc thanh toán. Đoạn mã tùy chỉnh thanh toán không tương tác với thanh toán tức thì vì phương thức thanh toán tức thì sẽ hiển thị cho khách hàng trước khi họ đến trang thanh toán.
Để dùng các mẫu trên trang này, hãy tạo đoạn mã mới với mẫu trống.
Các bước:
Từ bảng điều khiển quản trị Shopify, đi tới Ứng dụng > Script Editor.
Nhấp vào Tạo đoạn mã.
Nhấp vào Cổng thanh toán.
Chọn Mẫu trống, sau đó nhấp vào Tạo đoạn mã.
Trong phần Mã nguồn Ruby, xóa dòng mã mặc định:
Output.cart = Input.cartSao chép đoạn mã từ trang này rồi dán vào phần Mã nguồn Ruby.
Chỉnh sửa phần Cài đặt có thể tùy chỉnh của đoạn mã để chạy trong cửa hàng.
Kiểm tra đoạn mã. Để biết thêm thông tin, tham khảo mục Kiểm tra và gỡ lỗi Đoạn mã Shopify.
Sau khi kiểm tra:
- nhấp vào Lưu bản nháp để lưu bản nháp chưa đăng của đoạn mã, hoặc
- nhấp vào Lưu và đăng để tạo và đăng đoạn mã.
Trên trang này
Hiển thị cổng thanh toán đối với khách hàng cụ thể
Sử dụng đoạn mã này để chỉ hiển thị tùy chọn cổng thanh toán cụ thể cho những khách hàng được gắn thẻ cụ thể.
Ví dụ: chỉ hiển thị cổng thanh toán nhất định cho khách hàng có thẻ VIP – cổng thanh toán này sẽ bị ẩn đối với khách hàng khác.
# ================================ Customizable Settings ================================
# ================================================================
# Show Gateways For Customer Tag
#
# If we have a matching customer, the entered gateway(s) will be
# shown, and all others will be hidden. Otherwise, the entered
# gateway(s) will be hidden.
#
# - 'customer_tag_match_type' determines whether we look for the customer
# to be tagged with any of the entered tags or not. Can be:
# - ':include' to check if the customer is tagged
# - ':exclude' to make sure the customer isn't tagged
# - 'customer_tags' is a list of customer tags to trigger the
# campaign
# - 'gateway_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'gateway_names' is a list of strings to identify gateways
# ================================================================
SHOW_GATEWAYS_FOR_CUSTOMER_TAG = [
{
customer_tag_match_type: :include,
customer_tags: ["customer_tag", "another_tag"],
gateway_match_type: :exact,
gateway_names: ["Gateway", "Other Gateway"],
},
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# CustomerTagSelector
#
# Finds whether the supplied customer has any of the entered tags.
# ================================================================
class CustomerTagSelector
def initialize(match_type, tags)
@comparator = match_type == :include ? 'any?' : 'none?'
@tags = tags.map { |tag| tag.downcase.strip }
end
def match?(customer)
customer_tags = customer.tags.map { |tag| tag.downcase.strip }
(@tags & customer_tags).send(@comparator)
end
end
# ================================================================
# GatewayNameSelector
#
# Finds whether the supplied gateway name matches any of the
# entered names.
# ================================================================
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end
def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
# ================================================================
# ShowGatewaysForCustomerTagCampaign
#
# If the customer has any of the entered tags, the entered gateways
# are shown/hidden depending on the entered settings
# ================================================================
class ShowGatewaysForCustomerTagCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, payment_gateways)
@campaigns.each do |campaign|
customer_tag_selector = CustomerTagSelector.new(
campaign[:customer_tag_match_type],
campaign[:customer_tags],
)
customer_match = cart.customer.nil? ? false : customer_tag_selector.match?(cart.customer)
gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)
payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway) != customer_match
end
end
end
end
CAMPAIGNS = [
ShowGatewaysForCustomerTagCampaign.new(SHOW_GATEWAYS_FOR_CUSTOMER_TAG),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gatewaysẨn cổng thanh toán đối với khách hàng cụ thể
Sử dụng đoạn mã này để ẩn cổng thanh toán cụ thể đối với những khách hàng được gắn thẻ cụ thể.
Ví dụ: ẩn cổng thanh toán nhất định đối với khách hàng có thẻ HIDE_GATEWAY.
# ================================ Customizable Settings ================================
# ================================================================
# Hide Gateways For Customer Tag
#
# If we have a matching customer, the entered gateway(s) will be
# hidden.
#
# - 'customer_tag_match_type' determines whether we look for the customer
# to be tagged with any of the entered tags or not. Can be:
# - ':include' to check if the customer is tagged
# - ':exclude' to make sure the customer isn't tagged
# - 'customer_tags' is a list of customer tags to trigger the
# campaign
# - 'gateway_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'gateway_names' is a list of strings to identify gateways
# ================================================================
HIDE_GATEWAYS_FOR_CUSTOMER_TAG = [
{
customer_tag_match_type: :include,
customer_tags: ["customer_tag", "another_tag"],
gateway_match_type: :exact,
gateway_names: ["Gateway", "Other Gateway"],
},
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# CustomerTagSelector
#
# Finds whether the supplied customer has any of the entered tags.
# ================================================================
class CustomerTagSelector
def initialize(match_type, tags)
@comparator = match_type == :include ? 'any?' : 'none?'
@tags = tags.map { |tag| tag.downcase.strip }
end
def match?(customer)
customer_tags = customer.tags.map { |tag| tag.downcase.strip }
(@tags & customer_tags).send(@comparator)
end
end
# ================================================================
# GatewayNameSelector
#
# Finds whether the supplied gateway name matches any of the
# entered names.
# ================================================================
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end
def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
# ================================================================
# HideGatewaysForCustomerTagCampaign
#
# If we have a matching customer, the entered gateway(s) will be
# hidden.
# ================================================================
class HideGatewaysForCustomerTagCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, payment_gateways)
return if cart.customer.nil?
@campaigns.each do |campaign|
customer_tag_selector = CustomerTagSelector.new(
campaign[:customer_tag_match_type],
campaign[:customer_tags],
)
next unless customer_tag_selector.match?(cart.customer)
gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)
payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway)
end
end
end
end
CAMPAIGNS = [
HideGatewaysForCustomerTagCampaign.new(HIDE_GATEWAYS_FOR_CUSTOMER_TAG),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gatewaysẨn cổng thanh toán đối với sản phẩm cụ thể
Sử dụng đoạn mã này để ẩn cổng thanh toán cụ thể khi thêm các mặt hàng cụ thể vào giỏ hàng.
Ví dụ: ẩn cổng thanh toán nhất định nếu khách hàng đặt mua một chiếc mũ.
# ================================ Customizable Settings ================================
# ================================================================
# Hide Gateway(s) for Product
#
# If the cart contains any matching items, the entered gateway(s)
# are hidden.
#
# - 'product_selector_match_type' determines whether we look for
# products that do or don't match the entered selectors. Can
# be:
# - ':include' to check if the product does match
# - ':exclude' to make sure the product doesn't match
# - 'product_selector_type' determines how eligible products
# will be identified. Can be either:
# - ':tag' to find products by tag
# - ':type' to find products by type
# - ':vendor' to find products by vendor
# - ':product_id' to find products by ID
# - ':variant_id' to find products by variant ID
# - ':subscription' to find subscription products
# - 'product_selectors' is a list of strings or numbers to
# identify products by the above selector type
# - 'gateway_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'gateway_names' is a list of strings to identify gateways
# ================================================================
HIDE_GATEWAY_FOR_PRODUCT = [
{
product_selector_match_type: :include,
product_selector_type: :product_id,
product_selectors: [1234567890987, 1234567890986],
gateway_match_type: :exact,
gateway_names: ["Gateway", "Other Gateway"],
},
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# ProductSelector
#
# Finds matching products by the entered criteria.
# ================================================================
class ProductSelector
def initialize(match_type, selector_type, selectors)
@match_type = match_type
@comparator = match_type == :include ? 'any?' : 'none?'
@selector_type = selector_type
@selectors = selectors
end
def match?(line_item)
if self.respond_to?(@selector_type)
self.send(@selector_type, line_item)
else
raise RuntimeError.new('Invalid product selector type')
end
end
def tag(line_item)
product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@selectors & product_tags).send(@comparator)
end
def type(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@match_type == :include) == @selectors.include?(line_item.variant.product.product_type.downcase.strip)
end
def vendor(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@match_type == :include) == @selectors.include?(line_item.variant.product.vendor.downcase.strip)
end
def product_id(line_item)
(@match_type == :include) == @selectors.include?(line_item.variant.product.id)
end
def variant_id(line_item)
(@match_type == :include) == @selectors.include?(line_item.variant.id)
end
def subscription(line_item)
!line_item.selling_plan_id.nil?
end
end
# ================================================================
# GatewayNameSelector
#
# Finds whether the supplied gateway name matches any of the
# entered names.
# ================================================================
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end
def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
# ================================================================
# HideGatewayForProductCampaign
#
# If the cart contains any matching items, the entered gateway(s)
# are hidden.
# ================================================================
class HideGatewayForProductCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, payment_gateways)
@campaigns.each do |campaign|
product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors],
)
next unless cart.line_items.any? { |line_item| product_selector.match?(line_item) }
gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)
payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway)
end
end
end
end
CAMPAIGNS = [
HideGatewayForProductCampaign.new(HIDE_GATEWAY_FOR_PRODUCT),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gatewaysHiển thị cổng thanh toán đối với quốc gia cụ thể
Sử dụng đoạn mã này để chỉ hiển thị cổng thanh toán cụ thể khi khách hàng đặt hàng từ quốc gia cụ thể.
Ví dụ: chỉ hiển thị cổng thanh toán nhất định nếu khách hàng đặt hàng từ Canada – cổng thanh toán này sẽ bị ẩn đối với các địa chỉ ở quốc gia khác.
# ================================ Customizable Settings ================================
# ================================================================
# Show Gateway(s) for Country
#
# If the shipping address country matches any of the entered
# countries, the entered gateway(s) will be shown, and all others
# will be hidden. Otherwise, the entered gateway(s) will be hidden.
#
# - 'country_code_match_type' determines whether we look for the cart
# country to match the entered selectors or not. Can be:
# - ':include' to look for a country in the list
# - ':exclude' to make sure the country is not in the list
# - 'country_codes' is a list of 2-character abbreviations for
# the applicable countries
# - 'gateway_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'gateway_names' is a list of strings to identify gateways
# ================================================================
SHOW_GATEWAYS_FOR_COUNTRY = [
{
country_code_match_type: :include,
country_codes: ["CA"],
gateway_match_type: :exact,
gateway_names: ["Gateway", "Other Gateway"],
},
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# CountrySelector
#
# Finds whether the supplied country code matches the entered
# string.
# ================================================================
class CountrySelector
def initialize(match_type, countries)
@match_type = match_type
@countries = countries.map { |country| country.upcase.strip }
end
def match?(country_code)
(@match_type == :include) == @countries.include?(country_code.upcase.strip)
end
end
# ================================================================
# GatewayNameSelector
#
# Finds whether the supplied gateway name matches any of the
# entered names.
# ================================================================
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end
def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
# ================================================================
# ShowGatewaysForCountryCampaign
#
# If the shipping address country matches any of the entered
# countries, the entered gateway(s) will be shown, and all others
# will be hidden. Otherwise, the entered gateway(s) will be hidden.
# ================================================================
class ShowGatewaysForCountryCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, payment_gateways)
address = cart.shipping_address
@campaigns.each do |campaign|
country_selector = CountrySelector.new(
campaign[:country_code_match_type],
campaign[:country_codes],
)
country_match = address.nil? ? false : country_selector.match?(address.country_code)
gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)
payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway) != country_match
end
end
end
end
CAMPAIGNS = [
ShowGatewaysForCountryCampaign.new(SHOW_GATEWAYS_FOR_COUNTRY),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gatewaysẨn cổng thanh toán đối với quốc gia cụ thể
Sử dụng đoạn mã này để ẩn cổng thanh toán ở các quốc gia cụ thể.
Ví dụ: ẩn cổng thanh toán nhất định đối với khách hàng ở Canada.
# ================================ Customizable Settings ================================
# ================================================================
# Hide Gateway(s) for Country
#
# If the shipping address country matches any of the entered
# countries, the entered gateway(s) will be hidden.
#
# - 'country_code_match_type' determines whether we look for the cart
# country to match the entered selectors or not. Can be:
# - ':include' to look for a country in the list
# - ':exclude' to make sure the country is not in the list
# - 'country_codes' is a list of 2-character abbreviations for
# the applicable countries
# - 'gateway_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'gateway_names' is a list of strings to identify gateways
# ================================================================
HIDE_GATEWAYS_FOR_COUNTRY = [
{
country_code_match_type: :include,
country_codes: ["CA"],
gateway_match_type: :exact,
gateway_names: ["Gateway", "Other Gateway"],
},
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# CountrySelector
#
# Finds whether the supplied country code matches the entered
# string.
# ================================================================
class CountrySelector
def initialize(match_type, countries)
@match_type = match_type
@countries = countries.map { |country| country.upcase.strip }
end
def match?(country_code)
(@match_type == :include) == @countries.include?(country_code.upcase.strip)
end
end
# ================================================================
# GatewayNameSelector
#
# Finds whether the supplied gateway name matches any of the
# entered names.
# ================================================================
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end
def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
# ================================================================
# HideGatewaysForCountryCampaign
#
# If the shipping address country matches any of the entered
# countries, the entered gateway(s) will be hidden.
# ================================================================
class HideGatewaysForCountryCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, payment_gateways)
address = cart.shipping_address
return if address.nil?
@campaigns.each do |campaign|
country_selector = CountrySelector.new(
campaign[:country_code_match_type],
campaign[:country_codes],
)
next unless country_selector.match?(address.country_code)
gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)
payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway)
end
end
end
end
CAMPAIGNS = [
HideGatewaysForCountryCampaign.new(HIDE_GATEWAYS_FOR_COUNTRY),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gatewaysHiển thị cổng thanh toán theo mức chi tiêu
Sử dụng đoạn mã này để hiển thị cổng thanh toán cụ thể nếu khách hàng chi tiêu vượt quá mức tiền nhất định.
Ví dụ: hiển thị cổng thanh toán nhất định nếu khách hàng chi tiêu từ 1.000 $ trở lên, ngược lại thì ẩn đi.
# ================================ Customizable Settings ================================
# ================================================================
# Show Gateway(s) for Spend Threshold
#
# If the cart total is greater than, or equal to, the entered
# threshold, the entered gateway(s) are shown.
#
# - 'threshold' is the dollar amount the customer must spend in
# order to see the entered gateway(s)
# - 'gateway_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'gateway_names' is a list of strings to identify gateways
# ================================================================
SHOW_GATEWAYS_FOR_THRESHOLD = [
{
threshold: 500,
gateway_match_type: :exact,
gateway_names: ["Gateway", "Other Gateway"],
},
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# GatewayNameSelector
#
# Finds whether the supplied gateway name matches any of the
# entered names.
# ================================================================
class GatewayNameSelector
def initialize(match_type, gateway_names)
@comparator = match_type == :exact ? '==' : 'include?'
@gateway_names = gateway_names.map { |name| name.downcase.strip }
end
def match?(payment_gateway)
@gateway_names.any? { |name| payment_gateway.name.downcase.strip.send(@comparator, name) }
end
end
# ================================================================
# ShowGatewaysForThresholdCampaign
#
# If the cart total is greater than, or equal to, the entered
# threshold, the entered gateway(s) are shown.
# ================================================================
class ShowGatewaysForThresholdCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, payment_gateways)
@campaigns.each do |campaign|
next unless cart.subtotal_price < (Money.new(cents: 100) * campaign[:threshold])
gateway_name_selector = GatewayNameSelector.new(
campaign[:gateway_match_type],
campaign[:gateway_names],
)
payment_gateways.delete_if do |payment_gateway|
gateway_name_selector.match?(payment_gateway)
end
end
end
end
CAMPAIGNS = [
ShowGatewaysForThresholdCampaign.new(SHOW_GATEWAYS_FOR_THRESHOLD),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gatewaysSắp xếp lại cổng thanh toán
Sử dụng đoạn mã này để thay đổi thứ tự mặc định của các cổng thanh toán cung cấp cho khách hàng.
# ================================ Customizable Settings ================================
# ================================================================
# Reorder Gateways
#
# The order in which you would like your gateways to display
# ================================================================
DESIRED_GATEWAY_ORDER = [
"Payment Gateway 1", "Payment Gateway 2", "Payment Gateway 3",
]
# ================================ Script Code (do not edit) ================================
# ================================================================
# ReorderGatewaysCampaign
#
# Reorders gateways into the entered order
# ================================================================
class ReorderGatewaysCampaign
def initialize(desired_order)
@desired_order = desired_order.map { |item| item.downcase.strip }
end
def run(cart, payment_gateways)
payment_gateways.sort_by! { |payment_gateway| @desired_order.index(payment_gateway.name.downcase.strip) || Float::INFINITY }
end
end
CAMPAIGNS = [
ReorderGatewaysCampaign.new(DESIRED_GATEWAY_ORDER),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.payment_gateways)
end
Output.payment_gateways = Input.payment_gateways