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\webmoney; |
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 Money\Currency; |
18
|
|
|
use Money\Money; |
19
|
|
|
use Omnipay\WebMoney\Gateway; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class WebmoneyMerchant. |
23
|
|
|
* |
24
|
|
|
* @author Dmytro Naumenko <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class WebmoneyMerchant extends AbstractMerchant |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Gateway |
30
|
|
|
*/ |
31
|
|
|
protected $gateway; |
32
|
|
|
|
33
|
3 |
|
protected function createGateway() |
34
|
|
|
{ |
35
|
3 |
|
return $this->gatewayFactory->build('WebMoney', [ |
36
|
3 |
|
'merchantPurse' => $this->credentials->getPurse(), |
37
|
3 |
|
'secretKey' => $this->credentials->getKey1(), |
38
|
3 |
|
'testMode' => $this->credentials->isTestMode(), |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param InvoiceInterface $invoice |
44
|
|
|
* @return RedirectPurchaseResponse |
45
|
|
|
*/ |
46
|
1 |
|
public function requestPurchase(InvoiceInterface $invoice) |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var \Omnipay\WebMoney\Message\PurchaseResponse |
50
|
|
|
*/ |
51
|
1 |
|
$response = $this->gateway->purchase([ |
52
|
1 |
|
'transactionId' => $invoice->getId(), |
53
|
1 |
|
'description' => $invoice->getDescription(), |
54
|
1 |
|
'amount' => $this->moneyFormatter->format($invoice->getAmount()), |
55
|
1 |
|
'currency' => $invoice->getCurrency()->getCode(), |
56
|
1 |
|
'returnUrl' => $invoice->getReturnUrl(), |
57
|
1 |
|
'returnMethod' => $invoice->getReturnMethod(), |
58
|
1 |
|
'notifyUrl' => $invoice->getNotifyUrl(), |
59
|
1 |
|
'notifyMethod' => $invoice->getNotifyMethod(), |
60
|
1 |
|
'cancelUrl' => $invoice->getCancelUrl(), |
61
|
1 |
|
'cancelMethod' => $invoice->getCancelMethod(), |
62
|
1 |
|
])->send(); |
63
|
|
|
|
64
|
1 |
|
return new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $data |
69
|
|
|
* @return CompletePurchaseResponse |
70
|
|
|
*/ |
71
|
1 |
View Code Duplication |
public function completePurchase($data) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
/** @var \Omnipay\WebMoney\Message\CompletePurchaseResponse $response */ |
74
|
1 |
|
$response = $this->gateway->completePurchase($data)->send(); |
75
|
|
|
|
76
|
1 |
|
return (new CompletePurchaseResponse()) |
77
|
1 |
|
->setIsSuccessful($response->isSuccessful()) |
|
|
|
|
78
|
1 |
|
->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency())) |
79
|
1 |
|
->setFee(new Money(0, new Currency($response->getCurrency()))) |
80
|
1 |
|
->setTransactionReference($response->getTransactionReference()) |
81
|
1 |
|
->setTransactionId($response->getTransactionId()) |
82
|
1 |
|
->setPayer($response->getData()['LMI_PAYER_PURSE']) |
83
|
1 |
|
->setTime(new \DateTime($response->getData()['LMI_SYS_TRANS_DATE'])); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.