Add xps integration

This commit is contained in:
Nathan Chapman 2023-11-17 18:59:06 -07:00
parent 4b31688c25
commit 520142fd62
No known key found for this signature in database
GPG Key ID: 22E944DA99EBFAA3
2 changed files with 21 additions and 33 deletions

21
core/xps.py Normal file
View 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()

View File

@ -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):