<?php
namespace App\Service;
use App\Entity\Donation;
use App\Service\ApiService;
use App\Entity\FilmProject;
use App\Entity\FilmProjectProgressReport;
use App\Entity\FilmProjectProgressReportFile;
use App\Entity\FilmProjectMember;
use App\Entity\OrderBillingDetails;
use App\Entity\OrderDetails;
use App\Entity\User;
use App\String\Constant;
use App\Serializer\SalesforceObjectConverter;
use App\Utility\SalesforceValues;
use bjsmasth\Salesforce\Authentication\PasswordAuthentication;
use bjsmasth\Salesforce\CRUD;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
final class SalesforceService extends ApiService
{
private ContainerBagInterface $params;
private CRUD $salesforceCrud;
private SalesforceObjectConverter $salesforceObjectConverter;
public function __construct(ContainerBagInterface $params, SalesforceObjectConverter $salesforceObjectConverter)
{
$this->params = $params;
$this->salesforceObjectConverter = $salesforceObjectConverter;
$this->salesforceCrud = $this->authenticate();
}
/**
* Setup access token and instance URL in session
*
* @return void
*/
public function authenticate()
{
$options = [
'grant_type' => 'password',
'client_id' => $this->params->get('app.salesforce_client_id'),
'client_secret' => $this->params->get('app.salesforce_client_secret'),
'username' => $this->params->get('app.salesforce_username'),
'password' => $this->params->get('app.salesforce_password'),
];
$salesforce = new PasswordAuthentication($options);
$salesforce->setEndpoint(Constant::SALESFORCE_TOKEN_URL);
$salesforce->authenticate();
return new CRUD();
}
/**
* get contact ID if exists, return false if doesnt exists
*
* @param User $user
* @return string|void
*/
public function getContact(string $email)
{
$query = 'SELECT Id FROM Contact WHERE Email = \''. $email .'\'';
$result = $this->salesforceCrud->query($query);
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['Id'];
return $id;
}
/**
* get contact ID if exists, return false if doesnt exists
*
* @param User $user
* @return string|void
*/
public function getContactByEmailandFullName(string $firstName, string $lastName, string $email)
{
$firstName = addslashes($firstName);
$lastName = addslashes($lastName);
$query = 'SELECT Id FROM Contact WHERE Email = \''. $email .'\' AND FirstName = \''. $firstName .'\' AND LastName = \''. $lastName .'\'';
$result = $this->salesforceCrud->query($query);
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['Id'];
return $id;
}
/**
* Create salesforce contact obj
*
* @param User $user
* @return string
*/
public function createContact(User $user): string
{
// $id = $this->getContact($user->getEmail());
$id = $this->getContactByEmailandFullName($user->getFirstName(), $user->getLastName(), $user->getEmail());
if (!$id) {
$data = $this->salesforceObjectConverter->convertUserToContact($user);
$id = $this->salesforceCrud->create(Constant::CONTACT_OBJ, $data); #returns id
}
return $id;
}
/**
* Update Salesforce contact obj
*
* @param User $user
* @return void
*/
public function updateContact(User $user)
{
$userInformation = $user->getUserInformation();
$id = $userInformation->getSalesforceId();
$data = $this->salesforceObjectConverter->convertUserToContact($user);
$this->salesforceCrud->update(Constant::CONTACT_OBJ, $id, $data); #returns id
}
/**
* Create contact salesforce object from FilmProjectMember entity
*
* @param FilmProjectMember $filmProjectMember
* @return void|string
*/
public function createContactFromFilmMember(FilmProjectMember $filmProjectMember)
{
$id = $this->getContact($filmProjectMember->getEmail());
// $id = $this->getContactByEmailandFullName($filmProjectMember->getName(), $filmProjectMember->getLastName(), $filmProjectMember->getEmail());
if (!$id) {
$data = $this->salesforceObjectConverter->convertFilmMemberToContact($filmProjectMember);
try {
$id = $this->salesforceCrud->create(Constant::CONTACT_OBJ, $data); #returns id
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
// var_dump($responseBodyAsString); exit();
}
}
return $id;
}
/**
* Update contact salesforce object from FilmProjectMember entity
*
* @param FilmProjectMember $filmProjectMember
* @return void|string
*/
public function updateContactFromFilmMember(FilmProjectMember $filmProjectMember)
{
$salesforceId = $filmProjectMember->getSalesforceId();
if (!$salesforceId) {
$data = $this->salesforceObjectConverter->convertFilmMemberToContact($filmProjectMember);
try {
$this->salesforceCrud->update(Constant::CONTACT_OBJ, $salesforceId, $data); #returns id
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
}
}
}
/**
* Create salesforce contact obj
*
* @param User $user
* @return string
*/
public function createGuestContactFromOrderDetails(OrderBillingDetails $orderBillingDetails): string
{
// $id = $this->getContact($orderBillingDetails->getEmailAddress());
$id = $this->getContactByEmailandFullName($orderBillingDetails->getFirstName(), $orderBillingDetails->getLastName(), $orderBillingDetails->getEmailAddress());
if (!$id) {
$data = $this->salesforceObjectConverter->convertBillingDetailsToContact($orderBillingDetails);
$id = $this->salesforceCrud->create(Constant::CONTACT_OBJ, $data); #returns id
}
return $id;
}
/**
* Create account salesforce object from film member on film application
*
* @return void|string
*/
public function createAccount(string $organisationName, string $primaryContactSfId = null)
{
if ($organisationName) {
$id = $this->getAccount($organisationName);
if (!$id) {
$data = [
'Name' => $organisationName,
'RecordTypeId' => Constant::SALESFORCE_ORGANISATION_FILMMAKER,
];
if ($primaryContactSfId) {
$data['npe01__One2OneContact__c'] = $primaryContactSfId;
}
$id = $this->salesforceCrud->create(Constant::ACCOUNT_OBJ, $data); #returns id
} else {
if ($primaryContactSfId) {
// $data['npe01__One2OneContact__c'] = $primaryContactSfId;
// $statusCode = $this->salesforceCrud->update(Constant::ACCOUNT_OBJ, $id, $data); // Update Account
$data = [
'npsp__Primary_Affiliation__c' => $id
];
$this->salesforceCrud->update(Constant::CONTACT_OBJ, $primaryContactSfId, $data); // Update contact affiliation to account
}
}
return $id;
}
return false;
}
/**
* Create account salesforce object from film member on film application
*
* @return void|string
*/
public function createAccountWithOrderDetails(string $organisationName, OrderDetails $orderDetails = null, string $primaryContactSfId = null,)
{
if ($organisationName) {
$id = $this->getAccount($organisationName);
$data = [];
if ($orderDetails) {
$orderBillingDetails = $orderDetails->getOrderBillingDetails();
if ($orderBillingDetails) {
$data['Email__c'] = $orderBillingDetails->getEmailAddress();
$data['Phone'] = $orderBillingDetails->getPhoneNumber();
$data['BillingStreet'] = $orderBillingDetails->getStreet();
$data['BillingState'] = $orderBillingDetails->getState();
$data['BillingPostalCode'] = $orderBillingDetails->getPostcode();
$data['BillingCountry'] = $orderBillingDetails->getCountry();
$data['BillingCity'] = $orderBillingDetails->getSuburb();
}
}
if (!$id) {
$data['Name'] = $organisationName;
$data['RecordTypeId'] = Constant::SALESFORCE_ORGANISATION_FILMMAKER;
if ($primaryContactSfId) {
$data['npe01__One2OneContact__c'] = $primaryContactSfId;
}
// TODO: IF not exists, create account with primary contact
$id = $this->salesforceCrud->create(Constant::ACCOUNT_OBJ, $data); #returns id
} else {
// TODO: IF exists, update account with affiliate contact
// $statusCode = $this->salesforceCrud->update(Constant::ACCOUNT_OBJ, $id, $data); // Update Account
$data = [
'npsp__Primary_Affiliation__c' => $id
];
$this->salesforceCrud->update(Constant::CONTACT_OBJ, $primaryContactSfId, $data); // Update contact affiliation to account
}
return $id;
}
return false;
}
/**
* get organisation ID if exists, return false if doesnt exists
*
* @param User $user
* @return void
*/
public function getAccount(string $organisationName)
{
$organisationName = addslashes($organisationName);
// $organisationName = str_replace("'", "\'", $organisationName);
$query = 'SELECT Id FROM Account WHERE Name = \''. $organisationName .'\'';
$result = $this->salesforceCrud->query($query);
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['Id'];
return $id;
}
/**
* Create account salesforce object from film member on film application
*
* @return void|string
*/
public function createAccountFromFilmProjectMember(FilmProjectMember $filmProjectMember)
{
if ($organisationName = $filmProjectMember->getOrganisation()) {
$id = $this->getAccount($organisationName);
if (!$id) {
$data = [
'Name' => $organisationName,
'RecordTypeId' => Constant::SALESFORCE_ORGANISATION_FILMMAKER,
];
$id = $this->salesforceCrud->create(Constant::ACCOUNT_OBJ, $data); #returns id
}
return $id;
}
return false;
}
/**
* Create campaign object from film project entity
*
* @param FilmProject $filmProject
* @return void|string
*/
public function createCampaign(FilmProject $filmProject)
{
$id = null;
if (!empty($filmProject->getOldWordpressId())) {
$id = $this->getCampaignByWordpressId($filmProject->getOldWordpressId());
}
if (!$id) {
$data = $this->salesforceObjectConverter->convertProjectToCampaign($filmProject);
try {
$id = $this->salesforceCrud->create(Constant::CAMPAIGN_OBJ, $data);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
return null;
}
}
return $id;
}
/**
* Update campaign object from film project entity
*
* @param FilmProject $filmProject
* @return void
*/
public function updateCampaign(FilmProject $filmProject)
{
$salesforceId = $filmProject->getSalesforceId();
$data = $this->salesforceObjectConverter->convertProjectToCampaign($filmProject, true);
try {
$id = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $data);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
return $id;
}
public function updateCampaignAdmin(FilmProject $filmProject) {
$salesforceId = $filmProject->getSalesforceId();
$data = [
'Application_Fee_Paid__c' => 0,
'Application_Fee_Amount__c' => 0,
];
try {
$id = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $data);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
return $id;
}
/**
* Update campaign object to be approved
*
* @param FilmProject $filmProject
* @return void
*/
public function approveCampaign(FilmProject $filmProject)
{
$salesforceId = $filmProject->getSalesforceId();
$updatedData = [
'Project_Approved__c' => 1,
'Project_Approval_Date__c' => (new \DateTime('today'))->format('Y-m-d'),
'Wordpress_ID__c' => $filmProject->getWordpressId(),
'IsActive' => 1,
];
$statusCode = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $updatedData);
}
/**
* Updated campaign object to be declined
*
* @param FilmProject $filmProject
* @return void
*/
public function declineCampaign(FilmProject $filmProject)
{
$salesforceId = $filmProject->getSalesforceId();
$updatedData = [
'Project_Approved__c' => 0,
'Rejected_Date__c' => (new \DateTime('today'))->format('Y-m-d'),
'Resubmit__c' => 0,
];
$statusCode = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $updatedData);
}
/**
* Updated campaign object to be asked to resubmit
*
* @param FilmProject $filmProject
* @return void
*/
public function resubmitCampaign(FilmProject $filmProject)
{
$salesforceId = $filmProject->getSalesforceId();
$updatedData = [
'Project_Approved__c' => 0,
'Rejected_Date__c' => (new \DateTime('today'))->format('Y-m-d'),
'Resubmit__c' => 1,
];
$statusCode = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $updatedData);
}
/**
* Create Film Application Transaction
*
* @param FilmProject $filmProject
* @param OrderDetails $orderDetails
* @return string
*/
public function createFilmApplicationTransaction(FilmProject $filmProject)
{
$orderDetails = $filmProject->getOrderDetails();
$projectOwner = $filmProject->getOwner();
$accountName = sprintf('%s (%s) Household', $projectOwner->getLastName(), $projectOwner->getFirstName());
$ownerAccountId = "";
$ownerSfId = $this->getContact($projectOwner->getEmail());
if ($ownerSfId) {
$ownerAccountId = $this->createAccountWithOrderDetails($accountName, $orderDetails, $ownerSfId);
} else {
$ownerAccountId = $this->createAccountWithOrderDetails($accountName, $orderDetails);
}
$data = [
'RecordTypeId' => Constant::SALESFORCE_TRANSACTION_TRANSACTION,
'Name' => 'Film Application - '. $filmProject->getTitle(),
'AccountId' => $ownerAccountId,
// 'npsp__Primary_Contact__c' => $projectOwner->getUserInformation()->getSalesforceId(),
'LeadSource' => 'Website',
'Type' => 'Film Application Fee',
'StageName' => 'Closed',
'CloseDate' => (new \DateTime('now'))->format('Y-m-d'),
'Probability' => 100,
'DAF_retention__c' => '100%',
'Consent_to_be_identified__c' => 'Does NOT consent',
'Share_details__c' => 'DOESN\'T consent',
'Payment_Type__c' => 'Credit Card',
// 'Order_ID__c' => $orderDetails->getInvoiceNumber(),
'Order_ID__c' => 'MW'. $orderDetails->getId(),
'Description' => $filmProject->getTitle(),
'Amount' => number_format(((float)$orderDetails->getTotal() / 100), 2, '.', ''),
// 'npe01__Amount_Written_Off__c' => 0,
// 'npe01__Amount_Outstanding__c' => 0,
'npsp__Acknowledgment_Status__c' => (($orderDetails->getTotal() / 100) < 200) ? 'Do Not Acknowledge' : 'To Be Acknowledged',
'CampaignId' => ($filmProject->getSalesforceId()) ? $filmProject->getSalesforceId() : '',
];
if ($ownerSfId) {
$data['npsp__Primary_Contact__c'] = $ownerSfId;
}
try {
$id = $this->salesforceCrud->create(Constant::TRANSACTION_OBJ, $data);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
return $id;
}
/**
* Create Donation Transaction
*
* @param Donation $donation
* @param OrderDetails $orderDetails
* @return void
*/
public function createDonationTransaction(Donation $donation)
{
$orderDetails = $donation->getOrderDetails();
$filmProject = $donation->getFilmProject();
$orderBillingDetails = $orderDetails->getOrderBillingDetails();
$accountName = $orderBillingDetails->getOrganisation();
$user = $orderDetails->getUser();
$salesforceDonatorId = null;
if ($user) {
$salesforceDonatorId = $user->getUserInformation()->getSalesforceId();
} else { // GUEST
$salesforceDonatorId = $this->createGuestContactFromOrderDetails($orderBillingDetails);
}
if (empty($accountName)) {
$accountName = sprintf('%s (%s) Household', $orderBillingDetails->getLastName(), $orderBillingDetails->getFirstName());
}
$ownerAccountId = $this->createAccountWithOrderDetails($accountName, $orderDetails, $salesforceDonatorId); // Get owner Id
$data = [
'RecordTypeId' => Constant::SALESFORCE_TRANSACTION_TRANSACTION,
'Name' => 'Donation - '. $filmProject->getTitle(),
'AccountId' => $ownerAccountId,
'npsp__Primary_Contact__c' => $salesforceDonatorId,
'LeadSource' => 'Website',
'Type' => 'Film Donation',
'StageName' => 'Closed',
'CloseDate' => (new \DateTime('now'))->format('Y-m-d'),
'Probability' => 100,
'DAF_retention__c' => '5%',
'Consent_to_be_identified__c' => ($donation->isIsOrganisationConsent()) ? 'Does consent' : 'Does NOT consent',
'Share_details__c' => ($donation->isIsContactShared()) ? 'Does consent' : 'DOESN\'T consent',
'Payment_Type__c' => 'Credit Card',
'Order_ID__c' => 'MW'. $orderDetails->getId(),
'Description' => 'Donation for project: '. $filmProject->getTitle(),
'Amount' => number_format(((float)($orderDetails->getTotal() - $donation->getSupportdaf()) / 100), 2, '.', ''),
// 'npe01__Amount_Written_Off__c' => 0,
// 'npe01__Amount_Outstanding__c' => 0,
'npsp__Acknowledgment_Status__c' => ((($orderDetails->getTotal() - $donation->getSupportdaf()) / 100) < 200) ? 'Do Not Acknowledge' : 'To Be Acknowledged',
'CampaignId' => $filmProject->getSalesforceId(),
// 'WordPress_ID__c' => $filmProject->getWordpressId(),
// 'Wordpress_ID_from_2023__c' => $filmProject->getWordpressId(),
];
// Check payment details
$paymentDetails = $orderDetails->getPaymentDetails();
if ($paymentDetails) {
if ($paymentDetails->getType() == Constant::PAYMENT_EFT) {
$data['Payment_Type__c'] = Constant::PAYMENT_EFT;
}
}
// Change close date
if ($orderDate = $orderDetails->getCreatedAt()) {
$data['CloseDate'] = $orderDate->format('Y-m-d');
}
try {
$id = $this->salesforceCrud->create(Constant::TRANSACTION_OBJ, $data);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
return $id;
}
/**
* Create DAF Donation Transaction
*
* @param Donation $donation
* @param OrderDetails $orderDetails
* @return string
*/
public function createDafDonationTransaction(Donation $donation, $popUpSalesforceId = null)
{
$orderDetails = $donation->getOrderDetails();
$user = $orderDetails->getUser();
$orderBillingDetails = $orderDetails->getOrderBillingDetails();
$accountName = $orderBillingDetails->getOrganisation();
$salesforceDonatorId = null;
if ($user) {
$salesforceDonatorId = $user->getUserInformation()->getSalesforceId();
} else { // GUEST
$salesforceDonatorId = $this->createGuestContactFromOrderDetails($orderBillingDetails);
}
if (empty($accountName)) {
$accountName = sprintf('%s (%s) Household', $orderBillingDetails->getLastName(), $orderBillingDetails->getFirstName());
}
$ownerAccountId = $this->createAccountWithOrderDetails($accountName, $orderDetails, $salesforceDonatorId); // Get owner Id
$data = [
'RecordTypeId' => Constant::SALESFORCE_TRANSACTION_TRANSACTION,
'Name' => 'Support Our Work',
'AccountId' => $ownerAccountId,
'npsp__Primary_Contact__c' => $salesforceDonatorId,
'LeadSource' => 'Website',
'Type' => 'Donation',
'StageName' => 'Closed',
'CloseDate' => (new \DateTime('now'))->format('Y-m-d'),
'Probability' => 100,
'DAF_retention__c' => '100%',
'Consent_to_be_identified__c' => ($donation->isIsOrganisationConsent()) ? 'Does consent' : 'Does NOT consent',
'Share_details__c' => ($donation->isIsContactShared()) ? 'Does consent' : 'DOESN\'T consent',
'Payment_Type__c' => 'Credit Card',
'Order_ID__c' => 'MW'. $orderDetails->getId(),
'Description' => 'Support Our Work',
'Amount' => number_format(((float)$donation->getSupportdaf() / 100), 2, '.', ''),
// 'npe01__Amount_Written_Off__c' => 0,
// 'npe01__Amount_Outstanding__c' => 0,
'npsp__Acknowledgment_Status__c' => (($donation->getSupportdaf() / 100) < 200) ? 'Do Not Acknowledge' : 'To Be Acknowledged',
];
if (!$popUpSalesforceId) {
if ($donation->getFilmProject()) {
$filmProject = $donation->getFilmProject();
$data['CampaignId'] = $filmProject->getSalesforceId();
if ($filmProject->isIsDafCore() && $filmProject->isIsDafProgram()) {
$data['Name'] = 'Donation - '. $filmProject->getTitle();
$data['Description'] = 'Donation - '. $filmProject->getTitle();
// $data['Wordpress_ID_from_2023__c'] = $filmProject->getWordpressId();
}
}
} else {
$data['CampaignId'] = $popUpSalesforceId;
}
// Check payment details
$paymentDetails = $orderDetails->getPaymentDetails();
if ($paymentDetails) {
if ($paymentDetails->getType() == Constant::PAYMENT_EFT) {
$data['Payment_Type__c'] = Constant::PAYMENT_EFT;
}
}
// Change close date
if ($orderDate = $orderDetails->getCreatedAt()) {
$data['CloseDate'] = $orderDate->format('Y-m-d');
}
try {
$id = $this->salesforceCrud->create(Constant::TRANSACTION_OBJ, $data);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
return $id;
}
/**
* Updated transaction details
*
* @param OrderDetails $orderDetails
* @return void
*/
public function updateTransaction(OrderDetails $orderDetails)
{
$salesforceId = $orderDetails->getSalesforceId();
if (empty($salesforceId)) {
return;
}
$updatedData = [
'Amount' => $orderDetails->getTotal() / 100,
];
if ($donation = $orderDetails->getDonation()) {
$filmProject = $donation->getFilmProject();
$updatedData = [
'Name' => 'Donation - '. $filmProject->getTitle(),
'Amount' => $orderDetails->getTotal() / 100,
'CampaignId' => $filmProject->getSalesforceId(),
];
}
try {
$statusCode = $this->salesforceCrud->update(Constant::TRANSACTION_OBJ, $salesforceId, $updatedData);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
// Update payment amount as well
$paymentId = $this->getPaymentByTransactionId($salesforceId);
if ($paymentId) {
$this->updatePaymentAmountRefund($paymentId);
}
}
/**
* Updated campaign object to be refunded
*
* @param OrderDetails $orderDetails
* @return void
*/
public function refundTransaction(OrderDetails $orderDetails)
{
$salesforceId = $orderDetails->getSalesforceId();
if (empty($salesforceId)) {
return;
}
$updatedData = [
'Amount' => 0,
'Refunded__c' => 1,
];
try {
$statusCode = $this->salesforceCrud->update(Constant::TRANSACTION_OBJ, $salesforceId, $updatedData);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
// Update payment amount as well
$paymentId = $this->getPaymentByTransactionId($salesforceId);
if ($paymentId) {
$this->updatePaymentAmountRefund($paymentId);
}
}
/**
* Updated campaign object to be refunded
*
* @param OrderDetails $orderDetails
* @return void
*/
public function refundDafSupportTransaction(Donation $donation)
{
$salesforceId = $donation->getSupportDafSalesforce();
if (empty($salesforceId)) {
return;
}
$updatedData = [
'Amount' => 0,
'Refunded__c' => 1,
];
try {
$statusCode = $this->salesforceCrud->update(Constant::TRANSACTION_OBJ, $salesforceId, $updatedData);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
$this->throwExceptionMessage($responseBodyAsString);
var_dump($responseBodyAsString); exit();
}
// Update payment amount as well
$paymentId = $this->getPaymentByTransactionId($salesforceId);
if ($paymentId) {
$this->updatePaymentAmountRefund($paymentId);
}
}
/**
* get campaign with wordpress ID
*
* @param User $user
* @return void
*/
public function getCampaignByWordpressId(int $id)
{
$query = 'SELECT Id FROM Campaign WHERE Wordpress_ID__c = \''. $id .'\'';
$result = $this->salesforceCrud->query($query);
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['Id'];
return $id;
}
/**
* Create campaign object from film project entity
*
* @param FilmProject $filmProject
* @return void|string
*/
public function updateCampaignByPayload($salesforceId, array $payLoad)
{
try {
$id = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $payLoad);
} catch (ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
var_dump($salesforceId .' is having error'); exit();
$this->throwExceptionMessage($responseBodyAsString);
return null;
}
return $id;
}
/**
* get contact ID if exists, return false if doesnt exists
*
* @param User $user
* @return string|void
*/
public function getContactBySalesforceId(string $id)
{
$query = 'SELECT Id FROM Contact WHERE Id = \''. $id .'\'';
$result = $this->salesforceCrud->query($query);
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['Id'];
return $id;
}
/**
* Get Payment ID by Transaction ID
*
* @param string $id
* @return false|String
*/
public function getPaymentByTransactionId(string $id)
{
$query = 'SELECT Id FROM npe01__OppPayment__c WHERE npe01__Opportunity__c = \''. $id .'\'';
try {
$result = $this->salesforceCrud->query($query);
} catch (ClientException $e) {
return false;
}
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['Id'];
return $id;
}
/**
* Update campaign object to be approved
*
* @param String $salesforceId
* @return void
*/
public function updatePaymentAmountRefund(String $salesforceId)
{
$updatedData = [
'npe01__Payment_Amount__c' => number_format(0, 2, '.', ''),
];
$statusCode = $this->salesforceCrud->update('npe01__OppPayment__c', $salesforceId, $updatedData);
}
public function updateProgressReport(FilmProjectProgressReport $progressReport) {
$filmProject = $progressReport->getFilmProject();
$salesforceId = $filmProject->getSalesforceId();
$updatedData = $this->salesforceObjectConverter->convertProgressReportToCampaign($progressReport);
$statusCode = $this->salesforceCrud->update(Constant::CAMPAIGN_OBJ, $salesforceId, $updatedData);
}
/**
* Creates content version data to upload file to Salesforce
*/
public function createContentVersionData(FilmProjectProgressReportFile $progressReportFile) {
$data = [
'Title' => $progressReportFile->getFileName(),
'PathOnClient' => $progressReportFile->getFileName(),
'VersionData' => Constant::LOCALHOST_URL . '/uploads/progress_reports/'. $progressReportFile->getFileName(),
];
$id = $this->salesforceCrud->create('ContentVersion', $data); #returns id
return $id;
}
/**
* Gets Document ID from uploaded ContentVersionData
*/
public function getDocumentIdFromContentVersion(String $contentVersionId) {
$query = 'SELECT ContentDocumentId FROM ContentVersion WHERE Id = \''. $contentVersionId .'\'';
try {
$result = $this->salesforceCrud->query($query);
} catch (ClientException $e) {
return false;
}
if ($result['totalSize'] <= 0) {
return false;
}
$id = $result['records'][0]['ContentDocumentId'];
return $id;
}
/**
* Links between the uplaoded file and campaign ID on Salesforce end
*/
public function createDocumentLink(String $contentDocumentId, String $campaignId) {
$data = [
'ContentDocumentId' => $contentDocumentId,
'LinkedEntityId' => $campaignId,
'ShareType' => 'V', // Viewer
];
$id = $this->salesforceCrud->create('ContentDocumentLink', $data); #returns id
}
}