Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ nosetests.xml

# Sphinx documentation
docs/_build/

#Generated Files

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add space after start of comment

examples/*/success.csv
examples/*/errors.csv
15 changes: 13 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ cd examples/

### Create letters from CSV

An example showing how to validate and clean addresses from a CSV spreadsheet full of shipping addresses using Lob's [Address Verification API](https://lob.com/verification/address) and then using the cleaned, valid addresses to dynamically create sample billing letters with variable data using Lob's [Letter API](https://lob.com/services/letters).
An example showing how to dynamically create sample billing letters with variable data using Lob's [Letter API](https://lob.com/services/letters).

In order to run the program enter:

```
cd verify_and_create_letters_from_csv/
cd create_letters_from_csv/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about letters as part of this PR or another.

python letter.py input.csv
```

Expand All @@ -32,6 +32,17 @@ cd create_postcards_from_csv/
python create_postcards_from_csv.py input.csv
```

### Create checks from CSV

An example showing how to dynamically create checks from a CSV with variable data using Lob's [Postcard API](https://lob.com/services/postcards).

In order to run the program enter:

```
cd create_check_from_csv/
python check.py input.csv
```

### Verify addresses from CSV

An example showing how to validate and cleanse a CSV spreadsheet full of shipping addresses using Lob's [Address Verification API](https://lob.com/verification/address).
Expand Down
59 changes: 59 additions & 0 deletions examples/create_checks_from_csv/check.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
<title>Lob.com Sample Check</title>
<style>
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

body {
width: 8.5in;
height: 11in;
margin: 0;
padding: 0;
}

.text {
position: relative;
left: 20px;
top: 20px;
width: 6in;
font-family: 'Open Sans';
font-size: 30px;
}

#ink-free-area {
position: absolute;
left: 0;
top: 0;
width: 8.5in;
height: 3.625in;
}

#page-content {
position: relative;
width: 8.125in;
height: 7.1875in;
left: 0.1875in;
top: 3.625in;
}

@font-face {
font-family: 'Loved by the King';
src: url('https://s3-us-west-2.amazonaws.com/lob-assets/LovedbytheKing.ttf');
}
</style>
</head>

<body>
<div id="ink-free-area"></div>
<div id="page-content">
<h1>Demo check for {{name}}</h1>
</div>
</body>

</html>
108 changes: 108 additions & 0 deletions examples/create_checks_from_csv/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Usage
# python check.py input.csv
import sys,os

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space after ,

sys.path.insert(0, os.path.abspath(__file__+'../../../..'))

import lob
import csv

lob.api_key = 'test_fc26575412e92e22a926bc96c857f375f8b' # TODO Replace with your API key

skipFirstLine = True

# Column indices in CSV
name = 0
amount = 1
address_line1 = 2
address_line2 = 3
address_city = 4
address_state = 5
address_zip = 6
country = 'US'

try:
sys.argv[1]
except IndexError:
print "Please provide an input CSV file as an argument."
sys.exit()

# Open input files
inputFile = open(sys.argv[1], 'rU')
csvInput = csv.reader(inputFile)

# Create output files
errors = open(os.path.dirname(os.path.abspath(__file__)) + '/errors.csv', 'w')
success = open(os.path.dirname(os.path.abspath(__file__)) + '/success.csv', 'w')

# Creating a Bank Account using the previously created account_address

bankAccount = lob.BankAccount.create(
description = 'Example bank account',
routing_number = '122100024',
account_number = '0123456478',
account_type = 'company',
signatory = 'John Doe'
)

# Verifying a Bank Account with the microdeposit amounts

example_bank_account = lob.BankAccount.verify(
id = bankAccount.id,
amounts = [23, 77]
)

# Loop through input CSV rows
for idx, row in enumerate(csvInput):
if skipFirstLine and idx == 0:
continue

# Create letter for verified addresses
try:
createdCheck = lob.Check.create(
description = 'Check for ' + row[name],
metadata = {
'campaign': 'Bill pay',
'csv': inputFile.name
},
to_address = {
'name': row[name],
'address_line1': row[address_line1],
'address_line2': row[address_line2],
'address_city': row[address_city],
'address_zip': row[address_zip],
'address_state': row[address_state],
},
from_address = {
'name': 'Your Name/Company',
'address_line1': '123 Test Avenue',
'address_city': 'San Francisco',
'address_state': 'CA',
'address_zip': '94107',
},
bank_account = bankAccount,
amount = row[amount],
memo = 'Service payment',
check_bottom = open(os.path.dirname(os.path.abspath(__file__)) + '/check.html', 'r').read(),
data = {
'name': row[name],
},
logo = 'https://s3-us-west-2.amazonaws.com/lob-assets/lob_check_logo.png'
)
except Exception, e:
print "Error: " + str(e) + " in " + str(row)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment from @dmlittle regarding writing to errors.csv

else:
outputRow = createdCheck.id + ","
outputRow += createdCheck.url + ","
outputRow += row[name] + ","
outputRow += row[amount] + ","
outputRow += (createdCheck.to_address.address_line1 if createdCheck.to_address.address_line1 != None else " ") + ","
outputRow += (createdCheck.to_address.address_line2 if createdCheck.to_address.address_line2 != None else " ") + ","
outputRow += (createdCheck.to_address.address_city if createdCheck.to_address.address_city != None else " ") + ","
outputRow += (createdCheck.to_address.address_state if createdCheck.to_address.address_state != None else " ") + ","
outputRow += (createdCheck.to_address.address_zip if createdCheck.to_address.address_zip != None else " ") + "\n"
success.write(outputRow)


errors.close()
success.close()
print "\n"
8 changes: 8 additions & 0 deletions examples/create_letters_from_csv/input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name, amount,address1,address2,city,state,postcode
Kelly Jones,185.54,185 Berry St,,San Francisco,CA,94107
Margaret Smith,387.45,6575 W Rialto,,fresno,CA,93723
Jess Smith,19.87,537 Fillmore Street, Apt #2,San Francicso,ca,94117
Michael Thruman,347.21,1625 Post St,,San Francisco,CA,94115
Gerald Merritt,278.40,333 Post St,,San Francisco,CA,94108
Linda Anderson,425.40,655 Fake Address,Fake Apartment,Not a City,CA,06475-1246
John Travolta,175.84,111 NotAnAddress,,San Francisco,CA,94107
91 changes: 91 additions & 0 deletions examples/create_letters_from_csv/letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Usage
Copy link

@charlesbjohnson charlesbjohnson Aug 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these letters examples be part of this PR? The title says this is just for checks.

# python letter.py input.csv
import sys,os
sys.path.insert(0, os.path.abspath(__file__+'../../../..'))

import lob
import csv
import datetime

lob.api_key = 'test_fc26575412e92e22a926bc96c857f375f8b'

skipFirstLine = True

# Column indices in CSV
name = 0
amount = 1
address_line1 = 2
address_line2 = 3
address_city = 4
address_state = 5
address_zip = 6
country = 'US'

try:
sys.argv[1]
except IndexError:
print "Please provide an input CSV file as an argument."
sys.exit()

# Open input files
inputFile = open(sys.argv[1], 'rU')
csvInput = csv.reader(inputFile)

# Create output files
errors = open(os.path.dirname(os.path.abspath(__file__)) + '/errors.csv', 'w')
success = open(os.path.dirname(os.path.abspath(__file__)) + '/success.csv', 'w')

# Loop through input CSV rows
for idx, row in enumerate(csvInput):
if skipFirstLine and idx == 0:
continue

# Create letter for verified addresses
try:
createdLetter = lob.Letter.create(
description = 'Bill for ' + row[name],
metadata = {
'campaign': 'billing_statements',
'csv': inputFile.name
},
to_address = {
'name': row[name],
'address_line1': row[address_line1],
'address_line2': row[address_line2],
'address_city': row[address_city],
'address_zip': row[address_zip],
'address_state': row[address_state],
},
from_address = {
'name': 'Your Name/Company',
'address_line1': '123 Test Avenue',
'address_city': 'San Francisco',
'address_state': 'CA',
'address_zip': '94107',
},
file = open(os.path.dirname(os.path.abspath(__file__)) + '/letter.html', 'r').read(),
data = {
'date': datetime.datetime.now().strftime("%m/%d/%Y"),
'name': row[name],
'amountDue': row[amount]
},
color = True
)
except Exception, e:
print "Error: " + str(e) + " in " + str(row)
else:
outputRow = createdLetter.id + ","
outputRow += createdLetter.url + ","
outputRow += row[name] + ","
outputRow += row[amount] + ","
outputRow += (createdLetter.to_address.address_line1 if createdLetter.to_address.address_line1 != None else " ") + ","
outputRow += (createdLetter.to_address.address_line2 if createdLetter.to_address.address_line2 != None else " ") + ","
outputRow += (createdLetter.to_address.address_city if createdLetter.to_address.address_city != None else " ") + ","
outputRow += (createdLetter.to_address.address_state if createdLetter.to_address.address_state != None else " ") + ","
outputRow += (createdLetter.to_address.address_zip if createdLetter.to_address.address_zip != None else " ") + "\n"
success.write(outputRow)


errors.close()
success.close()
print "\n"
Loading