| 1 | package Net::Braintree::WebhookTestingGateway; | ||
| 2 | |||
| 3 | use MIME::Base64; | ||
| 4 | use POSIX qw(strftime); | ||
| 5 | use Net::Braintree::Digest qw(hexdigest); | ||
| 6 | use Net::Braintree::WebhookNotification::Kind; | ||
| 7 | use Moose; | ||
| 8 | |||
| 9 | has 'gateway' => (is => 'ro'); | ||
| 10 | |||
| 11 | sub sample_notification { | ||
| 12 | my ($self, $kind, $id) = @_; | ||
| 13 | |||
| 14 | my $sample_xml = $self->_sample_xml($kind, $id); | ||
| 15 | my $payload = encode_base64($sample_xml); | ||
| 16 | my $signature = $self->gateway->config->public_key . "|" . hexdigest($self->gateway->config->private_key, $payload); | ||
| 17 | |||
| 18 | return ($signature, $payload); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub _sample_xml { | ||
| 22 | my ($self, $kind, $id) = @_; | ||
| 23 | my $subject_sample_xml = $self->_subject_sample_xml($kind, $id); | ||
| 24 | my $timestamp = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()); | ||
| 25 | |||
| 26 | return <<XML | ||
| 27 | <notification> | ||
| 28 | <timestamp type="datetime">$timestamp</timestamp> | ||
| 29 | <kind>$kind</kind> | ||
| 30 | <subject>$subject_sample_xml</subject> | ||
| 31 | </notification> | ||
| 32 | XML | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _subject_sample_xml { | ||
| 36 | my ($self, $kind, $id) = @_; | ||
| 37 | |||
| 38 | my $dispatch = { | ||
| 39 | Net::Braintree::WebhookNotification::Kind::TransactionDisbursed => sub { _transaction_disbursed_sample_xml(@_) }, | ||
| 40 | Net::Braintree::WebhookNotification::Kind::DisbursementException => sub { _disbursement_exception_sample_xml(@_) }, | ||
| 41 | Net::Braintree::WebhookNotification::Kind::Disbursement => sub { _disbursement_sample_xml(@_) }, | ||
| 42 | Net::Braintree::WebhookNotification::Kind::SubMerchantAccountApproved => sub { _merchant_account_approved_sample_xml(@_) }, | ||
| 43 | Net::Braintree::WebhookNotification::Kind::SubMerchantAccountDeclined => sub { _merchant_account_declined_sample_xml(@_) }, | ||
| 44 | Net::Braintree::WebhookNotification::Kind::PartnerMerchantConnected => sub { _partner_merchant_connected_sample_xml(@_) }, | ||
| 45 | Net::Braintree::WebhookNotification::Kind::PartnerMerchantDisconnected => sub { _partner_merchant_disconnected_sample_xml(@_) }, | ||
| 46 | Net::Braintree::WebhookNotification::Kind::PartnerMerchantDeclined => sub { _partner_merchant_declined_sample_xml(@_) }, | ||
| 47 | Net::Braintree::WebhookNotification::Kind::DisputeOpened => sub { _dispute_opened_sample_xml(@_) }, | ||
| 48 | Net::Braintree::WebhookNotification::Kind::DisputeLost => sub { _dispute_lost_sample_xml(@_) }, | ||
| 49 | Net::Braintree::WebhookNotification::Kind::DisputeWon => sub { _dispute_won_sample_xml(@_) } | ||
| 50 | }; | ||
| 51 | |||
| 52 | my $templater = $dispatch->{$kind} || sub { _subscription_sample_xml(@_) }; | ||
| 53 | |||
| 54 | return $templater->($id); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _transaction_disbursed_sample_xml { | ||
| 58 | my $id = shift; | ||
| 59 | |||
| 60 | return <<XML | ||
| 61 | <transaction> | ||
| 62 | <id>$id</id> | ||
| 63 | <amount>100</amount> | ||
| 64 | <disbursement-details> | ||
| 65 | <disbursement-date type="date">2013-07-09</disbursement-date> | ||
| 66 | </disbursement-details> | ||
| 67 | </transaction> | ||
| 68 | XML | ||
| 69 | }; | ||
| 70 | |||
| 71 | sub _dispute_opened_sample_xml { | ||
| 72 | my $id = shift; | ||
| 73 | |||
| 74 | return <<XML | ||
| 75 | <dispute> | ||
| 76 | <amount>250.00</amount> | ||
| 77 | <currency-iso-code>USD</currency-iso-code> | ||
| 78 | <received-date type="date">2014-03-01</received-date> | ||
| 79 | <reply-by-date type="date">2014-03-21</reply-by-date> | ||
| 80 | <status>open</status> | ||
| 81 | <reason>fraud</reason> | ||
| 82 | <id>$id</id> | ||
| 83 | <transaction> | ||
| 84 | <id>$id</id> | ||
| 85 | <amount>250.00</amount> | ||
| 86 | </transaction> | ||
| 87 | </dispute> | ||
| 88 | XML | ||
| 89 | }; | ||
| 90 | |||
| 91 | sub _dispute_lost_sample_xml { | ||
| 92 | my $id = shift; | ||
| 93 | |||
| 94 | return <<XML | ||
| 95 | <dispute> | ||
| 96 | <amount>250.00</amount> | ||
| 97 | <currency-iso-code>USD</currency-iso-code> | ||
| 98 | <received-date type="date">2014-03-01</received-date> | ||
| 99 | <reply-by-date type="date">2014-03-21</reply-by-date> | ||
| 100 | <status>lost</status> | ||
| 101 | <reason>fraud</reason> | ||
| 102 | <id>$id</id> | ||
| 103 | <transaction> | ||
| 104 | <id>$id</id> | ||
| 105 | <amount>250.00</amount> | ||
| 106 | </transaction> | ||
| 107 | </dispute> | ||
| 108 | XML | ||
| 109 | }; | ||
| 110 | |||
| 111 | sub _dispute_won_sample_xml { | ||
| 112 | my $id = shift; | ||
| 113 | |||
| 114 | return <<XML | ||
| 115 | <dispute> | ||
| 116 | <amount>250.00</amount> | ||
| 117 | <currency-iso-code>USD</currency-iso-code> | ||
| 118 | <received-date type="date">2014-03-01</received-date> | ||
| 119 | <reply-by-date type="date">2014-03-21</reply-by-date> | ||
| 120 | <status>won</status> | ||
| 121 | <reason>fraud</reason> | ||
| 122 | <id>$id</id> | ||
| 123 | <transaction> | ||
| 124 | <id>$id</id> | ||
| 125 | <amount>250.00</amount> | ||
| 126 | </transaction> | ||
| 127 | </dispute> | ||
| 128 | XML | ||
| 129 | }; | ||
| 130 | |||
| 131 | sub _disbursement_exception_sample_xml { | ||
| 132 | my $id = shift; | ||
| 133 | |||
| 134 | return <<XML | ||
| 135 | <disbursement> | ||
| 136 | <id>$id</id> | ||
| 137 | <transaction-ids type="array"> | ||
| 138 | <item>afv56j</item> | ||
| 139 | <item>kj8hjk</item> | ||
| 140 | </transaction-ids> | ||
| 141 | <success type="boolean">false</success> | ||
| 142 | <retry type="boolean">false</retry> | ||
| 143 | <merchant-account> | ||
| 144 | <id>merchant_account_token</id> | ||
| 145 | <currency-iso-code>USD</currency-iso-code> | ||
| 146 | <sub-merchant-account type="boolean">false</sub-merchant-account> | ||
| 147 | <status>active</status> | ||
| 148 | </merchant-account> | ||
| 149 | <amount>100.00</amount> | ||
| 150 | <disbursement-date type="date">2014-02-10</disbursement-date> | ||
| 151 | <exception-message>bank_rejected</exception-message> | ||
| 152 | <follow-up-action>update_funding_information</follow-up-action> | ||
| 153 | </disbursement> | ||
| 154 | XML | ||
| 155 | }; | ||
| 156 | |||
| 157 | sub _disbursement_sample_xml { | ||
| 158 | my $id = shift; | ||
| 159 | |||
| 160 | return <<XML | ||
| 161 | <disbursement> | ||
| 162 | <id>$id</id> | ||
| 163 | <transaction-ids type="array"> | ||
| 164 | <item>afv56j</item> | ||
| 165 | <item>kj8hjk</item> | ||
| 166 | </transaction-ids> | ||
| 167 | <success type="boolean">true</success> | ||
| 168 | <retry type="boolean">false</retry> | ||
| 169 | <merchant-account> | ||
| 170 | <id>merchant_account_token</id> | ||
| 171 | <currency-iso-code>USD</currency-iso-code> | ||
| 172 | <sub-merchant-account type="boolean">false</sub-merchant-account> | ||
| 173 | <status>active</status> | ||
| 174 | </merchant-account> | ||
| 175 | <amount>100.00</amount> | ||
| 176 | <disbursement-date type="date">2014-02-10</disbursement-date> | ||
| 177 | <exception-message nil="true"/> | ||
| 178 | <follow-up-action nil="true"/> | ||
| 179 | </disbursement> | ||
| 180 | XML | ||
| 181 | }; | ||
| 182 | |||
| 183 | sub _merchant_account_approved_sample_xml { | ||
| 184 | my $id = shift; | ||
| 185 | |||
| 186 | return <<XML | ||
| 187 | <merchant_account> | ||
| 188 | <id>$id</id> | ||
| 189 | <master_merchant_account> | ||
| 190 | <id>master_ma_for_$id</id> | ||
| 191 | <status>active</status> | ||
| 192 | </master_merchant_account> | ||
| 193 | <status>active</status> | ||
| 194 | </merchant_account> | ||
| 195 | XML | ||
| 196 | }; | ||
| 197 | |||
| 198 | sub _merchant_account_declined_sample_xml { | ||
| 199 | my $id = shift; | ||
| 200 | |||
| 201 | return <<XML | ||
| 202 | <api-error-response> | ||
| 203 | <message>Credit score is too low</message> | ||
| 204 | <errors> | ||
| 205 | <errors type="array"/> | ||
| 206 | <merchant-account> | ||
| 207 | <errors type="array"> | ||
| 208 | <error> | ||
| 209 | <code>82621</code> | ||
| 210 | <message>Credit score is too low</message> | ||
| 211 | <attribute type="symbol">base</attribute> | ||
| 212 | </error> | ||
| 213 | </errors> | ||
| 214 | </merchant-account> | ||
| 215 | </errors> | ||
| 216 | <merchant-account> | ||
| 217 | <id>$id</id> | ||
| 218 | <status>suspended</status> | ||
| 219 | <master-merchant-account> | ||
| 220 | <id>master_ma_for_$id</id> | ||
| 221 | <status>suspended</status> | ||
| 222 | </master-merchant-account> | ||
| 223 | </merchant-account> | ||
| 224 | </api-error-response> | ||
| 225 | XML | ||
| 226 | }; | ||
| 227 | |||
| 228 | sub _subscription_sample_xml { | ||
| 229 | my $id = shift; | ||
| 230 | |||
| 231 | return <<XML | ||
| 232 | <subscription> | ||
| 233 | <id>$id</id> | ||
| 234 | <transactions type="array"> | ||
| 235 | </transactions> | ||
| 236 | <add_ons type="array"> | ||
| 237 | </add_ons> | ||
| 238 | <discounts type="array"> | ||
| 239 | </discounts> | ||
| 240 | </subscription> | ||
| 241 | XML | ||
| 242 | } | ||
| 243 | |||
| 244 | sub _partner_merchant_connected_sample_xml { | ||
| 245 | return <<XML | ||
| 246 | <partner-merchant> | ||
| 247 | <merchant-public-id>public_id</merchant-public-id> | ||
| 248 | <public-key>public_key</public-key> | ||
| 249 | <private-key>private_key</private-key> | ||
| 250 | <partner-merchant-id>abc123</partner-merchant-id> | ||
| 251 | <client-side-encryption-key>cse_key</client-side-encryption-key> | ||
| 252 | </partner-merchant> | ||
| 253 | XML | ||
| 254 | } | ||
| 255 | |||
| 256 | sub _partner_merchant_disconnected_sample_xml { | ||
| 257 | return <<XML | ||
| 258 | <partner-merchant> | ||
| 259 | <partner-merchant-id>abc123</partner-merchant-id> | ||
| 260 | </partner-merchant> | ||
| 261 | XML | ||
| 262 | } | ||
| 263 | |||
| 264 | sub _partner_merchant_declined_sample_xml { | ||
| 265 | return <<XML | ||
| 266 | <partner-merchant> | ||
| 267 | <partner-merchant-id>abc123</partner-merchant-id> | ||
| 268 | </partner-merchant> | ||
| 269 | XML | ||
| 270 | } | ||
| 271 | |||
| 272 | 1; |