Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Place this file in your WHMCS /includes/api folder.
- // Load WHMCS bootstrap files
- require_once __DIR__ . '/../../init.php';
- require_once __DIR__ . '/../../includes/api.php';
- // Load client functions if needed
- require_once __DIR__ . '/../../includes/clientfunctions.php';
- require_once __DIR__ . '/../../includes/invoicefunctions.php';
- // Only allow POST requests
- if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
- outputResults([
- "result" => "error",
- "message" => "Invalid Request Method. POST required."
- ]);
- exit;
- }
- $paymentmethod = $_POST['paymentmethod'] ?? null;
- $clientemail = $_POST['clientemail'] ?? null;
- $clientpass = $_POST['clientpassword'] ?? null;
- if (!$paymentmethod || !$clientemail || !$clientpass) {
- outputResults([
- "result" => "error",
- "message" => "Missing required parameters. Ensure paymentmethod, clientemail, and clientpassword are provided."
- ]);
- exit;
- }
- $loginResult = localAPI("ValidateLogin", ['email'=>$clientemail, 'password2'=>$clientpass]);
- if ($loginResult['result'] == 'error') {
- outputResults([
- "result" => "error",
- "message" => "Invalid login"
- ]);
- exit;
- }
- $clientid = $loginResult['userid'];
- $apiParams = [
- 'clientid' => $clientid,
- 'paymentmethod' => $paymentmethod,
- ];
- $mandatoryParams = ['pid', 'configoptions', 'qty'];
- $notFilledParams = [];
- foreach ($mandatoryParams as $param) {
- if (!isset($_POST[$param])) {
- $notFilledParams[] = $param;
- }
- }
- if (count($notFilledParams) > 0) {
- outputResults([
- "result" => "error",
- "message" => "Missing fields",
- "fields" => $notFilledParams
- ]);
- exit;
- }
- $possibleParams = [
- 'pid', 'qty', 'domain', 'billingcycle', 'domaintype', 'regperiod', 'idnlanguage', 'eppcode',
- 'nameserver1', 'nameserver2', 'nameserver3', 'nameserver4', 'nameserver5', 'customfields', 'configoptions',
- 'priceoverride', 'promocode', 'promooverride', 'affid', 'noinvoice', 'noinvoiceemail', 'noemail', 'addons', 'addonsqty',
- 'hostname', 'ns1prefix', 'ns2prefix', 'rootpw', 'contactid', 'dnsmanagement', 'domainfields', 'emailforwarding', 'idprotection',
- 'domainpriceoverride', 'domainrenewoverride', 'domainrenewals', 'clientip', 'addonid', 'addonidqty', 'serviceid',
- 'addonids', 'addonidsqty', 'serviceids', 'servicerenewals', 'addonrenewals'
- ];
- foreach ($possibleParams as $param) {
- if (isset($_POST[$param])) {
- $apiParams[$param] = $_POST[$param];
- }
- }
- try {
- $orderResult = localAPI("AddOrder", $apiParams);
- if (!is_array($orderResult) || !isset($orderResult['result'])) {
- outputResults([
- "result" => "error",
- "message" => "No response from AddOrder API command. Please check your configuration."
- ]);
- exit;
- }
- if ($orderResult['result'] !== "success") {
- outputResults([
- "result" => "error",
- "message" => "Order creation failed via AddOrder command.",
- "details" => $orderResult
- ]);
- exit;
- }
- $orderid = $orderResult['orderid'] ?? null;
- if (!$orderid) {
- outputResults([
- "result" => "error",
- "message" => "Order created but no order ID was returned.",
- "details" => $orderResult
- ]);
- exit;
- }
- } catch (Exception $e) {
- outputResults([
- "result" => "error",
- "message" => "Order creation error: " . $e->getMessage()
- ]);
- exit;
- }
- $invoiceId = $orderResult['invoiceid'];
- $invoiceResult = localAPI('GetInvoice', ['invoiceid'=>$invoiceId]);
- $invoice = new WHMCS\Invoice();
- $invoice->setId($invoiceId);
- $reflection = new ReflectionClass($invoice);
- $method = $reflection->getMethod('loadData');
- $method->setAccessible(true);
- $method->invoke($invoice);
- $paymentMethod = $reflection->getMethod('getGatewayInvoiceParams');
- $paymentMethod->setAccessible(true);
- $data = $paymentMethod->invoke($invoice);
- /*
- $mollie = new \Mollie\Api\MollieApiClient();
- $data = $paymentMethod->invoke($invoice);
- //$data['key']
- $mollie->setApiKey('test_efxRgxUVcsTCwybAyJbuDTQdfkWunD');
- $payment = $mollie->payments->create([
- "amount" => [
- "currency" => $data['currency'],
- "value" => $data['amount'],
- ],
- "description" => $data['description'],
- "redirectUrl" => $data['returnurl'],
- "webhookUrl" => 'https://billing.gameunit.pro/modules/gateways/mollie/ajisdhyjkalsd.php',
- "metadata" => [
- "order_id" => $data['invoiceid'],
- ]]);
- update_query("tblinvoices", array( "paymentmethod" => $gateway ), array( "id" => $invoiceid ));
- $paymentMethod = null;
- */
- if ($paymentmethod == 'molliepaypal_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::PAYPAL;
- } else if ($paymentmethod == 'molliebanktransfer_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::BANKTRANSFER;
- } else if ($paymentmethod == 'molliecreditcard_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::CREDITCARD;
- } else if ($paymentmethod == 'mollieideal_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::IDEAL;
- } else if ($paymentmethod == 'molliebancontact_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::BANCONTACT;
- } else if ($paymentmethod == 'molliepaysafecard_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::PAYSAFECARD;
- } else if ($paymentmethod == 'mollieapplepay_devapp') {
- $met = \Mollie\Api\Types\PaymentMethod::APPLEPAY;
- }
- $output = [
- "result" => "success",
- "orderid" => $orderid,
- "invoiceid" => $invoiceId,
- "paymentlink" => mollie_link2($data, $met)
- ];
- if (isset($invoiceError)) {
- $output['invoiceError'] = $invoiceError;
- }
- if (isset($orderDetailsError)) {
- $output['orderDetailsError'] = $orderDetailsError;
- }
- outputResults($output);
- exit();
- /*
- * Helper function to output JSON results.
- */
- function outputResults($results) {
- header('Content-Type: application/json');
- echo json_encode($results);
- }
- function mollie_link2($params, $method = Mollie_API_Object_Method::IDEAL)
- {
- global $whmcs;
- /**
- *
- * Setting requirements and includes
- *
- */
- if (substr($params['returnurl'], 0, 1) == '/')
- $params['returnurl'] = $params['systemurl'] . $params['returnurl'];
- if (empty($params['language']))
- $params['language'] = ((isset($_SESSION['language'])) ? $_SESSION['language'] : $whmcs->get_config('Language'));
- if (empty($params['language']))
- $params['language'] = 'english';
- if (!file_exists(__DIR__ . '/../../modules/gateways/mollie/lang/' . $params['language'] . '.php'))
- $params['language'] = 'english';
- /* @var array $_GATEWAYLANG */
- require __DIR__ . '/../../modules/gateways/mollie/lang/' . $params['language'] . '.php';
- $tableCheckQuery = full_query('SHOW TABLES LIKE \'gateway_mollie\'');
- if (mysql_num_rows($tableCheckQuery) != 1) {
- full_query('CREATE TABLE IF NOT EXISTS `gateway_mollie` (`id` int(11) NOT NULL AUTO_INCREMENT, `paymentid` varchar(15), `amount` double NOT NULL, `currencyid` int(11) NOT NULL, `ip` varchar(50) NOT NULL, `userid` int(11) NOT NULL, `invoiceid` int(11) NOT NULL, `status` ENUM(\'open\',\'paid\',\'closed\') NOT NULL DEFAULT \'open\', `method` VARCHAR(25) NOT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `paymentid` (`paymentid`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;');
- }
- $mollie = new \Mollie\Api\MollieApiClient();
- $mollie->setApiKey($params['key']);
- /**
- *
- * Check if good state to open transaction.
- *
- */
- $transactionCurrency = select_query('tblcurrencies', '', array('code' => $params['currency']), null, null, 1);
- $transactionCurrency = mysql_fetch_assoc($transactionCurrency);
- $transactionId = insert_query('gateway_mollie', array(
- 'amount' => $params['amount'],
- 'currencyid' => $transactionCurrency['id'],
- 'ip' => $_SERVER['REMOTE_ADDR'],
- 'userid' => $params['clientdetails']['userid'],
- 'invoiceid' => $params['invoiceid'],
- 'method' => $method
- ));
- $payment = $mollie->payments->create(array(
- 'amount' => [
- 'value' => $params['amount'],
- 'currency' => $params['currency'],
- ],
- 'method' => $method,
- 'description' => $params['description'],
- 'redirectUrl' => $params['returnurl'] . '&check_payment=' . $transactionId,
- 'webhookUrl' => $params['systemurl'] . '/modules/gateways/mollie/callback.php',
- 'metadata' => array(
- 'invoice_id' => $params['invoiceid'],
- ),
- 'issuer' => ((isset($_POST['issuer']) && !empty($_POST['issuer'])) ? $_POST['issuer'] : NULL)
- ));
- update_query('gateway_mollie', array('paymentid' => $payment->id), array('id' => $transactionId));
- return $payment->getCheckoutUrl();
- }
Add Comment
Please, Sign In to add comment