buzzthedev

Untitled

Apr 21st, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Place this file in your WHMCS /includes/api folder.
  2.  
  3. // Load WHMCS bootstrap files
  4. require_once __DIR__ . '/../../init.php';
  5. require_once __DIR__ . '/../../includes/api.php';
  6. // Load client functions if needed
  7. require_once __DIR__ . '/../../includes/clientfunctions.php';
  8. require_once __DIR__ . '/../../includes/invoicefunctions.php';
  9.  
  10. // Only allow POST requests
  11.  
  12.  
  13. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  14. outputResults([
  15. "result" => "error",
  16. "message" => "Invalid Request Method. POST required."
  17. ]);
  18. exit;
  19. }
  20. $paymentmethod = $_POST['paymentmethod'] ?? null;
  21. $clientemail = $_POST['clientemail'] ?? null;
  22. $clientpass = $_POST['clientpassword'] ?? null;
  23.  
  24. if (!$paymentmethod || !$clientemail || !$clientpass) {
  25. outputResults([
  26. "result" => "error",
  27. "message" => "Missing required parameters. Ensure paymentmethod, clientemail, and clientpassword are provided."
  28. ]);
  29. exit;
  30. }
  31.  
  32. $loginResult = localAPI("ValidateLogin", ['email'=>$clientemail, 'password2'=>$clientpass]);
  33.  
  34. if ($loginResult['result'] == 'error') {
  35. outputResults([
  36. "result" => "error",
  37. "message" => "Invalid login"
  38. ]);
  39. exit;
  40. }
  41. $clientid = $loginResult['userid'];
  42.  
  43. $apiParams = [
  44. 'clientid' => $clientid,
  45. 'paymentmethod' => $paymentmethod,
  46. ];
  47.  
  48. $mandatoryParams = ['pid', 'configoptions', 'qty'];
  49. $notFilledParams = [];
  50. foreach ($mandatoryParams as $param) {
  51. if (!isset($_POST[$param])) {
  52. $notFilledParams[] = $param;
  53. }
  54. }
  55.  
  56. if (count($notFilledParams) > 0) {
  57. outputResults([
  58. "result" => "error",
  59. "message" => "Missing fields",
  60. "fields" => $notFilledParams
  61. ]);
  62. exit;
  63. }
  64.  
  65. $possibleParams = [
  66. 'pid', 'qty', 'domain', 'billingcycle', 'domaintype', 'regperiod', 'idnlanguage', 'eppcode',
  67. 'nameserver1', 'nameserver2', 'nameserver3', 'nameserver4', 'nameserver5', 'customfields', 'configoptions',
  68. 'priceoverride', 'promocode', 'promooverride', 'affid', 'noinvoice', 'noinvoiceemail', 'noemail', 'addons', 'addonsqty',
  69. 'hostname', 'ns1prefix', 'ns2prefix', 'rootpw', 'contactid', 'dnsmanagement', 'domainfields', 'emailforwarding', 'idprotection',
  70. 'domainpriceoverride', 'domainrenewoverride', 'domainrenewals', 'clientip', 'addonid', 'addonidqty', 'serviceid',
  71. 'addonids', 'addonidsqty', 'serviceids', 'servicerenewals', 'addonrenewals'
  72. ];
  73.  
  74. foreach ($possibleParams as $param) {
  75. if (isset($_POST[$param])) {
  76. $apiParams[$param] = $_POST[$param];
  77. }
  78. }
  79.  
  80. try {
  81. $orderResult = localAPI("AddOrder", $apiParams);
  82. if (!is_array($orderResult) || !isset($orderResult['result'])) {
  83. outputResults([
  84. "result" => "error",
  85. "message" => "No response from AddOrder API command. Please check your configuration."
  86. ]);
  87. exit;
  88. }
  89. if ($orderResult['result'] !== "success") {
  90. outputResults([
  91. "result" => "error",
  92. "message" => "Order creation failed via AddOrder command.",
  93. "details" => $orderResult
  94. ]);
  95. exit;
  96. }
  97.  
  98. $orderid = $orderResult['orderid'] ?? null;
  99. if (!$orderid) {
  100. outputResults([
  101. "result" => "error",
  102. "message" => "Order created but no order ID was returned.",
  103. "details" => $orderResult
  104. ]);
  105. exit;
  106. }
  107. } catch (Exception $e) {
  108. outputResults([
  109. "result" => "error",
  110. "message" => "Order creation error: " . $e->getMessage()
  111. ]);
  112. exit;
  113. }
  114. $invoiceId = $orderResult['invoiceid'];
  115.  
  116. $invoiceResult = localAPI('GetInvoice', ['invoiceid'=>$invoiceId]);
  117.  
  118. $invoice = new WHMCS\Invoice();
  119. $invoice->setId($invoiceId);
  120. $reflection = new ReflectionClass($invoice);
  121.  
  122. $method = $reflection->getMethod('loadData');
  123. $method->setAccessible(true);
  124. $method->invoke($invoice);
  125.  
  126. $paymentMethod = $reflection->getMethod('getGatewayInvoiceParams');
  127. $paymentMethod->setAccessible(true);
  128. $data = $paymentMethod->invoke($invoice);
  129. /*
  130. $mollie = new \Mollie\Api\MollieApiClient();
  131.  
  132. $data = $paymentMethod->invoke($invoice);
  133. //$data['key']
  134. $mollie->setApiKey('test_efxRgxUVcsTCwybAyJbuDTQdfkWunD');
  135.  
  136. $payment = $mollie->payments->create([
  137. "amount" => [
  138. "currency" => $data['currency'],
  139. "value" => $data['amount'],
  140. ],
  141. "description" => $data['description'],
  142. "redirectUrl" => $data['returnurl'],
  143. "webhookUrl" => 'https://billing.gameunit.pro/modules/gateways/mollie/ajisdhyjkalsd.php',
  144. "metadata" => [
  145. "order_id" => $data['invoiceid'],
  146. ]]);
  147.  
  148. update_query("tblinvoices", array( "paymentmethod" => $gateway ), array( "id" => $invoiceid ));
  149.  
  150. $paymentMethod = null;
  151. */
  152. if ($paymentmethod == 'molliepaypal_devapp') {
  153. $met = \Mollie\Api\Types\PaymentMethod::PAYPAL;
  154. } else if ($paymentmethod == 'molliebanktransfer_devapp') {
  155. $met = \Mollie\Api\Types\PaymentMethod::BANKTRANSFER;
  156. } else if ($paymentmethod == 'molliecreditcard_devapp') {
  157. $met = \Mollie\Api\Types\PaymentMethod::CREDITCARD;
  158. } else if ($paymentmethod == 'mollieideal_devapp') {
  159. $met = \Mollie\Api\Types\PaymentMethod::IDEAL;
  160. } else if ($paymentmethod == 'molliebancontact_devapp') {
  161. $met = \Mollie\Api\Types\PaymentMethod::BANCONTACT;
  162. } else if ($paymentmethod == 'molliepaysafecard_devapp') {
  163. $met = \Mollie\Api\Types\PaymentMethod::PAYSAFECARD;
  164. } else if ($paymentmethod == 'mollieapplepay_devapp') {
  165. $met = \Mollie\Api\Types\PaymentMethod::APPLEPAY;
  166. }
  167.  
  168.  
  169. $output = [
  170. "result" => "success",
  171. "orderid" => $orderid,
  172. "invoiceid" => $invoiceId,
  173. "paymentlink" => mollie_link2($data, $met)
  174. ];
  175.  
  176. if (isset($invoiceError)) {
  177. $output['invoiceError'] = $invoiceError;
  178. }
  179. if (isset($orderDetailsError)) {
  180. $output['orderDetailsError'] = $orderDetailsError;
  181. }
  182.  
  183. outputResults($output);
  184. exit();
  185.  
  186. /*
  187. * Helper function to output JSON results.
  188. */
  189. function outputResults($results) {
  190. header('Content-Type: application/json');
  191. echo json_encode($results);
  192. }
  193.  
  194. function mollie_link2($params, $method = Mollie_API_Object_Method::IDEAL)
  195. {
  196. global $whmcs;
  197.  
  198. /**
  199. *
  200. * Setting requirements and includes
  201. *
  202. */
  203. if (substr($params['returnurl'], 0, 1) == '/')
  204. $params['returnurl'] = $params['systemurl'] . $params['returnurl'];
  205.  
  206. if (empty($params['language']))
  207. $params['language'] = ((isset($_SESSION['language'])) ? $_SESSION['language'] : $whmcs->get_config('Language'));
  208.  
  209. if (empty($params['language']))
  210. $params['language'] = 'english';
  211.  
  212. if (!file_exists(__DIR__ . '/../../modules/gateways/mollie/lang/' . $params['language'] . '.php'))
  213. $params['language'] = 'english';
  214.  
  215. /* @var array $_GATEWAYLANG */
  216. require __DIR__ . '/../../modules/gateways/mollie/lang/' . $params['language'] . '.php';
  217.  
  218. $tableCheckQuery = full_query('SHOW TABLES LIKE \'gateway_mollie\'');
  219.  
  220. if (mysql_num_rows($tableCheckQuery) != 1) {
  221. 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;');
  222. }
  223.  
  224. $mollie = new \Mollie\Api\MollieApiClient();
  225. $mollie->setApiKey($params['key']);
  226.  
  227. /**
  228. *
  229. * Check if good state to open transaction.
  230. *
  231. */
  232. $transactionCurrency = select_query('tblcurrencies', '', array('code' => $params['currency']), null, null, 1);
  233. $transactionCurrency = mysql_fetch_assoc($transactionCurrency);
  234.  
  235. $transactionId = insert_query('gateway_mollie', array(
  236. 'amount' => $params['amount'],
  237. 'currencyid' => $transactionCurrency['id'],
  238. 'ip' => $_SERVER['REMOTE_ADDR'],
  239. 'userid' => $params['clientdetails']['userid'],
  240. 'invoiceid' => $params['invoiceid'],
  241. 'method' => $method
  242. ));
  243.  
  244. $payment = $mollie->payments->create(array(
  245. 'amount' => [
  246. 'value' => $params['amount'],
  247. 'currency' => $params['currency'],
  248. ],
  249. 'method' => $method,
  250. 'description' => $params['description'],
  251. 'redirectUrl' => $params['returnurl'] . '&check_payment=' . $transactionId,
  252. 'webhookUrl' => $params['systemurl'] . '/modules/gateways/mollie/callback.php',
  253. 'metadata' => array(
  254. 'invoice_id' => $params['invoiceid'],
  255. ),
  256. 'issuer' => ((isset($_POST['issuer']) && !empty($_POST['issuer'])) ? $_POST['issuer'] : NULL)
  257. ));
  258.  
  259. update_query('gateway_mollie', array('paymentid' => $payment->id), array('id' => $transactionId));
  260.  
  261. return $payment->getCheckoutUrl();
  262. }
Add Comment
Please, Sign In to add comment