Created
August 23, 2022 09:34
-
-
Save mpociot/26c93619bc68b4616399f7d7d2346adc to your computer and use it in GitHub Desktop.
Retrieve Paddle transactions and overview
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
<?php | |
namespace App; | |
use Symfony\Component\BrowserKit\HttpBrowser; | |
use Symfony\Component\HttpClient\HttpClient; | |
class Paddle | |
{ | |
protected HttpBrowser $browser; | |
public function __construct(public $email, public $password) | |
{ | |
$this->browser = new HttpBrowser(HttpClient::create()); | |
$this->login(); | |
} | |
protected function login() | |
{ | |
$this->browser->request('GET', 'https://vendors.paddle.com/login'); | |
$this->browser->submitForm('Login', [ | |
'email' => $this->email, | |
'password' => $this->password, | |
]); | |
} | |
public function getOverview() | |
{ | |
$this->browser->request('GET', 'https://vendors.paddle.com/overview'); | |
$output = []; | |
$this->browser->getCrawler()->filter('span.financial-stats-slide-title') | |
->reduce(function ($node, $i) use (&$output) { | |
$text = $node->closest('p')->text(); | |
$res = preg_replace("/[^0-9.]/", "", $text); | |
$output[] = $res; | |
}); | |
return $output; | |
} | |
public function getTransactions($limit = 10) | |
{ | |
$transactions = []; | |
$this->browser->request('GET', 'https://vendors.paddle.com/orders'); | |
$this->browser->getCrawler()->filter('pui-tr') | |
->reduce(function ($node) use (&$transactions) { | |
$order = []; | |
$node->filter('pui-td')->reduce(function ($node, $i) use (&$order) { | |
if ($i === 1) { | |
$order['product'] = $node->text(); | |
} | |
if ($i === 4) { | |
$order['value'] = $node->text(); | |
} | |
if ($i === 5) { | |
$order['date'] = $node->text(); | |
} | |
}); | |
if (count($transactions) < 10 && isset($order['product'])) { | |
$transactions[] = $order; | |
} | |
}); | |
return $transactions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment