invoices->retrieve($id); } /** * @throws ApiErrorException */ public function void(string $id): Invoice { return self::getClient()->invoices->voidInvoice($id); } /** * @throws ApiErrorException * @throws Exception */ public function generate(int $order_id, string $stripe_customer_id): Invoice { $order = (new orders_o())->select($order_id); $order->requireSelected(); // Get the order items $order_items = $order->getOrderItems($order_id); // Create a new line items object $line_items = new stripe_line_items('invoice'); // Loop through the order items foreach ( $order_items as $order_item ) { // Add the order item to the line items $line_items->add( new stripe_line_item( $order_item['product_id'], $order_item['price'], $order_item['quantity'] ) ); } // Create the payment link return $this->create( $line_items, $stripe_customer_id, $this->buildOrderMetadata($order, $stripe_customer_id) ); } private function buildOrderMetadata(orders_o $order, string $stripe_customer_id): array { $metadata = [ 'order_id' => (string)$order->id, 'customer_id' => (string)$order->customer_id->value(), 'stripe_customer_id' => $stripe_customer_id, 'department_id' => (string)$order->department_id->value(), ]; $reference = trim((string)$order->reference->value()); if ($reference !== '') { $metadata['reference'] = $reference; } $po = trim((string)$order->po->value()); if ($po !== '') { $metadata['po'] = $po; } $reg1 = trim((string)$order->reg_1->value()); if ($reg1 !== '') { $metadata['reg_1'] = $reg1; } $reg2 = trim((string)$order->reg_2->value()); if ($reg2 !== '') { $metadata['reg_2'] = $reg2; } $reg3 = trim((string)$order->reg_3->value()); if ($reg3 !== '') { $metadata['reg_3'] = $reg3; } return $metadata; } /** * Create a new payment link * @param stripe_line_items $line_items * @param string $stripe_customer_id * @param array $metadata Additional metadata * @return Invoice * @throws ApiErrorException * @throws Exception */ public function create(stripe_line_items $line_items, string $stripe_customer_id, array $metadata = []): Invoice { $invoice = self::getClient()->invoices->create([ 'customer' => $stripe_customer_id, 'collection_method' => 'send_invoice', 'days_until_due' => 30, 'metadata' => $metadata, 'auto_advance' => true, //'payment_settings' => [ // 'payment_method_types' => [ // 'card', // 'paypal' // ], //], ]); // Add the line items to the invoice foreach ( $line_items->get() as $line_item ) { self::getClient()->invoiceItems->create([ 'customer' => $stripe_customer_id, 'invoice' => $invoice->id, 'price' => $line_item['price'], ]); } // Finalize the invoice return $invoice->finalizeInvoice(); } }