Created
September 10, 2016 19:45
-
-
Save siscodev93/9acbc73426cd7b7ad6f2baa4b9cc4556 to your computer and use it in GitHub Desktop.
Easily Create or Get Customer, and create Order/Subscriptions in one go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * wooManager to manage subscription based products | |
| * Written By Derek Sisco | |
| */ | |
| class wooManager | |
| { | |
| protected $api_url, $consumer_key, $consumer_secret; | |
| protected $current_customer; | |
| protected $current_customer_id; | |
| protected $current_order; | |
| protected $current_order_id; | |
| protected $current_order_raw; | |
| public function __construct($api_url, $consumer_key, $consumer_secret) | |
| { | |
| $this->api_url = $api_url; | |
| $this->consumer_key = $consumer_key; | |
| $this->consumer_secret = $consumer_secret; | |
| } | |
| protected function doCurlPost($subject ,$data) | |
| { | |
| $curl = curl_init(); | |
| curl_setopt ($curl, CURLOPT_URL,$this->api_url. $subject.'s/?consumer_key='.$this->consumer_key.'&consumer_secret='.$this->consumer_secret); | |
| curl_setopt ($curl, CURLOPT_POST, false); | |
| curl_setopt ($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
| curl_setopt ($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json')); | |
| curl_setopt ($curl, CURLOPT_TIMEOUT, 30); | |
| curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0); | |
| curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt ($curl, CURLOPT_POSTFIELDS, json_encode( array($subject => $data)) ); | |
| $order_result = curl_exec($curl); | |
| $order_curl_info = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
| $order_error = curl_error ( $curl ); | |
| curl_close($curl); | |
| return json_decode($order_result); | |
| } | |
| public function captureCustomer($customerDetails) | |
| { | |
| $password_words = array('apple', 'king', 'catnip', 'south', 'soccer'); | |
| $password_symbols = array('!', '#', '@', '?', '*'); | |
| $random_num = rand(100,999); | |
| $password = $password_words[rand(0,count($password_words)-1)].$password_symbols[rand(0,count($password_symbols)-1)].$random_num; | |
| echo $password; | |
| $customerPost = array( | |
| 'email' => $customerDetails['billing_address']['email'], | |
| 'first_name' => $customerDetails['billing_address']['first_name'], | |
| 'last_name' => $customerDetails['billing_address']['last_name'], | |
| 'username' => $customerDetails['billing_address']['last_name'].'.'.$customerDetails['billing_address']['first_name'], | |
| 'password' => $password, | |
| 'billing_address' => $customerDetails['billing_address'], | |
| 'shipping_address' => (!empty($customerDetails['shipping_address'])) ? $customerDetails['shipping_address'] : null | |
| ); | |
| $customerDetails = $this->doCurlPost('customer', $customerPost); | |
| //Error Creating the Customer, Lets try to grab the customer id | |
| if(!empty($customerDetails->errors)) | |
| { | |
| if($customerDetails->errors[0]->code === 'registration-error-email-exists') | |
| { | |
| $customerDetails = file_get_contents($this->api_url.'customers/email/'.$customerPost['email'].'?consumer_key='.$this->consumer_key.'&consumer_secret='.$this->consumer_secret); | |
| } | |
| $customerDetails = json_decode($customerDetails); | |
| } | |
| $this->current_customer = $customerDetails->customer; | |
| $this->current_customer_id = $this->current_customer->id; | |
| var_dump($this->current_customer); | |
| return $this->current_customer_id; | |
| } | |
| public function createOrder($orderPost) | |
| { | |
| $orderPost['customer_id'] = $this->current_customer_id; | |
| $orderDetails = $this->doCurlPost('order', $orderPost); | |
| $this->current_order_raw = $orderPost; | |
| $this->current_order = $orderDetails->order; | |
| $this->current_order_id = $this->current_order->id; | |
| return $this->current_order_id; | |
| } | |
| /* | |
| * TODO: | |
| * Implement feature to create subscription from orders already in database | |
| */ | |
| public function create_subscription($order_id = null){ | |
| $order_id = (!is_null($order_id)) ? $order_id : $this->current_order_id; | |
| $sub_details = $this->current_order_raw; | |
| //Sets the parent ID to be tracked in woocommerce | |
| $sub_details['order_id'] = $order_id; | |
| $sub_details['billing_interval'] = '1'; | |
| $sub_details['billing_period'] = 'day'; | |
| $subscriptionDetails = $this->doCurlPost('subscription', $sub_details); | |
| return $subscriptionDetails; | |
| } | |
| public function getProducts() | |
| { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once 'class.wooManager.php'; | |
| $billing_address = array( | |
| 'first_name' => 'WorkingCase4', | |
| 'last_name' => 'WorkingCase4', | |
| 'company' => '', | |
| 'email' => 'WorkingCase4@noemail.com', | |
| 'phone' => '2076120000', | |
| 'address_1' => 'TestCaseWorking Address #3', | |
| 'address_2' => '', | |
| 'city' => 'Canaan', | |
| 'state' => 'ME', | |
| 'postcode' => '04924', | |
| 'country' => 'US', | |
| ); | |
| $shipping_address = array( | |
| 'first_name' => 'WorkingCase4', | |
| 'last_name' => 'WorkingCase4', | |
| 'company' => '', | |
| 'email' => 'WorkingCase4@noemail.com', | |
| 'phone' => '2076120000', | |
| 'address_1' => 'TestCaseWorking Address #3', | |
| 'address_2' => '', | |
| 'city' => 'Canaan', | |
| 'state' => 'ME', | |
| 'postcode' => '04924', | |
| 'country' => 'US', | |
| ); | |
| /* | |
| * Patch added to allow the api to function without oAuth via http | |
| * Patch Details: | |
| ** Filepath: wp-content\plugins\woocommerce\includes\api\legacy\v2\class-wc-api-authentication.php | |
| ** Line 46: replace if ( is_ssl() ) with if ( is_ssl() || $_SERVER['HTTP_HOST'] === 'localhost' ) | |
| * This makes testing a little easier while not opening a vulnerability with the auth method | |
| */ | |
| $wooManager = new wooManager('http://localhost/wptest/wc-api/v2/', 'ck_7069d0baa34343a9f29abd2b940d465be5a1e5ee', 'cs_51dcdd4c3a52c2e82e093109296e78b8c71f885a'); | |
| $customerId = $wooManager->captureCustomer(array('billing_address' => $billing_address)); | |
| $payment_details = array( | |
| 'method_id' => 'stripe', | |
| 'method_title' => 'Stripe (Credit Card)', | |
| 'paid' => true, | |
| 'post_meta' => array( | |
| '_stripe_customer_id' => 'cus_T35TCU57M3R', | |
| '_stripe_card_id' => 'card_T35TC4RD' | |
| ) | |
| ); | |
| $order_details = array( | |
| 'billing_address' => $billing_address, | |
| 'shipping_address' => $shipping_address, | |
| 'payment_details' => array( | |
| 'method_id' => 'stripe', | |
| 'method_title' => 'Stripe (Credit Card)', | |
| 'paid' => true, | |
| 'post_meta' => array( | |
| '_stripe_customer_id' => 'cus_T35TCU57M3R', | |
| '_stripe_card_id' => 'card_T35TC4RD' | |
| ) | |
| ), | |
| 'line_items' => array(array('product_id' => 19,'quantity' => 1)) | |
| ); | |
| $orderId = $wooManager->createOrder($order_details); | |
| /* | |
| * If the product is a subscription run this last line, the order | |
| * just created will recur | |
| */ | |
| $sub_details = $wooManager->create_subscription(); | |
| /* | |
| * Dump the new subscription details to help debug | |
| * and verify that the system worked correctly | |
| */ | |
| var_dump($sub_details); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment