use
5.010001;
our
$VERSION
=
'0.05'
;
};
sub
new
{
my
$class
=
shift
;
my
$key
=
shift
||
undef
;
my
$secret
=
shift
||
undef
;
my
$redirect_uri
=
shift
||
undef
;
my
$permissions
=
shift
|| [
'send'
,
'transactions'
,
'balance'
,
'request'
,
'contacts'
,
'accountinfofull'
,
'funding'
];
my
$mode
=
shift
||
'live'
;
my
$debug_mode
=
shift
|| 0;
my
$self
= {};
$self
->{
'api_key'
} =
$key
;
$self
->{
'api_secret'
} =
$secret
;
$self
->{
'permissions'
} =
$permissions
;
$self
->{
'mode'
} =
$mode
;
$self
->{
'errors'
} = [];
$self
->{
'redirect_uri'
} =
$redirect_uri
;
$self
->{
'debug_mode'
} =
$debug_mode
;
bless
(
$self
,
$class
);
return
$self
;
}
sub
set_api_config_from_file
{
my
$self
=
shift
;
my
$file
=
shift
;
my
$config
= IO::File->new(
$file
,
'r'
);
if
(!
defined
(
$config
)) {
return
0;
}
while
(!
$config
->
eof
()) {
my
$line
=
$config
->getline();
$line
=~ s/\n//g;
my
(
$key
,
$value
) =
split
(/\=/,
$line
);
if
(
$key
eq
'key'
) {
$self
->{
'api_key'
} =
$value
;
}
if
(
$key
eq
'secret'
) {
$self
->{
'api_secret'
} =
$value
;
}
if
(
$key
eq
'token'
) {
$self
->{
'oauth_token'
} =
$value
;
}
}
$config
->
close
();
}
sub
get_auth_url
{
my
$self
=
shift
;
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'response_type'
=>
'code'
,
'scope'
=>
join
(
'|'
,@{
$self
->{
'permissions'
}})
};
if
(
defined
(
$self
->{
'redirect_uri'
})) {
$params
->{
'redirect_uri'
} =
$self
->{
'redirect_uri'
};
}
my
$url
= AUTH_URL .
'/?'
.
$self
->_http_build_query(
$params
);
return
$url
;
}
sub
request_token
{
my
$self
=
shift
;
my
$code
=
shift
;
if
(!
defined
(
$code
)) {
$self
->set_error(
'Please pass a valid OAuth code.'
);
return
0;
}
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
'redirect_uri'
=>
$self
->{
'redirect_uri'
},
'grant_type'
=>
'authorization_code'
,
'code'
=>
$code
};
my
$url
= TOKEN_URL .
'?'
.
$self
->_http_build_query(
$params
);
my
$response
=
$self
->_api_request(
$url
,
'GET'
);
if
(
$response
->{
'error'
}) {
$self
->set_error(
$response
->{
'error_description'
});
return
0;
}
return
$response
->{
'access_token'
};
}
sub
set_token
{
my
$self
=
shift
;
my
$token
=
shift
;
$self
->{
'oauth_token'
} =
$token
;
}
sub
set_mode
{
my
$self
=
shift
;
my
$mode
=
shift
;
if
(
$mode
ne
'test'
&&
$mode
ne
'live'
) {
$self
->set_error(
"Invalid mode. Please use 'test' / 'live'."
);
return
0;
}
$self
->{
'mode'
} =
$mode
;
}
sub
get_mode
{
my
$self
=
shift
;
return
$self
->{
'mode'
};
}
sub
get_token
{
my
$self
=
shift
;
return
$self
->{
'oauth_token'
};
}
sub
me
{
my
$self
=
shift
;
my
$response
=
$self
->_get(
"users"
);
return
$response
;
}
sub
get_user
{
my
$self
=
shift
;
my
$id
=
shift
;
if
(!
$self
->is_id_valid(
$id
)) {
}
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
};
my
$response
=
$self
->_get(
"users/$id"
,
$params
);
return
$response
;
}
sub
users_nearby
{
my
$self
=
shift
;
my
$lat
=
shift
;
my
$long
=
shift
;
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
'latitude'
=>
$lat
,
'longitude'
=>
$long
};
my
$response
=
$self
->_get(
"users/nearby"
,
$params
);
return
$response
;
}
sub
register
{
my
$self
=
shift
;
my
$email
=
shift
;
my
$password
=
shift
;
my
$pin
=
shift
;
my
$first_name
=
shift
;
my
$last_name
=
shift
;
my
$address
=
shift
;
my
$address2
=
shift
||
''
;
my
$city
=
shift
;
my
$state
=
shift
;
my
$zip
=
shift
;
my
$phone
=
shift
;
my
$date_of_birth
=
shift
;
my
$accept_terms
=
shift
|| 0;
my
$type
=
shift
||
'Personal'
;
my
$organization
=
shift
||
''
;
my
$ein
=
shift
||
undef
;
my
$errors
= 0;
if
(
$type
ne
'Personal'
&&
$type
ne
'Commercial'
&&
$type
ne
'NonProfit'
) {
$self
->set_error(
"Please enter a valid account type."
);
$errors
++;
}
if
(!
defined
(
$date_of_birth
) ||
$date_of_birth
!~ /^\d{2}\-\d{2}\-\d{4}$/) {
$self
->set_error(
"Please enter a valid date of birth."
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
'email'
=>
$email
,
'password'
=>
$password
,
'pin'
=>
$pin
,
'firstName'
=>
$first_name
,
'lastName'
=>
$last_name
,
'address'
=>
$address
,
'address2'
=>
$address2
,
'city'
=>
$city
,
'state'
=>
$state
,
'zip'
=>
$zip
,
'phone'
=>
$phone
,
'dateOfBirth'
=>
$date_of_birth
,
'type'
=>
$type
,
'organization'
=>
$organization
,
'ein'
=>
$ein
,
'acceptTerms'
=>
$accept_terms
};
my
$response
=
$self
->_post(
"register/"
,
$params
,0);
return
$response
;
}
sub
contacts
{
my
$self
=
shift
;
my
$search
=
shift
;
my
$types
=
shift
|| [
'Dwolla'
];
my
$limit
=
shift
|| 10;
my
$params
= {
'search'
=>
$self
->{
'api_key'
},
'types'
=>
join
(
','
,@{
$types
}),
'limit'
=>
$limit
};
my
$response
=
$self
->_get(
"contacts"
,
$params
);
return
$response
;
}
sub
nearby_contacts
{
my
$self
=
shift
;
my
$lat
=
shift
;
my
$long
=
shift
;
my
$range
=
shift
|| 10;
my
$limit
=
shift
|| 10;
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
'latitude'
=>
$lat
,
'longitude'
=>
$long
,
'range'
=>
$range
,
'limit'
=>
$limit
};
my
$response
=
$self
->_get(
"contacts/nearby"
,
$params
);
return
$response
;
}
sub
funding_sources
{
my
$self
=
shift
;
my
$response
=
$self
->_get(
"fundingsources"
);
return
$response
;
}
sub
funding_source
{
my
$self
=
shift
;
my
$sourceid
=
shift
;
my
$response
=
$self
->_get(
"fundingsources/$sourceid"
);
return
$response
;
}
sub
add_funding_source
{
my
$self
=
shift
;
my
$acctnum
=
shift
||
undef
;
my
$trnnum
=
shift
||
undef
;
my
$accttype
=
shift
||
undef
;
my
$acctname
=
shift
||
undef
;
my
$errors
= 0;
if
(!
defined
(
$acctnum
)) {
$self
->set_error(
'Please supply a valid account number.'
);
$errors
++;
}
if
(!
defined
(
$trnnum
) ||
$trnnum
!~ /^[0-9]{9}$/) {
$self
->set_error(
'Please supply a valid routing number.'
);
$errors
++;
}
if
(!
defined
(
$accttype
) || (
$accttype
ne
'Checking'
&&
$accttype
ne
'Savings'
)) {
$self
->set_error(
'Please supply a valid account type.'
);
$errors
++;
}
if
(!
defined
(
$acctname
)) {
$self
->set_error(
'Please supply a valid account name.'
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'account_number'
=>
$acctnum
,
'routing_number'
=>
$trnnum
,
'account_type'
=>
$accttype
,
'name'
=>
$acctname
};
my
$response
=
$self
->_post(
"fundingsources/"
,
$params
);
return
$response
;
}
sub
verify_funding_source
{
my
$self
=
shift
;
my
$sourceid
=
shift
||
undef
;
my
$deposit1
=
shift
||
undef
;
my
$deposit2
=
shift
||
undef
;
my
$errors
= 0;
if
(!
defined
(
$sourceid
)) {
$self
->set_error(
"Please provide a valid funding source."
);
$errors
++;
}
if
(!
defined
(
$deposit1
)) {
$self
->set_error(
"Please provide deposit #1."
);
$errors
++;
}
if
(!
defined
(
$deposit2
)) {
$self
->set_error(
"Please provide deposit #2."
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'deposit1'
=>
$deposit1
,
'deposit2'
=>
$deposit2
};
my
$response
=
$self
->_post(
"fundingsources/$sourceid/verify"
,
$params
);
return
$response
;
}
sub
withdraw
{
my
$self
=
shift
;
my
$sourceid
=
shift
||
undef
;
my
$pin
=
shift
||
undef
;
my
$amount
=
shift
||
undef
;
my
$errors
= 0;
if
(!
defined
(
$pin
) ||
$pin
!~ /^[0-9]{4}$/) {
$self
->set_error(
'Please supply a valid pin.'
);
$errors
++;
}
if
(!
defined
(
$sourceid
)) {
$self
->set_error(
'Please supply a fund source.'
);
$errors
++;
}
if
(!
defined
(
$amount
)) {
$self
->set_error(
'Please supply an amount.'
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'pin'
=>
$pin
,
'amount'
=>
$amount
};
my
$response
=
$self
->_post(
"fundingsources/$sourceid/withdraw"
,
$params
);
return
$response
;
}
sub
deposit
{
my
$self
=
shift
;
my
$sourceid
=
shift
||
undef
;
my
$pin
=
shift
||
undef
;
my
$amount
=
shift
||
undef
;
my
$errors
= 0;
if
(!
defined
(
$pin
) ||
$pin
!~ /^[0-9]{4}$/) {
$self
->set_error(
'Please supply a valid pin.'
);
$errors
++;
}
if
(!
defined
(
$sourceid
)) {
$self
->set_error(
'Please supply a fund source.'
);
$errors
++;
}
if
(!
defined
(
$amount
)) {
$self
->set_error(
'Please supply an amount'
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'pin'
=>
$pin
,
'amount'
=>
$amount
};
my
$response
=
$self
->_post(
"fundingsources/$sourceid/deposit"
,
$params
);
return
$response
;
}
sub
balance
{
my
$self
=
shift
;
my
$response
=
$self
->_get(
"balance"
);
return
$response
;
}
sub
send
{
my
$self
=
shift
;
my
$pin
=
shift
||
undef
;
my
$destid
=
shift
||
undef
;
my
$amount
=
shift
||
undef
;
my
$dtype
=
shift
||
'Dwolla'
;
my
$notes
=
shift
||
''
;
my
$facilitator_amount
=
shift
|| 0;
my
$assume_costs
=
shift
|| 0;
my
$fund_source
=
shift
||
'balance'
;
my
$errors
= 0;
if
(!
defined
(
$pin
) ||
$pin
!~ /^[0-9]+$/) {
$self
->set_error(
'Please supply a valid pin.'
);
$errors
++;
}
if
(!
defined
(
$destid
)) {
$self
->set_error(
'Please supply a valid destination.'
);
$errors
++;
}
if
(!
defined
(
$amount
)) {
$self
->set_error(
'Please supply a valid amount.'
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'pin'
=>
$pin
,
'destinationId'
=>
$destid
,
'destinationType'
=>
$dtype
,
'amount'
=>
$amount
,
'facilitatorAmount'
=>
$facilitator_amount
,
'assumeCosts'
=>
$assume_costs
,
'notes'
=>
$notes
,
'fundsSource'
=>
$fund_source
};
my
$response
=
$self
->_post(
"transactions/send"
,
$params
);
return
$response
;
}
sub
guest_send
{
my
$self
=
shift
;
my
$destid
=
shift
;
my
$amount
=
shift
;
my
$first_name
=
shift
;
my
$last_name
=
shift
;
my
$email
=
shift
;
my
$trnnum
=
shift
;
my
$acctnum
=
shift
;
my
$accttype
=
shift
;
my
$assume_costs
=
shift
|| 0;
my
$dtype
=
shift
||
'Dwolla'
;
my
$notes
=
shift
||
''
;
my
$group_id
=
shift
||
undef
;
my
$addtl_fees
=
shift
||
undef
;
my
$errors
= 0;
if
(!
defined
(
$destid
)) {
$self
->set_error(
'Please supply a valid destination.'
);
$errors
++;
}
if
(!
defined
(
$amount
)) {
$self
->set_error(
'Please supply a valid amount.'
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
'destinationId'
=>
$destid
,
'destinationType'
=>
$dtype
,
'amount'
=>
$amount
,
'emailAddress'
=>
$email
,
'accountNumber'
=>
$acctnum
,
'routingNumber'
=>
$trnnum
,
'accountType'
=>
$accttype
,
'firstName'
=>
$first_name
,
'lastName'
=>
$last_name
,
'assumeCosts'
=>
$assume_costs
,
'notes'
=>
$notes
,
'groupId'
=>
$group_id
,
'additionalFees'
=>
$addtl_fees
};
my
$response
=
$self
->_post(
"transactions/guestsend"
,
$params
);
return
$response
;
}
sub
request
{
my
$self
=
shift
;
my
$sourceid
=
shift
||
undef
;
my
$amount
=
shift
||
undef
;
my
$stype
=
shift
||
'Dwolla'
;
my
$notes
=
shift
||
''
;
my
$facilitator_amount
=
shift
|| 0;
my
$errors
= 0;
if
(!
defined
(
$sourceid
)) {
$self
->set_error(
'Please supply a fund source.'
);
$errors
++;
}
if
(!
defined
(
$amount
)) {
$self
->set_error(
'Please supply a valid amount.'
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$params
= {
'sourceId'
=>
$sourceid
,
'sourceType'
=>
$stype
,
'amount'
=>
$amount
,
'facilitatorAmount'
=>
$facilitator_amount
,
'notes'
=>
$notes
};
my
$response
=
$self
->_post(
"requests/"
,
$params
);
return
$response
;
}
sub
request_by_id
{
my
$self
=
shift
;
my
$id
=
shift
;
my
$response
=
$self
->_get(
"requests/$id"
);
return
$response
;
}
sub
fulfill_request
{
my
$self
=
shift
;
my
$id
=
shift
||
undef
;
my
$pin
=
shift
||
undef
;
my
$amount
=
shift
||
undef
;
my
$notes
=
shift
||
''
;
my
$fund_source
=
shift
||
'balance'
;
my
$assume_costs
=
shift
;
my
$params
= {
'pin'
=>
$pin
};
if
(
defined
(
$amount
)) {
$params
->{
'amount'
} =
$amount
;
}
if
(
defined
(
$notes
)) {
$params
->{
'notes'
} =
$notes
;
}
if
(
defined
(
$fund_source
)) {
$params
->{
'fundsSource'
} =
$fund_source
;
}
if
(!
defined
(
$assume_costs
)) {
$assume_costs
= 0;
}
$params
->{
'assumeCosts'
} =
$assume_costs
;
my
$response
=
$self
->_post(
"requests/$id/fulfill"
,
$params
);
return
$response
;
}
sub
cancel_request
{
my
$self
=
shift
;
my
$id
=
shift
||
undef
;
if
(!
defined
(
$id
)) {
$self
->set_error(
'Must supply request id.'
);
return
0;
}
my
$response
=
$self
->_post(
"requests/$id/cancel"
,{});
return
$response
;
}
sub
requests
{
my
$self
=
shift
;
my
$response
=
$self
->_get(
"requests"
);
return
$response
;
}
sub
transaction
{
my
$self
=
shift
;
my
$transaction
=
shift
||
undef
;
if
(!
defined
(
$transaction
)) {
$self
->set_error(
'Must supply transaction id.'
);
return
0;
}
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
};
my
$response
=
$self
->_get(
"transactions/$transaction"
,
$params
);
return
$response
;
}
sub
listings
{
my
$self
=
shift
||
undef
;
my
$since
=
shift
||
undef
;
my
$types
=
shift
||
undef
;
my
$limit
=
shift
|| 10;
my
$skip
=
shift
|| 0;
my
$groupid
=
shift
||
undef
;
my
$params
= {
'client_id'
=>
$self
->{
'api_key'
},
'client_secret'
=>
$self
->{
'api_secret'
},
'limit'
=>
$limit
,
'skip'
=>
$skip
,
'groupId'
=>
$groupid
};
if
(
defined
(
$since
)) {
if
(
$since
=~ /^\d{2}\-\d{2}\-\d{4}$/) {
$params
->{
'sinceDate'
} =
$since
;
}
else
{
$self
->set_error(
"Please supply a date in 'MM-DD-YYYY' format."
);
return
0;
}
}
if
(
defined
(
$types
)) {
$params
->{
'types'
} =
join
(
','
,@{
$types
});
}
my
$response
=
$self
->_get(
"transactions/"
,
$params
);
return
$response
;
}
sub
stats
{
my
$self
=
shift
;
my
$types
=
shift
|| [
'TransactionsCount'
,
'TransactionsTotal'
];
my
$start_date
=
shift
||
undef
;
my
$end_date
=
shift
||
undef
;
my
$params
= {
'types'
=>
join
(
','
,@{
$types
}),
'startDate'
=>
$start_date
,
'endDate'
=>
$end_date
};
my
$response
=
$self
->_get(
"transactions/stats"
,
$params
);
return
$response
;
}
sub
start_gateway_session
{
my
$self
=
shift
;
$self
->{
'gateway_session'
} = [];
}
sub
add_gateway_product
{
my
$self
=
shift
;
my
$name
=
shift
;
my
$price
=
shift
;
my
$quantity
=
shift
||
undef
;
my
$description
=
shift
||
''
;
if
(!
defined
(
$quantity
)) {
$quantity
= 1;
}
my
$product
= {
'Name'
=>
$name
,
'Price'
=>
$price
,
'Description'
=>
$description
,
'Quantity'
=>
$quantity
};
push
(@{
$self
->{
'gateway_session'
}},
$product
);
}
sub
get_gateway_url
{
my
$self
=
shift
;
my
$destid
=
shift
;
my
$orderid
=
shift
;
my
$discount
=
shift
|| 0;
my
$shipping
=
shift
|| 0;
my
$tax
=
shift
|| 0;
my
$notes
=
shift
||
''
;
my
$callback
=
shift
||
undef
;
my
$allow_funding_sources
=
shift
;
if
(!
$self
->is_id_valid(
$destid
)) {
$self
->set_error(
"Please supply a valid Dwolla Id."
);
return
0;
}
if
(!
defined
(
$allow_funding_sources
)) {
$allow_funding_sources
= 1;
}
my
$subtotal
= 0;
foreach
my
$product
(@{
$self
->{
'gateway_session'
}}) {
$subtotal
+=
$product
->{
'Price'
} *
$product
->{
'Quantity'
};
}
my
$total
=
sprintf
(
"%.2f"
,(
$subtotal
-
abs
(
$discount
) +
$shipping
+
$tax
));
my
$request
= {
'Key'
=>
$self
->{
'api_key'
},
'Secret'
=>
$self
->{
'api_secret'
},
'Test'
=> (
$self
->{
'mode'
} eq
'test'
) ? 1 : 0,
'AllowFundingSources'
=> (
$allow_funding_sources
) ?
'true'
:
'false'
,
'PurchaseOrder'
=> {
'DestinationId'
=>
$destid
,
'OrderItems'
=>
$self
->{
'gateway_session'
},
'Discount'
=> (
abs
(
$discount
) * -1),
'Shipping'
=>
$shipping
,
'Tax'
=>
$tax
,
'Total'
=>
$total
,
'Notes'
=>
$notes
,
}
};
if
(
defined
(
$self
->{
'redirect_uri'
})) {
$request
->{
'Redirect'
} =
$self
->{
'redirect_uri'
};
}
if
(
defined
(
$callback
)) {
$request
->{
'Callback'
} =
$callback
;
}
if
(
defined
(
$orderid
)) {
$request
->{
'OrderId'
} =
$orderid
;
}
my
$response
=
$self
->_api_request(GATEWAY_URL,
'POST'
,
$request
);
if
(
$response
!= 0) {
if
(
$response
->{
'Result'
} ne
'Success'
) {
$self
->set_error(
$response
->{
'Message'
});
return
0;
}
}
else
{
return
$response
;
}
return
CHECKOUT_URL .
'/'
.
$response
->{
'CheckoutId'
};
}
sub
verify_gateway_signature
{
my
$self
=
shift
;
my
$signature
=
shift
||
undef
;
my
$checkout_id
=
shift
||
undef
;
my
$amount
=
shift
||
undef
;
my
$errors
= 0;
if
(!
defined
(
$signature
)) {
$self
->set_error(
"Please pass a proposed signature."
);
$errors
++;
}
if
(!
defined
(
$checkout_id
)) {
$self
->set_error(
"Please pass a checkout id."
);
$errors
++;
}
if
(!
defined
(
$amount
)) {
$self
->set_error(
"Please pass an amount."
);
$errors
++;
}
if
(
$errors
) {
return
0;
}
my
$hmac
= Digest::HMAC_SHA1->new(
$checkout_id
.
'&'
.
$amount
,
$self
->{
'api_secret'
});
my
$hash
=
$hmac
->hexdigest;
if
(
$hash
ne
$signature
) {
$self
->set_error(
'Dwolla signature verification failed.'
);
return
0;
}
return
1;
}
sub
verify_webhook_signature
{
my
$self
=
shift
;
my
$sheader
=
shift
;
my
$body
=
shift
;
my
$hmac
= Digest::HMAC_SHA1->new(
$body
,
$self
->{
'api_secret'
});
my
$hash
=
$hmac
->hexdigest;
if
(
$hash
ne
$sheader
) {
$self
->set_error(
'Dwolla signature verification failed.'
);
return
0;
}
return
1;
}
sub
masspay_create_job
{
my
$self
=
shift
;
my
$pin
=
shift
;
my
$email
=
shift
;
my
$user_job_id
=
shift
;
my
$assume_costs
=
shift
;
my
$source
=
shift
||
'balance'
;
my
$filedata
=
shift
||
undef
;
my
$test_string
= (
$self
->{
'mode'
} eq
'test'
) ?
'true'
:
'false'
;
my
$params
= {
'pin'
=>
$pin
,
'email'
=>
$email
,
'source'
=>
$source
,
'user_job_id'
=>
$user_job_id
,
'test'
=>
$test_string
,
'filedata'
=>
$filedata
,
'assumeCosts'
=>
$assume_costs
,
'token'
=>
$self
->{
'oauth_token'
}
};
my
$response
=
$self
->_parse_masspay(
$self
->_api_request(
MASSPAY_URL .
'/create'
,
'POST'
,
$params
)
);
return
$response
;
}
sub
masspay_job_details
{
my
$self
=
shift
;
my
$uid
=
shift
;
my
$job_id
=
shift
;
my
$user_job_id
=
shift
||
undef
;
my
$params
= {
'uid'
=>
$uid
,
'job_id'
=>
$job_id
,
'user_job_id'
=>
$user_job_id
};
my
$response
=
$self
->_parse_masspay(
$self
->_api_request(
MASSPAY_URL .
'/status'
,
'POST'
,
$params
)
);
return
$response
;
}
sub
is_id_valid
{
my
$self
=
shift
;
my
$id
=
shift
;
my
$valid
= 0;
if
(
defined
(
$id
) &&
$id
=~ /([0-9]{3})\-*([0-9]{3})\-*([0-9]{4})/) {
$valid
= 1;
}
return
$valid
;
}
sub
set_error
{
my
$self
=
shift
;
my
$error
=
shift
;
push
(@{
$self
->{
'errors'
}},
$error
);
}
sub
get_errors
{
my
$self
=
shift
;
my
@err
= ();
@err
= @{
$self
->{
'errors'
}};
$self
->{
'errors'
} = [];
return
\
@err
;
}
sub
set_debug_mode
{
my
$self
=
shift
;
my
$debug_mode
=
shift
|| 0;
$self
->{
'debug_mode'
} =
$debug_mode
;
}
sub
_http_build_query
{
my
$self
=
shift
;
my
$params
=
shift
;
my
@tmp
= ();
my
$str
;
foreach
my
$key
(
keys
%{
$params
}) {
if
(
defined
(
$params
->{
$key
})) {
$str
= uri_escape(
$key
) .
'='
. uri_escape(
$params
->{
$key
});
push
(
@tmp
,
$str
);
}
}
return
join
(
q{&}
,
@tmp
);
}
sub
_get
{
my
$self
=
shift
;
my
$url
=
shift
;
my
$params
=
shift
;
$params
->{
'oauth_token'
} =
$self
->{
'oauth_token'
};
my
$rurl
= API_SERVER .
'/'
.
$url
.
'?'
.
$self
->_http_build_query(
$params
);
my
$response
=
$self
->_parse(
$self
->_api_request(
$rurl
,
'GET'
));
return
$response
;
}
sub
_post
{
my
$self
=
shift
;
my
$url
=
shift
;
my
$params
=
shift
;
my
$include_token
=
shift
;
my
$rurl
= API_SERVER .
'/'
.
$url
;
if
(!
defined
(
$include_token
) ||
$include_token
!= 0) {
$rurl
.=
'?'
.
$self
->_http_build_query({
'oauth_token'
=>
$self
->{
'oauth_token'
}
});
}
my
$response
=
$self
->_parse(
$self
->_api_request(
$rurl
,
'POST'
,
$params
));
return
$response
;
}
sub
_api_request
{
my
$self
=
shift
;
my
$url
=
shift
;
my
$method
=
shift
;
my
$params
=
shift
||
undef
;
my
$content_type
=
'application/json;charset=UTF-8'
;
my
$ua
= LWP::UserAgent->new;
$ua
->agent(
'Dwolla Perl API V'
.
$VERSION
);
if
(
$self
->{
'debug_mode'
}) {
print
"Making '$method' request to '$url'\n"
;
}
my
$request
;
if
(
$method
eq
'GET'
) {
$request
= HTTP::Request->new(
GET
=>
$url
);
}
elsif
(
$method
eq
'POST'
) {
my
$data
= JSON->new->utf8->encode(
$params
);
if
(
$self
->{
'debug_mode'
}) {
print
"POST DATA: $data\n"
;
}
$request
= HTTP::Request->new(
POST
=>
$url
);
$request
->content_length(
length
(
$data
));
$request
->content(
$data
);
}
$request
->content_type(
$content_type
);
my
$response
=
$ua
->request(
$request
);
if
(
$response
->code() ne
'200'
) {
if
(
$self
->{
'debug_mode'
}) {
print
Data::Dumper->Dump([
$response
],
'response'
);
}
$self
->set_error(
"Request failed. HTTP status code: "
.
$response
->code());
return
0;
}
my
$obj
= JSON->new->utf8->decode(
$response
->content);
return
$obj
;
}
sub
_parse
{
my
$self
=
shift
;
my
$response
=
shift
;
if
(
$self
->{
'debug_mode'
}) {
print
Data::Dumper->Dump([
$response
],
'response'
);
}
my
$errstring
=
''
;
my
$errors
= 0;
if
(
$response
->{
'Success'
} == 0) {
$errstring
=
$response
->{
'Message'
};
$errors
++;
if
(
$response
->{
'Response'
}) {
$errstring
.=
' :: '
.
join
(
','
,@{
$response
->{
'Response'
}});
}
}
if
(
$errors
) {
$self
->set_error(
$errstring
);
return
0;
}
return
$response
->{
'Response'
};
}
sub
_parse_masspay
{
my
$self
=
shift
;
my
$response
=
shift
;
if
(
$self
->{
'debug_mode'
}) {
print
Data::Dumper->Dump([
$response
],
'response'
);
}
if
(!
$response
->{
'success'
}) {
$self
->set_error(
$response
->{
'message'
});
return
0;
}
return
$response
->{
'job'
};
}
1;