Speicher210 / fastbill-bundle

[DEPRECATED] Fastbill-bundle (use speicher210/monsum-bundle)
MIT License
1 stars 2 forks source link

JMS DateHandler #3

Open lunetics opened 8 years ago

lunetics commented 8 years ago

Hi,

Currently using the "testnotifications" from fastbill. e.g. using subscription.created returns an error on symfony side:

Invalid hook payload ...

... Invalid datetime \"\", expected format Y-m-d H:i:s. at /usr/sites/acunio/webapp/vendor/jms/serializer/src/JMS/Serializer/Handler/DateHandler.php:111 ...

i saw there's an extra DateHandler in the fastbill-api dependency, does that need to be configured?

Some basic install instructions / needs would be nice :)

lunetics commented 8 years ago

Just looked further.

in https://github.com/Speicher210/fastbill-bundle/blob/master/src/Controller/Notification/AbstractController.php#L62

the default jms_serializer is used without the custom DateHandler

lunetics commented 8 years ago

I would propose to just pullout the serializer generation from the abstract class into a dedicated service, e.g. on my "custom" service:

    <service id="app.service.fastbill_automatic.serializer" class="JMS\Serializer\Serializer">
            <factory method="build">
                <service class="Speicher210\FastbillBundle\Serializer\SerializerBuilder">
                    <call method="addHandler">
                        <argument type="service">
                            <service class="Speicher210\Fastbill\Api\Serializer\Handler\DateHandler"/>
                        </argument>
                    </call>
                    <call method="create"/>
                </service>
            </factory>
        </service>

        <service id="app.service.fastbill_automatic.abstract" class="Speicher210\Fastbill\Api\AbstractService" abstract="true" public="false">
            <argument type="service">
                <service class="AppBundle\Services\FastbillAutomatic\Transport\GuzzleTransport">
                    <argument type="service" id="csa_guzzle.client.fastbill_automatic"/>
                </service>
            </argument>
            <argument type="service" id="app.service.fastbill_automatic.serializer" />
        </service>

and in the controller we can then get the configured serializer:

       ->get('app.service.fastbill_automatic.serializer')
                ->deserialize(
                    $request->getContent(),
                    $payloadDataClass,
                    'json'
                );
dragosprotung commented 8 years ago

The custom Serializer is used in the controller with the custom DateHandler.

The problem in your case is because of inconsistencies in Fastbill API. It sends the dates in many formats (with or without time, empty string or null when no value should be there, etc). There are some local fixes for various services where the serialized type is string and we convert it to \DateTime in the service handler itself.

This is where the custom DateHandler came into play, to handle all this inconsistencies. For now it covers only the empty string / null scenarios, but in the new versions we will handle other cases as well.