Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Protect direct access
- */
- if ( ! defined( 'ABSPATH' ) ) die( 'Sorry cowboy! This is not your place' );
- if( ! class_exists( 'NC_Model_SF' ) )
- {
- class NC_Model_SF
- {
- private $username;
- private $password;
- private $token;
- private $security_token;
- private $con;
- private $path;
- protected function __construct( $base )
- {
- $this->username = defined( 'SF_USERNAME' ) && SF_USERNAME ? SF_USERNAME : 'xxxxxx';
- $this->password = defined( 'SF_PASSWORD' ) && SF_PASSWORD ? SF_PASSWORD : 'xxxxxx';
- $this->token = defined( 'SF_TOKEN' ) && SF_TOKEN ? SF_TOKEN : 'xxxxxx';
- $this->security_token = $this->password . $this->token;
- $this->_init( $base );
- }
- static public function get_instance( $base = 'enterprise' )
- {
- static $Inst = null;
- if( $Inst == null )
- {
- $Inst = new self( $base );
- }
- return $Inst;
- }
- private function _print_exception( $error )
- {
- echo "Exception " . $error->faultstring . "<br/><br/>\n";
- echo "Last Request: <br/><br/>\n";
- echo $this->con->getLastRequestHeaders();
- echo "<br/><br/>\n";
- echo $this->con->getLastRequest();
- echo "<br/><br/>\n";
- echo "Last Response:<br/><br/>\n";
- echo $this->con->getLastResponseHeaders();
- echo "<br/><br/>\n";
- echo $this->con->getLastResponse();
- }
- private function _init( $base )
- {
- require_once ( NC_FILES_DIR . '/lib/salesforce/soapclient/SforcePartnerClient.php' );
- require_once ( NC_FILES_DIR . '/lib/salesforce/soapclient/SforceEnterpriseClient.php' );
- if( $base = 'partner' )
- {
- try
- {
- $this->con = new SforcePartnerClient();
- $this->path = $this->con->createConnection( NC_FILES_DIR . '/lib/salesforce/soapclient/partner.wsdl.xml' );
- $this->con->setEndpoint( 'https://cs43.salesforce.com/services/Soap/u/27.0' );
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- elseif( $base = 'enterprise' )
- {
- try
- {
- $this->con = new SforceEnterpriseClient();
- $this->path = $this->con->createConnection( NC_FILES_DIR . '/lib/salesforce/soapclient/enterprise.wsdl.xml' );
- $this->con->setEndpoint( 'https://cs43.salesforce.com/services/Soap/c/27.0' );
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- }
- public function login()
- {
- $con = $this->con->login( $this->username, $this->security_token );
- }
- public function create_account( $customer )
- {
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- //'Name' => $customer['first_name'] . ' ' . $customer['last_name'],
- 'Name' => $customer['company'],
- 'Email_Address__c' => $customer['email'],
- 'Phone' => $customer['phone'],
- 'BillingCountry' => $customer['country'],
- 'BillingPostalCode' => $customer['postal_code'],
- //'BillingState' => 'AL',
- 'BillingStreet' => $customer['address'],
- 'BillingCity' => $customer['city'],
- 'ShippingCountry' => $customer['country'],
- 'ShippingPostalCode' => $customer['postal_code'],
- //'BillingState' => 'AL',
- 'ShippingStreet' => $customer['address'],
- 'ShippingCity' => $customer['city']
- );
- $records[0]->type = 'Account';
- try
- {
- $account = $this->con->create( $records, 'Account' );
- return $account[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function create_contact( $account_id, $customer )
- {
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'FirstName' => $customer['first_name'],
- 'LastName' => $customer['last_name'],
- 'Email' => $customer['email'],
- 'Phone' => $customer['phone'],
- 'AccountId' => $account_id
- );
- $records[0]->type = 'Contact';
- try
- {
- $contact = $this->con->create( $records, 'Contact' );
- return $contact[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function create_opportunity( $account_id, $customer, $sf_custom_data )
- {
- $amount = ltrim( $sf_custom_data['data']['cart']['total'], '$' );
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'Name' => $customer['first_name'] . ' ' . $customer['last_name'],
- 'StageName' => 'closed won',
- 'CloseDate' => '2017-12-31',
- 'AccountId' => $account_id,
- 'Amount' => $amount,
- 'Stripe_Card_ID__c' => $sf_custom_data['customer']['value']->default_source,
- 'Stripe_Customer_ID__c' => $sf_custom_data['customer']['value']->id,
- //'Stripe_Payment_ID__c' => '',
- 'Stripe_Subscription_ID__c' => $sf_custom_data['customer']['value']->subscriptions->data[0]->id
- );
- $records[0]->type = 'Opportunity';
- try
- {
- $opportunity = $this->con->create( $records, 'Opportunity' );
- return $opportunity[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data )
- {
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'SBQQ__Opportunity2__c' => $opportunity_id,
- //'SBQQ__PrimaryContact__c' => $contact_id,
- //'SBQQ__SubscriptionTerm__c' => 'sales',
- //'SBQQ__StartDate__c' => '2017-12-31',
- 'SBQQ__Primary__c' => '1',
- );
- $records[0]->type = 'SBQQ__Quote__c';
- try
- {
- $quotes = $this->con->create( $records, 'SBQQ__Quote__c' );
- return $quotes[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function create_quoteLine( $opportunity_id, $account_id, $contact_id, $quote_id, $customer, $sf_custom_data )
- {
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'SBQQ__Quote__c' => $quote_id,
- 'SBQQ__Product__c' => '01ti0000007JWqG'
- );
- $records[0]->type = 'SBQQ__QuoteLine__c';
- try
- {
- $quoteLine = $this->con->create( $records, 'SBQQ__QuoteLine__c' );
- echo "<pre>";
- print_r($quoteLine);
- echo "</pre>";
- return $quoteLine[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function add_to_sf( $customer, $sf_custom_data )
- {
- $account_id = $this->create_account( $customer );
- $contact_id = $this->create_contact( $account_id, $customer );
- $opportunity_id = $this->create_opportunity( $account_id, $customer, $sf_custom_data );
- $quote_id = $this->create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data );
- $quoteLine_id = $this->create_quoteLine( $opportunity_id, $account_id, $contact_id, $quote_id, $customer, $sf_custom_data );
- return array( $account_id, $contact_id, $opportunity_id, $quote_id, $quoteLine_id );
- //return array( $account_id, $contact_id, $opportunity_id, $quote_id );
- //return array( $account_id, $contact_id, $opportunity_id );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement