src/Service/SalesforceService.php line 30

Open in your IDE?
  1. <?php 
  2. namespace App\Service;
  3. use App\Entity\Donation;
  4. use App\Service\ApiService;
  5. use App\Entity\FilmProject;
  6. use App\Entity\FilmProjectMember;
  7. use App\Entity\FilmProjectProgressReport;
  8. use App\Entity\FilmProjectProgressReportFile;
  9. use App\Entity\OrderBillingDetails;
  10. use App\Entity\OrderDetails;
  11. use App\Entity\User;
  12. use App\String\Constant;
  13. use App\Serializer\SalesforceObjectConverter;
  14. use App\Utility\SalesforceValues;
  15. use bjsmasth\Salesforce\Authentication\PasswordAuthentication;
  16. use bjsmasth\Salesforce\CRUD;
  17. use GuzzleHttp\Exception\ClientException;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  19. use Exception;
  20. final class SalesforceService extends ApiService
  21. {
  22.     private ContainerBagInterface $params;
  23.     private CRUD $salesforceCrud;
  24.     private SalesforceObjectConverter $salesforceObjectConverter;
  25.     public function __construct(ContainerBagInterface $paramsSalesforceObjectConverter $salesforceObjectConverter)
  26.     {
  27.         $this->params $params;
  28.         $this->salesforceObjectConverter $salesforceObjectConverter;
  29.         $this->salesforceCrud $this->authenticate();
  30.     }
  31.     
  32.     /**
  33.      * Setup access token and instance URL in session
  34.      *
  35.      * @return void
  36.      */
  37.     public function authenticate() 
  38.     {
  39.         $options = [
  40.             'grant_type' => 'password',
  41.             'client_id' => $this->params->get('app.salesforce_client_id'),
  42.             'client_secret' => $this->params->get('app.salesforce_client_secret'),
  43.             'username' => $this->params->get('app.salesforce_username'),
  44.             'password' => $this->params->get('app.salesforce_password'),
  45.         ];
  46.         $salesforce = new PasswordAuthentication($options);
  47.         $salesforce->setEndpoint(Constant::SALESFORCE_TOKEN_URL);
  48.         $salesforce->authenticate();
  49.         return new CRUD();
  50.     }
  51.     /**
  52.      * get contact ID if exists, return false if doesnt exists
  53.      *
  54.      * @param User $user
  55.      * @return string|void
  56.      */
  57.     public function getContact(string $email)
  58.     {
  59.         try {
  60.             $query 'SELECT Id FROM Contact WHERE Email = \''$email .'\'';
  61.             $result $this->salesforceCrud->query($query);
  62.         } catch (ClientException $e) {
  63.             $response $e->getResponse();
  64.             $responseBodyAsString $response->getBody()->getContents();
  65.             $this->throwExceptionMessage($responseBodyAsString);
  66.             return null;
  67.         }
  68.         if ($result['totalSize'] <= 0) {
  69.             return false;
  70.         }
  71.         $id $result['records'][0]['Id'];
  72.         return $id;
  73.     }
  74.      /**
  75.      * get contact ID if exists, return false if doesnt exists
  76.      *
  77.      * @param User $user
  78.      * @return string|void
  79.      */
  80.     public function getContactByEmailandFullName(string $firstNamestring $lastNamestring $email)
  81.     {
  82.         // $firstName = preg_replace('/[^a-zA-Z0-9\s]/', '\\\\$0', $firstName);
  83.         // $lastName = preg_replace('/[^a-zA-Z0-9\s]/', '\\\\$0', $lastName);
  84.         $firstName str_replace("'""''"$firstName);
  85.         $lastName str_replace("'""''"$lastName);
  86.         try {
  87.             $query 'SELECT Id FROM Contact WHERE Email = \''$email .'\' AND FirstName = \''$firstName .'\' AND LastName = \''$lastName .'\'';
  88.             $result $this->salesforceCrud->query($query);
  89.         } catch (ClientException $e) {
  90.             $response $e->getResponse();
  91.             $responseBodyAsString $response->getBody()->getContents();
  92.             $this->throwExceptionMessage($responseBodyAsString);
  93.             return null;
  94.         }
  95.         if ($result['totalSize'] <= 0) {
  96.             // Fallback: Last check by email 
  97.             $id $this->getContact($email);
  98.             return $id ?? false;
  99.         }
  100.         $id $result['records'][0]['Id'];
  101.         return $id;
  102.     }
  103.     /**
  104.      * get contact ID if exists, return false if doesnt exists
  105.      *
  106.      * @param User $user
  107.      * @return string|void
  108.      */
  109.     public function getContactByFullName(string $firstNamestring $lastNamestring $email)
  110.     {
  111.         // $firstName = preg_replace('/[^a-zA-Z0-9\s]/', '\\\\$0', $firstName);
  112.         // $lastName = preg_replace('/[^a-zA-Z0-9\s]/', '\\\\$0', $lastName);
  113.         $firstName str_replace("'""''"$firstName);
  114.         $lastName str_replace("'""''"$lastName);
  115.         
  116.         $query 'SELECT Id FROM Contact WHERE FirstName = \''$firstName .'\' AND LastName = \''$lastName .'\'';
  117.         $result $this->salesforceCrud->query($query);
  118.         if ($result['totalSize'] <= 0) {
  119.             return false;
  120.         }
  121.         $id $result['records'][0]['Id'];
  122.         if ($result['totalSize'] > 1) {
  123.             $id $this->getContact($email);
  124.         }
  125.         return $id;
  126.     }
  127.     /**
  128.      * Create salesforce contact obj
  129.      *
  130.      * @param User $user
  131.      * @return string
  132.      */
  133.     public function createContact(User $user): string
  134.     {
  135.         $id $this->getContact($user->getEmail());
  136.         // $id = $this->getContactByEmailandFullName($user->getFirstName(), $user->getLastName(), $user->getEmail());
  137.         if (!$id) {
  138.             $data =  $this->salesforceObjectConverter->convertUserToContact($user);
  139.         
  140.             $id $this->salesforceCrud->create(Constant::CONTACT_OBJ$data);  #returns id
  141.         }
  142.     
  143.         return $id;
  144.     }
  145.     /**
  146.      * Update Salesforce contact obj
  147.      *
  148.      * @param User $user
  149.      * @return void
  150.      */
  151.     public function updateContact(User $user)
  152.     {
  153.         $userInformation $user->getUserInformation();
  154.         $id $userInformation->getSalesforceId();
  155.         $data =  $this->salesforceObjectConverter->convertUserToContact($user);
  156.         
  157.         $this->salesforceCrud->update(Constant::CONTACT_OBJ$id$data);  #returns id
  158.     }
  159.     /**
  160.      * Create contact salesforce object from FilmProjectMember entity
  161.      *
  162.      * @param FilmProjectMember $filmProjectMember
  163.      * @return void|string
  164.      */
  165.     public function createContactFromFilmMember(FilmProjectMember $filmProjectMember)
  166.     {
  167.         $id $this->getContact($filmProjectMember->getEmail());
  168.         // $id = $this->getContactByEmailandFullName($filmProjectMember->getName(), $filmProjectMember->getLastName(), $filmProjectMember->getEmail());
  169.         if (!$id) {
  170.             $data $this->salesforceObjectConverter->convertFilmMemberToContact($filmProjectMember);            
  171.             try {
  172.                 $id $this->salesforceCrud->create(Constant::CONTACT_OBJ$data);  #returns id
  173.             } catch (ClientException $e) {
  174.                 $response $e->getResponse();
  175.                 $responseBodyAsString $response->getBody()->getContents();
  176.                 $this->throwExceptionMessage($responseBodyAsString);
  177.                 // var_dump($responseBodyAsString); exit();
  178.             }
  179.         }
  180.     
  181.         return $id;
  182.     }
  183.     /**
  184.      * Update contact salesforce object from FilmProjectMember entity
  185.      *
  186.      * @param FilmProjectMember $filmProjectMember
  187.      * @return void|string
  188.      */
  189.     public function updateContactFromFilmMember(FilmProjectMember $filmProjectMember)
  190.     {
  191.         $salesforceId $filmProjectMember->getSalesforceId();
  192.         if (!$salesforceId) {
  193.             $data $this->salesforceObjectConverter->convertFilmMemberToContact($filmProjectMember);            
  194.             try {
  195.                 $this->salesforceCrud->update(Constant::CONTACT_OBJ$salesforceId$data);  #returns id
  196.             } catch (ClientException $e) {
  197.                 $response $e->getResponse();
  198.                 $responseBodyAsString $response->getBody()->getContents();
  199.                 $this->throwExceptionMessage($responseBodyAsString);
  200.             }
  201.         }
  202.     }
  203.     /**
  204.      * Create salesforce contact obj
  205.      *
  206.      * @param User $user
  207.      * @return string
  208.      */
  209.     public function createGuestContactFromOrderDetails(OrderBillingDetails $orderBillingDetails): ?string
  210.     {
  211.         $id $this->getContactByEmailandFullName($orderBillingDetails->getFirstName(), $orderBillingDetails->getLastName(), $orderBillingDetails->getEmailAddress()) ?? $this->getContact($orderBillingDetails->getEmailAddress());
  212.         if (!$id) {
  213.             $data =  $this->salesforceObjectConverter->convertBillingDetailsToContact($orderBillingDetails);
  214.         
  215.             $id $this->salesforceCrud->create(Constant::CONTACT_OBJ$data);  #returns id
  216.         }
  217.     
  218.         return $id;
  219.     }
  220.     /**
  221.      * Create account salesforce object from film member on film application
  222.      *
  223.      * @return void|string
  224.      */
  225.     public function createAccount(string $organisationNamestring $primaryContactSfId null)
  226.     {
  227.         if ($organisationName) {
  228.             $id $this->getAccount($organisationName);
  229.             if (!$id) {
  230.                 $data = [
  231.                     'Name' => $organisationName,
  232.                     'RecordTypeId' => Constant::SALESFORCE_ORGANISATION_FILMMAKER,
  233.                 ];
  234.                 if ($primaryContactSfId) {
  235.                     $data['npe01__One2OneContact__c'] = $primaryContactSfId;
  236.                 }
  237.                 
  238.                 $id $this->salesforceCrud->create(Constant::ACCOUNT_OBJ$data);  #returns id
  239.             } else {
  240.                 if ($primaryContactSfId) {
  241.                     // $data['npe01__One2OneContact__c'] = $primaryContactSfId;
  242.                     // $statusCode = $this->salesforceCrud->update(Constant::ACCOUNT_OBJ, $id, $data);  // Update Account
  243.                     $data = [
  244.                         'npsp__Primary_Affiliation__c' => $id
  245.                     ];
  246.                     
  247.                     $this->salesforceCrud->update(Constant::CONTACT_OBJ$primaryContactSfId$data); // Update contact affiliation to account
  248.                 }
  249.             }
  250.             
  251.             return $id;
  252.         }
  253.         return false;
  254.     }
  255.     /**
  256.      * Create account salesforce object from film member on film application
  257.      *
  258.      * @return void|string
  259.      */
  260.     public function createAccountWithOrderDetails(string $organisationName, ?OrderDetails $orderDetails null, ?string $primaryContactSfId null)
  261.     {
  262.         if ($organisationName) {
  263.             $id $this->getAccount($organisationName);
  264.             $data = [];
  265.             if ($orderDetails) {
  266.                 $orderBillingDetails $orderDetails->getOrderBillingDetails();
  267.                 if ($orderBillingDetails) {
  268.                     $data['Email__c'] = $orderBillingDetails->getEmailAddress();
  269.                     $data['Phone'] = $orderBillingDetails->getPhoneNumber();
  270.                     $data['BillingStreet'] = $orderBillingDetails->getStreet();
  271.                     $data['BillingState'] = $orderBillingDetails->getState();
  272.                     $data['BillingPostalCode'] = $orderBillingDetails->getPostcode();
  273.                     $data['BillingCountry'] = $orderBillingDetails->getCountry();
  274.                     $data['BillingCity'] = $orderBillingDetails->getSuburb();
  275.                 }
  276.             }
  277.             if (!$id) {
  278.                 $data['Name'] = $organisationName;
  279.                 $data['RecordTypeId'] = Constant::SALESFORCE_ORGANISATION_FILMMAKER;
  280.                 if ($primaryContactSfId) {
  281.                     $data['npe01__One2OneContact__c'] = $primaryContactSfId;
  282.                 }
  283.                 // TODO: IF not exists, create account with primary contact
  284.                 $id $this->salesforceCrud->create(Constant::ACCOUNT_OBJ$data);  #returns id
  285.             } else {
  286.                 // TODO: IF exists, update account with affiliate contact
  287.                 if ($primaryContactSfId) {
  288.                     // Update organisation salesforce object
  289.                     
  290.                     try {
  291.                         $data['npe01__One2OneContact__c'] = $primaryContactSfId;
  292.                         $statusCode $this->salesforceCrud->update(Constant::ACCOUNT_OBJ$id$data);  // Update Account
  293.                     } catch (ClientException $e) {
  294.                     }
  295.                     // Update Contact Object
  296.                     try {
  297.                         $contactData = ['npsp__Primary_Affiliation__c' => $id];
  298.                         $this->salesforceCrud->update(Constant::CONTACT_OBJ$primaryContactSfId$contactData); // Update contact affiliation to account
  299.                     } catch (ClientException $e) {
  300.                     }
  301.                 }
  302.             }
  303.             
  304.             return $id;
  305.         }
  306.         return false;
  307.     }
  308.     /**
  309.      * get organisation ID if exists, return false if doesnt exists
  310.      *
  311.      * @param User $user
  312.      * @return void
  313.      */
  314.     public function getAccount(string $organisationName)
  315.     {
  316.         $organisationName addslashes($organisationName);
  317.         $query 'SELECT Id FROM Account WHERE Name = \''$organisationName .'\'';
  318.         $result $this->salesforceCrud->query($query);
  319.         if ($result['totalSize'] <= 0) {
  320.             return false;
  321.         }
  322.         $id $result['records'][0]['Id'];
  323.         return $id;
  324.     }
  325.     /**
  326.      * Create account salesforce object from film member on film application
  327.      *
  328.      * @return void|string
  329.      */
  330.     public function createAccountFromFilmProjectMember(FilmProjectMember $filmProjectMember)
  331.     {
  332.         if ($organisationName $filmProjectMember->getOrganisation()) {
  333.             $id $this->getAccount($organisationName);
  334.             if (!$id) {
  335.                 $data = [
  336.                     'Name' => $organisationName,
  337.                     'RecordTypeId' => Constant::SALESFORCE_ORGANISATION_FILMMAKER,
  338.                 ];
  339.                 
  340.                 $id $this->salesforceCrud->create(Constant::ACCOUNT_OBJ$data);  #returns id
  341.             }
  342.         
  343.             return $id;
  344.         }
  345.         return false;
  346.     }
  347.     /**
  348.      * Create campaign object from film project entity
  349.      *
  350.      * @param FilmProject $filmProject
  351.      * @return void|string
  352.      */
  353.     public function createCampaign(FilmProject $filmProject)
  354.     {
  355.         $id null;
  356.         if (!empty($filmProject->getOldWordpressId())) {
  357.             $id $this->getCampaignByWordpressId($filmProject->getOldWordpressId());
  358.         } 
  359.         if (!$id) {
  360.             $data $this->salesforceObjectConverter->convertProjectToCampaign($filmProject);
  361.             try {
  362.                 $id $this->salesforceCrud->create(Constant::CAMPAIGN_OBJ$data); 
  363.             } catch (ClientException $e) {
  364.                 $response $e->getResponse();
  365.                 $responseBodyAsString $response->getBody()->getContents();
  366.                 $this->throwExceptionMessage($responseBodyAsString);
  367.                 return null;
  368.             }
  369.         }
  370.     
  371.         return $id;
  372.     }
  373.     /**
  374.      * Update campaign object from film project entity
  375.      *
  376.      * @param FilmProject $filmProject
  377.      * @return void
  378.      */
  379.     public function updateCampaign(FilmProject $filmProject)
  380.     {
  381.         $salesforceId $filmProject->getSalesforceId();
  382.         $data $this->salesforceObjectConverter->convertProjectToCampaign($filmProjecttrue);
  383.         try {
  384.             $id $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$data); 
  385.         } catch (ClientException $e) {
  386.             $response $e->getResponse();
  387.             $responseBodyAsString $response->getBody()->getContents();
  388.             $this->throwExceptionMessage($responseBodyAsString);
  389.             var_dump($responseBodyAsString); exit();
  390.         }
  391.     
  392.         return $id;
  393.     }
  394.     public function updateCampaignAdmin(FilmProject $filmProject) {
  395.         $salesforceId $filmProject->getSalesforceId();
  396.         $data = [
  397.             'Application_Fee_Paid__c' => 0,
  398.             'Application_Fee_Amount__c' => 0,
  399.         ];
  400.         try {
  401.             $id $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$data); 
  402.         } catch (ClientException $e) {
  403.             $response $e->getResponse();
  404.             $responseBodyAsString $response->getBody()->getContents();
  405.             $this->throwExceptionMessage($responseBodyAsString);
  406.             var_dump($responseBodyAsString); exit();
  407.         }
  408.         return $id;
  409.     }
  410.     /**
  411.      * Update campaign object to be approved
  412.      *
  413.      * @param FilmProject $filmProject
  414.      * @return void
  415.      */
  416.     public function approveCampaign(FilmProject $filmProject
  417.     {
  418.         $salesforceId $filmProject->getSalesforceId();
  419.         $updatedData = [
  420.             'Project_Approved__c' => 1,
  421.             // 'Project_Approval_Date__c' => (new \DateTime('today'))->format('Y-m-d'),
  422.             'Wordpress_ID__c' => $filmProject->getWordpressId() ? $filmProject->getWordpressId() : '',
  423.             // 'Wordpress_ID_2023__c' => $filmProject->getWordpressId() ? $filmProject->getWordpressId() : '',
  424.             'IsActive' => 1,
  425.             'Status' => 'Completed'
  426.         ];
  427.         
  428.         if ($filmProject->getOldWordpressId()) { // If an old migrated project
  429.             $updatedData['Wordpress_ID__c'] = $filmProject->getOldWordpressId();
  430.         } else { // New project
  431.             $updatedData['Project_Approval_Date__c'] = (new \DateTime('today'))->format('Y-m-d');
  432.         }
  433.         $statusCode =  $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$updatedData);
  434.     }
  435.     /**
  436.      * Updated campaign object to be declined
  437.      *
  438.      * @param FilmProject $filmProject
  439.      * @return void
  440.      */
  441.     public function declineCampaign(FilmProject $filmProject)
  442.     {
  443.         $salesforceId $filmProject->getSalesforceId();
  444.         $updatedData = [
  445.             'Project_Approved__c' => 0,
  446.             'Rejected_Date__c' => (new \DateTime('today'))->format('Y-m-d'),
  447.             'Resubmit__c' => 0,
  448.         ];
  449.         $statusCode =  $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$updatedData);
  450.     }
  451.     /**
  452.      * Updated campaign object to be asked to resubmit
  453.      *
  454.      * @param FilmProject $filmProject
  455.      * @return void
  456.      */
  457.     public function resubmitCampaign(FilmProject $filmProject)
  458.     {
  459.         $salesforceId $filmProject->getSalesforceId();
  460.         $updatedData = [
  461.             'Project_Approved__c' => 0,
  462.             // 'Rejected_Date__c' => (new \DateTime('today'))->format('Y-m-d'),
  463.             'Status' => 'In Progress',
  464.             'Resubmit__c' => 1,
  465.         ];
  466.         $statusCode =  $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$updatedData);
  467.     }
  468.     /**
  469.      * Create Film Application Transaction
  470.      *
  471.      * @param FilmProject $filmProject
  472.      * @param OrderDetails $orderDetails
  473.      * @return string
  474.      */
  475.     public function createFilmApplicationTransaction(FilmProject $filmProject)
  476.     {
  477.         $orderDetails $filmProject->getOrderDetails();
  478.         $projectOwner $filmProject->getOwner();
  479.         $accountName sprintf('%s (%s) Household'$projectOwner->getLastName(), $projectOwner->getFirstName());
  480.         
  481.         $ownerAccountId "";
  482.         $ownerSfId $this->getContact($projectOwner->getEmail());
  483.         if ($ownerSfId) {
  484.             $ownerAccountId $this->createAccountWithOrderDetails($accountName$orderDetails$ownerSfId);
  485.         } else {
  486.             $ownerAccountId $this->createAccountWithOrderDetails($accountName$orderDetails);
  487.         }
  488.         $data = [
  489.             'RecordTypeId' => Constant::SALESFORCE_TRANSACTION_TRANSACTION,
  490.             'Name' => 'Film Application - '$filmProject->getTitle(),
  491.             'AccountId' =>  $ownerAccountId,
  492.             // 'npsp__Primary_Contact__c' => $projectOwner->getUserInformation()->getSalesforceId(),
  493.             'LeadSource' => 'Website',
  494.             'Type' => 'Film Application Fee',
  495.             'StageName' => 'Closed',
  496.             'CloseDate' => (new \DateTime('now'))->format('Y-m-d'),
  497.             'Probability' => 100,
  498.             'DAF_retention__c' => '100%',
  499.             'Consent_to_be_identified__c' => 'Does NOT consent',
  500.             'Share_details__c' => 'DOESN\'T consent',
  501.             'Payment_Type__c' => 'Credit Card',
  502.             // 'Order_ID__c' => $orderDetails->getInvoiceNumber(),
  503.             'Order_ID__c' => 'MW'$orderDetails->getId(),
  504.             'Description' => $filmProject->getTitle(),
  505.             'Amount' => number_format(((float)$orderDetails->getTotal() / 100), 2'.'''),
  506.             // 'npe01__Amount_Written_Off__c' => 0,
  507.             // 'npe01__Amount_Outstanding__c' => 0,
  508.             'npsp__Acknowledgment_Status__c' => 'Do Not Acknowledge',
  509.             'CampaignId' =>  ($filmProject->getSalesforceId()) ? $filmProject->getSalesforceId() : '',
  510.         ];
  511.         
  512.         if ($ownerSfId) {
  513.             $data['npsp__Primary_Contact__c'] = $ownerSfId;
  514.         }
  515.         try {
  516.             $id $this->salesforceCrud->create(Constant::TRANSACTION_OBJ$data);
  517.         } catch (ClientException $e) {
  518.             $response $e->getResponse();
  519.             $responseBodyAsString $response->getBody()->getContents();
  520.             $this->throwExceptionMessage($responseBodyAsString);
  521.             var_dump($responseBodyAsString); exit();
  522.         }
  523.         return $id;
  524.     }
  525.     /**
  526.      * Create Donation Transaction
  527.      *
  528.      * @param Donation $donation
  529.      * @param OrderDetails $orderDetails
  530.      * @return void
  531.      */
  532.     public function createDonationTransaction(Donation $donation)
  533.     {
  534.         $orderDetails $donation->getOrderDetails();
  535.         $filmProject $donation->getFilmProject();
  536.         $orderBillingDetails $orderDetails->getOrderBillingDetails();
  537.         $accountName $orderBillingDetails->getOrganisation();
  538.         $user $orderDetails->getUser();
  539.         $salesforceDonatorId $user ? ($user->isAdmin() ? $this->createGuestContactFromOrderDetails($orderBillingDetails) : $user->getUserInformation()->getSalesforceId()) : $this->createGuestContactFromOrderDetails($orderBillingDetails);
  540.         if (empty($accountName)) {
  541.             $accountName sprintf('%s (%s) Household'$orderBillingDetails->getLastName(), $orderBillingDetails->getFirstName());
  542.         }
  543.         $ownerAccountId $this->createAccountWithOrderDetails($accountName$orderDetails$salesforceDonatorId); // Get owner Id
  544.         $data = [
  545.             'RecordTypeId' => Constant::SALESFORCE_TRANSACTION_TRANSACTION,
  546.             'Name' => 'Donation - '$filmProject->getTitle(),
  547.             'AccountId' =>  $ownerAccountId,
  548.             'npsp__Primary_Contact__c' => $salesforceDonatorId,
  549.             'LeadSource' => 'Website',
  550.             'Type' => 'Film Donation',
  551.             'StageName' => 'Closed',
  552.             'CloseDate' => (new \DateTime('now'))->format('Y-m-d'),
  553.             'Probability' => 100,
  554.             'DAF_retention__c' => '5%',
  555.             'Consent_to_be_identified__c' =>  ($donation->isIsOrganisationConsent()) ? 'Does consent' 'Does NOT consent',
  556.             'Share_details__c' => ($donation->isIsContactShared()) ? 'Does consent' 'DOESN\'T consent',
  557.             'Payment_Type__c' => 'Credit Card',
  558.             'Order_ID__c' => 'MW'$orderDetails->getId(),
  559.             'Description' => 'Donation for project: '$filmProject->getTitle(),
  560.             'Amount' => number_format(((float)($orderDetails->getTotal() - $donation->getSupportdaf()) / 100), 2'.'''),
  561.             // 'npe01__Amount_Written_Off__c' => 0,
  562.             // 'npe01__Amount_Outstanding__c' => 0,
  563.             'npsp__Acknowledgment_Status__c' => 'Do Not Acknowledge',
  564.             'CampaignId' => $filmProject->getSalesforceId(),
  565.             // 'WordPress_ID__c' => $filmProject->getWordpressId(),
  566.             // 'Wordpress_ID_from_2023__c' => $filmProject->getWordpressId(),
  567.         ];
  568.         // Check payment details
  569.         $paymentDetails $orderDetails->getPaymentDetails();
  570.         if ($paymentDetails) {
  571.             if ($paymentDetails->getType() == Constant::PAYMENT_EFT) {
  572.                 $data['Payment_Type__c'] = Constant::PAYMENT_EFT;
  573.             }
  574.         }
  575.         // Change close date
  576.         if ($orderDate $orderDetails->getCreatedAt()) {
  577.             $data['CloseDate'] = $orderDate->format('Y-m-d');
  578.         }
  579.         try {
  580.             $id $this->salesforceCrud->create(Constant::TRANSACTION_OBJ$data);
  581.         } catch (ClientException $e) {
  582.             $response $e->getResponse();
  583.             $responseBodyAsString $response->getBody()->getContents();
  584.             $this->throwExceptionMessage($responseBodyAsString);
  585.             var_dump($responseBodyAsString); exit();
  586.         }
  587.     
  588.         return $id;
  589.     }
  590.      /**
  591.      * Create DAF Donation Transaction
  592.      *
  593.      * @param Donation $donation
  594.      * @param OrderDetails $orderDetails
  595.      * @return string
  596.      */
  597.     public function createDafDonationTransaction(Donation $donation$popUpSalesforceId null)
  598.     {
  599.         $orderDetails $donation->getOrderDetails();
  600.         $user $orderDetails->getUser();
  601.         $orderBillingDetails $orderDetails->getOrderBillingDetails();
  602.         $accountName $orderBillingDetails->getOrganisation();
  603.         
  604.         $salesforceDonatorId null;
  605.         if ($user) { 
  606.             $salesforceDonatorId $user->getUserInformation()->getSalesforceId();
  607.         } else { // GUEST
  608.             $salesforceDonatorId $this->createGuestContactFromOrderDetails($orderBillingDetails);
  609.         }
  610.         if (empty($accountName)) {
  611.             $accountName sprintf('%s (%s) Household'$orderBillingDetails->getLastName(), $orderBillingDetails->getFirstName());
  612.         }
  613.         $ownerAccountId $this->createAccountWithOrderDetails($accountName$orderDetails$salesforceDonatorId); // Get owner Id
  614.         $data = [
  615.             'RecordTypeId' => Constant::SALESFORCE_TRANSACTION_TRANSACTION,
  616.             'Name' => 'Donation - Support Documentary Australia',
  617.             'AccountId' =>  $ownerAccountId,
  618.             'npsp__Primary_Contact__c' => $salesforceDonatorId,
  619.             'LeadSource' => 'Website',
  620.             'Type' => 'Donation',
  621.             'StageName' => 'Closed',
  622.             'CloseDate' => (new \DateTime('now'))->format('Y-m-d'),
  623.             'Probability' => 100,
  624.             'DAF_retention__c' => '100%',
  625.             'Consent_to_be_identified__c' =>  ($donation->isIsOrganisationConsent()) ? 'Does consent' 'Does NOT consent',
  626.             'Share_details__c' => ($donation->isIsContactShared()) ? 'Does consent' 'DOESN\'T consent',
  627.             'Payment_Type__c' => 'Credit Card',
  628.             'Order_ID__c' => 'MW'$orderDetails->getId(),
  629.             'Description' => 'Support Documentary Australia',
  630.             // 'Wordpress_ID_from_2023__c' => '35040', // Latest Support Documentary Australia Project
  631.             'Amount' => number_format(((float)$donation->getSupportdaf() / 100), 2'.'''),
  632.             // 'npe01__Amount_Written_Off__c' => 0,
  633.             // 'npe01__Amount_Outstanding__c' => 0,
  634.             'npsp__Acknowledgment_Status__c' => 'Do Not Acknowledge',
  635.         ];
  636.         if (!$popUpSalesforceId) {
  637.             if ($donation->getFilmProject()) {
  638.                 $filmProject $donation->getFilmProject();
  639.                 $data['CampaignId'] = $filmProject->getSalesforceId();
  640.                 if ($filmProject->isIsDafCore() && $filmProject->isIsDafProgram()) {
  641.                     $data['Name'] = 'Donation - '$filmProject->getTitle();
  642.                     $data['Description'] = 'Donation - '$filmProject->getTitle();
  643.                     // $data['Wordpress_ID_from_2023__c'] = $filmProject->getWordpressId();
  644.                     $data['Amount'] = number_format(((float)$orderDetails->getTotal() / 100), 2'.''');
  645.                 }
  646.             }
  647.         } else {
  648.             $data['CampaignId'] = $popUpSalesforceId;
  649.             // $data['Wordpress_ID_from_2023__c'] = '17135'; // hardcoded latest popup filmproject
  650.             $data['Name'] = 'Donation - Documentary Australia\'s core operations_POP-UP';
  651.         }
  652.         // Check payment details
  653.         $paymentDetails $orderDetails->getPaymentDetails();
  654.         if ($paymentDetails) {
  655.             if ($paymentDetails->getType() == Constant::PAYMENT_EFT) {
  656.                 $data['Payment_Type__c'] = Constant::PAYMENT_EFT;
  657.             }
  658.         }
  659.         // Change close date
  660.         if ($orderDate $orderDetails->getCreatedAt()) {
  661.             $data['CloseDate'] = $orderDate->format('Y-m-d');
  662.         }
  663.         try {
  664.             $id $this->salesforceCrud->create(Constant::TRANSACTION_OBJ$data);
  665.         } catch (ClientException $e) {
  666.             $response $e->getResponse();
  667.             $responseBodyAsString $response->getBody()->getContents();
  668.             $this->throwExceptionMessage($responseBodyAsString);
  669.             var_dump($responseBodyAsString); exit();
  670.         }
  671.     
  672.         return $id;
  673.     }
  674.     /**
  675.      * Updated transaction details
  676.      *
  677.      * @param OrderDetails $orderDetails
  678.      * @return void
  679.      */
  680.     public function updateTransaction(OrderDetails $orderDetails)
  681.     {
  682.         $salesforceId $orderDetails->getSalesforceId();
  683.         if (empty($salesforceId)) {
  684.             return;
  685.         }
  686.         $updatedData = [
  687.             'Amount' => $orderDetails->getTotal() / 100,
  688.         ];
  689.         if ($donation $orderDetails->getDonation()) {
  690.             $filmProject $donation->getFilmProject();
  691.             $updatedData = [
  692.                 'Name' => 'Donation - '$filmProject->getTitle(),
  693.                 'Amount' => $orderDetails->getTotal() / 100,
  694.                 'CampaignId' => $filmProject->getSalesforceId(),
  695.                 'StageName' => 'Closed',
  696.                 'npsp__Acknowledgment_Status__c' => 'Do Not Acknowledge',
  697.             ];
  698.     
  699.         }
  700.         try {
  701.             $statusCode =  $this->salesforceCrud->update(Constant::TRANSACTION_OBJ$salesforceId$updatedData);
  702.         } catch (ClientException $e) {
  703.             $response $e->getResponse();
  704.             $responseBodyAsString $response->getBody()->getContents();
  705.             $this->throwExceptionMessage("Opportunity object on Salesforce is not found. Please contact admin for more information.");
  706.             var_dump($responseBodyAsString); exit();
  707.         }
  708.         // Update payment amount as well
  709.         $paymentId $this->getPaymentByTransactionId($salesforceId);
  710.         if ($paymentId) {
  711.             $this->updatePaymentAmountReceived($paymentId$orderDetails->getTotal() / 100);
  712.         }
  713.     }
  714.      /**
  715.      * Updated campaign object to be refunded
  716.      *
  717.      * @param OrderDetails $orderDetails
  718.      * @return void
  719.      */
  720.     public function refundTransaction(OrderDetails $orderDetails)
  721.     {
  722.         $salesforceId $orderDetails->getSalesforceId();
  723.         if (empty($salesforceId)) {
  724.             return;
  725.         }
  726.         $updatedData = [
  727.             'Amount' => 0,
  728.             'Refunded__c' => 1,
  729.         ];
  730.         try {
  731.             $statusCode =  $this->salesforceCrud->update(Constant::TRANSACTION_OBJ$salesforceId$updatedData);
  732.         } catch (ClientException $e) {
  733.             $response $e->getResponse();
  734.             $responseBodyAsString $response->getBody()->getContents();
  735.             $this->throwExceptionMessage($responseBodyAsString);
  736.             var_dump($responseBodyAsString); exit();
  737.         }
  738.         // Update payment amount as well
  739.         $paymentId $this->getPaymentByTransactionId($salesforceId);
  740.         if ($paymentId) {
  741.             $this->updatePaymentAmountRefund($paymentId);
  742.         }
  743.     }
  744.     /**
  745.      * Updated campaign object to be refunded
  746.      *
  747.      * @param OrderDetails $orderDetails
  748.      * @return void
  749.      */
  750.     public function refundDafSupportTransaction(Donation $donation)
  751.     {
  752.         $salesforceId $donation->getSupportDafSalesforce();
  753.         if (empty($salesforceId)) {
  754.             return;
  755.         }
  756.         $updatedData = [
  757.             'Amount' => 0,
  758.             'Refunded__c' => 1,
  759.         ];
  760.         try {
  761.             $statusCode =  $this->salesforceCrud->update(Constant::TRANSACTION_OBJ$salesforceId$updatedData);
  762.         } catch (ClientException $e) {
  763.             $response $e->getResponse();
  764.             $responseBodyAsString $response->getBody()->getContents();
  765.             $this->throwExceptionMessage($responseBodyAsString);
  766.             var_dump($responseBodyAsString); exit();
  767.         }
  768.         // Update payment amount as well
  769.         $paymentId $this->getPaymentByTransactionId($salesforceId);
  770.         if ($paymentId) {
  771.             $this->updatePaymentAmountRefund($paymentId);
  772.         }
  773.     }
  774.     /**
  775.      * get campaign with wordpress ID
  776.      *
  777.      * @param User $user
  778.      * @return void
  779.      */
  780.     public function getCampaignByWordpressId(int $id)
  781.     {
  782.         $query 'SELECT Id FROM Campaign WHERE Wordpress_ID__c = \''$id .'\'';
  783.         $result $this->salesforceCrud->query($query);
  784.         if ($result['totalSize'] <= 0) {
  785.             return false;
  786.         }
  787.         $id $result['records'][0]['Id'];
  788.         return $id;
  789.     }
  790.     /**
  791.      * Create campaign object from film project entity
  792.      *
  793.      * @param FilmProject $filmProject
  794.      * @return void|string
  795.      */
  796.     public function updateCampaignByPayload($salesforceId, array $payLoad)
  797.     {
  798.         try {
  799.             $id $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$payLoad); 
  800.         } catch (ClientException $e) {
  801.             $response $e->getResponse();
  802.             $responseBodyAsString $response->getBody()->getContents();
  803.             var_dump($salesforceId .' is having error'); exit();
  804.             $this->throwExceptionMessage($responseBodyAsString);
  805.             return null;
  806.         }
  807.     
  808.         return $id;
  809.     }
  810.     /**
  811.      * get contact ID if exists, return false if doesnt exists
  812.      *
  813.      * @param User $user
  814.      * @return string|void
  815.      */
  816.     public function getContactBySalesforceId(string $id)
  817.     {
  818.         $query 'SELECT Id FROM Contact WHERE Id = \''$id .'\'';
  819.         $result $this->salesforceCrud->query($query);
  820.         if ($result['totalSize'] <= 0) {
  821.             return false;
  822.         }
  823.         $id $result['records'][0]['Id'];
  824.         return $id;
  825.     }
  826.     /**
  827.      * Get Payment ID by Transaction ID
  828.      *
  829.      * @param string $id
  830.      * @return false|String
  831.      */
  832.     public function getPaymentByTransactionId(string $id
  833.     {
  834.         $query 'SELECT Id FROM npe01__OppPayment__c WHERE npe01__Opportunity__c = \''$id .'\'';
  835.         try {
  836.             $result $this->salesforceCrud->query($query);
  837.         } catch (ClientException $e) {
  838.             return false;
  839.         }
  840.         if ($result['totalSize'] <= 0) {
  841.             return false;
  842.         }
  843.         $id $result['records'][0]['Id'];
  844.         return $id;
  845.     }
  846.     /**
  847.      * Update campaign object to be approved
  848.      *
  849.      * @param String $salesforceId
  850.      * @return void
  851.      */
  852.     public function updatePaymentAmountReceived(String $salesforceId$amount
  853.     {
  854.         $updatedData = [
  855.             'npe01__Payment_Amount__c' => $amount,
  856.         ];
  857.         $statusCode =  $this->salesforceCrud->update('npe01__OppPayment__c'$salesforceId$updatedData);
  858.     }
  859.     /**
  860.      * Update campaign object to be approved
  861.      *
  862.      * @param String $salesforceId
  863.      * @return void
  864.      */
  865.     public function updatePaymentAmountRefund(String $salesforceId
  866.     {
  867.         $updatedData = [
  868.             'npe01__Payment_Amount__c' => number_format(02'.'''),
  869.         ];
  870.         $statusCode =  $this->salesforceCrud->update('npe01__OppPayment__c'$salesforceId$updatedData);
  871.     }
  872.     public function getSalesforceCrud() {
  873.         return $this->salesforceCrud;
  874.     }
  875.     public function deleteContact(User $user)
  876.     {
  877.         $userInformation $user->getUserInformation();
  878.         $id $userInformation->getSalesforceId();
  879.         try {
  880.             $this->salesforceCrud->delete(Constant::CONTACT_OBJ$id);  #returns id
  881.         } catch(Exception $e) {
  882.             return;
  883.         }
  884.     }
  885.     public function replaceWordpressIds(FilmProject $filmProject
  886.     {
  887.         $salesforceId $filmProject->getSalesforceId();
  888.         $updatedData = [
  889.             'Wordpress_ID__c' => $filmProject->getOldWordpressId() ? $filmProject->getOldWordpressId() : '',
  890.             // 'Wordpress_ID_2023__c' => $filmProject->getWordpressId() ? $filmProject->getWordpressId() : '',
  891.         ];
  892.         $statusCode =  $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$updatedData);
  893.     }
  894.     public function updateProgressReport(FilmProjectProgressReport $progressReport) {
  895.         $filmProject $progressReport->getFilmProject();
  896.         $salesforceId $filmProject->getSalesforceId();
  897.         $updatedData =  $this->salesforceObjectConverter->convertProgressReportToCampaign($progressReport);
  898.        
  899.         $statusCode =  $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ$salesforceId$updatedData);
  900.     }
  901.     /**
  902.      * Creates content version data to upload file to Salesforce
  903.      */
  904.     public function createContentVersionData(FilmProjectProgressReportFile $progressReportFile) {
  905.         $data = [
  906.             'Title' => $progressReportFile->getFileName(),
  907.             'PathOnClient' => $progressReportFile->getFileName(),
  908.             'VersionData' => Constant::LOCALHOST_URL '/uploads/progress_reports/'$progressReportFile->getFileName(),
  909.         ];
  910.         
  911.         $id $this->salesforceCrud->create('ContentVersion'$data);  #returns id
  912.         return $id;
  913.     }
  914.     /**
  915.      * Gets Document ID from uploaded ContentVersionData
  916.      */
  917.     public function getDocumentIdFromContentVersion(String $contentVersionId) {
  918.         $query 'SELECT ContentDocumentId FROM ContentVersion WHERE Id = \''$contentVersionId .'\'';
  919.         try {
  920.             $result $this->salesforceCrud->query($query);
  921.         } catch (ClientException $e) {
  922.             return false;
  923.         }
  924.         if ($result['totalSize'] <= 0) {
  925.             return false;
  926.         }
  927.         $id $result['records'][0]['ContentDocumentId'];
  928.         return $id;
  929.     }
  930.     /**
  931.      * Links between the uplaoded file and campaign ID on Salesforce end
  932.      */
  933.     public function createDocumentLink(String $contentDocumentIdString $campaignId) {
  934.         $data = [
  935.             'ContentDocumentId' => $contentDocumentId,
  936.             'LinkedEntityId' => $campaignId,
  937.             'ShareType' => 'V'// Viewer
  938.         ];
  939.         
  940.         $id $this->salesforceCrud->create('ContentDocumentLink'$data);  #returns id
  941.     }
  942. }