1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SanderVanHooft\PayableRedirect; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Mollie_API_Client; |
7
|
|
|
use SanderVanHooft\PayableRedirect\PaymentGateway; |
8
|
|
|
use SanderVanHooft\PayableRedirect\Payment; |
9
|
|
|
|
10
|
|
|
class MolliePaymentGateway implements PaymentGateway |
11
|
|
|
{ |
12
|
|
|
private $gateway; |
13
|
|
|
private $response; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->response = false; |
18
|
|
|
$this->gateway = new Mollie_API_Client; |
19
|
|
|
$this->gateway->setApiKey(config('payable.mollie.key')); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Int |
24
|
|
|
* @param Illuminate\Database\Eloquent\Model |
25
|
|
|
* @return App\Payment |
26
|
|
|
*/ |
27
|
|
|
public function chargeAmountForPayable( |
28
|
|
|
Int $amount, |
29
|
|
|
Model $payable, |
30
|
|
|
String $description, |
31
|
|
|
array $params = [] |
32
|
|
|
): Payment { |
33
|
|
|
|
34
|
|
|
$this->response = $this->gateway->payments->create(array( |
35
|
|
|
"amount" => number_format($amount / 100, 2), |
36
|
|
|
"description" => $description, |
37
|
|
|
"redirectUrl" => $params['return_url'], |
38
|
|
|
"webhookUrl" => $params['webhook_url'], |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
// create payment record for payable model |
42
|
|
|
$payment = $payable->payments()->create([ |
43
|
|
|
'amount' => $amount, |
44
|
|
|
'status' => $this->response->status, |
45
|
|
|
'description' => $description, |
46
|
|
|
'redirect_url' => $this->response->links->paymentUrl, |
47
|
|
|
'return_url' => $this->response->links->redirectUrl, |
48
|
|
|
'gateway_name' => $this->getGatewayName(), |
49
|
|
|
'gateway_payment_reference' => $this->response->id, |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
return $payment; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return String |
57
|
|
|
*/ |
58
|
|
|
public function getGatewayName() : String |
59
|
|
|
{ |
60
|
|
|
return self::class; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Payment |
65
|
|
|
* @return Payment |
66
|
|
|
*/ |
67
|
|
|
public function fetchUpdateFor(Payment $payment) : Payment |
68
|
|
|
{ |
69
|
|
|
$this->response = $this->gateway->payments->get($payment->gateway_payment_reference); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$status = $this->response->status; |
72
|
|
|
$update = ['status' => $status]; |
73
|
|
|
|
74
|
|
|
if ($status !== 'open') { |
75
|
|
|
$update['redirect_url'] = null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$payment->update($update); |
79
|
|
|
|
80
|
|
|
return $payment; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.