View difference between Paste ID: ZD8Hcabh and sFPfg3Af
SHOW: | | - or go back to the newest paste.
1
<?php
2
3
/**
4
 * Protect direct access
5
 */
6
if ( ! defined( 'ABSPATH' ) ) die( 'Sorry cowboy! This is not your place' );
7
8
if( ! class_exists( 'NC_Model_SF' ) )
9
{
10
    class NC_Model_SF
11
    {
12
        private $username;
13
        
14
        private $password;
15
        
16
        private $token;
17
        
18
        private $security_token;
19
        
20
        private $con;
21
        
22
        private $path;
23
        
24
        protected function __construct( $base )
25
        {
26
            $this->username = defined( 'SF_USERNAME' ) && SF_USERNAME ? SF_USERNAME : '';
27
            $this->password = defined( 'SF_PASSWORD' ) && SF_PASSWORD ? SF_PASSWORD : '';
28
            $this->token = defined( 'SF_TOKEN' ) && SF_TOKEN ? SF_TOKEN : '';
29
            $this->security_token = $this->password . $this->token;
30
            $this->_init( $base );
31
        }
32
        
33
        static public function get_instance( $base = 'enterprise' )
34
        {
35
            static $Inst = null;
36
            if( $Inst == null )
37
            {
38
                $Inst = new self( $base );
39
            }
40
            
41
            return $Inst;
42
        }
43
        
44
        private function _print_exception( $error )
45
        {
46
            echo "Exception " . $error->faultstring . "<br/><br/>\n";
47
            echo "Last Request: <br/><br/>\n";
48
            echo $this->con->getLastRequestHeaders();
49
            echo "<br/><br/>\n";
50
            echo $this->con->getLastRequest();
51
            echo "<br/><br/>\n";
52
            echo "Last Response:<br/><br/>\n";
53
            echo $this->con->getLastResponseHeaders();
54
            echo "<br/><br/>\n";
55
            echo $this->con->getLastResponse();
56
        }
57
        
58
        private function _init( $base )
59
        {
60
            require_once ( NC_FILES_DIR . '/lib/salesforce/soapclient/SforcePartnerClient.php' );
61
            require_once ( NC_FILES_DIR . '/lib/salesforce/soapclient/SforceEnterpriseClient.php' );
62
            
63
            if( $base = 'partner' )
64
            {
65
                try
66
                {
67
                    $this->con = new SforcePartnerClient();
68
                    $this->path = $this->con->createConnection( NC_FILES_DIR . '/lib/salesforce/soapclient/partner.wsdl.xml' );
69
                    // This is for sandbox
70
                    //$this->con->setEndpoint( 'https://cs43.salesforce.com/services/Soap/u/27.0' );
71
                    
72
                    // This is live
73
                    $this->con->setEndpoint( 'https://na15.salesforce.com/services/Soap/u/27.0' );
74
                }
75
                catch( Exception $e )
76
                {
77
                    $this->_print_exception( $e );
78
                }
79
            }
80
            elseif( $base = 'enterprise' )
81
            {
82
                try
83
                {
84
                    $this->con = new SforceEnterpriseClient();
85
                    $this->path = $this->con->createConnection( NC_FILES_DIR . '/lib/salesforce/soapclient/enterprise.wsdl.xml' );
86
                    $this->con->setEndpoint( 'https://na15.salesforce.com/services/Soap/c/27.0' );
87
                }
88
                catch( Exception $e )
89
                {
90
                    $this->_print_exception( $e );
91
                }
92
            }
93
            
94
        }
95
        
96
        public function login()
97
        {
98
            $con = $this->con->login( $this->username, $this->security_token );
99
        }
100
        
101
        public function create_account( $customer )
102
        {
103
            $records = array();
104
            $records[0] = new stdclass();
105
            $records[0]->fields = array(
106
                                    //'Name' => $customer['first_name'] . ' ' . $customer['last_name'],
107
                                    'Name' => $customer['company'],
108
                                    'Email_Address__c' => $customer['email'],
109
                                    'Phone' => $customer['phone'],
110
                                    'BillingCountry' => $customer['country'],
111
                                    'BillingPostalCode' => $customer['postal_code'],
112
                                    //'BillingState' => 'AL',
113
                                    'BillingStreet' => $customer['address'],
114
                                    'BillingCity' => $customer['city'],
115
                                    
116
                                    'ShippingCountry' => $customer['country'],
117
                                    'ShippingPostalCode' => $customer['postal_code'],
118
                                    //'BillingState' => 'AL',
119
                                    'ShippingStreet' => $customer['address'],
120
                                    'ShippingCity' => $customer['city']
121
                                );
122
            $records[0]->type = 'Account';
123
            
124
            try
125
            {
126
                $account = $this->con->create( $records, 'Account' );echo "<pre>";
127
print_r($account);
128
echo "</pre>";
129
                return $account[0]->id;
130
            }
131
            catch( Exception $e )
132
            {
133
                $this->_print_exception( $e );
134
            }
135
        }
136
        
137
        public function create_contact( $account_id, $customer )
138
        {
139
            $records = array();
140
            $records[0] = new stdclass();
141
            
142
            $records[0]->fields = array(
143
                                    'FirstName' => $customer['first_name'],
144
                                    'LastName' => $customer['last_name'],
145
                                    'Email' => $customer['email'],
146
                                    'Phone' => $customer['phone'],
147
                                    'AccountId' => $account_id,
148
                                    'MailingStreet' => $customer['address'],
149
                                    'MailingCity' => $customer['city'],
150
                                    'MailingPostalCode' => $customer['postal_code'],
151
                                    'MailingStateCode' => $customer['state'],
152
                                    'MailingCountryCode' => $customer['country'],
153
                                );
154
            $records[0]->type = 'Contact';
155
            
156
            try
157
            {
158
                $contact = $this->con->create( $records, 'Contact' );echo "<pre>";
159
print_r($contact);
160
echo "</pre>";
161
                return $contact[0]->id;
162
            }
163
            catch( Exception $e )
164
            {
165
                $this->_print_exception( $e );
166
            }
167
        }
168
        
169
        public function create_opportunity( $account_id, $contact_id, $customer, $sf_custom_data )
170
        {
171
            $amount = ltrim( $sf_custom_data['data']['cart']['total'], '$' );
172
            $records = array();
173
            $records[0] = new stdclass();
174
            $records[0]->fields = array(
175
                                    'Name'  => 'Online Store - ' . $customer['company'] . ' - ' . date( 'Y-m-d' ),
176
                                    'StageName' => 'closed won',
177
                                    'CloseDate' => date( 'Y-m-d' ),
178
                                    'AccountId' => $account_id,
179
                                    'Amount' => $amount,
180
                                    'Stripe_Card_ID__c' => $sf_custom_data['customer']['value']->default_source,
181
                                    'Stripe_Customer_ID__c' => $sf_custom_data['customer']['value']->id,
182
                                    'Stripe_Payment_ID__c' => $sf_custom_data['customer']['payment_id'],
183
                                    'Stripe_Subscription_ID__c' => $sf_custom_data['customer']['value']->subscriptions->data[0]->id,
184-
                                    'Pricebook2Id' => '01si0000000Cxqn'
184+
                                    'Pricebook2Id' => 'xxxxxxxxxxxx'
185
                                );
186
            
187
            $records[0]->type = 'Opportunity';
188
            
189
            try
190
            {
191
                $opportunity = $this->con->create( $records, 'Opportunity' );echo "<pre>";
192
print_r($opportunity);
193
echo "</pre>";
194
                return $opportunity[0]->id;
195
            }
196
            catch( Exception $e )
197
            {
198
                $this->_print_exception( $e );
199
            }
200
        }
201
        
202
        public function OpportunityContactRole( $account_id, $contact_id, $opportunity_id, $customer, $sf_custom_data )
203
        {
204
            $records = array();
205
            $records[0] = new stdclass();
206
            $records[0]->fields = array(
207
                                    'ContactId' => $contact_id,
208
                                    'IsPrimary' => true,
209
                                    'OpportunityId' => $opportunity_id,
210
                                    'Role' => 'Technical Buyer'
211
                                );
212
            $records[0]->type = 'OpportunityContactRole';
213
            
214
            try
215
            {
216
                $OpportunityContactRole = $this->con->create( $records, 'OpportunityContactRole' );echo "<pre>";
217
print_r($OpportunityContactRole);
218
echo "</pre>";
219
                return $OpportunityContactRole[0]->id;
220
            }
221
            catch( Exception $e )
222
            {
223
                $this->_print_exception( $e );
224
            }
225
        }
226
        
227
        public function create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data )
228
        {   
229
            $records = array();
230
            $records[0] = new stdclass();
231
            $records[0]->fields = array(
232
                                    'SBQQ__Opportunity2__c' => $opportunity_id,
233
                                    'SBQQ__Primary__c' => '1',
234
                                    'SBQQ__BillingCountry__c' => $customer['country'],
235
                                    'ship_to_contact__c' => $contact_id,
236
                                    'bill_to_contact__c' => $contact_id,
237
                                    'SBQQ__SubscriptionTerm__c' => 12,
238
                                    'SBQQ__StartDate__c' => date( 'Y-m-d' ),
239
                                    'SBQQ__ExpirationDate__c' => date( 'Y-m-d', strtotime( '+1 years' ) ),
240
                                    //'discount__c' => '4',
241
                                    //'discount_reason__c' => 'Coupon'
242
                                );
243
            
244
            $discount = $sf_custom_data['data']['customer']['post_data']['nc_copuon_amount'];
245
            if( isset( $discount ) && $discount != '' )
246
            {
247
                $records[0]->fields['discount_reason__c'] = 'NGINX Product Coupon';
248
                // Need permission
249
                //$records[0]->fields['discount__c'] = number_format( $discount, 2, '.', '' );
250
            }
251
            
252
            $records[0]->type = 'SBQQ__Quote__c';
253
            
254
            try
255
            {
256
                $quotes = $this->con->create( $records, 'SBQQ__Quote__c' );echo "<pre>";
257
print_r($quotes);
258
echo "</pre>";
259
                return $quotes[0]->id;
260
            }
261
            catch( Exception $e )
262
            {
263
                $this->_print_exception( $e );
264
            }
265
        }
266
        
267
        public function create_quoteLine( $opportunity_id, $account_id, $contact_id, $quote_id, $customer, $sf_custom_data )
268
        {
269
            $cart = $sf_custom_data['data']['cart'];
270
            $product_id = $cart['items'][0]['product_id'];
271
            $qty = $cart['items'][0]['qty'];
272
            $product = NC_Model_Product::get_instance( $product_id );
273
            
274
            $query = "SELECT Name from Product2 WHERE Id = '" . $product->sf_product . "'";
275
            $queryResult = $this->con->query($query);
276
            for ($queryResult->rewind(); $queryResult->pointer < $queryResult->size; $queryResult->next()) {
277
                $record = $queryResult->current();
278
                $productName = $record->fields->Name;
279
            }
280
            
281
            $records = array();
282
            $records[0] = new stdclass();
283
            $records[0]->fields = array(
284
                                    'SBQQ__Quote__c' => $quote_id,
285
                                    'SBQQ__Product__c' => $product->sf_product,
286
                                    'SBQQ__Quantity__c' => $qty,
287
                                    'SBQQ__Description__c' => $productName,
288
                                    'SBQQ__SubscriptionTerm__c' => 12, // Opportunity Product term
289
                                    'SBQQ__StartDate__c' => date( 'Y-m-d' ),
290
                                    'SBQQ__EndDate__c' => date( 'Y-m-d', strtotime( '+1 years' ) )
291
                                    //'SBQQ__Discount_Reason__c' => 'Coupon'
292
                                );
293
            
294
            $discount = $sf_custom_data['data']['customer']['post_data']['nc_copuon_amount'];
295
            if( isset( $discount ) && $discount != '' )
296
            {
297
                $records[0]->fields['SBQQ__Discount__c'] = $discount;
298
            }
299
        
300
            $records[0]->type = 'SBQQ__QuoteLine__c';
301
            
302
            try
303
            {
304
                $quoteLine = $this->con->create( $records, 'SBQQ__QuoteLine__c' );echo "<pre>";
305
print_r($quoteLine);
306
echo "</pre>";
307
                return $quoteLine[0]->id;
308
            }
309
            catch( Exception $e )
310
            {
311
                $this->_print_exception( $e );
312
            }
313
        }
314
        
315
        /*public function create_lineItems( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data )
316
        {
317
            $cart = $sf_custom_data['data']['cart'];
318
            $product_id = $cart['items'][0]['product_id'];
319
            $qty = $cart['items'][0]['qty'];
320
            $product = NC_Model_Product::get_instance( $product_id );
321
            
322
            $records = array();
323
            $records[0] = new stdclass();
324
            $records[0]->fields = array(
325
                                    'OpportunityId' => $opportunity_id,
326
                                    'PricebookEntryId' => '01s630000004YC3',
327
                                    'Product2Id' => $product->sf_product
328
                                );
329
            
330
            $records[0]->type = 'OpportunityLineItem';
331
            
332
            try
333
            {
334
                $OpportunityLineItem = $this->con->create( $records, 'OpportunityLineItem' );
335
                return $OpportunityLineItem[0]->id;
336
            }
337
            catch( Exception $e )
338
            {
339
                $this->_print_exception( $e );
340
            }
341
        }*/
342
        
343
        public function add_to_sf( $customer, $sf_custom_data )
344
        {
345
            $account_id = $this->create_account( $customer );
346
            $contact_id = $this->create_contact( $account_id, $customer );
347
            $opportunity_id = $this->create_opportunity( $account_id, $contact_id, $customer, $sf_custom_data );
348
            $OpportunityContactRole = $this->OpportunityContactRole( $account_id, $contact_id, $opportunity_id, $customer, $sf_custom_data );
349
            $quote_id = $this->create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data );
350
            $quoteLine_id = $this->create_quoteLine( $opportunity_id, $account_id, $contact_id, $quote_id, $customer, $sf_custom_data );
351
            
352
            //$lineItem_id = $this->create_lineItems( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data );
353
            //return array( $account_id, $contact_id, $opportunity_id, $quote_id );
354
            //return array( $account_id, $contact_id, $opportunity_id );
355
            
356
            return array( $account_id, $contact_id, $opportunity_id, $OpportunityContactRole, $quote_id, $quoteLine_id );
357
        }
358
    }
359
}