Created
September 15, 2016 15:43
-
-
Save slogsdon/ab59ad89cf25a6284295a9710d680def to your computer and use it in GitHub Desktop.
This file contains 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
# useful imports | |
from securesubmit.services import HpsServicesConfig | |
from securesubmit.services.gateway import ( | |
HpsCreditService, | |
HpsTrackData, | |
HpsEncryptionData, | |
HpsException | |
) | |
from time import sleep | |
# filename template for controlling the onboard LEDs | |
filename = '/sys/class/leds/led{0}/brightness' | |
def change(led, what): | |
''' | |
changes a given onboard `led` to the `what` brightness level | |
## Expected values | |
`led` | |
- `0` - activity LED | |
- `1` - power LED | |
`what` | |
- `0` - off | |
- `1` - on | |
''' | |
tf = open(filename.format(led), 'w') | |
tf.write(what) | |
tf.close() | |
def blink(led): | |
''' | |
blinks a given onboard `led` for 2 seconds | |
## Expected values | |
`led` | |
- `0` - activity LED | |
- `1` - power LED | |
''' | |
for i in range(10): | |
change(led, '1') | |
sleep(0.2) | |
change(led, '0') | |
sleep(0.2) | |
# start with the LEDs off | |
change(0, '0') | |
change(1, '0') | |
# build our configuration object | |
config = HpsServicesConfig() | |
config.secret_api_key = 'skapi_cert_MaePAQBr-1QAqjfckFC8FTbRTT120bVQUlfVOjgCBw' | |
config.version_number = '0000' | |
config.developer_id = '000000' | |
# create a new service object to call Portico | |
service = HpsCreditService(config) | |
# loop while waiting for input from card reader | |
while True: | |
# read input from keyboard | |
# the card reader sends track data + '\n' | |
track_data_string = raw_input("> ") | |
# build track data object to be passed to `service` | |
track_data = HpsTrackData() | |
track_data.value = track_data_string | |
track_data.method = 'swipe' | |
track_data.encryption_data = HpsEncryptionData() | |
track_data.encryption_data.version = '01' | |
try: | |
# attempt a charge (CreditSale) using the track data obtained from the reader | |
response = service.charge( | |
1.00, | |
'usd', | |
track_data | |
) | |
# log the transaction id | |
print response.transaction_id | |
# blink the activity LED (green) for success | |
blink(0) | |
except HpsException, e: | |
print "failure" | |
# blink the power LED (red) for failure | |
blink(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment