|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Generalization over Omnipay and Payum |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/php-merchant |
|
6
|
|
|
* @package php-merchant |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\merchant\bitpay; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\merchant\Helper; |
|
14
|
|
|
use hiqdev\php\merchant\RequestInterface; |
|
15
|
|
|
use Omnipay\BitPay\Gateway; |
|
16
|
|
|
use Omnipay\Common\GatewayInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* BitPay Omnipay Merchant class. |
|
20
|
|
|
*/ |
|
21
|
|
|
class OmnipayMerchant extends \hiqdev\php\merchant\OmnipayMerchant |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @return Gateway|GatewayInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getWorker() |
|
27
|
|
|
{ |
|
28
|
|
|
return parent::getWorker(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function prepareData(array $data) |
|
36
|
|
|
{ |
|
37
|
|
|
return array_merge([ |
|
38
|
|
|
'token' => $data['purse'], |
|
39
|
|
|
'privateKey' => $data['secret'], |
|
40
|
|
|
'publicKey' => $data['secret2'], |
|
41
|
|
|
'testMode' => false |
|
42
|
|
|
], $data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function request($type, array $data) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!empty($data['inputs'])) { |
|
48
|
|
|
return Helper::createObject([ |
|
49
|
|
|
'class' => FakeRequest::class, |
|
50
|
|
|
'merchant' => $this, |
|
51
|
|
|
'type' => $type, |
|
52
|
|
|
'data' => array_merge((array)$this->data, (array)$data), |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return parent::request($type, $data); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function response(RequestInterface $request) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($request instanceof FakeRequest) { |
|
62
|
|
|
/** @var PurchaseRequest $realRequest */ |
|
63
|
|
|
$realRequest = $request->merchant->getWorker()->purchase($this->data); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
return new PurchaseResponse($realRequest, $this->data['inputs']); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return parent::response($request); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: