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 : '';
- $this->password = defined( 'SF_PASSWORD' ) && SF_PASSWORD ? SF_PASSWORD : '';
- $this->token = defined( 'SF_TOKEN' ) && SF_TOKEN ? SF_TOKEN : '';
- $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 is for sandbox
- //$this->con->setEndpoint( 'https://cs43.salesforce.com/services/Soap/u/27.0' );
- // This is live
- $this->con->setEndpoint( 'https://na15.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://na15.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' );echo "<pre>";
- print_r($account);
- echo "</pre>";
- 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,
- 'MailingStreet' => $customer['address'],
- 'MailingCity' => $customer['city'],
- 'MailingPostalCode' => $customer['postal_code'],
- 'MailingStateCode' => $customer['state'],
- 'MailingCountryCode' => $customer['country'],
- );
- $records[0]->type = 'Contact';
- try
- {
- $contact = $this->con->create( $records, 'Contact' );echo "<pre>";
- print_r($contact);
- echo "</pre>";
- return $contact[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function create_opportunity( $account_id, $contact_id, $customer, $sf_custom_data )
- {
- $amount = ltrim( $sf_custom_data['data']['cart']['total'], '$' );
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'Name' => 'Online Store - ' . $customer['company'] . ' - ' . date( 'Y-m-d' ),
- 'StageName' => 'closed won',
- 'CloseDate' => date( 'Y-m-d' ),
- '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' => $sf_custom_data['customer']['payment_id'],
- 'Stripe_Subscription_ID__c' => $sf_custom_data['customer']['value']->subscriptions->data[0]->id,
- 'Pricebook2Id' => 'xxxxxxxxxxxx'
- );
- $records[0]->type = 'Opportunity';
- try
- {
- $opportunity = $this->con->create( $records, 'Opportunity' );echo "<pre>";
- print_r($opportunity);
- echo "</pre>";
- return $opportunity[0]->id;
- }
- catch( Exception $e )
- {
- $this->_print_exception( $e );
- }
- }
- public function OpportunityContactRole( $account_id, $contact_id, $opportunity_id, $customer, $sf_custom_data )
- {
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'ContactId' => $contact_id,
- 'IsPrimary' => true,
- 'OpportunityId' => $opportunity_id,
- 'Role' => 'Technical Buyer'
- );
- $records[0]->type = 'OpportunityContactRole';
- try
- {
- $OpportunityContactRole = $this->con->create( $records, 'OpportunityContactRole' );echo "<pre>";
- print_r($OpportunityContactRole);
- echo "</pre>";
- return $OpportunityContactRole[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__Primary__c' => '1',
- 'SBQQ__BillingCountry__c' => $customer['country'],
- 'ship_to_contact__c' => $contact_id,
- 'bill_to_contact__c' => $contact_id,
- 'SBQQ__SubscriptionTerm__c' => 12,
- 'SBQQ__StartDate__c' => date( 'Y-m-d' ),
- 'SBQQ__ExpirationDate__c' => date( 'Y-m-d', strtotime( '+1 years' ) ),
- //'discount__c' => '4',
- //'discount_reason__c' => 'Coupon'
- );
- $discount = $sf_custom_data['data']['customer']['post_data']['nc_copuon_amount'];
- if( isset( $discount ) && $discount != '' )
- {
- $records[0]->fields['discount_reason__c'] = 'NGINX Product Coupon';
- // Need permission
- //$records[0]->fields['discount__c'] = number_format( $discount, 2, '.', '' );
- }
- $records[0]->type = 'SBQQ__Quote__c';
- try
- {
- $quotes = $this->con->create( $records, 'SBQQ__Quote__c' );echo "<pre>";
- print_r($quotes);
- echo "</pre>";
- 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 )
- {
- $cart = $sf_custom_data['data']['cart'];
- $product_id = $cart['items'][0]['product_id'];
- $qty = $cart['items'][0]['qty'];
- $product = NC_Model_Product::get_instance( $product_id );
- $query = "SELECT Name from Product2 WHERE Id = '" . $product->sf_product . "'";
- $queryResult = $this->con->query($query);
- for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
- $record = $queryResult->current();
- $productName = $record->fields->Name;
- }
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'SBQQ__Quote__c' => $quote_id,
- 'SBQQ__Product__c' => $product->sf_product,
- 'SBQQ__Quantity__c' => $qty,
- 'SBQQ__Description__c' => $productName,
- 'SBQQ__SubscriptionTerm__c' => 12, // Opportunity Product term
- 'SBQQ__StartDate__c' => date( 'Y-m-d' ),
- 'SBQQ__EndDate__c' => date( 'Y-m-d', strtotime( '+1 years' ) )
- //'SBQQ__Discount_Reason__c' => 'Coupon'
- );
- $discount = $sf_custom_data['data']['customer']['post_data']['nc_copuon_amount'];
- if( isset( $discount ) && $discount != '' )
- {
- $records[0]->fields['SBQQ__Discount__c'] = $discount;
- }
- $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 create_lineItems( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data )
- {
- $cart = $sf_custom_data['data']['cart'];
- $product_id = $cart['items'][0]['product_id'];
- $qty = $cart['items'][0]['qty'];
- $product = NC_Model_Product::get_instance( $product_id );
- $records = array();
- $records[0] = new stdclass();
- $records[0]->fields = array(
- 'OpportunityId' => $opportunity_id,
- 'PricebookEntryId' => '01s630000004YC3',
- 'Product2Id' => $product->sf_product
- );
- $records[0]->type = 'OpportunityLineItem';
- try
- {
- $OpportunityLineItem = $this->con->create( $records, 'OpportunityLineItem' );
- return $OpportunityLineItem[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, $contact_id, $customer, $sf_custom_data );
- $OpportunityContactRole = $this->OpportunityContactRole( $account_id, $contact_id, $opportunity_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 );
- //$lineItem_id = $this->create_lineItems( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data );
- //return array( $account_id, $contact_id, $opportunity_id, $quote_id );
- //return array( $account_id, $contact_id, $opportunity_id );
- return array( $account_id, $contact_id, $opportunity_id, $OpportunityContactRole, $quote_id, $quoteLine_id );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement