How to code and test refund requests in Swift and Xcode

A step by step guide on implementing and testing StoreKit refund requests for In-App Purchases and Subscriptions.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 03/26/2023 at 08:51
Last Updated on 03/27/2023 at 15:28
Three flowers that represent StoreKit, Swift and XCode side by side. Beneath them sits the text “Code & Test StoreKit Refund Requests.”

A step by step guide on implementing and testing StoreKit refund requests for In-App Purchases and Subscriptions.

The guide was made using our Free iOS StoreKit 2 Open Source project which is recommended for developers or business folk wishing to learn about how In-App Purchases or Subscriptions work and the code behind them.

SubscribeFree Open-Source Apple StoreKit 2 Swift ProjectHow do Apple App Store Refunds work?

The following walkthrough assumes that you have already setup StoreKit with an offering, have developed a UI that allows an app to display, buy and refund products and have already made at least one successful purchase.

Please consult the guides below to learn how to setup StoreKit, create a StoreKit offering, design a UI that considers all StoreKit scenarios or make StoreKit product purchases.

How to setup StoreKit in XcodeHow to create a StoreKit offering in XCode or App Store ConnectFree iOS e-commerce Figma design fileHow to code and test StoreKit purchases in Swift and XCode

Step One: Add the Request Refund Utility

A screenshot of Xcode showing the code provided in the snippet below.

Add a new file called performStoreKitRefundRequest.swift and paste in the code below.

We recommend placing it in the StoreKit folder under utilities, to make sure your code remains structured.

Please note that we have created this as a UIView extension so that you can call it from a UIView.

How to create a structured XCode project

Step Two: Implement the code

A screenshot of the snippet of code provided below.

Where ever you have setup your refund UI, call the refund request using the async/await code below.

Please note that if you're calling it from a UIView controller, you should use self.view.performStoreKitRefundRequest(product:product) instead of self.performStoreKitRefundRequest(product:product).

How to use async and await in Swift projects

Step Three: Verify

A screenshot of the Refund UI in an iOS app when you begin a refund request.

Run the app, perform a refund request and check that it shows up in the StoreKit Transaction Manager.

A screenshot of the Xcode StoreKit transaction manager showing a refund.

Looking to simulate a StoreKit refund that occurred outside of an app ?

Consult the guide below to learn how to simulate StoreKit refunds outside of an iOS app in Xcode.

How to simulate a StoreKit transaction refund in XCode

Looking to learn more about developing apps with StoreKit 2?

Read our comprehensive development guide linked below to learn about all the secrets behind developing In-App Purchases and Subscriptions with StoreKit 2.

Read Apple StoreKit 2 Development Guide

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe