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
|
from braintree.util.crypto import Crypto
from braintree.webhook_notification import WebhookNotification
import base64
from datetime import datetime
class WebhookTestingGateway(object):
def __init__(self, gateway):
self.gateway = gateway
self.config = gateway.config
def sample_notification(self, kind, id):
payload = base64.encodestring(self.__sample_xml(kind, id))
hmac_payload = Crypto.sha1_hmac_hash(self.gateway.config.private_key, payload)
signature = "%s|%s" % (self.gateway.config.public_key, hmac_payload)
return signature, payload
def __sample_xml(self, kind, id):
timestamp = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
sample_xml = """
<notification>
<timestamp type="datetime">%s</timestamp>
<kind>%s</kind>
<subject>%s</subject>
</notification>
""" % (timestamp, kind, self.__subject_sample_xml(kind, id))
return sample_xml.encode('utf-8')
def __subject_sample_xml(self, kind, id):
if kind == WebhookNotification.Kind.SubMerchantAccountApproved:
return self.__merchant_account_approved_sample_xml(id)
elif kind == WebhookNotification.Kind.SubMerchantAccountDeclined:
return self.__merchant_account_declined_sample_xml(id)
elif kind == WebhookNotification.Kind.TransactionDisbursed:
return self.__transaction_disbursed_sample_xml(id)
elif kind == WebhookNotification.Kind.PartnerMerchantConnected:
return self.__partner_merchant_connected_sample_xml()
elif kind == WebhookNotification.Kind.PartnerMerchantDisconnected:
return self.__partner_merchant_disconnected_sample_xml()
elif kind == WebhookNotification.Kind.PartnerMerchantDeclined:
return self.__partner_merchant_declined_sample_xml()
elif kind == WebhookNotification.Kind.DisbursementException:
return self.__disbursement_exception_sample_xml(id)
elif kind == WebhookNotification.Kind.Disbursement:
return self.__disbursement_sample_xml(id)
elif kind == WebhookNotification.Kind.DisputeOpened:
return self.__dispute_opened_sample_xml(id)
elif kind == WebhookNotification.Kind.DisputeLost:
return self.__dispute_lost_sample_xml(id)
elif kind == WebhookNotification.Kind.DisputeWon:
return self.__dispute_won_sample_xml(id)
else:
return self.__subscription_sample_xml(id)
def __transaction_disbursed_sample_xml(self, id):
return """
<transaction>
<id>%s</id>
<amount>100</amount>
<tax-amount>10</tax-amount>
<disbursement-details>
<settlement-amount>100</settlement-amount>
<settlement-currency-exchange-rate>10</settlement-currency-exchange-rate>
<disbursement-date type="datetime">2013-07-09T18:23:29Z</disbursement-date>
</disbursement-details>
</transaction>
""" % id
def __disbursement_exception_sample_xml(self, id):
return """
<disbursement>
<id>%s</id>
<transaction-ids type="array">
<item>afv56j</item>
<item>kj8hjk</item>
</transaction-ids>
<success type="boolean">false</success>
<retry type="boolean">false</retry>
<merchant-account>
<id>merchant_account_token</id>
<currency-iso-code>USD</currency-iso-code>
<sub-merchant-account type="boolean">false</sub-merchant-account>
<status>active</status>
</merchant-account>
<amount>100.00</amount>
<disbursement-date type="date">2014-02-09</disbursement-date>
<exception-message>bank_rejected</exception-message>
<follow-up-action>update_funding_information</follow-up-action>
</disbursement>
""" % id
def __disbursement_sample_xml(self, id):
return """
<disbursement>
<id>%s</id>
<transaction-ids type="array">
<item>afv56j</item>
<item>kj8hjk</item>
</transaction-ids>
<success type="boolean">true</success>
<retry type="boolean">false</retry>
<merchant-account>
<id>merchant_account_token</id>
<currency-iso-code>USD</currency-iso-code>
<sub-merchant-account type="boolean">false</sub-merchant-account>
<status>active</status>
</merchant-account>
<amount>100.00</amount>
<disbursement-date type="date">2014-02-09</disbursement-date>
<exception-message nil="true"/>
<follow-up-action nil="true"/>
</disbursement>
""" % id
def __dispute_opened_sample_xml(self, id):
return """
<dispute>
<amount>250.00</amount>
<currency-iso-code>USD</currency-iso-code>
<received-date type="date">2014-03-01</received-date>
<reply-by-date type="date">2014-03-21</reply-by-date>
<status>open</status>
<reason>fraud</reason>
<id>%s</id>
<transaction>
<id>%s</id>
<amount>250.00</amount>
</transaction>
</dispute>
""" % (id, id)
def __dispute_lost_sample_xml(self, id):
return """
<dispute>
<amount>250.00</amount>
<currency-iso-code>USD</currency-iso-code>
<received-date type="date">2014-03-01</received-date>
<reply-by-date type="date">2014-03-21</reply-by-date>
<status>lost</status>
<reason>fraud</reason>
<id>%s</id>
<transaction>
<id>%s</id>
<amount>250.00</amount>
</transaction>
</dispute>
""" % (id, id)
def __dispute_won_sample_xml(self, id):
return """
<dispute>
<amount>250.00</amount>
<currency-iso-code>USD</currency-iso-code>
<received-date type="date">2014-03-01</received-date>
<reply-by-date type="date">2014-03-21</reply-by-date>
<status>won</status>
<reason>fraud</reason>
<id>%s</id>
<transaction>
<id>%s</id>
<amount>250.00</amount>
</transaction>
</dispute>
""" % (id, id)
def __subscription_sample_xml(self, id):
return """
<subscription>
<id>%s</id>
<transactions type="array"></transactions>
<add_ons type="array"></add_ons>
<discounts type="array"></discounts>
</subscription>
""" % id
def __merchant_account_approved_sample_xml(self, id):
return """
<merchant-account>
<id>%s</id>
<status>active</status>
<master-merchant-account>
<id>master_ma_for_%s</id>
<status>active</status>
</master-merchant-account>
</merchant-account>
""" % (id, id)
def __merchant_account_declined_sample_xml(self, id):
return """
<api-error-response>
<message>Credit score is too low</message>
<errors>
<errors type="array"/>
<merchant-account>
<errors type="array">
<error>
<code>82621</code>
<message>Credit score is too low</message>
<attribute type="symbol">base</attribute>
</error>
</errors>
</merchant-account>
</errors>
<merchant-account>
<id>%s</id>
<status>suspended</status>
<master-merchant-account>
<id>master_ma_for_%s</id>
<status>suspended</status>
</master-merchant-account>
</merchant-account>
</api-error-response>
""" % (id, id)
def __partner_merchant_connected_sample_xml(self):
return """
<partner-merchant>
<partner-merchant-id>abc123</partner-merchant-id>
<public-key>public_key</public-key>
<private-key>private_key</private-key>
<merchant-public-id>public_id</merchant-public-id>
<client-side-encryption-key>cse_key</client-side-encryption-key>
</partner-merchant>
"""
def __partner_merchant_disconnected_sample_xml(self):
return """
<partner-merchant>
<partner-merchant-id>abc123</partner-merchant-id>
</partner-merchant>
"""
def __partner_merchant_declined_sample_xml(self):
return """
<partner-merchant>
<partner-merchant-id>abc123</partner-merchant-id>
</partner-merchant>
"""
|