Passed
Push — master ( 4164b7...fac48b )
by Stavros
23:01 queued 07:51
created

Transaction::getByTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9
1
<?php
2
3
namespace Stadem\VivaPayments\Request;
4
5
use Stadem\VivaPayments\Traits\getConfigSettings;
6
use Stadem\VivaPayments\Services\CurlWrapper;
7
use  Stadem\VivaPayments\Enums;
8
9
10
11
class Transaction
12
{
13
14
    use getConfigSettings;
15
16
    private $accessToken;
17
18
19
    public function __construct($accessToken)
20
    {
21
        $this->accessToken = $accessToken;
22
    }
23
24
25
    public function getnById($vivaOrdercode)
26
    {
27
        return $vivaOrdercode;
28
    }
29
30
    public function getByTransaction($vivaTransaction)
31
    {
32
        // https://demo.vivapayments.com/api/transactions/:transaction_id
33
        if (!$vivaTransaction)   throw new \Exception('Invalid transaction code');
34
        $url = $this->getConfigSettings()->getEnvConfig('VIVA_URL');
35
        $curl = new CurlWrapper($url . '/api/transactions/' . $vivaTransaction);
36
        $curl->addHeader('Content-Type: application/json');
37
        $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1');
38
        $curl->addHeader('Accept: */*');
39
        $curl->setBasicAuth($this->getConfigSettings()->getEnvConfig('VIVA_MERCHANT_ID'), $this->getConfigSettings()->getEnvConfig('VIVA_API_KEY'));
40
        $response = $curl->get();
41
        // $response= '{"Transactions":[{"Fee":0.00,"BankId":"CLK_ALPHA","ParentId":null,"Switching":false,"Amount":1.20,"StatusId":"F","ChannelId":"245a790d-c98d-4d80-ad97-a9b1efb5771e","MerchantId":"41ebd27c-d39c-418f-875f-433fec10c9ac","ResellerId":null,"InsDate":"2024-06-08T23:30:14.65+03:00","CreatedBy":null,"TipAmount":0.00,"SourceCode":"8541","TransactionId":"f1bad807-15d7-4c41-b578-656e5cf40a16","Commission":0.27,"PanEntryMode":"01","MerchantTrns":"This is a short description that helps you uniquely identify the transaction","CurrencyCode":"978","CustomerTrns":"Test POST - No End Payment","IsManualRefund":false,"TargetPersonId":null,"AcquirerApproved":false,"SourceTerminalId":79504090,"RedeemedAmount":0.00,"AuthorizationId":"208330","TotalInstallments":0,"CurrentInstallment":0,"ClearanceDate":"2024-06-09T02:05:57.6+03:00","ConversionRate":1.0000000,"OriginalAmount":1.2000,"ResellerSourceCode":null,"OriginalCurrencyCode":"978","RetrievalReferenceNumber":"416020208330","Order":{"OrderCode":4694439209472602,"ChannelId":"245a790d-c98d-4d80-ad97-a9b1efb5771e","ResellerId":null,"SourceCode":"8541","Tags":["tag1","tag2"],"RequestLang":"el-GR","ResellerSourceCode":null},"Payment":{"Email":"[email protected]","Phone":"+306987654321","ChannelId":"245a790d-c98d-4d80-ad97-a9b1efb5771e","FullName":"Stavros Dem","Installments":0,"RecurringSupport":false},"TransactionType":{"Name":"CardCharge","TransactionTypeId":5},"CreditCard":{"Token":"2BFCB1FD4F000DB41CB5BA76CAFB68D76AB87752","Number":"414746XXXXXX0133","CountryCode":"SG","IssuingBank":"Citibank Singapore Ltd.","CardHolderName":"Lang WoodrowKyprios Testopoulos","ExpirationDate":"2028-01-31T00:00:00","CardType":{"Name":"VISA","CardTypeId":0}}}],"ErrorCode":0,"ErrorText":null,"TimeStamp":"2024-06-09T12:11:27.1514931+03:00","CorrelationId":null,"EventId":0,"Success":true}';
42
        $response = json_decode($response, true);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $response = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
43
        $response['Payment'] = $this->getByTransactionMapper($response);
44
        return $response;
45
    }
46
47
48
    public function getByTransactionMapper($response)
49
    {
50
        $mapper['TransactionId'] = $response['Transactions'][0]['TransactionId'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$mapper was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mapper = array(); before regardless.
Loading history...
51
        $mapper['OrderCode'] = $response['Transactions'][0]['Order']['OrderCode'];
52
        $mapper['PaymentStatus'] = Enums\TransactionStatus::fromValue($response['Transactions'][0]['StatusId']);
53
        $mapper['TransactionType'] = Enums\TransactionType::fromValue($response['Transactions'][0]['TransactionType']['TransactionTypeId']);
54
        $mapper['Amount'] = $response['Transactions'][0]['Amount'];
55
        $mapper['Commission'] = $response['Transactions'][0]['Commission'];
56
        $mapper['Tags'] = $response['Transactions'][0]['Order']['Tags'];
57
        return $mapper;
58
    }
59
60
61
    public function getByTransactionV2($vivaTransaction)
62
    {
63
        // https://demo.vivapayments.com/checkout/v2/transactions/:transaction_id
64
        if (!$vivaTransaction)   throw new \Exception('Invalid transaction code');
65
        $token = $this->accessToken->getToken();
66
        $url = $this->getConfigSettings()->getEnvConfig('VIVA_API_URL') . '/checkout/v2/transactions/' . $vivaTransaction;
67
        $curl = new CurlWrapper($url);
68
        $curl->addHeader('Content-Type: application/json');
69
        $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1');
70
        $curl->setBearer($token);
71
        $response = $curl->get();
72
        $response = json_decode($response, true);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $response = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
73
        $response['PaymentStatus'] =  Enums\TransactionStatus::fromValue($response['statusId']);
74
        $response['TransactionType'] = Enums\TransactionType::fromValue($response['transactionTypeId']);
75
        return $response;
76
    }
77
78
79
    public function getByOrderCode($orderCode)
80
    {
81
        // https://demo.vivapayments.com/api/orders/:orderCode
82
        if (!$orderCode)   throw new \Exception('Invalid transaction code');
83
        $url = $this->getConfigSettings()->getEnvConfig('VIVA_URL');
84
        $curl = new CurlWrapper($url . '/api/orders/' . $orderCode);
85
        $curl->addHeader('Content-Type: application/json');
86
        $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1');
87
        $curl->addHeader('Accept: */*');
88
        $curl->setBasicAuth($this->getConfigSettings()->getEnvConfig('VIVA_MERCHANT_ID'), $this->getConfigSettings()->getEnvConfig('VIVA_API_KEY'));
89
        $response = $curl->get();
90
        $response = json_decode($response, true);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        $response = json_decode(/** @scrutinizer ignore-type */ $response, true);
Loading history...
91
        $response['orderStatus'] = Enums\OrderStatus::fromValue($response['StateId']);
92
        // $response['PaymentStatus'] = Enums\TransactionStatus::fromValue($response['StateId']);
93
  
94
        return $response;
95
    }
96
97
98
}
99