Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

previousAttributes on Webhook events #251

Open
Steven4294 opened this issue Mar 18, 2024 · 14 comments
Open

previousAttributes on Webhook events #251

Steven4294 opened this issue Mar 18, 2024 · 14 comments

Comments

@Steven4294
Copy link

Presently there's no implementation on previous attributes so cannot see what field changed on webhooks for update events. There's a TODO: noted currently in Event.swift

    // TODO: - Figure out how to decode this.
    /// Object containing the names of the attributes that have changed, and their previous values (sent along only with *.updated events).
    //public var previousAttributes: [String: Any]?
    
@Andrewangeta
Copy link
Member

@Steven4294 Not entirely sure how this can be done. Because the previous attributes aren't a known type and could be literally a subset of data for any given stripe model type it's damn near impossible to make it codable to decode the response. I'm open to any ideas you might have around this.

@Steven4294
Copy link
Author

So the previous attributes are guaranteed to be the same type as the actual event. E.g. for a Charge event the previous attributes would be decoded as a Charge object. As long as all the fields on each model are optional it should work to just decode it as another EventObject I believe.

@Andrewangeta
Copy link
Member

I'm not sure but as a test can you give that a try in a fork/branch? And see if it decodes if that type is an EventObject

@Andrewangeta
Copy link
Member

Also some fields on stripe aren't optional like created and the id field of course so that might be tricky as well if only the changed attributes would show up

@Steven4294
Copy link
Author

Steven4294 commented Mar 18, 2024

    "previous_attributes": {
      "attempt_count": 0,
      "attempted": false,
      "charge": null,
      "effective_at": null,
      "ending_balance": null,
      "hosted_invoice_url": null,
      "invoice_pdf": null,
      "next_payment_attempt": xxxxx,
      "number": null,
      "payment_intent": null,
      "status": "draft",
      "status_transitions": {
        "finalized_at": null
      }
    }

So here's what the previous_attributes JSON looks like. Doesn't include some of the non-null attributes. Would need to make the created etc values optional for this to work via that method

@Andrewangeta
Copy link
Member

That's be a massive breaking change. Also these fields are guaranteed by stripe and it makes it easier for people to use. Otherwise people would have to nil coalesce an id Stripe guarantees which isn't an ideal experience.

@Steven4294
Copy link
Author

Steven4294 commented Mar 18, 2024

Putting it in a dictionary for now so that I can manually decode the value I'm interested in could work for now while a more stable solution is figured out:

public var previousAttributes: [String: Any]?
Uncommenting this I think should just work

@Steven4294
Copy link
Author

Other idea would be to generate a protocol that includes the non-optional fields, then extend the base Charge (which dont include the non-nullable's) with the protocol, so the base event decodes the extended structs and the previousAttribute decodes the base objects

@Andrewangeta
Copy link
Member

Any is not a Codable type. So the Event won't be Codable anymore and can't easily be decoded by responses from the API

@Steven4294
Copy link
Author

One more idea, just create a new Struct PreviousAttributes which is a huge struct of all optional fields from all the other objects 🗡️

@Andrewangeta
Copy link
Member

What would that example look like? Can you show some code for an ideal API and how it would look?

@Andrewangeta
Copy link
Member

One more idea, just create a new Struct PreviousAttributes which is a huge struct of all optional fields from all the other objects 🗡️

Lmao I wish. Then we'd have to keep up with all the attributes that get added to any stripe resource which would be a nightmare

@Steven4294
Copy link
Author

Steven4294 commented Mar 18, 2024

class BaseCharge { 
   let amount Double: ...
}
class Charge: StripeProtocol {
   let id: String // inherit from protocl
   ...
}

public struct Event: Codable {
    public var id: String
    public var data: EventData?
    public var previousAttributes: EventDateBase
 }

somethign like this

@Frizlab
Copy link
Contributor

Frizlab commented May 26, 2024

Another solution would be to use e.g. https://github.com/Frizlab/generic-json which is basically Any, but Codable 🙂
Not ideal, but better than nothing I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants