Skip to content

Commit

Permalink
Merge pull request bunq#15 from bunq/feature/bunq/tinker_php#14_autom…
Browse files Browse the repository at this point in the history
…atic_money_request

Request money on Sandbox if balance zero. (bunq#14)
  • Loading branch information
OGKevin authored Apr 12, 2018
2 parents 26dd869 + ac5977a commit baa8fc5
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/BunqLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,24 @@ class BunqLib
const PREG_MATCH_SUCCESS = 1;
const REGEX_E164_PHONE = '/^\+\d{3,15}$/';

/**
* Spending money request constants.
*/
const REQUEST_SPENDING_MONEY_DESCRIPTION = "Requesting some spending money.";
const REQUEST_SPENDING_MONEY_RECIPIENT = "[email protected]";
const REQUEST_SPENDING_MONEY_AMOUNT = "500.0";
const REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS = 1;

/**
* Zero balance constant.
*/
const BALANCE_ZERO = 0.0;

/**
* @var UserCompany|UserPerson|UserLight
*/
protected $user;


/**
* @var BunqEnumApiEnvironmentType
*/
Expand All @@ -85,6 +97,7 @@ public function __construct(BunqEnumApiEnvironmentType $bunqEnumApiEnvironmentTy
$this->environment = $bunqEnumApiEnvironmentType;
$this->setupContext();
$this->setupCurrentUser();
$this->requestSpendingMoneyIfNeeded();
}

/**
Expand Down Expand Up @@ -464,4 +477,30 @@ public function getCurrentUser()
{
return $this->user;
}

/**
* Requests money if the balance of the primary MA is equal to or less than zero.
*/
private function requestSpendingMoneyIfNeeded()
{
if ($this->shouldRequestSpendingMoney()) {
RequestInquiry::create(
new Amount(self::REQUEST_SPENDING_MONEY_AMOUNT, self::CURRENCY_TYPE_EUR),
new Pointer(self::POINTER_TYPE_EMAIL, self::REQUEST_SPENDING_MONEY_RECIPIENT),
self::REQUEST_SPENDING_MONEY_DESCRIPTION,
false
);
sleep(self::REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS);
}
}

/**
* @return bool
*/
private function shouldRequestSpendingMoney(): bool
{
return BunqEnumApiEnvironmentType::SANDBOX()->equals($this->environment)
&& (floatval(BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getBalance()->getValue())
<= self::BALANCE_ZERO);
}
}

0 comments on commit baa8fc5

Please sign in to comment.