/** * Handle actions from the Edit Order order actions select box * * @since 3.0 * @param object $order WC_Order object */ public function process_order_meta_box_actions($order) { $order = new WC_FreshBooks_Order($order->id); switch (current_action()) { case 'woocommerce_order_action_wc_freshbooks_create_and_send_invoice': $order->create_invoice(); break; case 'woocommerce_order_action_wc_freshbooks_create_draft_invoice': $order->create_invoice(false); break; case 'woocommerce_order_action_wc_freshbooks_send_invoice': $order->send_invoice(); break; case 'woocommerce_order_action_wc_freshbooks_apply_invoice_payment': $order->apply_invoice_payment(); break; case 'woocommerce_order_action_wc_freshbooks_update_invoice': $order->update_invoice_from_order(); break; default: return; } }
/** * Create a payment for the invoice * * @link http://developers.freshbooks.com/docs/payments/#payment.create * * @param \WC_FreshBooks_Order $order order instance * @since 3.0 */ public function create_payment(WC_FreshBooks_Order $order) { // store the order object for later use $this->order = $order; // guess payment type switch ($order->payment_method) { case 'bacs': $type = 'Bank Transfer'; break; case 'cheque': $type = 'Check'; break; case 'cod': $type = 'Cash'; break; case 'paypal': $type = 'PayPal'; break; default: /** * Filter the payment type * * @param string $type The Freshbooks payment type * @param string $payment_method The WooCommerce payment method * @since 3.3.3 */ $type = apply_filters('wc_freshbooks_payment_type', '', $order->payment_method); } // base request $this->request_data = array('request' => array('@attributes' => array('method' => 'payment.create'), 'payment' => array('invoice_id' => $order->wc_freshbooks_invoice_id, 'date' => date('Y-m-d', strtotime(isset($order->paid_date) ? $order->paid_date : $order->order_date)), 'amount' => number_format($order->get_total(), 2, '.', '')))); if (!empty($type)) { $this->request_data['request']['payment']['type'] = $type; } }
/** * Handle payment webhook events * * @link http://developers.freshbooks.com/docs/callbacks/#events * * @param string $action the event type * @param string $payment_id the associated payment ID * @since 3.0 */ public function handle_payment($action, $payment_id) { try { // try to find matching order by payment ID $order_id = $this->get_object_by_freshbooks_id('order', 'payment', $payment_id); // if a payment was created in FreshBooks manually, look up the associated invoice first if (!$order_id) { $payment = wc_freshbooks()->get_api()->get_payment($payment_id); // no payment found if (!$payment) { return; } // invoice IDs included as part of the payment response contain // leading zeroes for some odd reason, so strip them first $order_id = $this->get_object_by_freshbooks_id('order', 'invoice', ltrim($payment['invoice_id'], '0')); // no matching invoice found, likely deleted if (!$order_id) { return; } } $order = new WC_FreshBooks_Order($order_id); if ('delete' == $action) { delete_post_meta($order->id, '_wc_freshbooks_payment_id', $payment_id); } else { update_post_meta($order->id, '_wc_freshbooks_payment_id', $payment_id); } $order->refresh_invoice(); } catch (SV_WC_API_Exception $e) { wc_freshbooks()->log(sprintf('Payment handling failed: %s', $e->getMessage())); } }