src/Service/SalesforceService.php line 53

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