Add xps integration
This commit is contained in:
parent
4b31688c25
commit
520142fd62
21
core/xps.py
Normal file
21
core/xps.py
Normal file
@ -0,0 +1,21 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from . import ShippingContainer
|
||||
|
||||
api_key = "Gj0MZRxvpJz2YbWaroKAjstYGPIHf738"
|
||||
customer_id = "12375190"
|
||||
headers = {'Authorization': 'RSIS ' + api_key, 'Content-Type': 'application/json'}
|
||||
base_url = "https://xpsshipper.com/restapi/v1/customers/" + customer_id
|
||||
|
||||
|
||||
def get_quote(weight):
|
||||
resp = request("/quote", data)
|
||||
|
||||
|
||||
def request(url, data):
|
||||
r = requests.post(base_url + url, headers=headers, data=json.dumps(data))
|
||||
return r.json()
|
||||
|
||||
@ -8,7 +8,6 @@ from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.exceptions import ValidationError
|
||||
from localflavor.us.us_states import USPS_CHOICES
|
||||
from core.usps import USPSApi, Address
|
||||
from django_measurement.forms import MeasurementField
|
||||
|
||||
from core.models import (
|
||||
@ -86,38 +85,6 @@ class AddressForm(forms.Form):
|
||||
|
||||
return first_name, last_name
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
address = Address(
|
||||
name=quote(cleaned_data.get('full_name')),
|
||||
address_1=quote(cleaned_data.get('street_address_1')),
|
||||
address_2=quote(cleaned_data.get('street_address_2')),
|
||||
city=quote(cleaned_data.get('city')),
|
||||
state=quote(cleaned_data.get('state')),
|
||||
zipcode=quote(cleaned_data.get('postal_code'))
|
||||
)
|
||||
usps = USPSApi(SiteSettings.load().usps_user_id)
|
||||
|
||||
try:
|
||||
validation = usps.validate_address(address)
|
||||
except ConnectionError:
|
||||
raise ValidationError(
|
||||
'Could not connect to USPS, try again.'
|
||||
)
|
||||
|
||||
if 'Error' in validation.result['AddressValidateResponse']['Address']:
|
||||
error = validation.result['AddressValidateResponse']['Address']['Error']['Description']
|
||||
raise ValidationError(
|
||||
"USPS: " + error
|
||||
)
|
||||
|
||||
try:
|
||||
cleaned_data['postal_code'] = validation.result['AddressValidateResponse']['Address']['Zip5']
|
||||
except KeyError:
|
||||
raise ValidationError(
|
||||
'Could not find Zip5'
|
||||
)
|
||||
|
||||
|
||||
class CheckoutShippingForm(forms.Form):
|
||||
def __init__(self, containers, *args, **kwargs):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user