db = $db; } /** * Add M-Pesa payment option to invoice card * * @param array $parameters Hook parameters * @param CommonObject $object Current object (e.g., Facture) * @param string $action Current action * @param HookManager $hookmanager Hook manager instance * @return int 0 on success, < 0 on error */ public function formObjectOptions($parameters, &$object, &$action, $hookmanager) { global $langs, $conf; $this->hookmanager = $hookmanager; // Check if we're on the invoice card and the invoice is validated (statut = 1) if (in_array('invoicecard', explode(':', $parameters['currentcontext'])) && $object->statut == 1) { // Load module configuration $shortcode = $conf->global->MPESAPAY_SHORTCODE; if (empty($shortcode)) { return 0; // Do nothing if module not configured } // Generate payment URL for the public payment page $paymentUrl = DOL_MAIN_URL_ROOT . '/public/payment/newpayment.php?source=invoice&ref=' . urlencode($object->ref); // Output M-Pesa payment button print ''; print '
'; print '' . $langs->trans('PayWithMpesa') . ''; print '
'; print ''; } return 0; } /** * Add M-Pesa transaction input to payment card * * @param array $parameters Hook parameters * @param CommonObject $object Current object (e.g., Payment) * @param string $action Current action * @param HookManager $hookmanager Hook manager instance * @return int 0 on success, < 0 on error */ public function formAddObjectLine($parameters, &$object, &$action, $hookmanager) { global $langs, $conf; $this->hookmanager = $hookmanager; // Check if we're on the payment card and payment is draft (statut = 0) if (in_array('paymentcard', explode(':', $parameters['currentcontext'])) && $object->statut == 0) { // Load module configuration $shortcode = $conf->global->MPESAPAY_SHORTCODE; if (empty($shortcode)) { return 0; // Do nothing if module not configured } // Output M-Pesa transaction ID input field print ''; print '
'; print ' '; print ''; print '
'; print ''; } return 0; } /** * Handle actions after payment creation (e.g., process M-Pesa transaction ID) * * @param array $parameters Hook parameters * @param CommonObject $object Current object (e.g., Payment) * @param string $action Current action * @param HookManager $hookmanager Hook manager instance * @return int 0 on success, < 0 on error */ public function createPayment($parameters, &$object, &$action, $hookmanager) { global $langs, $db; if ($action === 'addpayment' && !empty(GETPOST('mpesapay_transaction_id', 'alpha'))) { $transactionId = GETPOST('mpesapay_transaction_id', 'alpha'); // Here you could add logic to verify the transaction ID with M-Pesa API // For now, we'll just log it (assuming a transaction log table exists) $sql = "INSERT INTO " . MAIN_DB_PREFIX . "dolipesa_transactions (transaction_id, fk_payment, datec) "; $sql .= "VALUES ('" . $db->escape($transactionId) . "', " . $object->id . ", NOW())"; $resql = $db->query($sql); if (!$resql) { setEventMessages($langs->trans('ErrorSavingTransaction'), null, 'errors'); return -1; } } return 0; } } ?>