stadem /
viva-payments
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Stadem\VivaPayments\Request; |
||||
| 4 | use Stadem\VivaPayments\Config\Config; |
||||
| 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 | public Config|Array $configObject; |
||||
| 18 | |||||
| 19 | public function __construct($accessToken,Config|array $configObject) |
||||
| 20 | { |
||||
| 21 | $this->accessToken = $accessToken; |
||||
| 22 | $this->configObject=$configObject; |
||||
| 23 | } |
||||
| 24 | |||||
| 25 | |||||
| 26 | public function getnById($vivaOrdercode) |
||||
| 27 | { |
||||
| 28 | return $vivaOrdercode; |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | public function getByTransaction($vivaTransaction) |
||||
| 32 | { |
||||
| 33 | // https://demo.vivapayments.com/api/transactions/:transaction_id |
||||
| 34 | if (!$vivaTransaction) throw new \Exception('Invalid transaction code'); |
||||
| 35 | $config = $this->configObject; |
||||
| 36 | $url = $config->getEnvConfig('VIVA_URL'); |
||||
| 37 | $curl = new CurlWrapper($url . '/api/transactions/' . $vivaTransaction); |
||||
| 38 | $curl->addHeader('Content-Type: application/json'); |
||||
| 39 | $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
||||
| 40 | $curl->addHeader('Accept: */*'); |
||||
| 41 | $curl->setBasicAuth($config->getEnvConfig('VIVA_MERCHANT_ID'), $config->getEnvConfig('VIVA_API_KEY')); |
||||
| 42 | $response = $curl->get(); |
||||
| 43 | // $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}'; |
||||
| 44 | $response = json_decode($response, true); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 45 | $response['Payment'] = $this->getByTransactionMapper($response); |
||||
| 46 | return $response; |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | |||||
| 50 | public function getByTransactionMapper($response) |
||||
| 51 | { |
||||
| 52 | $mapper['TransactionId'] = $response['Transactions'][0]['TransactionId']; |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 53 | $mapper['OrderCode'] = $response['Transactions'][0]['Order']['OrderCode']; |
||||
| 54 | $mapper['PaymentStatus'] = Enums\TransactionStatus::fromValue($response['Transactions'][0]['StatusId']); |
||||
| 55 | $mapper['TransactionType'] = Enums\TransactionType::fromValue($response['Transactions'][0]['TransactionType']['TransactionTypeId']); |
||||
| 56 | $mapper['Amount'] = $response['Transactions'][0]['Amount']; |
||||
| 57 | $mapper['Commission'] = $response['Transactions'][0]['Commission']; |
||||
| 58 | $mapper['Tags'] = $response['Transactions'][0]['Order']['Tags']; |
||||
| 59 | return $mapper; |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | |||||
| 63 | public function getByTransactionV2($vivaTransaction) |
||||
| 64 | { |
||||
| 65 | // https://demo.vivapayments.com/checkout/v2/transactions/:transaction_id |
||||
| 66 | if (!$vivaTransaction) throw new \Exception('Invalid transaction code'); |
||||
| 67 | $token = $this->accessToken->getToken(); |
||||
| 68 | $config = $this->configObject; |
||||
| 69 | $url = $config->getEnvConfig('VIVA_API_URL') . '/checkout/v2/transactions/' . $vivaTransaction; |
||||
| 70 | $curl = new CurlWrapper($url); |
||||
| 71 | $curl->addHeader('Content-Type: application/json'); |
||||
| 72 | $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
||||
| 73 | $curl->setBearer($token); |
||||
| 74 | $response = $curl->get(); |
||||
| 75 | $response = json_decode($response, true); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 76 | $response['PaymentStatus'] = Enums\TransactionStatus::fromValue($response['statusId']); |
||||
| 77 | $response['TransactionType'] = Enums\TransactionType::fromValue($response['transactionTypeId']); |
||||
| 78 | return $response; |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | |||||
| 82 | public function getByOrderCode($orderCode) |
||||
| 83 | { |
||||
| 84 | // https://demo.vivapayments.com/api/orders/:orderCode |
||||
| 85 | if (!$orderCode) throw new \Exception('Invalid transaction code'); |
||||
| 86 | $config = $this->configObject; |
||||
| 87 | $url = $config->getEnvConfig('VIVA_URL'); |
||||
| 88 | $curl = new CurlWrapper($url . '/api/orders/' . $orderCode); |
||||
| 89 | $curl->addHeader('Content-Type: application/json'); |
||||
| 90 | $curl->addHeader('User-Agent: PHPGatewayRuntime/0.0.1'); |
||||
| 91 | $curl->addHeader('Accept: */*'); |
||||
| 92 | $curl->setBasicAuth($config->getEnvConfig('VIVA_MERCHANT_ID'), $config->getEnvConfig('VIVA_API_KEY')); |
||||
| 93 | $response = $curl->get(); |
||||
| 94 | $response = json_decode($response, true); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 95 | $response['orderStatus'] = Enums\OrderStatus::fromValue($response['StateId']); |
||||
| 96 | // $response['PaymentStatus'] = Enums\TransactionStatus::fromValue($response['StateId']); |
||||
| 97 | return $response; |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | |||||
| 101 | } |
||||
| 102 |