Skip to content

Commit

Permalink
refactor: remove ECPay_*
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Jan 26, 2022
1 parent f33e778 commit 38d4b1f
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 92 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ composer.lock
vendor
build
.idea
.php_cs.cache
.php-cs-fixer.cache
.phpunit.result.cache
28 changes: 19 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="Omnipay Test Suite">
<directory>./tests/</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
39 changes: 39 additions & 0 deletions src/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Omnipay\ECPay;

use Omnipay\Common\Item as BaseItem;

class Item extends BaseItem
{
public function getCurrency()
{
return $this->getParameter('currency') ?: 'TWD';
}

public function setCurrency($value)
{
return $this->setParameter('currency', $value);
}

public function getUrl()
{
return $this->getParameter('url');
}

public function setUrl($value)
{
return $this->setParameter('url', $value);
}

public function __toString()
{
return sprintf(
'#%s %d %s x %u',
$this->getName(),
$this->getPrice(),
$this->getCurrency(),
$this->getQuantity()
);
}
}
10 changes: 1 addition & 9 deletions src/Message/AcceptNotificationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Omnipay\ECPay\Message;

use Exception;

class AcceptNotificationResponse extends CompletePurchaseResponse
{
/**
Expand All @@ -13,12 +11,6 @@ class AcceptNotificationResponse extends CompletePurchaseResponse
*/
public function getMessage()
{
try {
$this->checkoutFeedback();

return '1|OK';
} catch (Exception $e) {
return '0|'.$e->getMessage();
}
return '1|OK';
}
}
26 changes: 24 additions & 2 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Omnipay\ECPay\Message;

use Exception;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\NotificationInterface;
use Omnipay\Common\Message\ResponseInterface;
use Omnipay\ECPay\Traits\HasCustomFields;
use Omnipay\ECPay\Traits\HasDefaults;
use Omnipay\ECPay\Traits\HasECPay;
use Omnipay\ECPay\Traits\HasMerchantTradeNo;
use Omnipay\ECPay\Traits\HasStoreID;

class CompletePurchaseRequest extends AbstractRequest implements NotificationInterface
{
use HasECPay;
use HasDefaults;
use HasMerchantTradeNo;
use HasStoreID;
Expand Down Expand Up @@ -191,12 +195,13 @@ public function setCheckMacValue($value)
/**
* @return array
* @throws InvalidRequestException
* @throws InvalidResponseException
*/
public function getData()
{
$this->validate('MerchantID', 'CheckMacValue');

return [
return $this->checkMacValue([
'CustomField1' => $this->getCustomField1(),
'CustomField2' => $this->getCustomField2(),
'CustomField3' => $this->getCustomField3(),
Expand All @@ -214,7 +219,7 @@ public function getData()
'TradeDate' => $this->getTradeDate(),
'TradeNo' => $this->getTransactionReference(),
'CheckMacValue' => $this->getCheckMacValue(),
];
]);
}

/**
Expand Down Expand Up @@ -243,4 +248,21 @@ private function getNotification()
{
return ! $this->response ? $this->send() : $this->response;
}

/**
* @param array $data
* @return array
* @throws InvalidResponseException
*/
private function checkMacValue($data)
{
try {
$this->updateCheckMacValueFromGlobals($data);
$this->createECPay($this)->CheckOutFeedback();
} catch (Exception $e) {
throw new InvalidResponseException($e->getMessage(), $e->getCode(), $e);
}

return $data;
}
}
25 changes: 1 addition & 24 deletions src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

namespace Omnipay\ECPay\Message;

use Exception;
use Omnipay\Common\Message\NotificationInterface;
use Omnipay\ECPay\Traits\HasECPay;

class CompletePurchaseResponse extends AbstractResponse implements NotificationInterface
{
use HasECPay;

public function isSuccessful()
{
return $this->valid() && $this->getCode() === '1';
return $this->getCode() === '1';
}

/**
Expand All @@ -29,23 +25,4 @@ public function getTransactionStatus()
{
return $this->isSuccessful() ? self::STATUS_COMPLETED : self::STATUS_FAILED;
}

protected function checkoutFeedback()
{
$this->createECPay($this->request)->CheckOutFeedback();

return true;
}

/**
* @return bool
*/
private function valid()
{
try {
return $this->checkoutFeedback();
} catch (Exception $e) {
return false;
}
}
}
20 changes: 4 additions & 16 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Omnipay\ECPay\Message;

use ECPay_InvoiceState;
use ECPay_PaymentMethod;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\ECPay\Traits\HasATMFields;
Expand Down Expand Up @@ -152,10 +150,7 @@ private function getSendExtend($sendFields)
*/
private function getCreditFields($choosePayment)
{
return in_array($choosePayment, [
ECPay_PaymentMethod::ALL,
ECPay_PaymentMethod::Credit,
], true) ? [
return in_array($choosePayment, ['ALL', 'Credit'], true) ? [
'CreditInstallment' => $this->getCreditInstallment(),
'InstallmentAmount' => $this->getInstallmentAmount(),
'Redeem' => $this->getRedeem(),
Expand All @@ -177,10 +172,7 @@ private function getCreditFields($choosePayment)
*/
private function getATMFields($choosePayment)
{
return in_array($choosePayment, [
ECPay_PaymentMethod::ALL,
ECPay_PaymentMethod::ATM,
], true) ? [
return in_array($choosePayment, ['ALL', 'ATM'], true) ? [
'ExpireDate' => $this->getExpireDate(),
'PaymentInfoURL' => $this->getPaymentInfoURL(),
'ClientRedirectURL' => $this->getClientRedirectURL(),
Expand All @@ -193,11 +185,7 @@ private function getATMFields($choosePayment)
*/
private function getCvsFields($choosePayment)
{
return in_array($choosePayment, [
ECPay_PaymentMethod::ALL,
ECPay_PaymentMethod::CVS,
ECPay_PaymentMethod::BARCODE,
], true) ? [
return in_array($choosePayment, ['ALL', 'CVS', 'BARCODE'], true) ? [
'Desc_1' => $this->getDesc_1(),
'Desc_2' => $this->getDesc_2(),
'Desc_3' => $this->getDesc_3(),
Expand All @@ -214,7 +202,7 @@ private function getCvsFields($choosePayment)
*/
private function getInvoiceFields($invoiceMark)
{
return $invoiceMark === ECPay_InvoiceState::Yes ? [
return $invoiceMark === 'Y' ? [
'RelateNumber' => $this->getRelateNumber(),
'CustomerIdentifier' => $this->getCustomerIdentifier(),
'CarruerType' => $this->getCarruerType(),
Expand Down
6 changes: 2 additions & 4 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function getRedirectData()
}
}


try {
return static::htmlToArray($ecPay->CheckoutString());
} catch (Exception $e) {
Expand All @@ -93,9 +92,8 @@ private static function htmlToArray($html)

return ! $matches ? [] : array_reduce($matches[0], static function ($data, $input) {
preg_match_all('/\s*([^=]+)=\"([^\"]*)\"*/', $input, $m);
list($type, $name, $value) = $m[2];
if ($type !== 'submit') {
$data[$name] = $value;
if ($m[2][0] !== 'submit') {
$data[$m[2][1]] = $m[2][2];
}

return $data;
Expand Down
3 changes: 1 addition & 2 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Omnipay\ECPay\Message;

use ECPay_ActionType;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\ECPay\Traits\HasDefaults;
use Omnipay\ECPay\Traits\HasECPay;
Expand Down Expand Up @@ -33,7 +32,7 @@ public function getData()
return [
'MerchantTradeNo' => $this->getTransactionId(),
'TradeNo' => $this->getTransactionReference(),
'Action' => ECPay_ActionType::R,
'Action' => 'R',
'TotalAmount' => $this->getAmount(),
];
}
Expand Down
4 changes: 1 addition & 3 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Omnipay\ECPay\Message;

use ECPay_ActionType;

class VoidRequest extends RefundRequest
{
public function setAction($value)
Expand All @@ -13,7 +11,7 @@ public function setAction($value)

public function getAction()
{
return $this->getParameter('action') ?: ECPay_ActionType::N;
return $this->getParameter('action') ?: 'N';
}

public function getData()
Expand Down
7 changes: 4 additions & 3 deletions src/Traits/HasECPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

trait HasECPay
{
private $globalBackup = [];

/**
* @param $request
* @return ECPay_AllInOne
*/
protected function createECPay($request)
{
$this->updateCheckMacValueFromGlobals($request->getData());
$ecPay = new ECPay_AllInOne();
$ecPay->HashKey = $request->getHashKey();
$ecPay->HashIV = $request->getHashIV();
Expand All @@ -28,8 +29,8 @@ protected function createECPay($request)
*/
private function updateCheckMacValueFromGlobals($data)
{
if (! empty($data['CheckMacValue']) && empty($_POST['CheckMacValue'])) {
$_POST = $data;
if (array_key_exists('CheckMacValue', $data)) {
$_POST = array_merge($_POST, $data);
}

return $data;
Expand Down
Loading

0 comments on commit 38d4b1f

Please sign in to comment.