jlevers / selling-partner-api

A PHP client library for Amazon's Selling Partner API
BSD 3-Clause "New" or "Revised" License
404 stars 198 forks source link

The function ReportsV20210630()->getReportDocument return error 400 #755

Open joseramoneras opened 4 months ago

joseramoneras commented 4 months ago

Problem description:

The function ReportsV20210630()->getReportDocument return error 400

Error:

"errors": [ { "code": "InvalidInput", "message": "Invalid or unsupported document ID found", "details": "" } ] image

Code

<?php

use SellingPartnerApi\SellingPartnerApi; use SellingPartnerApi\Enums\Endpoint; use SellingPartnerApi\Seller\ReportsV20210630\Dto\CreateReportSpecification;

require_once $_SERVER["DOCUMENT_ROOT"] . '/erp_produccion/libs/selling-partner-api/vendor/autoload.php';

new Pruebas();

class Pruebas { private $connector;

public function __construct() { $this->connector = SellingPartnerApi::seller( clientId: 'xxxxxxxxx', clientSecret: 'xxxxxxxxxxxxxxx', refreshToken:'xxxxxxxxxxxx', endpoint: Endpoint::EU );

$result_request_report = $this->requestReport('GET_FLAT_FILE_VAT_INVOICE_DATA_REPORT', 'A1RKKUPIHCS9HS');

}

private function requestReport($report_type, $marketplace_id = NULL){ date_default_timezone_set('UTC'); if (!$report_type) return false;

    $date_from = new DateTimeImmutable();
    $date_from = $date_from->sub(new DateInterval('P1M'));
    $date_from->setTimezone(new \DateTimeZone('GMT'));
    $date_from->format(DATE_RFC7231);

    $date_to = new DateTimeImmutable();
    $date_to->setTimezone(new \DateTimeZone('GMT'));
    $date_to->format(DATE_RFC7231);

    try {
        $reportsApi = $this->connector->ReportsV20210630();

        $createReportSpecification = new CreateReportSpecification($report_type, [$marketplace_id], null, $date_from, $date_to);
        $createReportResponse = $reportsApi->createReport($createReportSpecification);
        $requestId = $createReportResponse->dto()->reportId;
        if(!$requestId) return false;

        if (!$requestId) {
            return false;
        }

        do {
            sleep(20);
            $result_get_report = $reportsApi->getReport($requestId)->dto();
            var_dump($result_get_report->processingStatus);

            if (!$result_get_report) return false;
        } while ($result_get_report->processingStatus != "DONE"); // NOTE: Mientras el reporte no este listo no avanzamos en el proceso

        $result_get_report = $reportsApi->getReport($requestId)->dto();

        try {
            $responseDocument = $this->connector->ReportsV20210630()->getReportDocument($result_get_report->reportDocumentId, $report_type);
            echo '<pre>'; var_dump($responseDocument); echo '</pre>'; die;
            $reportDocument = $responseDocument->dto();

        } catch (\Throwable $th) {
            // file_put_contents("aaaaaaaaaaaaaaaa.txt", '<b>aaaaaaaaaaaaaaaaaa</b><br>'.$th);
            echo '<pre>'; var_dump($th ); echo '</pre>'; die;
        }
        return !$reportDocument ? $reportDocument : false;
        // return
    } catch (Exception $e) {
        echo 'Exception when calling ReportsApi->createReport: ', $e->getMessage(), PHP_EOL;
        return false;
    }

} }

joseramoneras commented 4 months ago

Good afternoon, I'm sending you the complete example in case you can't see it well.

sample.txt

jlevers commented 4 months ago

I don't see anything obviously wrong with the getReportDocument call, but try passing $date_from and $date_to to createReport as DateTime objects, instead of turning them into strings first.