Completed
Push — master ( 576b56...533c2b )
by Dmitry
14:42
created

src/merchants/coingate/CoinGateMerchant.php (1 issue)

duplicate/similar methods.

Duplication Informational

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace hiqdev\php\merchant\merchants\coingate;
4
5
use hiqdev\php\merchant\InvoiceInterface;
6
use hiqdev\php\merchant\merchants\AbstractMerchant;
7
use hiqdev\php\merchant\response\CompletePurchaseResponse;
8
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
9
use Omnipay\CoinGate\Gateway;
10
11
/**
12
 * Class CoinGateMerchant.
13
 *
14
 * @author Yurii Myronchuk <[email protected]>
15
 */
16
class CoinGateMerchant extends AbstractMerchant
17
{
18
    /**
19
     * @var Gateway
20
     */
21
    protected $gateway;
22
23
    protected function createGateway()
24
    {
25
        return $this->gatewayFactory->build('CoinGate', [
26
            'apiKey'  => $this->credentials->getKey1(),
27
           ]);
28
    }
29
30
    /**
31
     * @param InvoiceInterface $invoice
32
     * @return RedirectPurchaseResponse
33
     */
34 View Code Duplication
    public function requestPurchase(InvoiceInterface $invoice)
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
35
    {
36
        /**
37
         * @var \Omnipay\CoinGate\Message\PurchaseResponse $response
38
         */
39
        $response = $this->gateway->purchase([
40
            'transactionId' => $invoice->getId(),
41
            'currency' => $invoice->getCurrency()->getCode(),
42
            'description' => $invoice->getDescription(),
43
            'amount' => $this->moneyFormatter->format($invoice->getAmount()),
44
            'returnUrl' => $invoice->getReturnUrl(),
45
            'cancelUrl' => $invoice->getCancelUrl(),
46
            'notifyUrl' => $invoice->getNotifyUrl(),
47
        ])->send();
48
49
        if ($response->getRedirectUrl() === null) {
50
            throw new MerchantException('Failed to request purchase');
51
        }
52
53
        $response = new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
54
        $response->setMethod('GET');
55
56
        return $response;
57
    }
58
59
    /**
60
     * @param array $data
61
     * @return CompletePurchaseResponse
62
     */
63
    public function completePurchase($data)
64
    {
65
        /** @var \Omnipay\CoinGate\Message\CompletePurchaseResponse $response */
66
        $response = $this->gateway->completePurchase($data)->send();
67
68
        return (new CompletePurchaseResponse())
69
            ->setIsSuccessful($response->isSuccessful())
70
            ->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
71
            ->setTransactionReference($response->getTransactionReference())
72
            ->setTransactionId($response->getTransactionId())
73
            ->setPayer($response->getPayer())
74
            ->setTime(new \DateTime($response->getTime()));
75
    }
76
}
77