|
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\merchants\bitpay; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\merchant\InvoiceInterface; |
|
14
|
|
|
use hiqdev\php\merchant\merchants\AbstractMerchant; |
|
15
|
|
|
use hiqdev\php\merchant\response\CompletePurchaseResponse; |
|
16
|
|
|
use hiqdev\php\merchant\response\RedirectPurchaseResponse; |
|
17
|
|
|
use Omnipay\BitPay\Gateway; |
|
18
|
|
|
use hiqdev\php\merchant\exceptions\MerchantException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class BitPayAdapter. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class BitPayMerchant extends AbstractMerchant |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @return Gateway |
|
29
|
3 |
|
*/ |
|
30
|
|
|
protected function createGateway() |
|
31
|
3 |
|
{ |
|
32
|
3 |
|
return $this->gatewayFactory->build('BitPay', [ |
|
33
|
3 |
|
'token' => $this->credentials->getKey1(), |
|
34
|
3 |
|
'privateKey' => $this->credentials->getKey2(), |
|
35
|
3 |
|
'publicKey' => $this->credentials->getKey3(), |
|
36
|
|
|
'testMode' => $this->getCredentials()->isTestMode(), |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param InvoiceInterface $invoice |
|
42
|
|
|
* @return RedirectPurchaseResponse |
|
43
|
1 |
|
*/ |
|
44
|
|
|
public function requestPurchase(InvoiceInterface $invoice) |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var \Omnipay\BitPay\Message\PurchaseResponse |
|
48
|
1 |
|
*/ |
|
49
|
1 |
|
$response = $this->gateway->purchase([ |
|
|
|
|
|
|
50
|
1 |
|
'transactionId' => $invoice->getId(), |
|
51
|
1 |
|
'description' => $invoice->getDescription(), |
|
52
|
1 |
|
'amount' => $this->moneyFormatter->format($invoice->getAmount()), |
|
53
|
1 |
|
'currency' => $invoice->getCurrency()->getCode(), |
|
54
|
1 |
|
'returnUrl' => $invoice->getReturnUrl(), |
|
55
|
1 |
|
'notifyUrl' => $invoice->getNotifyUrl(), |
|
56
|
1 |
|
'cancelUrl' => $invoice->getCancelUrl(), |
|
57
|
|
|
])->send(); |
|
58
|
1 |
|
|
|
59
|
1 |
|
if ($response->getRedirectUrl() === null) { |
|
60
|
|
|
throw new MerchantException('Failed to request purchase'); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
$response = new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData()); |
|
64
|
|
|
$response->setMethod('GET'); |
|
65
|
|
|
|
|
66
|
|
|
return $response; |
|
67
|
|
|
} |
|
68
|
1 |
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $data |
|
71
|
1 |
|
* @return CompletePurchaseResponse |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function completePurchase($data) |
|
74
|
1 |
|
{ |
|
75
|
1 |
|
/** @var \Omnipay\BitPay\Message\CompletePurchaseResponse $response */ |
|
76
|
1 |
|
$response = $this->gateway->completePurchase($data)->send(); |
|
|
|
|
|
|
77
|
1 |
|
|
|
78
|
1 |
|
return (new CompletePurchaseResponse()) |
|
79
|
1 |
|
->setIsSuccessful($response->isSuccessful()) |
|
|
|
|
|
|
80
|
1 |
|
->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency())) |
|
81
|
|
|
->setFee($this->moneyParser->parse($response->getFee(), $response->getCurrency())) |
|
82
|
|
|
->setTransactionReference($response->getTransactionReference()) |
|
83
|
|
|
->setTransactionId($response->getTransactionId()) |
|
84
|
|
|
->setPayer($response->getPayer()) |
|
85
|
|
|
->setTime(new \DateTime($response->getTime())); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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: