Advertisement
Guest User

Untitled

a guest
Jul 11th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 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 : 'bappi@arcom.co';
  27. $this->password = defined( 'SF_PASSWORD' ) && SF_PASSWORD ? SF_PASSWORD : 'ap2.salesforce.com1';
  28. $this->token = defined( 'SF_TOKEN' ) && SF_TOKEN ? SF_TOKEN : '1F6nJjxdy8cEQ6t5FFtC9wpy6';
  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__Account__c' => $account_id,
  190. 'OpportunityId' => $opportunity_id,
  191. //'SBQQ__PrimaryContact__c' => $contact_id,
  192. //'SBQQ__SubscriptionTerm__c' => '',
  193. //'SBQQ__StartDate__c' => '2017-12-31',
  194. //'SBQQ__Primary__c' => '2017-12-31',
  195. );
  196.  
  197. $records[0]->type = 'Quote';
  198.  
  199. try
  200. {
  201. $quotes = $this->con->create( $records, 'Quotes' );
  202. return $quotes[0]->id;
  203. }
  204. catch( Exception $e )
  205. {
  206. $this->_print_exception( $e );
  207. }
  208. }
  209.  
  210. public function add_to_sf( $customer, $sf_custom_data )
  211. {
  212. $account_id = $this->create_account( $customer );
  213. $contact_id = $this->create_contact( $account_id, $customer );
  214. $opportunity_id = $this->create_opportunity( $account_id, $customer, $sf_custom_data );
  215. //$quote_id = $this->create_quotes( $opportunity_id, $account_id, $contact_id, $customer, $sf_custom_data );
  216.  
  217. //return array( $account_id, $contact_id, $opportunity_id, $quote_id );
  218. return array( $account_id, $contact_id, $opportunity_id );
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement