Lab - Postman Invoices and Payments Workflow
Introduction
In this lab, you’ll complete a standard purchasing workflow by managing invoices and their payments.
Prerequisites
- A BigCommerce sandbox store or trial store, or a full production store
- B2B Edition enabled in your store
- Postman or a similar API client
- An existing Postman environment, collection, and headers preset as configured in previous labs
- An existing company and Admin or Senior Buyer user login
- Minimal invoice settings as described below
In this lab, you will:
- Create API requests to inspect and manage invoices and payments
- Implement Postman scripts to automate values used in API requests
Required Invoice Settings
The lab will involve logging into your storefront as a company user and placing an order to pay an invoice. The following minimal settings are required in the B2B Edition section of your control panel:
- In Settings > General, “Invoices” must be checked under “Feature management.”
- In Settings > Invoice, at least one valid option must be enabled under “Payment methods for paying invoices.”
If you don’t have a valid payment method option available in the invoice settings, make sure you have enabled a method other than Check/Purchase Order in the main Settings > Payments section of the BigCommerce control panel.
Postman Recap
In addition to the usual environment variables and headers preset, make sure you have the following collection variables with valid values:
company_id, which was originally set by your “Create Company” requestorder_id, which was set by the “Get Recent Orders” request in the previous lab
If these collection variables are not set, you can manually set the values using the order you recently placed and the company it belongs to. (Use the “Get Companies” request if you need to find the company ID value.)
Step 1: Create an Invoice
Sales reps can create invoices for orders at any time from your B2B Edition admin, but if your implementation involves managing orders and payments in an external system, it’s common for invoice creation to be handled via API instead.
-
Create a new HTTP request and save it to your collection with the name “Create Invoice from Order.”
-
Configure the request with the following details.
-
In the Scripts tab, enter the following “Post-response” code.
In this case, you’re taking advantage of the fact that the invoice originates with your API request to capture the
invoice_idthat will be used in subsequent requests in a collection variable.Note that although this is a POST request, there is no request body. This request is creating an invoice from the details of the order, so beyond the order ID in the URL, there is no required input.
-
Send the request, verify that all tests succeed, and verify that the
invoice_idvariable is set on your collection.
With the new invoice created, you should also be able to see the details in the B2B Edition admin, or in the Buyer Portal when logged in as one of the company’s users.
Step 2: Get Invoices
The next two requests will be dedicated to the retrieval of invoice data.
With quotes and orders, we practiced fetching recent records as a likely common workflow. For invoices, we’ll take the opposite approach: a common scenario for many integrations would be to inspect invoices that are older than a certain date and still unpaid. The initial configuration of the request will fetch invoices older than 30 days.
-
Create a new HTTP request and save it to your collection with the name “Get Old Open Invoices.”
-
Configure the request with the following details.
-
In the Params tab, add the following Query Params.
Like in past requests, you’re referencing a collection variable that doesn’t exist here as the value of “endDateAt”. The “Pre-request” you’ll set up next will take care of calculating and setting this variable before the request.
Try out the effects of other filter params as well, including
beginDateAt,limit,offset,customerId, andcustomerName. -
In the Scripts tab, enter the following “Pre-request” code (not “Post-response” as usual).
-
In the Scripts tab, enter the following “Post-response” code.
-
Send the request.
Remember that the filter applied to the request will exclude any invoices newer than 30 days. If you happen to have invoices older than that in your store, you may have results, but you definitely won’t see your newly created invoice.
-
Temporarily un-check the “endDateAt” param in the Params tab, re-send the request, and verify that all tests succeed.
The next request will fetch an individual invoice’s details, and it’s here that you’ll take advantage of your stored ID to automatically fetch the recently created invoice.
-
Create a new HTTP request and save it to your collection with the name “Get Invoice.”
-
Configure the request with the following details.
-
In the Scripts tab, enter the following “Post-response” code.
-
Send the request and verify that all tests succeed.
Step 3: Export Invoice PDF
-
Create a new HTTP request and save it to your collection with the name “Export Invoice PDF.”
-
Configure the request with the following details.
-
In the Scripts tab, enter the following “Post-response” code.
-
Send the request and verify that all tests succeed.
-
Copy the URL value from the response and enter it in a browser to verify that the URL results in downloading a PDF with the details of the invoice.
Step 4: Apply an Invoice Payment
Invoice payment will commonly be handled directly in the shopper-facing Buyer Portal by company users. However, if your purchasing workflow involves handling payment in an external system, an API flow to import such payments as “offline payments” in B2B Edition will likely be part of your integration.
In this request, you’ll apply a partial payment to the recently created invoice.
-
Create a new HTTP request and save it to your collection with the name “Create Offline Payment.”
-
Configure the request with the following details.
-
In the Body tab, enter the following content.
Note that the body is setting the payment’s
processingStatusto 2, which indicates a “processing” status. -
Replace
<Amount>in the request body with a numerical value that is less than the full invoice amount. (View the invoice in the B2B Edition admin or with your “Get Invoice” request if you need a reminder of the full open balance.) -
In the Scripts tab, enter the following “Post-response” code.
This script captures the
payment_idof the new payment in a collection variable, for use in a later request to update its status. -
Send the request and verify that all tests succeed.
-
Re-run the “Get Invoice” request and observe the updated details of the invoice’s open balance and
pendingPaymentCount.
You can also log into your storefront as an Admin user for the company associated with the invoice and view the details of the invoice on the Invoice page. The amount due should reflect the partial payment.
Step 5: Get an Invoice’s Payments
The next request will fetch all payments related to the invoice you’ve been working with.
-
Create a new HTTP request and save it to your collection with the name “Get Invoice Payments.”
-
Configure the request with the following details.
-
In the Params tab, add the following Query Param.
-
In the Scripts tab, enter the following “Post-response” code.
-
Send the request and verify that all tests succeed.
Step 6: Apply Payment as a User
If you used a payment amount less than the invoice’s total open balance, the invoice still has a balance remaining.
Instead of applying an offline payment for the remaining amount, let’s perform a BigCommerce checkout to pay it “online” as a company user.
-
Log in to your storefront as an Admin user for the company the invoice is associated with.
-
Find the invoice in the Invoice tab and select “Pay” from the invoice’s action menu.
-
Complete the BigCommerce checkout to finish paying the open invoice balance.
-
Re-run the “Get Invoice Payments” request.
You should now observe the multiple existing payments for the current invoice, with the most recent reflecting the details of an online transaction.
-
Re-run the “Get Invoice” request to observe the new status and open balance.
Step 7: Update a Payment Status
You previously created an offline payment in “processing” status, and your invoice details should reflect one “pending” payment in the pendingPaymentCount.
Let’s create a final request to simulate the update you would send to B2B Edition in response to the payment being settled in your external system. This will rely on the payment_id captured when the offline payment was first created.
-
Create a new HTTP request and save it to your collection with the name “Update Payment Status.”
-
Configure the request with the following details.
-
In the Body tab, enter the following content.
You’re setting the status code 3, indicating the payment is now complete.
-
In the Scripts tab, enter the following “Post-response” code.
-
Send the request and verify that all tests succeed.
-
Re-run the “Get Invoice” request and observe the
pendingPaymentCount, which should now reflect no pending payments.
Taking It Further
- It’s also possible to create an ad-hoc invoice for an order with full control over the details. Create a request to create a full invoice.
- Complete your invoices workflow by creating requests to update and delete an invoice.
- Complete your payments workflow by creating requests to fetch an individual payment and delete a payment.