phpfui / ConstantContact

MIT License
16 stars 7 forks source link

Custom fields exist but are not populated on JsonImportContact #8

Closed mstrigkos closed 2 years ago

mstrigkos commented 2 years ago

I have created custom fields on the authorised account but they are not populated. There are no errors. I get 'initialized' state on response, the contacts are created with all predefined fields but no custom fields are populated. Any ideas?

My code looks like this:

            $import_data = array();
            foreach ($args['contacts'] as $contact) {
                if ($contact["status"] == "subscribed") {
                    $item = array(
                        "email" => $contact['email_address'],
                        "first_name" => $contact['merge_fields']['FNAME'],
                        "last_name" => $contact['merge_fields']['LNAME'],
                        "birthday_month" => \intval(\substr($contact['merge_fields']['BIRTHDAY'], 3, 2)),
                        "birthday_day" =>  \intval(\substr($contact['merge_fields']['BIRTHDAY'], 0, 2)),
                        "country" => $contact['merge_fields']['COUNTRY'],
                        "cf:gender" => $contact['merge_fields']['GENDER'],
                        "cf:nps_score" => (string) $contact['merge_fields']['NPS'],
                        "cf:language" => $contact['merge_fields']['LANGUAGE']
                    );
                    $import_data[] = new \PHPFUI\ConstantContact\Definition\JsonImportContact($item);
                }
            }
            $list_ids = array(new \PHPFUI\ConstantContact\UUID($args['listid']));
            $client->accessToken = $args['accessToken'];
            $client->refreshToken = $args['refreshToken'];
            $ContactsJsonImportDefinition = new \PHPFUI\ConstantContact\Definition\ContactsJsonImport(array('import_data' => $import_data, 'list_ids' => $list_ids));
            $contactsJsonImport = new \PHPFUI\ConstantContact\V3\Activities\ContactsJsonImport($client);
            return [
                'result' => $contactsJsonImport->post($ContactsJsonImportDefinition)
            ];
phpfui commented 2 years ago

I have not dealt with custom fields. It maybe related to the custom field syntax used by the YAML definition file that this library parses and generated code from. I see that one of the fields it expects is 'cf:custom_field_name'. That makes sense from what I see your code doing with cf:gender, cf:language, etc. where custom_field_name is just a place holder and not a literal name.

Try hacking the source you see here http://phpfui.com/?n=PHPFUI%5CConstantContact%5CDefinition&c=JsonImportContact&p=f to include your custom field names. If that works, then I need to account for the Constant Contact way of dealing with custom field names. It should be possible to do.

Unfortunately I will be traveling over the next few days, so I might not be able to offer a fix for a few weeks. Or you can submit a PR. You probably want to add some logic to the Base class here: http://phpfui.com/?n=PHPFUI%5CConstantContact%5CDefinition&c=Base&p=f This class validates the data as best as possible.

Another thing to check it what is actually sent to the CC API in the Client class. You may find the custom fields are stripped by the validation in the Definition\Base class.

Let me know what you find and we can get it fixed. And thanks for reporting the issue. Hope this helps.

mstrigkos commented 2 years ago

Thank you for your reply. I will try to contribute resolving this.

phpfui commented 2 years ago

I updated the library to deal with custom fields.

Since : is not a valid character for variables in PHP, you will need to use cf_lastName instead of cf:lastName.

Test it with composer using dev-mail instead of a version number (I think that will work, but confirm you have the latest code). If it works for you, let me know and I will publish a new version.

mstrigkos commented 2 years ago

I just tried with dev-main and changed : to _ but still doesn't work. Contacts are added but custom fields are not populated. I confirmed that vendors code has the latest changes from dev-main.

My current code is:

            $import_data = array();
            foreach ($args['contacts'] as $contact) {
                if ($contact["status"] == "subscribed") {
                    $item = array(
                        "email" => $contact['email_address'],
                        "first_name" => $contact['merge_fields']['FNAME'],
                        "last_name" => $contact['merge_fields']['LNAME'],
                        "birthday_month" => \intval(\substr($contact['merge_fields']['BIRTHDAY'], 3, 2)),
                        "birthday_day" =>  \intval(\substr($contact['merge_fields']['BIRTHDAY'], 0, 2)),
                        "country" => $contact['merge_fields']['COUNTRY'],
                        "cf_gender" => $contact['merge_fields']['GENDER'],
                        "cf_nps_score" => (string) $contact['merge_fields']['NPS'],
                        "cf_language" => $contact['merge_fields']['LANGUAGE']
                    );
                    $import_data[] = new \PHPFUI\ConstantContact\Definition\JsonImportContact($item);
                }
            }
            $list_ids = array(new \PHPFUI\ConstantContact\UUID($args['listid']));
            $client->accessToken = $args['accessToken'];
            $client->refreshToken = $args['refreshToken'];
            $ContactsJsonImportDefinition = new \PHPFUI\ConstantContact\Definition\ContactsJsonImport(array('import_data' => $import_data, 'list_ids' => $list_ids));
            $contactsJsonImport = new \PHPFUI\ConstantContact\V3\Activities\ContactsJsonImport($client);
            return [
                'result' => $contactsJsonImport->post($ContactsJsonImportDefinition)
            ];
phpfui commented 2 years ago

Turns out I was not constructing the objects correctly for custom fields via an array. This is now fixed. Please retest with the latest. If you confirm it works, I will release an update. I did confirm my instance of Constant Contact has custom fields and it works. Here is the code I used to test (you will supply $email, $listUID and $client):

echo '<pre>';

$customFields = new \PHPFUI\ConstantContact\V3\ContactCustomFields($client);
print_r($customFields->get());

$import_data = [];
$item = [
    "email" => $email,
    "cf_gender" => 'mail',
    "cf_language" => 'English',
    "birthday_month" => 10,
    "birthday_day" =>  11,
    "country" => 'USA',
];

$import_data[] = new \PHPFUI\ConstantContact\Definition\JsonImportContact($item);

$list_ids = [new \PHPFUI\ConstantContact\UUID($listUID)];
$contactsJsonImportDefinition = new \PHPFUI\ConstantContact\Definition\ContactsJsonImport();
$contactsJsonImportDefinition->import_data = $import_data;
$contactsJsonImportDefinition->list_ids = $list_ids;

$contactsJsonImport = new \PHPFUI\ConstantContact\V3\Activities\ContactsJsonImport($client);
print_r($contactsJsonImportDefinition->getJSON());
exit;

$result = $contactsJsonImport->post($contactsJsonImportDefinition);

$activityUrl = $result['_links']['self']['href'];
$parts = explode('/', $activityUrl);
$activityId = $parts[3] ?? 'Undefined';

echo "activityId = $activityId\n";

do
    {
    echo "Sleeping for 10 seconds\n";
    sleep(10);
    $activity = new \PHPFUI\ConstantContact\V3\Activity($client);
    $status = $activity->get($activityId);
    print_r($status);
    }
while ($status['state'] != 'completed');

$contacts = new \PHPFUI\ConstantContact\V3\Contacts($client);
$list = $contacts->get('all', $email, $listUID);

print_r($list);

echo "done\n";
echo '</pre>';
phpfui commented 2 years ago

Did this work for you? If so, please close this issue and I will make a new release.

Thanks!

mstrigkos commented 2 years ago

I haven't tried this yet because I am on vacation. I will have tried it by the end of this week.

mstrigkos commented 2 years ago

Hello, I can confirm that this works! Thank you.

phpfui commented 2 years ago

Thanks. Will publish.