Last active
May 7, 2024 12:49
-
-
Save reinvented/22d173c7f0a607a7069ad7324b125f57 to your computer and use it in GitHub Desktop.
Automating the use of Canada Revenue's Payroll Deductions Online Calculator in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* get-pdoc.php - Scrape Canada Revenue Agency payroll deductions values | |
* | |
* This script takes an hourly amount, a number of hours, and a year, | |
* month and day of a pay period ending, and passes these to the | |
* Canada Revenue Agency Payroll Deductions Online Calculator | |
* (https://www.canada.ca/en/revenue-agency/services/e-services/e-services-businesses/payroll-deductions-online-calculator.html), | |
* returning the provincial tax, federal tax, CPP and EI amounts. | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or (at | |
* your option) any later version. | |
* This program is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* General Public License for more details. | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
* USA | |
* | |
* @version 0.2, June 14, 2018 | |
* @author Peter Rukavina <[email protected]> | |
* @copyright Copyright © 2018, Reinvented Inc. | |
* @license http://www.fsf.org/licensing/licenses/gpl.txt GNU Public License | |
*/ | |
// Get from http://simplehtmldom.sourceforge.net/ | |
include('simple_html_dom.php'); | |
$retval = array(); | |
$retval['status'] = 'success'; | |
$required = array('hours', 'hourly', 'year', 'month', 'day'); | |
foreach($required as $key => $value) { | |
if (!array_key_exists($value, $_GET)) { | |
$retval['status'] = 'error'; | |
$retval['errormessage'] = "Missing '" . $value . "' parameter."; | |
} | |
} | |
if ($retval['status'] != 'error') { | |
$param = array(); | |
$param['income'] = ($_GET['hourly'] * $_GET['hours']); | |
$param['vacationPay'] = round($param['income'] * 0.04, 2); | |
$param['year'] = $_GET['year']; | |
$param['month'] = $_GET['month']; | |
$param['day'] = $_GET['day']; | |
$param['month'] = str_pad($param['month'], 2, '0', STR_PAD_LEFT); | |
$param['day'] = str_pad($param['day'], 2, '0', STR_PAD_LEFT); | |
$data = array( | |
"step0" => array( | |
"calculationType" => "SALARY", // Salary | |
"action:welcome_NextButton" => "Next", | |
"struts.token.name" => "166PXW9HKHFUX4B37TTDTLBYFEK2SGPC", | |
), | |
"step1" => array( | |
"employeeName" => "The Employee", | |
"employerName" => "The Employer", | |
"jurisdiction" => "PRINCE_EDWARD_ISLAND", // Prince Edward Island | |
"payPeriodFrequency" => "WEEKLY_52PP", // Weekly | |
"datePaidYear" => $param['year'], | |
"datePaidMonth" => $param['month'], | |
"datePaidDay" => $param['day'], | |
"action:payrollDeductionsStep1_NextButton" => "Next" | |
), | |
"step2" => array( | |
"incomeAmount" => $param['income'], | |
"vacationPay" => $param['vacationPay'], | |
"salaryType" => "NO_BONUS_PAY_NO_RETROACTIVE_PAY", | |
"clergyType" => "NOT_APPLICABLE", | |
"action:payrollDeductionsStep2a_NextButton" => "Next" | |
), | |
"step3" => array( | |
"federalClaimCode" => "CLAIM_CODE_1", | |
"provinceTerritoryClaimCode" => "CLAIM_CODE_1", | |
"requestedAdditionalTaxDeductions" => "0.00", | |
"pensionableEarningsYearToDate" => "0", | |
"cppOrQppContributionsDeductedYearToDate" => "0", | |
"cppQppType" => "CPP_QPP_YEAR_TO_DATE", | |
"insurableEarningsYearToDate" => "0", | |
"employmentInsuranceType" => "EI_YEAR_TO_DATE", | |
"employmentInsuranceDeductedYearToDate" => "0", | |
"reducedEIRate" => "0", | |
"employerEmploymentInsurancePremiumRate" => 1.4, | |
"action:payrollDeductionsStep3_CalculateButton" => "Calculate" | |
), | |
); | |
$triggers = array( | |
"federalTax" => array("Federal tax deduction", 3), | |
"provincialTax" => array("Provincial tax deduction", 4), | |
"CPP" => array("CPP deductions", 4), | |
"EI" => array("EI deductions", 4), | |
); | |
$result = fetch("https://apps.cra-arc.gc.ca/ebci/rhpd/prot/welcome.action", array('post' => false)); | |
$hidden = getHiddenFormValueFromHTML($result); | |
$data['step0'] = array_merge($data['step0'], $hidden); | |
$result = fetch("https://apps.cra-arc.gc.ca/ebci/rhpd/prot/welcome.action", array('hidden' => $hidden, 'refer' => 'https://apps.cra-arc.gc.ca/ebci/rhpd/prot/welcome.action', 'post' => http_build_query($data['step0'], '', '&'))); | |
$hidden = getHiddenFormValueFromHTML($result); | |
$data['step1'] = array_merge($data['step1'], $hidden); | |
$result = fetch("https://apps.cra-arc.gc.ca/ebci/rhpd/prot/payrollDeductionsStep1.action", array('refer' => 'https://apps.cra-arc.gc.ca/ebci/rhpd/prot/welcome.action', 'post' => http_build_query($data['step1'], '', '&'))); | |
$hidden = getHiddenFormValueFromHTML($result); | |
$data['step2'] = array_merge($data['step2'], $hidden); | |
$result = fetch("https://apps.cra-arc.gc.ca/ebci/rhpd/prot/payrollDeductionsStep2a.action", array('refer' => 'https://apps.cra-arc.gc.ca/ebci/rhpd/prot/payrollDeductionsStep1.action', 'post' => http_build_query($data['step2'], '', '&'))); | |
$hidden = getHiddenFormValueFromHTML($result); | |
$data['step3'] = array_merge($data['step3'], $hidden); | |
$result = fetch("https://apps.cra-arc.gc.ca/ebci/rhpd/prot/payrollDeductionsStep3.action", array('refer' => 'https://apps.cra-arc.gc.ca/ebci/rhpd/prot/payrollDeductionsStep2a.action', 'post' => http_build_query($data['step3'], '', '&'))); | |
$lines = explode("\n", $result); | |
foreach ($lines as $linenumber => $line) { | |
foreach ($triggers as $key => $trigger) { | |
if (strpos($line, $trigger[0]) !== FALSE) { | |
$values[$key] = trim($lines[$linenumber + $trigger[1]]); | |
} | |
} | |
} | |
$retval['values'] = $values; | |
} | |
print json_encode($retval); | |
function fetch($url, $z=null) { | |
$ch = curl_init(); | |
$useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'; | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_AUTOREFERER, true ); | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); | |
curl_setopt( $ch, CURLOPT_POST, isset($z['post']) ); | |
if( isset($z['post']) ) curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] ); | |
if( isset($z['refer']) ) curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] ); | |
curl_setopt( $ch, CURLOPT_USERAGENT, $useragent ); | |
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 ); | |
curl_setopt( $ch, CURLOPT_COOKIEJAR, '/tmp/cra-cookies.txt' ); | |
curl_setopt( $ch, CURLOPT_COOKIEFILE, '/tmp/cra-cookies.txt' ); | |
$result = curl_exec( $ch ); | |
curl_close( $ch ); | |
return $result; | |
} | |
function getHiddenFormValueFromHTML($html) { | |
$html = str_get_html($html); | |
$nodes = $html->find("input[type=hidden]"); | |
$hidden = array(); | |
foreach ($nodes as $node) { | |
$hidden[$node->attr['name']] = $node->attr['value']; | |
} | |
return $hidden; | |
} |
Meekcanadian
commented
Feb 8, 2024
via email
Thank you.
Michael (Mike) Coffill
Founder | Wages Corp.
Book a Demo Call Here: https://calendly.com/michaelc-wagescorp
Tel: 1-866-311-4441 ext.401
Email: ***@***.******@***.***>
Office Hours:
Monday through Friday 8am-4pm Excluding Holidays
***@***.***
From: Vince Elizaga ***@***.***>
Sent: Thursday, February 8, 2024 12:30 PM
To: vynci ***@***.***>
Cc: Mention ***@***.***>
Subject: Re: reinvented/pdoc.php
@vynci commented on this gist.
…________________________________
Awesome, I will keep you posted @Meekcanadian<https://github.com/Meekcanadian>. Thanks!
—
Reply to this email directly, view it on GitHub<https://gist.github.com/reinvented/22d173c7f0a607a7069ad7324b125f57#gistcomment-4884192> or unsubscribe<https://github.com/notifications/unsubscribe-auth/ASQU6GKCNIFDCZCQQXUDWQ3YSUDQJBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA2DINBXGAYTOMFHORZGSZ3HMVZKMY3SMVQXIZI>.
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment