Advertisement
Guest User

Untitled

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