Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 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. protected function __construct( $base )
  25. {
  26. $this->username = defined( 'SF_USERNAME' ) && SF_USERNAME ? SF_USERNAME : 'xxxxxx';
  27. $this->password = defined( 'SF_PASSWORD' ) && SF_PASSWORD ? SF_PASSWORD : 'xxxxxx';
  28. $this->token = defined( 'SF_TOKEN' ) && SF_TOKEN ? SF_TOKEN : 'xxxxxx';
  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->con->setEndpoint( 'https://cs43.salesforce.com/services/Soap/u/27.0' );
  70. }
  71. catch( Exception $e )
  72. {
  73. $this->_print_exception( $e );
  74. }
  75. }
  76. elseif( $base = 'enterprise' )
  77. {
  78. try
  79. {
  80. $this->con = new SforceEnterpriseClient();
  81. $this->path = $this->con->createConnection( NC_FILES_DIR . '/lib/salesforce/soapclient/enterprise.wsdl.xml' );
  82. $this->con->setEndpoint( 'https://cs43.salesforce.com/services/Soap/c/27.0' );
  83. }
  84. catch( Exception $e )
  85. {
  86. $this->_print_exception( $e );
  87. }
  88. }
  89.  
  90. }
  91.  
  92. public function login()
  93. {
  94. $con = $this->con->login( $this->username, $this->security_token );
  95. }
  96.  
  97. public function create_account( $customer )
  98. {
  99. $records = array();
  100. $records[0] = new stdclass();
  101. $records[0]->fields = array(
  102. //'Name' => $customer['first_name'] . ' ' . $customer['last_name'],
  103. 'Name' => $customer['company'],
  104. 'Email_Address__c' => $customer['email'],
  105. 'Phone' => $customer['phone'],
  106. 'BillingCountry' => $customer['country'],
  107. 'BillingPostalCode' => $customer['postal_code'],
  108. //'BillingState' => 'AL',
  109. 'BillingStreet' => $customer['address'],
  110. 'BillingCity' => $customer['city'],
  111.  
  112. 'ShippingCountry' => $customer['country'],
  113. 'ShippingPostalCode' => $customer['postal_code'],
  114. //'BillingState' => 'AL',
  115. 'ShippingStreet' => $customer['address'],
  116. 'ShippingCity' => $customer['city']
  117. );
  118. $records[0]->type = 'Account';
  119.  
  120. try
  121. {
  122. $account = $this->con->create( $records, 'Account' );
  123. return $account[0]->id;
  124. }
  125. catch( Exception $e )
  126. {
  127. $this->_print_exception( $e );
  128. }
  129. }
  130.  
  131. public function create_contact( $account_id, $customer )
  132. {
  133. $records = array();
  134. $records[0] = new stdclass();
  135. $records[0]->fields = array(
  136. 'FirstName' => $customer['first_name'],
  137. 'LastName' => $customer['last_name'],
  138. 'Email' => $customer['email'],
  139. 'Phone' => $customer['phone'],
  140. 'AccountId' => $account_id
  141. );
  142. $records[0]->type = 'Contact';
  143.  
  144. try
  145. {
  146. $contact = $this->con->create( $records, 'Contact' );
  147. return $contact[0]->id;
  148. }
  149. catch( Exception $e )
  150. {
  151. $this->_print_exception( $e );
  152. }
  153. }
  154.  
  155. public function create_opportunity( $account_id, $customer, $sf_custom_data )
  156. {
  157. $amount = ltrim( $sf_custom_data['data']['cart']['total'], '$' );
  158. $records = array();
  159. $records[0] = new stdclass();
  160. $records[0]->fields = array(
  161. 'Name' => $customer['first_name'] . ' ' . $customer['last_name'],
  162. 'StageName' => 'closed won',
  163. 'CloseDate' => '2017-12-31',
  164. 'AccountId' => $account_id,
  165. 'Amount' => $amount,
  166. 'Stripe_Card_ID__c' => $sf_custom_data['customer']['value']->default_source,
  167. 'Stripe_Customer_ID__c' => $sf_custom_data['customer']['value']->id,
  168. //'Stripe_Payment_ID__c' => '',
  169. 'Stripe_Subscription_ID__c' => $sf_custom_data['customer']['value']->subscriptions->data[0]->id
  170. );
  171. $records[0]->type = 'Opportunity';
  172.  
  173. try
  174. {
  175. $opportunity = $this->con->create( $records, 'Opportunity' );
  176. return $opportunity[0]->id;
  177. }
  178. catch( Exception $e )
  179. {
  180. $this->_print_exception( $e );
  181. }
  182. }
  183.  
  184. public function create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data )
  185. {
  186. $records = array();
  187. $records[0] = new stdclass();
  188. $records[0]->fields = array(
  189. 'SBQQ__Opportunity2__c' => $opportunity_id,
  190. //'SBQQ__PrimaryContact__c' => $contact_id,
  191. //'SBQQ__SubscriptionTerm__c' => 'sales',
  192. //'SBQQ__StartDate__c' => '2017-12-31',
  193. 'SBQQ__Primary__c' => '1',
  194. );
  195.  
  196. $records[0]->type = 'SBQQ__Quote__c';
  197.  
  198. try
  199. {
  200. $quotes = $this->con->create( $records, 'SBQQ__Quote__c' );
  201. return $quotes[0]->id;
  202. }
  203. catch( Exception $e )
  204. {
  205. $this->_print_exception( $e );
  206. }
  207. }
  208.  
  209. public function create_quoteLine( $opportunity_id, $account_id, $contact_id, $quote_id, $customer, $sf_custom_data )
  210. {
  211. $records = array();
  212. $records[0] = new stdclass();
  213. $records[0]->fields = array(
  214. 'SBQQ__Quote__c' => $quote_id,
  215. 'SBQQ__Product__c' => '01ti0000007JWqG'
  216. );
  217.  
  218. $records[0]->type = 'SBQQ__QuoteLine__c';
  219.  
  220. try
  221. {
  222. $quoteLine = $this->con->create( $records, 'SBQQ__QuoteLine__c' );
  223. echo "<pre>";
  224. print_r($quoteLine);
  225. echo "</pre>";
  226. return $quoteLine[0]->id;
  227. }
  228. catch( Exception $e )
  229. {
  230. $this->_print_exception( $e );
  231. }
  232. }
  233.  
  234. public function add_to_sf( $customer, $sf_custom_data )
  235. {
  236. $account_id = $this->create_account( $customer );
  237. $contact_id = $this->create_contact( $account_id, $customer );
  238. $opportunity_id = $this->create_opportunity( $account_id, $customer, $sf_custom_data );
  239. $quote_id = $this->create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data );
  240. $quoteLine_id = $this->create_quoteLine( $opportunity_id, $account_id, $contact_id, $quote_id, $customer, $sf_custom_data );
  241.  
  242. return array( $account_id, $contact_id, $opportunity_id, $quote_id, $quoteLine_id );
  243. //return array( $account_id, $contact_id, $opportunity_id, $quote_id );
  244. //return array( $account_id, $contact_id, $opportunity_id );
  245. }
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement