In-App Purchase (custom/simple)
Last updated
Last updated
The Simple IAP Plugin is a sub-plugin for WebtoNative that enables easy handling of in-app purchases (IAP) for iOS and Android. It provides a mechanism to verify purchases with the App Store and Google Play Store and allows developers to consume the result of the verification via a custom callback function.
In-App Purchase (IAP) Button: Easily integrate IAP functionality into your WordPress site using a shortcode.
Platform Detection: Automatically detects whether the user is on iOS or Android and processes the purchase accordingly.
Server-Side Verification: Verifies the purchase receipt with the respective store (App Store or Google Play).
Callback Integration: Enables developers to handle the verification result with a custom PHP callback function.
Navigate to Settings > WTN Simple IAP in your WordPress admin dashboard.
Configure the following options:
App Store Secret Key:
Enter the shared secret from your App Store Connect account. This is required for verifying iOS purchases.
Callback Function Name:
Specify the name of the PHP function that will handle the verification results.
Shortcode
Use the [simple_iap]
shortcode to add an IAP button to your pages or posts.
Attributes
google_play_id
The product ID of the in-app purchase on Google Play.
''
app_store_id
The product ID of the in-app purchase on the App Store.
''
product_type
The type of product (INAPP for consumable or SUBS
for subscription).
INAPP
is_consumable
Whether the product is consumable (true
or false
).
false
Example
IAP Initialization:
The user clicks the IAP button.
The platform (Android or iOS) is detected using the window.WTN
object.
The purchase process is initiated using WTN.inAppPurchase
.
Purchase Handling:
After the user completes the purchase, a receipt is generated.
The receipt is sent to the WordPress backend for server-side verification.
Server-Side Verification:
The plugin verifies the receipt with the respective store's API.
A success or failure message is returned based on the verification result.
Callback Execution:
If a custom callback function is configured, it is invoked with the platform and verification data.
Developers can consume the IAP verification result by implementing a custom PHP callback function.
Callback Function Requirements
Function Name: The name of the function must match the one configured in the plugin settings.
Parameters:
$platform
: The platform of the purchase (ios
or android
).
$receipt_data
: An array containing:
receipt
: The raw receipt data.
verification_result
: The verification result returned by the respective store's API.
Example Callback Function for Verification
Users must define a callback function in their theme’s functions.php
or a custom plugin. This function will process the verification result and take appropriate actions, such as granting access to content or updating the user’s status.
JavaScript Integration
The plugin includes a JavaScript file (simple-iap.js
) that handles the IAP process on the client side.
IAP Button:
When the button is clicked, the WTN.inAppPurchase
a method is invoked with the product details.
Purchase Callback:
The purchase callback (paymentCallback
) processes the response and sends the receipt to the server via an AJAX request.
Server Response Handling:
The front end displays appropriate success or error messages based on the server's response.
Example Workflow
The user clicks the "Buy Now" button.
The IAP process starts, and the receipt is obtained.
The receipt is sent to the WordPress backend for verification.
A success or error message is displayed based on the verification result.
Frontend Errors
Payment Failure: Alerts the user if the purchase fails.
Invalid Response: Logs and displays an error message if the server returns an invalid response.
Backend Errors
Missing App Store Secret: Returns an error if the secret key is not configured.
Invalid Receipt: Returns an error if the receipt verification fails.
This documentation provides a comprehensive guide for using the Simple IAP Plugin and customizing it to meet specific requirements.