def make_402_payment(self, response, max_price):
"""Make a bit-transfer payment to the payment-handling service."""
# Retrieve payment headers
headers = response.headers
price = headers.get(BitTransferRequests.HTTP_BITCOIN_PRICE)
payee_address = headers.get(BitTransferRequests.HTTP_BITCOIN_ADDRESS)
payee_username = headers.get(BitTransferRequests.HTTP_BITCOIN_USERNAME)
# Verify that the payment method is supported
if price is None or payee_address is None or payee_username is None:
raise UnsupportedPaymentMethodError(
'Resource does not support that payment method.')
# Convert string headers into correct data types
price = int(price)
# verify that we have the money to purchase the resource
buffer_balance = self.client.get_earnings()["total_earnings"]
if price > buffer_balance:
insuff_funds_err = 'Resource price ({}) exceeds buffer balance ({}).'
raise InsufficientBalanceError(insuff_funds_err.format(price, buffer_balance))
# Verify resource cost against our budget
if max_price and price > max_price:
max_price_err = 'Resource price ({}) exceeds max price ({}).'
raise ResourcePriceGreaterThanMaxPriceError(max_price_err.format(price, max_price))
# Get the signing public key
pubkey = self.wallet.get_public_key()
compressed_pubkey = codecs.encode(pubkey.compressed_bytes, 'base64').decode()
# Create and sign BitTranfer
bittransfer = json.dumps({
'payer': self.username,
'payer_pubkey': compressed_pubkey,
'payee_address': payee_address,
'payee_username': payee_username,
'amount': price,
'timestamp': time.time(),
'description': response.url
})
if not isinstance(bittransfer, str):
raise TypeError("Serialized bittransfer must be a string")
signature = self.wallet.sign_message(bittransfer)
logger.debug('[BitTransferRequests] Signature: {}'.format(signature))
logger.debug('[BitTransferRequests] BitTransfer: {}'.format(bittransfer))
return {
'Bitcoin-Transfer': bittransfer,
'Authorization': signature
}
评论列表
文章目录