Giter VIP home page Giter VIP logo

passkithelper's Introduction

PassKit Helper

Helper library for all your Apple PassKit (Apple Wallet, Apple Passbook) needs: create passes, sign pass packages, receive [de]install notifications and send pass updates.

Attention: Apple Developer Account required!

NuGet

Features

  1. Create pass packages (*.pkpass files):
    • With Fluent-styled PassInfoBuilder and PassPackageBuilder
    • Using byte[] and/or Stream as content images
    • Using byte[] or Stream or X509Certificate2 as certificates
    • Receive MemoryStream as result (save it to file or write to HttpResponse)
  2. Receive notifications from Apple about pass [de]installations and send updates:
    • Add UsePassKitMiddleware into your Startup.Configure()
    • Implement IPassKitService for real processing.

Samples

1. Configure to create passes

For console app

var options = new PassKitOptions()
{
    PassCertificate = new X509Certificate2(File.ReadAllBytes("pass.pfx")),
    AppleCertificate = new X509Certificate2(File.ReadAllBytes("AppleWWDRCA.cer")),
    ConfigureNewPass =
        p => p.Standard
                .PassTypeIdentifier("your-pass-type-identifier")
                .TeamIdentifier("your-team-identifier")
                // Add more "defaults" here if needed
};

IPassKitHelper passKitHelper = new PassKitHelper(options);

For web app

public void ConfigureServices(IServiceCollection services)
{
    services.AddPassKitHelper(options =>
    {
        options.PassCertificate = new X509Certificate2(File.ReadAllBytes("pass.pfx"));
        options.AppleCertificate = new X509Certificate2(File.ReadAllBytes("AppleWWDRCA.cer"));
        options.ConfigureNewPass =
            p => p.Standard
                    .PassTypeIdentifier("your-pass-type-identifier")
                    .TeamIdentifier("your-team-identifier")
                    // Add more "defaults" here if needed
    });
}

2. Creat pass and pass package file

Check Apple's PassKit Package Format Reference for detailed description of all fields and valid values.

var pass = passKitHelper.CreateNewPass()
    // Ths pass already have `PassTypeIdentifier`, `TeamIdentifier` 
    //   and all other values you configured in options.
    .Standard
        .SerialNumber("PassKitHelper")
        .OrganizationName("PassKit")
        .Description("PassKitHelper demo pass")
    .VisualAppearance
        .Barcodes("1234567890128", BarcodeFormat.Code128)
        .LogoText("PassKit Helper demo pass")
        .ForegroundColor("rgb(44, 62, 80)")
        .BackgroundColor("rgb(149, 165, 166)")
        .LabelColor("rgb(236, 240, 241)")
    .StoreCard
        .PrimaryFields
            .Add("version")
                .Label("Library version")
                .Value(libraryVersion)
        .AuxiliaryFields
            .Add("github")
                .Label("GitHub link")
                .Value("https://github.com/justdmitry/PassKitHelper");

var passPackage = passKitHelper.CreateNewPassPackage(pass)
    .Icon(await File.ReadAllBytesAsync("images/icon.png"))
    .Icon2X(await File.ReadAllBytesAsync("images/[email protected]"))
    .Icon3X(await File.ReadAllBytesAsync("images/[email protected]"))
    .Logo(await File.ReadAllBytesAsync("images/logo.jpg"))
    .Strip(await File.ReadAllBytesAsync("images/strip.jpg"))
    .Strip2X(await File.ReadAllBytesAsync("images/[email protected]"))
    .Strip3X(await File.ReadAllBytesAsync("images/[email protected]"));

MemoryStream packageFile = await passPackage.SignAndBuildAsync();

// Now you have to "deliver" package file to user using any channel you have
//   (save as attachment in email, download from your webapp etc)
await File.WriteAllBytesAsync("Sample.pkpass", packageFile.ToArray());

Code above will create this beautiful pass:

3. Implementing WebService for interaction

Apple's server can call your endpoint/server to notify about user installed/deinstalled your pass, to fetch updated version of pass (when user 'pulls down' pass in Wallet). You will be able to send pushes when you want to update pass in user's wallet. Check Apple's PassKit Web Service Reference for technical details.

3.1. Implement IPassKitService

public class PassKitService : IPassKitService
{
    public Task<int> RegisterDeviceAsync() {}

    public Task<int> UnregisterDeviceAsync() {}

    public Task<(int status, string[]? passes, string? tag)> GetAssociatedPassesAsync() {}

    public Task<(int statusCode, MemoryStream? passData)> GetPassAsync() {}

    public Task ProcessLogsAsync() {}
}

3.2. Register in Startup

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddSingleton<IPassService, PassService>();
}

public void Configure(IApplicationBuilder app)
{
    ...
    app.UsePassKitMiddleware("/callbacks/passkit");
    ...
}

3.3 Add information inside your passes

You need publicly-accessible url/hostname for your server, and it must be secured with https/ssl.

Add this information into your passes:

var pass = passKitHelper.CreateNewPass()
     // your 
     // original 
     // pass content 
     // goes here
   .WebService
       .AuthenticationToken("some-random-secret-string")
       .WebServiceURL("https://you.server.com/callbacks/passkit")

AuthenticationToken is some "secret" string that you use to differentiate legal pass owners and malicious hackers.

WebServiceURL is hostname of your server and path that equal to one in UsePassKitMiddleware in previous step.

3.4. Send push updates

When users install your pass packge to their iOS and Mac devices - Apple server call your RegisterDeviceAsync. Save pushToken value in database, and when you need to update pass on user device - call IPassKitHelper.SendPushNotificationAsync(pushToken).

Installation

Use NuGet package PassKitHelper

Dependencies

For netcoreapp3.1:

  • Microsoft.Extensions.Http, v3.1.1
  • Newtonsoft.Json, v12.0.2
  • System.Security.Cryptography.Pkcs, v4.6.0

For netstandard2.0:

  • Microsoft.AspNetCore.Http.Abstractions, v2.1.1
  • Microsoft.Extensions.DependencyInjection.Abstractions, v2.1.1
  • Microsoft.Extensions.Logging.Abstractions, v2.1.1
  • Microsoft.Extensions.Http, v2.1.1
  • Newtonsoft.Json, v12.0.2
  • System.Security.Cryptography.Pkcs, v4.6.0

Dvelopment & Testing

You need netcore3.1 to run build and tests;

Tests can be run with dotnet test.

Credits

passkithelper's People

Contributors

justdmitry avatar schjonhaug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

passkithelper's Issues

PassKit for google pay passes

Dmitry, have your considered to add support for google pay passes. Apple and Google seems to be very similar, like they copied each other, let me know vkroutik sobachka gmail tochka com

Spasibo, Vlad.

Localization

Hi, I can't find any options to pass in localization options to the creation of cards. Is that a possibility?
Love the package! Thanks!

How can we use the send push notification

Hi Dmitry , thanks for this amazing library it helped me a lot so far , but I if you can please clarify more how the RegisterDeviceAsync() sendPushNotificaion() feature can be implemented to be able to send notifications on pass updates .
Thanks in advance

Missing steps on how to install the pass

I have used the demo app to create a pass! Yeah. I can email it to myself and open on iPhone.

How do I install it on the phone with a link? Is there way to display it in the web page as well? Or generate barcode?

How to Expire a Pass

Hi @justdmitry,
Now that I'm able to update the passes using the push notification successfully. I have a request to have the card automatically "expire". This could be through the push request or by setting some expiration date on the card. Is there a way to put an expiration date on the card or even add some sort of watermark on the card saying "cancelled" or "expired"?

I've looked into Apple's documentation, but I was not able to find it.

Any help would be appreciated.

Not receiving push notification

  1. Created a pass
  2. Send it to ios device and added the pass. Got the logs and device pushToken.
  3. I updated the pass from the code like change the foreground color and expiry date.
  4. Send the notification to the pushToken I receive in step 2.
  5. Webservice is getting triggered but I did not get notification on my iOS device and also pass is not updated.

Push notification text is the same when two updates occur

Hello @justdmitry ,
Thanks for this amazing library , I successfully implemented the Update Pass and send push notification features .
But I am facing an issue when I update two fields in a pass , the Push Notification Text I receive is always the same "Store Card Changed" , but when only one field is updated , the Push Notification text successfully shows the changed field message .
So I want when multiple fields got updates to show their change messages also.
Can you please help or guide me to fix this problem .
Thanks in advance !

Android Wallet

Hi dmitry,

Is there any package available to create the android passes like an apple wallet?

Thanks

Any guidance on updating?

I have everything setup as in the readme but the apple server/device isn't hitting the RegisterDeviceAsync method. Should a webservice url be provided in the pass? And how to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.