In this post, we will be providing an overview into how you can automate running a script upon submission of a customer’s payment using WordPress. We’ll show you how to use Stripe for payment processing and a custom Gravity Form to get the arguments for the script. Our example will be building a link checking service, that upon submission of a payment form containing the customer email & link, will check if the link has a good response code and then email the result to the provided email.
 

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Pre-requisites:

Gravity Forms license

Stripe account

Gmail account

Environment with Python 2.7


 

Setting Up a Custom Payment Form with Gravity Forms & Stripe

Once you have a Gravity Forms & Stripe Account set up, simply enable the Gravity Forms + Stripe plugin.

After doing so, you will see a Stripe option in the Settings of Gravity Forms. Here you can enter your Stripe Secret and Publishable Keys to use.

To find your keys, head over to the API section on your Stripe dashboard:

You can also add the required Webhook for your Gravity Forms by clicking the Webhooks tab and adding an endpoint (your domain and ?callback=gravityformsstripe, i.e https://pericror.com/?callback=gravityformsstripe):

We then create a new form to pull in the customer link, email, and payment. We use Advanced Fields to include a Link (Website) and Email field in this example. Pricing fields from Stripe for our payment include a Credit Card field and a Total.

Once you build your form, there will be a Stripe section where you can pass in the payment amount dynamically from a field. In this case, we use Form Total as the payment amount, and we use the Email field to receive the Stripe receipt.

In the Notifications section of the form, set the email to send to the gmail account which you plan to access programmatically through the gmail api (this will be explained later in the post). In the message field, you can dynamically populate field values with the {FIELDNAME:ID} syntax, such as {Website:5}.

Above, we configured a notification to send the stripe email to our gmail account, pericrorlinkchecker@gmail.com (note this email is an example I will be using throughout this post, and is not actually active). It will include both the email and link the customer provided, which we will use later as the arguments to our link checking script.

Once our payment form is setup and added to a page, we can make a test submission to ensure the email arrives in our gmail account with the fields we have configured.

Checking the sender account specified above pericrorlinkchecker@gmail.com, The email body of our test submission contains the following:
email:example@customer.com
link:customer.website.com

Now that we have established our payment form successfully sends our gmail account an email containing the information we need for processing, we move onto setting up our gmail api access.
 

Using the Python Gmail API

To use the gmail API, we follow the setup steps from Google’s Gmail API Documentation. For Step 1 C we enter “Link Checker” as the product name, and for Step 1 E we enter “Link Checker Gmail API” as the name. On our development machine, we can create a new directory named LinkChecker to store the client_secret.json from step 1 H, as well as any scripts we will be writing. We can place the quickstart.py script from Step 3 in our LinkChecker directory to ensure we have set up the api credentials correctly. After completing Steps 1-3, we test if our script to use the Gmail API in Step 4. Note for Step 4, if you are running on a server (i.e AWS) where graphical browsers are unavailable, you will need to run python quickstart.py --noauth_local_webserver. We sign in with our pericrorlinkchecker@gmail.com, and upon authorization see that the script has printed all of the account’s Labels to the terminal. Now that we have established our script can access our gmail account, we work on building our own gmail wrapper to send and read emails.

Using code from guides found on Google’s Gmail API Documentation, we can build out our gmail wrapper. We have a wrapper already available for you here: gmail_wrapper.py.
We configured the following GmailWrapper member variables:

SCOPES = 'https://www.googleapis.com/auth/gmail.modify'
CLIENT_SECRET_FILE = 'client_secret.json' # https://console.developers.google.com/apis/credentials
APPLICATION_NAME = 'Link Checker'
AUTHORIZED_FROM = 'linksubmission@pericror.com'
SENDER = 'pericrorlinkchecker@gmail.com'

SCOPES defines the permissions the script requests when giving the application access to your gmail account (as was done in Step 4 of the quickstart above). We choose ‘modify’ permissions as we will be both reading and writing emails.
CLIENT_SECRET_FILE is the file we downloaded at the end of Step 1 in the quickstart above.
APPLICATION_NAME is the name of the application, and can be set to whatever we want.
AUTHORIZED_FROM we use internally to identify the email address that we expect to receive emails for processing from. I set this email to match what we configured in the gravity forms (VERIFY) email above.
SENDER is the address we will be sending emails from, and in our case we will be using the same address we authorized the script for. Optionally, if you add the snmp settings for your email (i.e linkchecker@pericror.com), you can send the results from the linkchecker@pericror.

The GmailWrapper has the following functions to facilitate reading an email:
get_unread_message_id - Get the id of the first available unread email.
get_message_data - Get the message date and body from a given email.
mark_as_read - Mark a given email as read.

The GmailWrapper uses the following functions to write an email:
create_message - Create a new email message.
send_message - Send an email message.

The GmailWrapper implements the same get_credentials as found in the quickstart guide above, used to grant the application access to our gmail account.

If we were to run the python gmail_wrapper.py, the wrapper should:
1. Retrieve the first available unread email
2. Retrieve the unread email body
4. Create an email response containing the retrieved email body
5. Send the email response to the sender of the retrieved email
3. Mark the email as read
 

Putting it All Together

We could then modify the default __main__ processing found in the gmail_wrapper.py and include basic link checking logic. We could write these changes in a new email processor that would do the same as the 5 steps above, but after retrieving the unread email body would process it. Processing would include checking the link and generating results for the response. Our email processor is available here: email_processor.py

At this point we’ve completed the entire project flow. To keep the script running on a server after we end our ssh connection to it, we can use a terminal manager such as screen. In summary, the results of a Gravity Form + Stripe submission trigger an email to be sent to our script, which will be processed and responded to. The sequence of events is shown in the following diagram:

By submitting the gravity form with the website input set to ‘https://pericror.com’, we receive the link results automatically within 5 minutes! The email result our script sends looks like:

You now have the ability to automatically charge and run a given script for a customer! It is now up to you to come up with useful applications of this project flow and create scripts that a customer would want to pay for!

Note: The code used in this article can be found on our GitHub.

Contact Us