Skip to content

Commit fa013c4

Browse files
committed
Merge pull request #26 from Payum/reuse-facade
reuse payum facade, cleanup\simplify things.
2 parents 0045862 + 69813ea commit fa013c4

20 files changed

+415
-723
lines changed

docs/eloquent_storage.md

+42-7
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ class Payment extends Illuminate\Database\Eloquent\Model
1717
Register a storage for it
1818

1919
```php
20-
// app/config/packages/payum/payum-laravel-package/config.php
20+
// bootstrap/start.php
2121

2222
use Payum\LaravelPackage\Storage\EloquentStorage;
2323

24-
return array(
25-
'storages' => array(
26-
'Payment' => new EloquentStorage('Payment'),
27-
)
28-
);
24+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
25+
$payumBuilder
26+
->addStorage(Payment::class, new EloquentStorage(Payment::class))
27+
;
28+
});
2929
```
3030

3131
## Models
3232

3333
The package provides two models `Payum\LaravelPackage\Model\Token` and `Payum\LaravelPackage\Model\Payment` which may be reused directly or extend with some custom logic.
3434
Here's the models schemas:
3535

36-
Order:
36+
Payment:
37+
38+
The database schema could be generated like this
39+
3740
```php
3841
<?php
3942

@@ -51,9 +54,26 @@ Order:
5154
});
5255
```
5356

57+
The storage could be registered like this
58+
59+
```php
60+
// bootstrap/start.php
61+
62+
use Payum\LaravelPackage\Storage\EloquentStorage;
63+
use Payum\LaravelPackage\Model\Payment;
64+
65+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
66+
$payumBuilder
67+
->addStorage(Payment::class, new EloquentStorage(Payment::class))
68+
;
69+
});
70+
```
71+
5472

5573
Token:
5674

75+
The database schema could be generated like this
76+
5777
```php
5878
<?php
5979

@@ -68,4 +88,19 @@ Token:
6888
});
6989
```
7090

91+
The token storage could be registered like this
92+
93+
```php
94+
// bootstrap/start.php
95+
96+
use Payum\LaravelPackage\Storage\EloquentStorage;
97+
use Payum\LaravelPackage\Model\Token;
98+
99+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
100+
$payumBuilder
101+
->setTokenStorage(new EloquentStorage(Token::class))
102+
;
103+
});
104+
```
105+
71106
Back to [index](index.md).

docs/examples.md

+98-78
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,102 @@
44

55
Described in [Get it started](get-it-started.md)
66

7+
## Payment model
8+
9+
* Configuration
10+
11+
```bash
12+
$ php composer.phar require payum/payum-laravel-package payum/offline
13+
```
14+
15+
```php
16+
// bootstrap/start.php
17+
18+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
19+
$payumBuilder
20+
->addGateway('offline', ['factory' => 'offline'])
21+
;
22+
});
23+
```
24+
25+
* Prepare payment
26+
27+
```php
28+
<?php
29+
// app/controllers/PaymentController.php
30+
31+
use Payum\LaravelPackage\Controller\PayumController;
32+
33+
cclass PaymentController extends PayumController
34+
{
35+
public function preparePayment()
36+
{
37+
$storage = $this->getPayum()->getStorage('Payum\Core\Model\Payment');
38+
39+
$payment = $storage->create();
40+
$payment->setNumber(uniqid());
41+
$payment->setCurrencyCode('EUR');
42+
$payment->setTotalAmount(123); // 1.23 EUR
43+
$payment->setDescription('A description');
44+
$payment->setClientId('anId');
45+
$payment->setClientEmail('[email protected]');
46+
$payment->setDetails(array(
47+
// put here any fields in a gateway format.
48+
// for example if you use Paypal ExpressCheckout you can define a description of the first item:
49+
// 'L_PAYMENTREQUEST_0_DESC0' => 'A desc',
50+
));
51+
$storage->update($payment);
52+
53+
$captureToken = $payum->getTokenFactory()->createCaptureToken('offline', $payment, 'payment_done');
54+
55+
return \Redirect::to($captureToken->getTargetUrl());
56+
}
57+
}
58+
```
59+
760
## Stripe.Js
861

962
* Configuration
1063

1164
```bash
1265
$ php composer.phar require payum/payum-laravel-package stripe/stripe-php payum/stripe
13-
$ php artisan config:publish payum/payum-laravel-package
1466
```
1567

1668
```php
17-
// app/config/packages/payum/payum-laravel-package/config.php
18-
19-
use Payum\LaravelPackage\Action\GetHttpRequestAction;
20-
use Payum\Core\Storage\FilesystemStorage;
21-
22-
$detailsClass = 'Payum\Core\Model\ArrayObject';
23-
$tokenClass = 'Payum\Core\Model\Token';
24-
25-
$stripeJsGatewayFactory = new \Payum\Stripe\StripeJsGatewayFactory();
26-
27-
return array(
28-
'token_storage' => new FilesystemStorage(__DIR__.'/../../../../storage/payments', $tokenClass, 'hash'),
29-
'gateways' => array(
30-
'stripe_js' => $stripeJsGatewayFactory->create(array(
31-
'publishable_key' => $_SERVER['payum.stripe.publishable_key'],
32-
'secret_key' => $_SERVER['payum.stripe.secret_key'],
33-
'payum.action.get_http_request' => new GetHttpRequestAction(),
34-
)),
35-
),
36-
'storages' => array(
37-
$detailsClass => new FilesystemStorage(__DIR__.'/../../../../storage/payments', $detailsClass),
38-
)
39-
);
69+
// bootstrap/start.php
70+
71+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
72+
$payumBuilder
73+
->addGateway('stripe_js', [
74+
'factory' => 'stripe_js',
75+
'publishable_key' => 'EDIT ME',
76+
'secret_key' => 'EDIT ME',
77+
])
78+
;
79+
});
4080
```
4181

4282
* Prepare payment
4383

4484
```php
4585
<?php
46-
// app/controllers/PaypalController.php
86+
// app/controllers/StripeController.php
4787

48-
cclass StripeController extends BaseController
88+
use Payum\LaravelPackage\Controller\PayumController;
89+
90+
cclass StripeController extends PayumController
4991
{
5092
public function prepareJs()
5193
{
52-
$storage = \App::make('payum')->getStorage('Payum\Core\Model\ArrayObject');
94+
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
5395

5496
$details = $storage->create();
5597
$details['amount'] = '100';
5698
$details['currency'] = 'USD';
5799
$details['description'] = 'a desc';
58100
$storage->update($details);
59101

60-
$captureToken = \App::make('payum.security.token_factory')->createCaptureToken('stripe_js', $details, 'payment_done');
102+
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('stripe_js', $details, 'payment_done');
61103

62104
return \Redirect::to($captureToken->getTargetUrl());
63105
}
@@ -70,54 +112,43 @@ cclass StripeController extends BaseController
70112

71113
```bash
72114
$ php composer.phar require payum/stripe payum/payum-laravel-package stripe/stripe-php
73-
$ php artisan config:publish payum/payum-laravel-package
74115
```
75116

76117
```php
77-
// app/config/packages/payum/payum-laravel-package/config.php
78-
79-
use Payum\LaravelPackage\Action\GetHttpRequestAction;
80-
use Payum\Core\Storage\FilesystemStorage;
81-
82-
$detailsClass = 'Payum\Core\Model\ArrayObject';
83-
$tokenClass = 'Payum\Core\Model\Token';
118+
// bootstrap/start.php
84119

85-
$stripeCheckoutGatewayFactory = new \Payum\Stripe\StripeCheckoutGatewayFactory();
86-
87-
return array(
88-
'token_storage' => new FilesystemStorage(__DIR__.'/../../../../storage/payments', $tokenClass, 'hash'),
89-
'gateways' => array(
90-
'stripe_checkout' => $stripeCheckoutGatewayFactory->create(array(
120+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
121+
$payumBuilder
122+
->addGateway('stripe_checkout', [
123+
'factory' => 'stripe_checkout',
91124
'publishable_key' => 'EDIT ME',
92125
'secret_key' => 'EDIT ME',
93-
'payum.action.get_http_request' => new GetHttpRequestAction(),
94-
)),
95-
),
96-
'storages' => array(
97-
$detailsClass => new FilesystemStorage(__DIR__.'/../../../../storage/payments', $detailsClass),
98-
)
99-
);
126+
])
127+
;
128+
});
100129
```
101130

102131
* Prepare payment
103132

104133
```php
105134
<?php
106-
// app/controllers/PaypalController.php
135+
// app/controllers/StripeController.php
136+
137+
use Payum\LaravelPackage\Controller\PayumController;
107138

108-
cclass StripeController extends BaseController
139+
cclass StripeController extends PayumController
109140
{
110141
public function prepareCheckout()
111142
{
112-
$storage = \App::make('payum')->getStorage('Payum\Core\Model\ArrayObject');
143+
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
113144

114145
$details = $storage->create();
115146
$details['amount'] = '100';
116147
$details['currency'] = 'USD';
117148
$details['description'] = 'a desc';
118149
$storage->update($details);
119150

120-
$captureToken = \App::make('payum.security.token_factory')->createCaptureToken('stripe_checkout', $details, 'payment_done');
151+
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('stripe_checkout', $details, 'payment_done');
121152

122153
return \Redirect::to($captureToken->getTargetUrl());
123154
}
@@ -130,56 +161,45 @@ cclass StripeController extends BaseController
130161

131162
```bash
132163
$ php composer.phar require payum/omnipay-bridge payum/payum-laravel-package omnipay/stripe
133-
$ php artisan config:publish payum/payum-laravel-package
134164
```
135165

136166
```php
137-
// app/config/packages/payum/payum-laravel-package/config.php
167+
// bootstrap/start.php
138168

139-
use Payum\LaravelPackage\Action\GetHttpRequestAction;
140-
use Payum\Core\Storage\FilesystemStorage;
141-
142-
$detailsClass = 'Payum\Core\Model\ArrayObject';
143-
$tokenClass = 'Payum\Core\Model\Token';
144-
145-
$omnipayDirectGatewayFactory = new \Payum\OmnipayBridge\OmnipayDirectGatewayFactory();
146-
147-
return array(
148-
'token_storage' => new FilesystemStorage(__DIR__.'/../../../../storage/payments', $tokenClass, 'hash'),
149-
'gateways' => array(
150-
'stripe_direct' => $omnipayDirectGatewayFactory->create(array(
169+
App::resolving('payum.builder', function(\Payum\Core\PayumBuilder $payumBuilder) {
170+
$payumBuilder
171+
->addGateway('stripe_direct', [
172+
'factory' => 'omnipay_direct',
151173
'type' => 'Stripe',
152174
'options' => array(
153175
'apiKey' => 'EDIT ME',
154176
'testMode' => true,
155177
),
156-
'payum.action.obtain_credit_card' => new ObtainCreditCardAction,
157-
)),
158-
),
159-
'storages' => array(
160-
$detailsClass => new FilesystemStorage(__DIR__.'/../../../../storage/payments', $detailsClass),
161-
)
162-
);
178+
])
179+
;
180+
});
163181
```
164182

165183
* Prepare payment
166184

167185
```php
168186
<?php
169-
// app/controllers/PaypalController.php
187+
// app/controllers/OmnipayController.php
188+
189+
use Payum\LaravelPackage\Controller\PayumController;
170190

171-
cclass StripeController extends BaseController
191+
cclass OmnipayController extends PayumController
172192
{
173193
public function prepareDirect()
174194
{
175-
$storage = \App::make('payum')->getStorage('Payum\Core\Model\ArrayObject');
195+
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
176196

177197
$details = $storage->create();
178198
$details['amount'] = '10.00';
179199
$details['currency'] = 'USD';
180200
$storage->update($details);
181201

182-
$captureToken = \App::make('payum.security.token_factory')->createCaptureToken('stripe_direct', $details, 'payment_done');
202+
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('stripe_direct', $details, 'payment_done');
183203

184204
return \Redirect::to($captureToken->getTargetUrl());
185205
}

0 commit comments

Comments
 (0)