forked from bunq/tinker_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bunq#15 from bunq/feature/bunq/tinker_php#14_autom…
…atic_money_request Request money on Sandbox if balance zero. (bunq#14)
- Loading branch information
Showing
1 changed file
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
@@ -85,6 +97,7 @@ public function __construct(BunqEnumApiEnvironmentType $bunqEnumApiEnvironmentTy | |
$this->environment = $bunqEnumApiEnvironmentType; | ||
$this->setupContext(); | ||
$this->setupCurrentUser(); | ||
$this->requestSpendingMoneyIfNeeded(); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
} |