Advertisement
khalequzzaman17

Credit Card Capture Attempt

Oct 8th, 2022 (edited)
1,243
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 1 0
  1. <?php
  2.  
  3. use WHMCS\Carbon;
  4. use WHMCS\Database\Capsule;
  5.  
  6. function cron_capture_payment($vars) {
  7.  
  8.     # How many days before the due date you wish to capture payment
  9.    # e.g. if set to 3 and todays date is 6th Sept, only unpaid invoice due on the 9th Sept will be checked
  10.    $daysBeforeDueDateToCapture = 0;
  11.  
  12.     # What hours of the day you wish to capture payment (e.g. '06' is 6am, '18' is 6pm)
  13.    $captureTimes = ['00', '06', '12', '18'];
  14.  
  15.     # Which minute after the hour you wish this check to run at (e.g. if your cron is set to run every 5 minutes, can set this to 5, 10, 15, 20, 25, etc...)
  16.    $captureMinute = 10;
  17.  
  18.     # If you only want to capture payments on certain payment methods, add them here (e.g. 'stripe', 'paypalcheckout')
  19.    $allowedPaymentMethods = [];
  20.  
  21.     # If enabled and the capture fails, log the error in the clients log
  22.    $logErrors = true;
  23.  
  24.     /* ------------------------------------------ */
  25.     /* ONLY EDIT VARIABLES ABOVE THIS LINE        */
  26.     /* ------------------------------------------ */
  27.  
  28.     # Grab the current time and date
  29.    $currentTime    = Carbon::now()->format('H');
  30.     $currentMinute  = Carbon::now()->format('i');
  31.     $currentDay     = Carbon::now()->format('Y/m/d');
  32.  
  33.     # Check if the current hour is in the $captureTimes array
  34.    if (in_array($currentTime, $captureTimes) && $currentMinute == $captureMinute) {
  35.  
  36.         # Calculate the due date based on the $daysBeforeDueDateToCapture variable
  37.        $theDueDate = Carbon::now()->addDays($daysBeforeDueDateToCapture)->format('Y-m-d');
  38.  
  39.         # Grab all unpaid invoices that match the due date
  40.        $invoices = Capsule::table('tblinvoices')->where('duedate', $theDueDate)->where('status', 'Unpaid')->get();
  41.  
  42.         # Loop through the invoices
  43.        foreach ($invoices as $invoice) {
  44.  
  45.             # If the $allowedPaymentMethods is not empty, check that the invoice's payment method is in it
  46.            if (!empty($allowedPaymentMethods) && !in_array($invoice->paymentmethod, $allowedPaymentMethods)) {
  47.                 return;
  48.             }
  49.  
  50.             # Attempt to capture payment
  51.            $result = localAPI('CapturePayment', [
  52.                 'invoiceid' => $invoice->id
  53.             ]);
  54.  
  55.             print_r($result);
  56.             # If $logErrors is true and an error is present, log it to the clients log
  57.            if ($result['result'] === 'error' && $logErrors) {
  58.                 logActivity("Automatic payment capture hook failed on invoice #{$invoice->id}: {$result['message']}", $invoice->userid);
  59.             }
  60.  
  61.         }
  62.  
  63.     }
  64.  
  65. }
  66.  
  67. add_hook('AfterCronJob', 1, 'cron_capture_payment');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement