moov-io / iso8583

A golang implementation to marshal and unmarshal iso8583 message.
https://moov.io
Apache License 2.0
304 stars 100 forks source link

Update `message.Marshal` message to accept structs with Go's native types #280

Closed alovak closed 9 months ago

alovak commented 10 months ago

It should be possible to use native types in data structures like this:

// define a struct for the authorization request
type AuthorizationRequest struct {
    MTI                  string `index:"0"`
    PrimaryAccountNumber string `index:"2"`
    ProcessingCode       string `index:"3"`
    AmountTransaction    int    `index:"4"`
    TransmissionDateTime string `index:"7"`
    STAN                 string `index:"11"`
    LocalTransactionTime string `index:"12"`
    LocalTransactionDate string `index:"13"`
    ExpirationDate       string `index:"14"`
    // ...
}

// usage
request := &AuthorizationRequest{
    MTI:                  "0100",
    PrimaryAccountNumber: data.CardNumber,
    ProcessingCode:       "200000",
    AmountTransaction:    data.Amount,
    TransmissionDateTime: data.CreatedAt.Format(TransmissionDateTimeFormat),
    STAN:                 data.STAN,
    LocalTransactionTime: data.Acceptor.LocalTime.Format(spec.LocalTimeFormat),
    LocalTransactionDate: data.Acceptor.LocalTime.Format(spec.LocalDateFormat),
}

message := iso8583.NewMessage(Spec)

err := message.Marshal(request)
// handle error
packed, err := message.Pack()
// handle error
// send packed message to the server

the reference implementation is here: https://github.com/moov-io/iso8583/pull/273/files. You can check the tests in the PR.

This PR should support keepzero value in the field tag so we don't skip zero values when we set values to the message field.

As part of the PR, please, extract code that reads field tag and returns field id and if KeepZero is set. POC is here: https://github.com/moov-io/iso8583/pull/273/files

alovak commented 9 months ago

Closed by #291 . There is a separate #281 issue to support iso8583 tag.