PHP Laravel Generate Apple Wallet Pass Example
In this tutorial, I will show you how to create apple wallet pass in php laravel application. Using the Apple Wallet API in Laravel, we can create various types of passes, including gift cards, coupon passes, boarding passes, event tickets, generic passes, and loyalty cards.
An Apple Wallet pass is a digital card stored on your iPhone. It can hold things like tickets, coupons, and loyalty cards, letting you quickly access these items, receive updates, and use them directly from your Apple Wallet app.
In this example, we will install the `thenextweb/passgenerator` composer package. Then, we will set up the Apple developer account certificate and add settings to the `.env` file. Finally, we will create a boarding pass in Apple Wallet. Let us go through the simple steps!.
Step 1: Install Laravel
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install thenextweb/passgenerator
Now, in this step, we need to use Composer command to install thenextweb/passgenerator composer package, so let's run the below command and install the below library.
composer require thenextweb/passgenerator
After install composer package we need to configuration apple certificate:
1. Go to the Apple Developer page - Identifiers - Pass Type IDs.
2. Next, create a pass type ID, which works like an app's bundle ID. This ID uniquely identifies a specific type of pass. It should follow a reverse-domain format, like `pass.com.example.appname`.
3. After creating the pass type ID, click on "Edit" and follow the steps to create a new certificate.
4. After completing the process, download the pass certificate. It comes as a `.cer` file, but you will need to convert it to `.p12` format for it to work. If you are using a Mac, import the `.cer` file into Keychain Access, then export it as `.p12`. Remember the password you set for the exported file you will need it later. Other tools can also convert the certificate, but make sure the exported `.p12` file includes the private key.
5. If you've done iOS development before, you may already have the Apple Worldwide Developer Relations Intermediate Certificate in your Macs Keychain. If not, you can download it from the Apple website in `.cer` format. It needs to be exported as `.pem`, which you can do through Keychain Access or any certificate management tool on your operating system.
CERTIFICATE_PATH=apple/pass.p12
CERTIFICATE_PASS=8469552012
WWDR_CERTIFICATE=apple/wwdr.pem
Then you need to publish configuration file using the following command:
php artisan vendor:publish --provider="Thenextweb\PassGeneratorServiceProvider"
Step 3: Create Route
we need to create one route to generate apple pass. so, let's update the web.php file:
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AppleController;
Route::get('/', function () {
return view('welcome');
});
Route::get("apple", [AppleController::class, "index"]);
Route::get("apple/pass", [AppleController::class, "generatePass"])->name("apple.pass");
Step 4: Create Controller
After adding the routes, we need to AppleController and write code of generate boarding pass. so, let's create and update the code:
app/Http/Controllers/AppleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Thenextweb\PassGenerator;
class AppleController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
return view("apple");
}
/**
* Write code on Method
*
* @return response()
*/
public function generatePass(Request $request)
{
$pass_identifier = '1004'; // This, if set, it would allow for retrieval later on of the created Pass
$pass = new PassGenerator($pass_identifier);
$pass_definition = [
"description" => "description",
"formatVersion" => 1,
"organizationName" => "HD Company",
"passTypeIdentifier"=> "pass.com.example.laraveldemo",
"serialNumber" => "10014",
"teamIdentifier" => "S5FJF47H8B",
"foregroundColor" => "rgb(99, 99, 99)",
"backgroundColor" => "rgb(212, 212, 212)",
"barcode" => [
"message" => "encodedmessageonQR",
"format" => "PKBarcodeFormatQR",
"altText" => "altextfortheQR",
"messageEncoding"=> "utf-8",
],
"boardingPass" => [
"headerFields" => [
[
"key" => "destinationDate",
"label" => "Trip to: BCN-SANTS",
"value" => "15/09/2015"
]
],
"primaryFields" => [
[
"key" => "boardingTime",
"label" => "MURCIA",
"value" => "13:54",
"changeMessage" => "Boarding time has changed to %@"
],
[
"key" => "destination",
"label" => "BCN-SANTS",
"value" => "21:09"
]
],
"secondaryFields" => [
[
"key" => "passenger",
"label" => "Passenger",
"value" => "Hardik D."
],
[
"key" => "bookingref",
"label" => "Booking Reference",
"value" => "4ZK6FG"
]
],
"auxiliaryFields" => [
[
"key" => "train",
"label" => "Train TALGO",
"value" => "00264"
],
[
"key" => "car",
"label" => "Car",
"value" => "009"
],
[
"key" => "seat",
"label" => "Seat",
"value" => "04A"
],
[
"key" => "classfront",
"label" => "Class",
"value" => "Tourist"
]
],
"backFields" => [
[
"key" => "ticketNumber",
"label" => "Ticket Number",
"value" => "7612800569875"
], [
"key" => "passenger-name",
"label" => "Passenger",
"value" => "John Doe"
], [
"key" => "classback",
"label" => "Class",
"value" => "Tourist"
]
],
"locations" => [
[
"latitude" => 37.97479,
"longitude" => -1.131522,
"relevantText" => "Departure station"
]
],
"transitType" => "PKTransitTypeTrain"
],
];
$pass->setPassDefinition($pass_definition);
// Definitions can also be set from a JSON string
// $pass->setPassDefinition(file_get_contents('/path/to/pass.json));
// Add assets to the PKPass package
$pass->addAsset(base_path('apple/background.png'));
// $pass->addAsset(base_path('resources/assets/wallet/thumbnail.png'));
$pass->addAsset(base_path('apple/icon.png'));
$pass->addAsset(base_path('apple/logo.png'));
$pkpass = $pass->create();
return response($pkpass, 200, [
'Content-Transfer-Encoding' => 'binary',
'Content-Description' => 'File Transfer',
'Content-Disposition' => 'attachment; filename="pass.pkpass"',
'Content-length' => strlen($pkpass),
'Content-Type' => PassGenerator::getPassMimeType(),
'Pragma' => 'no-cache',
]);
}
}
Step 5: Update Blade File
Ok, now at last we need to add blade view so first create new file apple.blade.php file and put bellow code:
resources/views/auth/apple.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Example</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="card mt-5">
<div class="card-header"><h4>Laravel Apple PassExample</h4></div>
<div class="card-body">
<a href="{{ route('apple.pass') }}" class="btn"><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQyX3wTamX8qNvjvom5L354PMYF8LwrP8DXsw&s">
</div>
</div>
</div>
</body>
</html>
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/apple
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Custom Forgot Password Tutorial
- Laravel 11 Socialite Login with Gitlab Account Example
- Laravel 11 Notifications With database Driver Example
- How to Save JSON Data in Database in Laravel 11?
- Laravel 11 REST API with Passport Authentication Tutorial
- Laravel Password and Confirm Password Validation Example
- Laravel Change Mail Driver Dynamically Example
- How to Automatically Generate Sitemap in Laravel?
- Laravel Send Scheduled Emails Tutorial
- Laravel Firebase Push Notification to Android and IOS App Example
- Laravel Two Factor Authentication using Email Tutorial
- How to Add Two Factor Authentication with SMS in Laravel?
- Laravel Phone Number Verification using Firebase Example