TomorrowIdeas / plaid-sdk-php

PHP bindings for the Plaid API
MIT License
111 stars 42 forks source link

sync requires handling the cursor #84

Open risner opened 7 months ago

risner commented 7 months ago

Is there any sample code to handle the initial sync or updates with enough to require handling the updated cursor?

I found some code example here: https://github.com/TomorrowIdeas/plaid-sdk-php/issues/70

But it doesn't seem to work or uses things I'm unsure how to load?

kgdiem commented 7 months ago

Here's some of my code.

Application code to sync the transactions and update the model w/ latest cursor

$transactionResponse = $this->plaid->syncTransactions($accessToken, $cursor);

PlaidTransaction::create([
    'plaid_access_token_id' => $accessTokenId,
    'raw' => $transactionResponse,
]);

$newTransactions = property_exists($transactionResponse, 'added') ? $transactionResponse->added : [];
$updatedTransactions = property_exists($transactionResponse, 'updated') ? $transactionResponse->updated : [];
$removedTransactions = property_exists($transactionResponse, 'removed') ? $transactionResponse->removed : [];

Log::debug('PlaidManager::syncTransactions() - Syncing transactions', [
    'accessTokenId' => $accessTokenId,
    'userId' => $userId,
    'newTransactions' => $newTransactions,
    'updatedTransactions' => $updatedTransactions,
    'removedTransactions' => $removedTransactions,
    'hasMore' => $transactionResponse->has_more,
]);

if ($transactionResponse->has_more) {
    $plaidAccessToken->cursor = $transactionResponse->item->next_cursor;
}

Wrapper around this SDK

public function syncTransactions(
    string $accessToken, 
    ?string $cursor = null,
    int $count = 500,
) {
        $options = [
            'include_original_description' => true,
        ];

        return $this->client->transactions->sync(
            $accessToken, 
            $cursor, 
            $count, 
            $options
        );
}