Created
May 1, 2013 18:24
-
-
Save lsolesen/5497174 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* @file | |
* Provides BillysBilling functionality. | |
*/ | |
/** | |
* Implements hook_menu(). | |
*/ | |
function billysbilling_menu() { | |
$items = array(); | |
$items['admin/config/services/billysbilling'] = array( | |
'title' => 'BillysBilling', | |
'description' => 'Manage BillysBilling Settings.', | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('billysbilling_admin_settings'), | |
'access arguments' => array('administer billysbilling'), | |
'file' => 'billysbilling.admin.inc', | |
'type' => MENU_NORMAL_ITEM, | |
); | |
return $items; | |
} | |
/** | |
* Implements hook_init(). | |
* | |
* Nags the user about the missing library on BillysBilling admin pages. | |
*/ | |
function billysbilling_server_init() { | |
$item = menu_get_item(); | |
if ($item['access'] && strpos($item['path'], 'admin/structure/billysbilling') === 0) { | |
$path = billysbilling_get_library_path(); | |
// Check for the existence of one file from the library. | |
if (!$path || !file_exists($path . '/bootstrap.php')) { | |
$message = t('The php-sdk for BillysBilling is required for the BillysBilling module to function. | |
Download the library from !url and place it in <em>!path</em>.', array('!path' => $path, '!url' => l('Github', 'https://github.com/billysbilling/billysbilling-php'))); | |
drupal_set_message($message, 'error'); | |
} | |
} | |
} | |
/** | |
* Implements hook_libraries_info(). | |
*/ | |
function billysbilling_libraries_info() { | |
$libraries = array(); | |
$libraries['billysbilling-php'] = array( | |
'name' => 'BillysBilling', | |
'vendor url' => 'https://github.com/billysbilling/billysbilling-php', | |
); | |
return $libraries; | |
} | |
/** | |
* Returns the filesystem path to the oauth2-server-php library. | |
*/ | |
function billysbilling_get_library_path() { | |
$path = 'sites/all/libraries/billysbilling-php'; | |
// If installed, use the Libraries API to locate the library. | |
if (module_exists('libraries')) { | |
module_load_include('module', 'libraries'); | |
$path = libraries_get_path('billysbilling-php'); | |
} | |
return $path; | |
} | |
/** | |
* Implements hook_permission(). | |
*/ | |
function billysbilling_permission() { | |
return array( | |
'create invoice with billysbilling' => array( | |
'title' => t('use billysbilling'), | |
'description' => t('Make it possible to create invoices using BillysBilling'), | |
), | |
'administer billysbilling' => array( | |
'title' => t('administer billysbilling'), | |
'description' => t('Administer API keys for BillysBilling'), | |
), | |
); | |
} | |
class Billy_FakeResponse { | |
public $id = 'fake-response'; | |
} | |
class Billy_FakeClient { | |
function get() { | |
return new Billy_FakeResponse; | |
} | |
function post() { | |
return new Billy_FakeResponse; | |
} | |
} | |
/** | |
* Get a BillysBilling API object for communication with the the Billys Billing server. | |
*/ | |
function billysbilling_get_api_object() { | |
require_once billysbilling_get_library_path() . DIRECTORY_SEPARATOR . 'bootstrap.php'; | |
return new Billy_FakeClient; | |
$q = new Billy_Client(variable_get('billysbilling_api_key', '')); | |
return $q; | |
} | |
/** | |
* Implements hook_commerce_checkout_complete(). | |
*/ | |
function billysbilling_commerce_checkout_complete($order) { | |
// TODO Which hook to use | |
} | |
/** | |
* Implements hook_commerce_order_presave(). | |
*/ | |
function billysbilling_commerce_order_presave($order) { | |
// TODO Which hook to use | |
billysbilling_create_invoice($order); | |
} | |
/** | |
* Implements hook_commerce_payment_order_paid_in_full(). | |
*/ | |
function billysbilling_commerce_payment_order_paid_in_full($transaction) { | |
// Is this the correct one to use for the payments - or should another hook be used. | |
} | |
/** | |
* Syncs a contact with BillysBilling | |
* | |
* @param integer $contact_id Drupal Commerce Contact Profile ID | |
* | |
* @return string Billys Billing Contact ID | |
*/ | |
function billysbilling_sync_contact($contact_id) { | |
$contact = commerce_customer_profile_load($contact_id); | |
$contact_wrapper = entity_metadata_wrapper('commerce_customer_profile', $contact); | |
/* | |
$contact->commerce_customer_address[LANGUAGE_NONE][0]['name_line']; | |
$contact->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare']; | |
$contact->commerce_customer_address[LANGUAGE_NONE][0]['postal_code']; | |
$contact->commerce_customer_address[LANGUAGE_NONE][0]['locality']; | |
$contact->commerce_customer_address[LANGUAGE_NONE][0]['country']; | |
$contact->field_customer_phone[LANGUAGE_NONE][0]['safe_value']; | |
*/ | |
$name = $contact->commerce_customer_address[LANGUAGE_NONE][0]['name_line']; | |
// TODO This should not be necessary. | |
if (empty($name)) { | |
return FALSE; | |
} | |
$data = array( | |
'name' => $name, | |
'countryId' => $contact->commerce_customer_address[LANGUAGE_NONE][0]['country'], | |
'phone' => $contact->field_customer_phone[LANGUAGE_NONE][0]['safe_value'], | |
'externalId' => $contact_id, | |
); | |
// Create or update the contact. | |
if (!empty($contact->billysbilling_contact_id[LANGUAGE_NONE][0]['safe_value'])) { | |
// Update contact. | |
// TODO Test whether Commerce or BillysBilling contact has been updated recently. | |
} | |
else { | |
// Create contact. | |
$client = billysbilling_get_api_object(); | |
$response = $client->post("contacts", $data); | |
$billysbilling_contact_id = $response->id; | |
} | |
//print_r($contact_wrapper->billysbilling_contact_id->get()); | |
// Save id to the entity. | |
$contact_wrapper->billysbilling_contact_id->set($billysbilling_contact_id); | |
//print_r($contact_wrapper->billysbilling_contact_id->get()); | |
$contact_wrapper->save(); | |
return $billysbilling_contact_id; | |
} | |
/** | |
* Syncs a product with BillysBilling | |
* | |
* @param integer $product_id Drupal Commerce Product Id | |
* | |
* @return string Billys Billing Product ID | |
*/ | |
function billysbilling_sync_product($product_id) { | |
$product = commerce_product_load($product_id); | |
$product_wrapper = entity_metadata_wrapper('commerce_product', $product); | |
/* | |
$product->title; | |
$product->commerce_price[LANGUAGE_NONE][0]['amount']; | |
$product->commerce_price[LANGUAGE_NONE][0]['currency_code']; | |
*/ | |
if (empty($product->commerce_price[LANGUAGE_NONE][0]['amount'])) { | |
return FALSE; | |
} | |
$prices[] = array( | |
'currencyId' => $product->commerce_price[LANGUAGE_NONE][0]['currency_code'], | |
// Return unitPrice in float. | |
'unitPrice' => $product->commerce_price[LANGUAGE_NONE][0]['amount'] / 100, | |
); | |
// TODO Give each product type the chance to have different Account and VAT IDs. | |
// TODO These are hardcoded at the moment. | |
$vatModelId = '5898547-0hyT082Inncj'; | |
$accountId = '5898564-3QpCGgJyu47C'; | |
$name = $product->title; | |
$data = array( | |
'name' => $name, | |
'accountId' => $accountId, | |
'vatModelId' => $vatModelId, | |
'externalId' => $product_id, | |
'prices' => $prices, | |
); | |
/* | |
$client = billysbilling_get_api_object(); | |
//print_r($client->get("vatModels")); | |
print_r($client->get("accounts")); | |
exit; | |
*/ | |
if (!empty($contact->billysbilling_product_id[LANGUAGE_NONE][0]['safe_value'])) { | |
// Update the product. | |
} | |
else { | |
// Create the product. | |
$client = billysbilling_get_api_object(); | |
$response = $client->post("products", $data); | |
$billysbilling_product_id = $response->id; | |
} | |
// Save id to the entity. | |
print_r($product_wrapper->getPropertyInfo()); | |
$product_wrapper->billysbilling_product_id->get(); | |
print_r($product_wrapper); | |
$product_wrapper->billysbilling_product_id->set($billysbilling_product_id); | |
print_r($product_wrapper); | |
$product_wrapper->save(); | |
return $billysbilling_product_id; | |
} | |
/** | |
* Creates an invoice in BillysBilling | |
* | |
* @param object $order Drupal Commerce Order Object | |
* | |
* @return string BillysBilling Invoice ID | |
*/ | |
function billysbilling_create_invoice($order) { | |
/* | |
$order->mail; | |
$order->status; | |
$order->order_id; | |
$order->commerce_line_items; | |
$order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id']; | |
*/ | |
//$order_wrapper = entity_metadata_wrapper('commerce_order', $order); | |
$billysbilling_contact_id = billysbilling_sync_contact($order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id']); | |
if ($billysbilling_contact_id === FALSE) { | |
return; | |
} | |
$billys_line_item = array(); | |
foreach ($order->commerce_line_items as $line_item) { | |
foreach ($line_item as $key => $item) { | |
$i = commerce_line_item_load($item['line_item_id']); | |
/* | |
$i->quantity; | |
$i->commerce_unit_price[LANGUAGE_NONE][0]['amount']; | |
$i->commerce_unit_price[LANGUAGE_NONE][0]['currency_code']; | |
$i->commerce_product[LANGUAGE_NONE][0]['product_id']; | |
*/ | |
switch ($i->type) { | |
case 'product': | |
$billysbilling_product_id = billysbilling_sync_product($i->commerce_product[LANGUAGE_NONE][0]['product_id']); | |
if ($billysbilling_product_id === FALSE) { | |
continue; | |
} | |
break; | |
case 'shipping': | |
break; | |
} | |
$billys_line_item[] = array( | |
'productId' => $billysbilling_product_id, | |
'quantity' => $i->quantity, | |
// Return unitPrice in float | |
'unitPrice' => $i->commerce_unit_price[LANGUAGE_NONE][0]['amount'] / 100, | |
); | |
} | |
} | |
// Create the invoice. | |
$data = array( | |
'type' => 'invoice', | |
'contactId' => $billysbilling_contact_id, | |
// 'attContactPersonId' => '121114-B94nbm1JVPTvD', | |
// 'contactMessage' => 'Thank you for being a happy customer.', | |
'externalId' => $order->order_id, | |
'entryDate' => date('Y-m-d'), | |
'dueDate' => date('Y-m-') . date('d') + 14, | |
'currencyId' => 'DKK', | |
'lines' => $billys_line_item, | |
); | |
$client = billysbilling_get_api_object(); | |
$response = $client->post("invoices", $data); | |
$billysbilling_invoice_id = $response->id; | |
//print_r($order_wrapper->billysbilling_invoice_id->get()); | |
// Save id to the entity. | |
//$order_wrapper->billysbilling_invoice_id->set($billysbilling_invoice_id); | |
//print_r($order_wrapper->billysbilling_invoice_id->get()); | |
//$order_wrapper->save(); | |
return $billysbilling_invoice_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment