/usr/share/pyshared/boto/fps/connection.py is in python-boto 2.2.2-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | # Copyright (c) 2008 Chris Moyer http://coredumped.org/
# Copyringt (c) 2010 Jason R. Coombs http://www.jaraco.com/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import base64
import hmac
import hashlib
import urllib
import xml.sax
import uuid
import boto
import boto.utils
from boto import handler
from boto.connection import AWSQueryConnection
from boto.resultset import ResultSet
from boto.exception import FPSResponseError
class FPSConnection(AWSQueryConnection):
APIVersion = '2007-01-08'
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
is_secure=True, port=None, proxy=None, proxy_port=None,
proxy_user=None, proxy_pass=None,
host='fps.sandbox.amazonaws.com', debug=0,
https_connection_factory=None, path="/"):
AWSQueryConnection.__init__(self, aws_access_key_id,
aws_secret_access_key,
is_secure, port, proxy, proxy_port,
proxy_user, proxy_pass, host, debug,
https_connection_factory, path)
def _required_auth_capability(self):
return ['fps']
def install_payment_instruction(self, instruction,
token_type="Unrestricted",
transaction_id=None):
"""
InstallPaymentInstruction
instruction: The PaymentInstruction to send, for example:
MyRole=='Caller' orSay 'Roles do not match';
token_type: Defaults to "Unrestricted"
transaction_id: Defaults to a new ID
"""
if(transaction_id == None):
transaction_id = uuid.uuid4()
params = {}
params['PaymentInstruction'] = instruction
params['TokenType'] = token_type
params['CallerReference'] = transaction_id
response = self.make_request("InstallPaymentInstruction", params)
return response
def install_caller_instruction(self, token_type="Unrestricted",
transaction_id=None):
"""
Set us up as a caller
This will install a new caller_token into the FPS section.
This should really only be called to regenerate the caller token.
"""
response = self.install_payment_instruction("MyRole=='Caller';",
token_type=token_type,
transaction_id=transaction_id)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
caller_token = rs.TokenId
try:
boto.config.save_system_option("FPS", "caller_token",
caller_token)
except(IOError):
boto.config.save_user_option("FPS", "caller_token",
caller_token)
return caller_token
else:
raise FPSResponseError(response.status, response.reason, body)
def install_recipient_instruction(self, token_type="Unrestricted",
transaction_id=None):
"""
Set us up as a Recipient
This will install a new caller_token into the FPS section.
This should really only be called to regenerate the recipient token.
"""
response = self.install_payment_instruction("MyRole=='Recipient';",
token_type=token_type,
transaction_id=transaction_id)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
recipient_token = rs.TokenId
try:
boto.config.save_system_option("FPS", "recipient_token",
recipient_token)
except(IOError):
boto.config.save_user_option("FPS", "recipient_token",
recipient_token)
return recipient_token
else:
raise FPSResponseError(response.status, response.reason, body)
def make_marketplace_registration_url(self, returnURL, pipelineName,
maxFixedFee=0.0, maxVariableFee=0.0,
recipientPaysFee=True, **params):
"""
Generate the URL with the signature required for signing up a recipient
"""
# use the sandbox authorization endpoint if we're using the
# sandbox for API calls.
endpoint_host = 'authorize.payments.amazon.com'
if 'sandbox' in self.host:
endpoint_host = 'authorize.payments-sandbox.amazon.com'
base = "/cobranded-ui/actions/start"
params['callerKey'] = str(self.aws_access_key_id)
params['returnURL'] = str(returnURL)
params['pipelineName'] = str(pipelineName)
params['maxFixedFee'] = str(maxFixedFee)
params['maxVariableFee'] = str(maxVariableFee)
params['recipientPaysFee'] = str(recipientPaysFee)
params["signatureMethod"] = 'HmacSHA256'
params["signatureVersion"] = '2'
if(not params.has_key('callerReference')):
params['callerReference'] = str(uuid.uuid4())
parts = ''
for k in sorted(params.keys()):
parts += "&%s=%s" % (k, urllib.quote(params[k], '~'))
canonical = '\n'.join(['GET',
str(endpoint_host).lower(),
base,
parts[1:]])
signature = self._auth_handler.sign_string(canonical)
params["signature"] = signature
urlsuffix = ''
for k in sorted(params.keys()):
urlsuffix += "&%s=%s" % (k, urllib.quote(params[k], '~'))
urlsuffix = urlsuffix[1:] # strip the first &
fmt = "https://%(endpoint_host)s%(base)s?%(urlsuffix)s"
final = fmt % vars()
return final
def make_url(self, returnURL, paymentReason, pipelineName,
transactionAmount, **params):
"""
Generate the URL with the signature required for a transaction
"""
# use the sandbox authorization endpoint if we're using the
# sandbox for API calls.
endpoint_host = 'authorize.payments.amazon.com'
if 'sandbox' in self.host:
endpoint_host = 'authorize.payments-sandbox.amazon.com'
base = "/cobranded-ui/actions/start"
params['callerKey'] = str(self.aws_access_key_id)
params['returnURL'] = str(returnURL)
params['paymentReason'] = str(paymentReason)
params['pipelineName'] = pipelineName
params['transactionAmount'] = transactionAmount
params["signatureMethod"] = 'HmacSHA256'
params["signatureVersion"] = '2'
if(not params.has_key('callerReference')):
params['callerReference'] = str(uuid.uuid4())
parts = ''
for k in sorted(params.keys()):
parts += "&%s=%s" % (k, urllib.quote(params[k], '~'))
canonical = '\n'.join(['GET',
str(endpoint_host).lower(),
base,
parts[1:]])
signature = self._auth_handler.sign_string(canonical)
params["signature"] = signature
urlsuffix = ''
for k in sorted(params.keys()):
urlsuffix += "&%s=%s" % (k, urllib.quote(params[k], '~'))
urlsuffix = urlsuffix[1:] # strip the first &
fmt = "https://%(endpoint_host)s%(base)s?%(urlsuffix)s"
final = fmt % vars()
return final
def pay(self, transactionAmount, senderTokenId,
recipientTokenId=None, callerTokenId=None,
chargeFeeTo="Recipient",
callerReference=None, senderReference=None, recipientReference=None,
senderDescription=None, recipientDescription=None,
callerDescription=None, metadata=None,
transactionDate=None, reserve=False):
"""
Make a payment transaction. You must specify the amount.
This can also perform a Reserve request if 'reserve' is set to True.
"""
params = {}
params['SenderTokenId'] = senderTokenId
# this is for 2008-09-17 specification
params['TransactionAmount.Amount'] = str(transactionAmount)
params['TransactionAmount.CurrencyCode'] = "USD"
#params['TransactionAmount'] = str(transactionAmount)
params['ChargeFeeTo'] = chargeFeeTo
params['RecipientTokenId'] = (
recipientTokenId if recipientTokenId is not None
else boto.config.get("FPS", "recipient_token")
)
params['CallerTokenId'] = (
callerTokenId if callerTokenId is not None
else boto.config.get("FPS", "caller_token")
)
if(transactionDate != None):
params['TransactionDate'] = transactionDate
if(senderReference != None):
params['SenderReference'] = senderReference
if(recipientReference != None):
params['RecipientReference'] = recipientReference
if(senderDescription != None):
params['SenderDescription'] = senderDescription
if(recipientDescription != None):
params['RecipientDescription'] = recipientDescription
if(callerDescription != None):
params['CallerDescription'] = callerDescription
if(metadata != None):
params['MetaData'] = metadata
if(callerReference == None):
callerReference = uuid.uuid4()
params['CallerReference'] = callerReference
if reserve:
response = self.make_request("Reserve", params)
else:
response = self.make_request("Pay", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def get_transaction_status(self, transactionId):
"""
Returns the status of a given transaction.
"""
params = {}
params['TransactionId'] = transactionId
response = self.make_request("GetTransactionStatus", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def cancel(self, transactionId, description=None):
"""
Cancels a reserved or pending transaction.
"""
params = {}
params['transactionId'] = transactionId
if(description != None):
params['description'] = description
response = self.make_request("Cancel", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def settle(self, reserveTransactionId, transactionAmount=None):
"""
Charges for a reserved payment.
"""
params = {}
params['ReserveTransactionId'] = reserveTransactionId
if(transactionAmount != None):
params['TransactionAmount'] = transactionAmount
response = self.make_request("Settle", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def refund(self, callerReference, transactionId, refundAmount=None,
callerDescription=None):
"""
Refund a transaction. This refunds the full amount by default
unless 'refundAmount' is specified.
"""
params = {}
params['CallerReference'] = callerReference
params['TransactionId'] = transactionId
if(refundAmount != None):
params['RefundAmount'] = refundAmount
if(callerDescription != None):
params['CallerDescription'] = callerDescription
response = self.make_request("Refund", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def get_recipient_verification_status(self, recipientTokenId):
"""
Test that the intended recipient has a verified Amazon Payments account.
"""
params ={}
params['RecipientTokenId'] = recipientTokenId
response = self.make_request("GetRecipientVerificationStatus", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def get_token_by_caller_reference(self, callerReference):
"""
Returns details about the token specified by 'CallerReference'.
"""
params ={}
params['CallerReference'] = callerReference
response = self.make_request("GetTokenByCaller", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def get_token_by_caller_token(self, tokenId):
"""
Returns details about the token specified by 'TokenId'.
"""
params ={}
params['TokenId'] = tokenId
response = self.make_request("GetTokenByCaller", params)
body = response.read()
if(response.status == 200):
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise FPSResponseError(response.status, response.reason, body)
def verify_signature(self, end_point_url, http_parameters):
params = dict(
UrlEndPoint = end_point_url,
HttpParameters = http_parameters,
)
response = self.make_request("VerifySignature", params)
body = response.read()
if(response.status != 200):
raise FPSResponseError(response.status, response.reason, body)
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
|