public function testSetCustom() { $this->specify('sets the custom field', function () { $i = new \CoinSimple\Invoice(); verify($i->data())->equals(array()); $i->setCustom("custom data"); $data = $i->data(); verify(array_key_exists('custom', $data))->true(); verify($data['custom'])->equals("custom data"); }); $this->specify('converts non arrays to strings', function () { $i = new \CoinSimple\Invoice(); verify($i->data())->equals(array()); $i->setCustom(34); $data = $i->data(); verify(array_key_exists('custom', $data))->true(); verify($data['custom'])->equals("34"); }); $this->specify('does not convert arrays or hashes', function () { $i = new \CoinSimple\Invoice(); verify($i->data())->equals(array()); $i->setCustom(array(3, 5, 6)); $data = $i->data(); verify(array_key_exists('custom', $data))->true(); verify($data['custom'])->equals(array(3, 5, 6)); $i->setCustom(array("id" => 23)); $data = $i->data(); verify(array_key_exists('custom', $data))->true(); verify($data['custom'])->equals(array("id" => 23)); }); }
/** * Translate WooCommerce order into coinsimple invoice and POST invoice to coinsimple.com. * * @access public * @todo determine how to handle order statuses in this function. * @param string $order_id rendezvous token that identifies a WooCommerce order. * @param $reply passed by reference, returned if successful. * @return boolean true if successful or false if unsuccessful. */ public function post_invoice($order_id, &$reply) { $wc_order = new WC_Order($order_id); $invoice = new \CoinSimple\Invoice(); $invoice->setName($wc_order->billing_first_name . ' ' . $wc_order->billing_last_name); $invoice->setEmail($wc_order->billing_email); $invoice->setCurrency(strtolower(get_woocommerce_currency())); // create line items $wc_items = $wc_order->get_items(); foreach ($wc_items as $wc_item) { if (get_option('woocommerce_prices_include_tax') === 'yes') { $line_total = $wc_order->get_line_subtotal($wc_item, true, true); } else { $line_total = $wc_order->get_line_subtotal($wc_item, false, true); } $item = array("description" => $wc_item['name'], "quantity" => floatval($wc_item['qty']), "price" => round($line_total / $wc_item['qty'], 2)); $invoice->addItem($item); } $invoice->setNotes($this->get_option("notes")); //tax if ($wc_order->get_total_tax() != 0 && get_option('woocommerce_prices_include_tax') !== 'yes') { $tax = 0; foreach ($wc_order->get_tax_totals() as $value) { $tax += $value->amount; } $tax = round($tax, 2); $invoice->addItem(array("description" => "Sales tax", "quantity" => 1, "price" => $tax)); } // shipping if ($wc_order->get_total_shipping() != 0) { $shipping = $wc_order->get_total_shipping(); if (get_option('woocommerce_prices_include_tax') === 'yes') { $shipping += $wc_order->get_shipping_tax(); } $invoice->addItem(array("description" => "Shipping and handling", "quantity" => 1, "price" => round($shipping, 2))); } // coupens if ($wc_order->get_total_discount() != 0) { $invoice->addItem(array("description" => "Discounts", "quantity" => 1, "price" => -round($wc_order->get_total_discount(), 2))); } // settings part $invoice->setCallbackUrl(add_query_arg('wc-api', 'wc_coinsimple', home_url('/'))); $invoice->setCustom(array("order_id" => $wc_order->id, "order_key" => $wc_order->order_key)); if ($this->get_option("redirect_url") != "") { $invoice->setRedirectUrl($this->get_option("redirect_url")); } $business = new \CoinSimple\Business($this->get_option("business_id"), $this->get_option('api_key')); $res = $business->sendInvoice($invoice); if ($res->status == "ok") { $reply = $res; return true; } else { return false; } }