PHP code example of eorplatform / laravel-pandadoc

1. Go to this page and download the library: Download eorplatform/laravel-pandadoc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eorplatform / laravel-pandadoc example snippets


$pandaApi = PandaDoc::addRecipient(
    '[email protected]', // email
    'John', // First name
    'Doe', // Last name
    'CEO', // Role
    1, // his signing order)
    ->addRecipient(
    '[email protected]',
    'Peter',
    'Foe',
    'CTO',
    2)
    ->addToken('Client', 'AnnexNumber', 'A') // Your dynamic variable inside the PandaDoc templates
    ->addToken('Document', 'EffectiveDate', now()->format('F jS, Y'))
    ->createDocumentFromTemplate('My new MSA Annex', 'panda_doc_template_id'); // First param is name and the other is your PandaDoc template ID
    

// You can then store the response in your database related model
$pandaDocument = 
            MyModel::pandaDocDocument()->create([
                'name' => 'My new MSA Annex',
                'document_id' => $pandaApi['document_id'],
                'template_id' => $pandaApi['template_id'],
                'tokens' => $pandaApi['tokens'],
                'recipients' => $pandaApi['recipients'],
                'invite_expire_at' => now()->addDays(config('panda-doc.invitation_expire_after_days'))
            ]);

// We are also using the spatie/status package so you can set the status like that
$pandaDocument->setStatus($pandaApi['status']);



return [
    'configs' => [
        [
            /*
             * This package supports multiple webhook receiving endpoints. If you only have
             * one endpoint receiving webhooks, you can use 'default'.
             */
            'name' => 'default',

            /*
             * We expect that every webhook call will be signed using a secret. This secret
             * is used to verify that the payload has not been tampered with.
             */
            'signing_secret' => env('WEBHOOK_CLIENT_SECRET'),

            /*
             * The name of the header containing the signature.
             */
            'signature_header_name' => 'Signature',

            /*
             *  This class will verify that the content of the signature header is valid.
             *
             * It should implement \Spatie\WebhookClient\SignatureValidator\SignatureValidator
             */
            'signature_validator' => \Spatie\WebhookClient\SignatureValidator\DefaultSignatureValidator::class,

            /*
             * This class determines if the webhook call should be stored and processed.
             */
            'webhook_profile' => \Spatie\WebhookClient\WebhookProfile\ProcessEverythingWebhookProfile::class,

            /*
             * This class determines the response on a valid webhook call.
             */
            'webhook_response' => \Spatie\WebhookClient\WebhookResponse\DefaultRespondsTo::class,

            /*
             * The classname of the model to be used to store webhook calls. The class should
             * be equal or extend Spatie\WebhookClient\Models\WebhookCall.
             */
            'webhook_model' => \Spatie\WebhookClient\Models\WebhookCall::class,

            /*
             * In this array, you can pass the headers that should be stored on
             * the webhook call model when a webhook comes in.
             *
             * To store all headers, set this value to `*`.
             */
            'store_headers' => [

            ],

            /*
             * The class name of the job that will process the webhook request.
             *
             * This should be set to a class that extends \Spatie\WebhookClient\Jobs\ProcessWebhookJob.
             */
            'process_webhook_job' => '',
        ],
    ],

    /*
     * The number of days after which models should be deleted.
     *
     * Set to null if no models should be deleted.
     */
    'delete_after_days' => 30,
];


/*
             * This package supports multiple webhook receiving endpoints. If you only have
             * one endpoint receiving webhooks, you can use 'default'.
             */
            'name' => 'pandadoc',
            
            /*
             * We expect that every webhook call will be signed using a secret. This secret
             * is used to verify that the payload has not been tampered with.
             */
            'signing_secret' => 'Your signing secret from PandaDoc',
            
             /*
             * The name of the header containing the signature.
             */
            'signature_header_name' => 'signature',
            
            /*
             *  This class will verify that the content of the signature header is valid.
             *
             * It should implement \Spatie\WebhookClient\SignatureValidator\SignatureValidator
             */
            'signature_validator' => \EorPlatform\LaravelPandaDoc\PandaDocWebhookSignatureValidator::class,
            
            /*
             * The class name of the job that will process the webhook request.
             *
             * This should be set to a class that extends \Spatie\WebhookClient\Jobs\ProcessWebhookJob.
             */
            'process_webhook_job' => \EorPlatform\LaravelPandaDoc\Jobs\PandaDocWebhookProcess::class,



class PandaDocWebhookProcess
{
    public function handle(): void
    {
        // $this->webhookCall // contains an instance of `WebhookCall`
        // get the body and parse
        $payload = $this->webhookCall->payload;

        if ( !empty( $payload[0] ) ) {

            $body = $payload[0];

            if ( isset( $body['event'] ) && $body['event'] === 'document_state_changed' ) {

                $model = app(PandaDocRegistrar::class)->getPandaDocModelClass();

                $doc = $model::findById($body['data']['id']);

                if ( !$doc ) {
                    return; // die silently because maybe is request from another environment
                }

                $doc->forceSetStatus($body['data']['status']);

                $newDoc = $model::find($doc->id);

                // Fire the event that the PandaDOcDocument status has been updated
                PandaDocDocumentStatusUpdated::dispatch($newDoc);
            }
        }
    }
}

 PandaDocumentStatusUpdated::class => [
            DoWhatever::class,
            DoSomethingElse::class
        ],
bash
php artisan laravel-pandadoc:install
bash
php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-config"