719-286-0751 [email protected]

Shopify Scripts Part II: Wholesale Payment

In this post, we are going to continue exploring the functionality of Shopify Scripts. In our previous post, we setup discounts site wide based on customer tags. In this next example, we are going to write a payment gateway script that will adjust available payment methods based on the customer tags we used in that example. In my example store, I have setup the standard Shopify “Bogus” payment method (available to development stores) which mimics a card payment and a few manual payment methods that I intend to only be used by my wholesale customers. I have included “Net 30” and “Net 60” as example common wholesale billing methods. In the real world, once a customer chose one of these payment methods they would be invoiced externally and have payment collected outside Shopify, on some terms I have established with wholesale customers ahead of time. Regular customers should be required to pay with a credit card, at the time of purchase only.

Before we start

We are going to skip a lot of the setup in this post, as that was covered in the previous post. We will be building upon the tag system we established in that post. Additionally, as outlined above, we will be filtering our stores payment methods so you should have at least a couple payment methods setup on your store (manual payment methods are easy to setup quickly, if only to test).

 

Getting started

Open up the script editor and create a new Payment Gateway script. Shopify offers a few boilerplates here, however I don’t feel that any of them really get us very close to our goal, so let’s choose Blank Template.

Although there isn’t a boilerplate inside the scripts app, Shopify does provide an example here that is pretty close to what we want to do. What I would like to do differently, though, is to just supply a start to match so that any customer tagged with a tag starting with WHOLESALE_ will be able to view Net 30 and Net 60 along with credit card options. My modified version of the code is below.

SHOW_GATEWAYS_FOR_WHOLESALE = [
  {
    customer_tag_start: "wholesale_",
    gateway_match_type: :exact,
    restricted_gateway_names: ["Net 30", "Net 60"],
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# CustomerTagSelector
#
# Finds whether the supplied customer has a matching tag starting with the supplied tag_start
# ================================================================
class CustomerTagSelector
  def initialize(tag_start)
    @tag_start = tag_start.downcase.strip
  end
  
  def downcase_tags(customer)
    customer.tags.map { |tag| tag.downcase.strip }
  end
  
  def match_start?(customer)
    customer_tags = downcase_tags(customer)
    !!customer_tags.index { |tag| tag.start_with? @tag_start }
  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 matches the supplied tag_start, then show the gateways that would be otherwise
# hidden.
# ================================================================
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_start]
      )

      customer_match = cart.customer.nil? ? false : customer_tag_selector.match_start?(cart.customer)

      gateway_name_selector = GatewayNameSelector.new(
        campaign[:gateway_match_type],
        campaign[:restricted_gateway_names],
      )

      payment_gateways.delete_if do |payment_gateway|
        gateway_name_selector.match?(payment_gateway) ? !customer_match : false
      end
    end
  end
end

CAMPAIGNS = [
  ShowGatewaysForCustomerTagCampaign.new(SHOW_GATEWAYS_FOR_WHOLESALE),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart, Input.payment_gateways)
end

Output.payment_gateways = Input.payment_gateways

The biggest changes I made were modifying the CustomerTagSelector — the match_start method will return true as long as any tag in the customer’s tags starts with the supplied tag_start. Additionally, in the ShowGatewaysForCustomerTagCampaign class, I modified the return of the run method (in addition to changes to accomodate syntax related to CustomerTagSelector). This method now will check to see if a payment gateway is in the restricted_gateway_names array, if it is then we delete it if the customer is not a wholesale customer. Otherwise, we leave it untouched (it is not a restricted payment gateway).

Following what we did in the last post, you can test this out by testing with no customer and with a customer that has the appropriate tags. You can also test the script on your live site using the “preview on the Online Store” link. here’s my checkout as a logged in wholesale user:

And not logged in:

What we’ve built

At this point, with the previous post, we now have a storefront that will display customer-tailored pricing as well as payment options, all with little interaction from you or the user. Once setup, you will be able to offer different prices and payment methods per customer or customer type!

 

Need help with Shopify?

We’ve had a lot of experience building websites in Shopify. If you need help, head over to the Cadence Labs contact page, or email us at [email protected].

Submit a Comment

Your email address will not be published. Required fields are marked *

Install our webapp on your iPhone! Tap and then Add to homescreen.
Share This