From af09ed1c8a5f9597346c82c21f325401947a93da Mon Sep 17 00:00:00 2001 From: Johnny <> Date: Wed, 14 Jul 2021 08:18:32 +0800 Subject: [PATCH 1/3] edit .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d2c28d1..8656288 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor composer.lock -phpunit.xml \ No newline at end of file +phpunit.xml +.idea/ \ No newline at end of file From 07e4652c6d041f561fbfcf1082fd7e0a755a540b Mon Sep 17 00:00:00 2001 From: Johnny <> Date: Wed, 14 Jul 2021 08:08:42 +0800 Subject: [PATCH 2/3] php 5.6 support --- Constants/Cipher.php | 2 +- Constants/Period/AlterType.php | 6 +- Constants/Period/PeriodStartType.php | 6 +- Constants/Period/VersionType.php | 4 +- Cryption.php | 28 +++--- Exceptions/TradeInfoException.php | 4 +- Info/AliPayBasicInfo.php | 2 +- Info/AliPayInfo.php | 16 ++- Info/BasicInfo.php | 2 +- Info/Decorator/AliPayProduct.php | 3 +- Info/Decorator/Comment.php | 8 +- Info/Decorator/Enable.php | 2 +- Info/Decorator/EnableAliPay.php | 2 +- Info/Decorator/EnableAmericanExpress.php | 2 +- Info/Decorator/EnableBarcode.php | 2 +- Info/Decorator/EnableCredit.php | 4 +- Info/Decorator/EnableCreditBonus.php | 2 +- Info/Decorator/EnableCvs.php | 2 +- Info/Decorator/EnableCvsCom.php | 7 +- Info/Decorator/EnableEzPay.php | 2 +- Info/Decorator/EnableGooglePay.php | 2 +- Info/Decorator/EnableLinePay.php | 2 +- Info/Decorator/EnableSamsungPay.php | 2 +- Info/Decorator/EnableUnionPay.php | 2 +- Info/Decorator/EnableVacc.php | 2 +- Info/Decorator/EnableWebAtm.php | 2 +- Info/Decorator/Language.php | 5 +- Info/Decorator/OfflinePay.php | 11 +- Info/Decorator/PayCancel.php | 5 +- Info/Decorator/PayComplete.php | 5 +- Info/Decorator/PayInInstallments.php | 3 +- Info/Decorator/PayerEmailEditable.php | 5 +- Info/Decorator/TradeLimit.php | 5 +- Info/Info.php | 2 +- Info/Period/Decorator/Memo.php | 3 +- Info/Period/Decorator/Webhook.php | 2 +- Info/Period/Info.php | 24 ++--- Merchant.php | 30 ++++-- NewebPay.php | 122 +++++++++++------------ Response.php | 2 +- Tests/AliPayInfoTest.php | 4 +- Tests/InfoTest.php | 13 ++- Tests/NewebPayTest.php | 77 +------------- composer.json | 53 +++++----- phpunit.xml.dist | 2 +- 45 files changed, 214 insertions(+), 277 deletions(-) diff --git a/Constants/Cipher.php b/Constants/Cipher.php index c7ee43a..38146db 100644 --- a/Constants/Cipher.php +++ b/Constants/Cipher.php @@ -4,5 +4,5 @@ class Cipher { - public const METHOD = 'AES-256-CBC'; + const METHOD = 'AES-256-CBC'; } diff --git a/Constants/Period/AlterType.php b/Constants/Period/AlterType.php index a1de4a9..d63a861 100644 --- a/Constants/Period/AlterType.php +++ b/Constants/Period/AlterType.php @@ -5,13 +5,13 @@ class AlterType { // 暫停授權 - public const SUSPEND = 'suspend'; + const SUSPEND = 'suspend'; // 終止委託 // 終止委託後無法再次以[restart]進行啟用 - public const TERMINATE = 'terminate'; + const TERMINATE = 'terminate'; // 重新啟用已暫停授權, 於最近一期開始取授權 // 訂期定額委託單暫停後再啟用總期數不變, 扣款時間將向後展延至期數滿期 - public const RESTART = 'restart'; + const RESTART = 'restart'; } diff --git a/Constants/Period/PeriodStartType.php b/Constants/Period/PeriodStartType.php index c0a19d7..1a6af5d 100644 --- a/Constants/Period/PeriodStartType.php +++ b/Constants/Period/PeriodStartType.php @@ -16,18 +16,18 @@ class PeriodStartType * 委託建立當下系統將發動一筆十元信用卡授權交易,此交易授權成功後,系統將立即自動取消授權, * 付款人將不會被扣款。商店可於平台銷售中心/定期定額管理中查看此筆十元交易取消授權紀錄。 */ - public const _1 = '1'; + const _1 = '1'; /** * 委託單建立當下,立即執行委託金額授權。 * 若信用卡授權失敗,則該筆委託單將自動取消。 */ - public const _2 = '2'; + const _2 = '2'; /** * 委託單自動成立。首期授權時授權失敗,則系統會自動終止該張委託單。 * 首期授權成功,但第二期授權因其他原因授權失敗(額度不足或該張卡號以掛失..等原因), * 則系統仍會自動執行第三期、第四期...之授權,直到委託單終止。 */ - public const _3 = '3'; + const _3 = '3'; } diff --git a/Constants/Period/VersionType.php b/Constants/Period/VersionType.php index 308745f..810e117 100644 --- a/Constants/Period/VersionType.php +++ b/Constants/Period/VersionType.php @@ -7,10 +7,10 @@ class VersionType /** * 帶入 1.1 版本,則[背面末三碼]將為非必填 */ - public const V1_1 = '1.1'; + const V1_1 = '1.1'; /** * 帶入 1.0 版本,則[背面末三碼]將為必填欄位 */ - public const V1_0 = '1.0'; + const V1_0 = '1.0'; } diff --git a/Cryption.php b/Cryption.php index d7c0913..c961ac4 100644 --- a/Cryption.php +++ b/Cryption.php @@ -5,18 +5,15 @@ use fall1600\Package\Newebpay\Constants\Cipher; use fall1600\Package\Newebpay\Contracts\InfoInterface; use fall1600\Package\Newebpay\Exceptions\TradeInfoException; +use LogicException; trait Cryption { - protected $hashKey; - - protected $hashIv; - /** * @param InfoInterface $info * @return string */ - public function countTradeInfo(InfoInterface $info) + public function countTradeInfo($info) { $infoPayload = $info->getInfo(); @@ -27,10 +24,10 @@ public function countTradeInfo(InfoInterface $info) * @param string $tradeInfo * @return string */ - public function countTradeSha(string $tradeInfo) + public function countTradeSha($tradeInfo) { - if (! $tradeInfo) { - throw new \LogicException('empty trade info'); + if (!$tradeInfo) { + throw new LogicException('empty trade info'); } return strtoupper( @@ -57,7 +54,7 @@ public function getHashIv() return $this->hashIv; } - public function createEncryptedStr(array $infoPayload = []) + public function createEncryptedStr($infoPayload = []) { return trim( bin2hex( @@ -65,7 +62,7 @@ public function createEncryptedStr(array $infoPayload = []) $this->addPadding(http_build_query($infoPayload)), Cipher::METHOD, $this->hashKey, - OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->hashIv ) ) @@ -78,10 +75,10 @@ public function createEncryptedStr(array $infoPayload = []) * @return string * @throws TradeInfoException */ - public function decryptTradeInfo(string $tradeInfo) + public function decryptTradeInfo($tradeInfo) { - if (! $tradeInfo) { - throw new \LogicException('empty trade info'); + if (!$tradeInfo) { + throw new LogicException('empty trade info'); } return $this->stripPadding( @@ -89,13 +86,14 @@ public function decryptTradeInfo(string $tradeInfo) hex2bin($tradeInfo), Cipher::METHOD, $this->hashKey, - OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->hashIv ) ); } - protected function addPadding(string $string, int $blockSize = 32) + + protected function addPadding($string, $blockSize = 32) { $len = strlen($string); $pad = $blockSize - ($len % $blockSize); diff --git a/Exceptions/TradeInfoException.php b/Exceptions/TradeInfoException.php index 8a3a841..bc3e3c0 100644 --- a/Exceptions/TradeInfoException.php +++ b/Exceptions/TradeInfoException.php @@ -2,7 +2,9 @@ namespace fall1600\Package\Newebpay\Exceptions; -class TradeInfoException extends \Exception +use Exception; + +class TradeInfoException extends Exception { } diff --git a/Info/AliPayBasicInfo.php b/Info/AliPayBasicInfo.php index 243a9b0..1862277 100644 --- a/Info/AliPayBasicInfo.php +++ b/Info/AliPayBasicInfo.php @@ -19,7 +19,7 @@ public function getInfo() 'ItemDesc' => $this->order->getItemDesc(), 'MerchantOrderNo' => $this->order->getMerchantOrderNo(), 'Email' => $this->payer->getEmail(), - 'LoginType' => $this->payer->getLoginType()? 1: 0, + 'LoginType' => $this->payer->getLoginType() ? 1 : 0, // AliPay 必填 'Receiver' => $this->payer->getReceiver(), 'Tel1' => $this->payer->getTel1(), diff --git a/Info/AliPayInfo.php b/Info/AliPayInfo.php index 9b78b26..790aa4d 100644 --- a/Info/AliPayInfo.php +++ b/Info/AliPayInfo.php @@ -2,9 +2,6 @@ namespace fall1600\Package\Newebpay\Info; -use fall1600\Package\Newebpay\Contracts\AliPayPayerInterface; -use fall1600\Package\Newebpay\Contracts\OrderInterface; - abstract class AliPayInfo extends Info { /** @@ -14,12 +11,13 @@ abstract class AliPayInfo extends Info protected $count; public function __construct( - string $merchantId, - string $notifyUrl, - OrderInterface $order, - AliPayPayerInterface $payer, - int $count - ) { + $merchantId = "", + $notifyUrl = "", + $order = null, + $payer = null, + $count = 0 + ) + { parent::__construct($merchantId, $notifyUrl, $order, $payer); $this->count = $count; diff --git a/Info/BasicInfo.php b/Info/BasicInfo.php index eb30983..bb73030 100644 --- a/Info/BasicInfo.php +++ b/Info/BasicInfo.php @@ -18,7 +18,7 @@ public function getInfo() 'ItemDesc' => $this->order->getItemDesc(), 'MerchantOrderNo' => $this->order->getMerchantOrderNo(), 'Email' => $this->payer->getEmail(), - 'LoginType' => $this->payer->getLoginType()? 1: 0, + 'LoginType' => $this->payer->getLoginType() ? 1 : 0, ]; } } diff --git a/Info/Decorator/AliPayProduct.php b/Info/Decorator/AliPayProduct.php index f1af7ed..d57361b 100644 --- a/Info/Decorator/AliPayProduct.php +++ b/Info/Decorator/AliPayProduct.php @@ -19,8 +19,9 @@ class AliPayProduct extends AliPayInfo /** @var AliPayProductInterface */ protected $product; - public function __construct(AliPayInfo $info, int $index, AliPayProductInterface $product) + public function __construct($info, $index, $product) { + parent::__construct(); $this->info = $info; $this->index = $index; diff --git a/Info/Decorator/Comment.php b/Info/Decorator/Comment.php index d674d5e..7f97fd7 100644 --- a/Info/Decorator/Comment.php +++ b/Info/Decorator/Comment.php @@ -4,6 +4,7 @@ use fall1600\Package\Newebpay\Info\Info; use fall1600\Package\Newebpay\Info\InfoDecorator; +use LogicException; class Comment extends InfoDecorator { @@ -17,8 +18,9 @@ class Comment extends InfoDecorator */ protected $comment; - public function __construct(Info $info, string $comment) + public function __construct($info, $comment) { + parent::__construct(); $this->info = $info; $this->setComment($comment); @@ -32,10 +34,10 @@ public function getInfo() ]; } - protected function setComment(string $comment) + protected function setComment($comment) { if (mb_strlen($comment) > 300) { - throw new \LogicException('comment must less or equal than 300'); + throw new LogicException('comment must less or equal than 300'); } $this->comment = $comment; diff --git a/Info/Decorator/Enable.php b/Info/Decorator/Enable.php index dde0be7..8020eef 100644 --- a/Info/Decorator/Enable.php +++ b/Info/Decorator/Enable.php @@ -13,7 +13,7 @@ abstract class Enable extends InfoDecorator /** @var bool */ protected $isEnable; - public function __construct(Info $info, bool $isEnable = true) + public function __construct($info, $isEnable = true) { $this->info = $info; diff --git a/Info/Decorator/EnableAliPay.php b/Info/Decorator/EnableAliPay.php index d8d6a24..9e518fc 100644 --- a/Info/Decorator/EnableAliPay.php +++ b/Info/Decorator/EnableAliPay.php @@ -12,7 +12,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'ALIPAY' => $this->isEnable? 1: 0, + 'ALIPAY' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableAmericanExpress.php b/Info/Decorator/EnableAmericanExpress.php index dce866e..ed12198 100644 --- a/Info/Decorator/EnableAmericanExpress.php +++ b/Info/Decorator/EnableAmericanExpress.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'CREDITAE' => $this->isEnable? 1: 0, + 'CREDITAE' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableBarcode.php b/Info/Decorator/EnableBarcode.php index 879345a..4c1610c 100644 --- a/Info/Decorator/EnableBarcode.php +++ b/Info/Decorator/EnableBarcode.php @@ -13,7 +13,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'BARCODE' => $this->isEnable? 1: 0, + 'BARCODE' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableCredit.php b/Info/Decorator/EnableCredit.php index 64c5a07..449b6b7 100644 --- a/Info/Decorator/EnableCredit.php +++ b/Info/Decorator/EnableCredit.php @@ -10,7 +10,7 @@ class EnableCredit extends Enable /** @var QuickCreditInterface|null */ protected $quickCredit; - public function __construct(Info $info, ?QuickCreditInterface $quickCredit = null) + public function __construct($info, $quickCredit = null) { parent::__construct($info); @@ -21,7 +21,7 @@ public function getInfo() { $result = $this->info->getInfo() + [ - 'CREDIT' => $this->isEnable? 1: 0, + 'CREDIT' => $this->isEnable ? 1 : 0, ]; if ($this->quickCredit) { diff --git a/Info/Decorator/EnableCreditBonus.php b/Info/Decorator/EnableCreditBonus.php index 34b4502..6eeb595 100644 --- a/Info/Decorator/EnableCreditBonus.php +++ b/Info/Decorator/EnableCreditBonus.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'CreditRed' => $this->isEnable? 1: 0, + 'CreditRed' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableCvs.php b/Info/Decorator/EnableCvs.php index 7f7b2a1..66bf8c4 100644 --- a/Info/Decorator/EnableCvs.php +++ b/Info/Decorator/EnableCvs.php @@ -13,7 +13,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'CVS' => $this->isEnable? 1: 0, + 'CVS' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableCvsCom.php b/Info/Decorator/EnableCvsCom.php index 62a37eb..68766f6 100644 --- a/Info/Decorator/EnableCvsCom.php +++ b/Info/Decorator/EnableCvsCom.php @@ -4,6 +4,7 @@ use fall1600\Package\Newebpay\Info\Info; use fall1600\Package\Newebpay\Info\InfoDecorator; +use LogicException; class EnableCvsCom extends InfoDecorator { @@ -20,7 +21,7 @@ class EnableCvsCom extends InfoDecorator */ protected $type; - public function __construct(Info $info, int $type) + public function __construct($info, $type) { $this->info = $info; @@ -43,10 +44,10 @@ public function getInfo() /** * @param int $type */ - protected function setType(int $type) + protected function setType($type) { if ($type < 0 || $type > 3) { - throw new \LogicException('Newebpay does not support this type'); + throw new LogicException('Newebpay does not support this type'); } $this->type = $type; diff --git a/Info/Decorator/EnableEzPay.php b/Info/Decorator/EnableEzPay.php index 613435a..4282c96 100644 --- a/Info/Decorator/EnableEzPay.php +++ b/Info/Decorator/EnableEzPay.php @@ -12,7 +12,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'P2G' => $this->isEnable? 1: 0, + 'P2G' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableGooglePay.php b/Info/Decorator/EnableGooglePay.php index 85bd7ea..0b079ac 100644 --- a/Info/Decorator/EnableGooglePay.php +++ b/Info/Decorator/EnableGooglePay.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'ANDROIDPAY' => $this->isEnable? 1: 0, + 'ANDROIDPAY' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableLinePay.php b/Info/Decorator/EnableLinePay.php index b0ecd82..e1b33aa 100644 --- a/Info/Decorator/EnableLinePay.php +++ b/Info/Decorator/EnableLinePay.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'LINEPAY' => $this->isEnable? 1: 0, + 'LINEPAY' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableSamsungPay.php b/Info/Decorator/EnableSamsungPay.php index 4e2079d..b3ae08c 100644 --- a/Info/Decorator/EnableSamsungPay.php +++ b/Info/Decorator/EnableSamsungPay.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'SAMSUNGPAY' => $this->isEnable? 1: 0, + 'SAMSUNGPAY' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableUnionPay.php b/Info/Decorator/EnableUnionPay.php index 4991287..feec31d 100644 --- a/Info/Decorator/EnableUnionPay.php +++ b/Info/Decorator/EnableUnionPay.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'UNIONPAY' => $this->isEnable? 1: 0, + 'UNIONPAY' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableVacc.php b/Info/Decorator/EnableVacc.php index 94401ba..68aa024 100644 --- a/Info/Decorator/EnableVacc.php +++ b/Info/Decorator/EnableVacc.php @@ -13,7 +13,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'VACC' => $this->isEnable? 1: 0, + 'VACC' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/EnableWebAtm.php b/Info/Decorator/EnableWebAtm.php index bbb5c6f..5f70cd7 100644 --- a/Info/Decorator/EnableWebAtm.php +++ b/Info/Decorator/EnableWebAtm.php @@ -8,7 +8,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'WEBATM' => $this->isEnable? 1: 0, + 'WEBATM' => $this->isEnable ? 1 : 0, ]; } } diff --git a/Info/Decorator/Language.php b/Info/Decorator/Language.php index f13982e..9db29ab 100644 --- a/Info/Decorator/Language.php +++ b/Info/Decorator/Language.php @@ -14,8 +14,9 @@ class Language extends InfoDecorator /** @var string */ protected $language; - public function __construct(Info $info, string $language) + public function __construct($info, $language) { + parent::__construct(); $this->info = $info; $this->language = $language; @@ -25,7 +26,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'LangType' => $this->language ?? LanguageType::ZH_TW, + 'LangType' => isset($this->language) ? $this->language : LanguageType::ZH_TW, ]; } } diff --git a/Info/Decorator/OfflinePay.php b/Info/Decorator/OfflinePay.php index a4e7fe6..5ccde91 100644 --- a/Info/Decorator/OfflinePay.php +++ b/Info/Decorator/OfflinePay.php @@ -4,6 +4,7 @@ use fall1600\Package\Newebpay\Info\Info; use fall1600\Package\Newebpay\Info\InfoDecorator; +use LogicException; class OfflinePay extends InfoDecorator { @@ -26,7 +27,7 @@ class OfflinePay extends InfoDecorator */ protected $customerUrl; - public function __construct(Info $info, string $customerUrl, int $ttl = null) + public function __construct($info, $customerUrl, $ttl = null) { $this->info = $info; @@ -44,14 +45,14 @@ public function getInfo() ]; } - protected function setTtl(int $ttl = null) + protected function setTtl($ttl = null) { if ($ttl <= 0) { - throw new \LogicException('ttl must be large than 1'); + throw new LogicException('ttl must be large than 1'); } if ($ttl > 180) { - throw new \LogicException('ttl must be less than or equal to 180'); + throw new LogicException('ttl must be less than or equal to 180'); } $this->ttl = $ttl; @@ -62,7 +63,7 @@ protected function setTtl(int $ttl = null) */ protected function countExpireDate() { - if (! $this->ttl) { + if (!$this->ttl) { return ''; } diff --git a/Info/Decorator/PayCancel.php b/Info/Decorator/PayCancel.php index 3b33fa4..e5d7446 100644 --- a/Info/Decorator/PayCancel.php +++ b/Info/Decorator/PayCancel.php @@ -17,8 +17,9 @@ class PayCancel extends InfoDecorator */ protected $clientBackUrl; - public function __construct(Info $info, string $clientBackUrl = null) + public function __construct($info, $clientBackUrl = null) { + parent::__construct(); $this->info = $info; $this->setClientBackUrl($clientBackUrl); @@ -32,7 +33,7 @@ public function getInfo() ]; } - protected function setClientBackUrl(string $clientBackUrl = null) + protected function setClientBackUrl($clientBackUrl = null) { $this->clientBackUrl = $clientBackUrl; } diff --git a/Info/Decorator/PayComplete.php b/Info/Decorator/PayComplete.php index c7a8657..450052f 100644 --- a/Info/Decorator/PayComplete.php +++ b/Info/Decorator/PayComplete.php @@ -17,8 +17,9 @@ class PayComplete extends InfoDecorator */ protected $returnUrl; - public function __construct(Info $info, string $returnUrl) + public function __construct($info, $returnUrl) { + parent::__construct(); $this->info = $info; $this->setReturnUrl($returnUrl); @@ -32,7 +33,7 @@ public function getInfo() ]; } - protected function setReturnUrl(string $returnUrl = null) + protected function setReturnUrl($returnUrl = null) { $this->returnUrl = $returnUrl; } diff --git a/Info/Decorator/PayInInstallments.php b/Info/Decorator/PayInInstallments.php index 9d44de6..2c474d6 100644 --- a/Info/Decorator/PayInInstallments.php +++ b/Info/Decorator/PayInInstallments.php @@ -19,8 +19,9 @@ class PayInInstallments extends InfoDecorator */ protected $instFlag; - public function __construct(Info $info, string $instFlag) + public function __construct($info, $instFlag) { + parent::__construct(); $this->info = $info; $this->instFlag = $instFlag; diff --git a/Info/Decorator/PayerEmailEditable.php b/Info/Decorator/PayerEmailEditable.php index d6f7813..810d665 100644 --- a/Info/Decorator/PayerEmailEditable.php +++ b/Info/Decorator/PayerEmailEditable.php @@ -13,8 +13,9 @@ class PayerEmailEditable extends InfoDecorator /** @var bool */ protected $canEditEmail; - public function __construct(Info $info, bool $canEditEmail = true) + public function __construct($info, $canEditEmail = true) { + parent::__construct(); $this->info = $info; $this->canEditEmail = $canEditEmail; @@ -24,7 +25,7 @@ public function getInfo() { return $this->info->getInfo() + [ - 'EmailModify' => $this->canEditEmail? 1: 0, + 'EmailModify' => $this->canEditEmail ? 1 : 0, ]; } } diff --git a/Info/Decorator/TradeLimit.php b/Info/Decorator/TradeLimit.php index 5d1240b..91cfdbd 100644 --- a/Info/Decorator/TradeLimit.php +++ b/Info/Decorator/TradeLimit.php @@ -19,8 +19,9 @@ class TradeLimit extends InfoDecorator */ protected $limit; - public function __construct(Info $info, int $limit = 0) + public function __construct($info, $limit = 0) { + parent::__construct(); $this->info = $info; $this->setLimit($limit); @@ -34,7 +35,7 @@ public function getInfo() ]; } - protected function setLimit(int $limit) + protected function setLimit($limit) { if (1 <= $limit && $limit <= 60) { $limit = 60; diff --git a/Info/Info.php b/Info/Info.php index 864b2a1..f67c9fa 100644 --- a/Info/Info.php +++ b/Info/Info.php @@ -40,7 +40,7 @@ abstract class Info implements InfoInterface */ abstract public function getInfo(); - public function __construct(string $merchantId, string $notifyUrl, OrderInterface $order, PayerInterface $payer) + public function __construct($merchantId = "", $notifyUrl = "", $order = null, $payer = null) { $this->order = $order; diff --git a/Info/Period/Decorator/Memo.php b/Info/Period/Decorator/Memo.php index d46f0af..ffa902f 100644 --- a/Info/Period/Decorator/Memo.php +++ b/Info/Period/Decorator/Memo.php @@ -3,6 +3,7 @@ namespace fall1600\Package\Newebpay\Info\Period\Decorator; use fall1600\Package\Newebpay\Info\Period\Info; +use LogicException; class Memo extends Info { @@ -34,7 +35,7 @@ public function getInfo() protected function setMemo(string $memo) { if (mb_strlen($memo) > 255) { - throw new \LogicException("unsupported length of this memo $memo"); + throw new LogicException("unsupported length of this memo $memo"); } $this->memo = $memo; diff --git a/Info/Period/Decorator/Webhook.php b/Info/Period/Decorator/Webhook.php index c033234..34dc55f 100644 --- a/Info/Period/Decorator/Webhook.php +++ b/Info/Period/Decorator/Webhook.php @@ -16,7 +16,7 @@ abstract class Webhook extends Info */ protected $url; - public function __construct(Info $info, string $url) + public function __construct($info, $url) { $this->info = $info; diff --git a/Info/Period/Info.php b/Info/Period/Info.php index 35a2bc8..64cca82 100644 --- a/Info/Period/Info.php +++ b/Info/Period/Info.php @@ -3,7 +3,6 @@ namespace fall1600\Package\Newebpay\Info\Period; //use fall1600\Package\Newebpay\Constants\Period\PeriodStartType; -use fall1600\Package\Newebpay\Constants\Period\VersionType; use fall1600\Package\Newebpay\Contracts\InfoInterface; use fall1600\Package\Newebpay\Contracts\Period\ContactInterface; use fall1600\Package\Newebpay\Contracts\Period\OrderInterface; @@ -38,17 +37,18 @@ abstract class Info implements InfoInterface abstract public function getInfo(); /** - * @param OrderInterface $order - * @param ContactInterface $contact - * @param string $periodStartType - * @param string $version + * @param OrderInterface $order + * @param ContactInterface $contact + * @param string $periodStartType + * @param string $version */ public function __construct( - OrderInterface $order, - ContactInterface $contact, - string $periodStartType, - string $version = VersionType::V1_0 - ) { + $order, + $contact, + $periodStartType, + $version + ) + { $this->order = $order; $this->contact = $contact; @@ -58,7 +58,7 @@ public function __construct( $this->setVersion($version); } - protected function setPeriodStartType(string $periodStartType) + protected function setPeriodStartType($periodStartType) { // if (! PeriodStartType::isValid($periodStartType)) { // throw new \LogicException("unsupported version $periodStartType"); @@ -67,7 +67,7 @@ protected function setPeriodStartType(string $periodStartType) $this->periodStartType = $periodStartType; } - protected function setVersion(string $version) + protected function setVersion($version) { // if (! VersionType::isValid($version)) { // throw new \LogicException("unsupported version $version"); diff --git a/Merchant.php b/Merchant.php index ad7b2e8..9009c24 100644 --- a/Merchant.php +++ b/Merchant.php @@ -3,6 +3,7 @@ namespace fall1600\Package\Newebpay; use fall1600\Package\Newebpay\Exceptions\TradeInfoException; +use LogicException; class Merchant { @@ -32,7 +33,7 @@ class Merchant */ protected $response; - public function __construct(string $id, string $hashKey, string $hashIv) + public function __construct($id, $hashKey, $hashIv) { $this->id = $id; @@ -44,12 +45,12 @@ public function __construct(string $id, string $hashKey, string $hashIv) /** * 因為商城代號, hashKey, hashIv 是一整組的, 要就一次修改 * response 是屬於個別商城的, 改商城順手清空response - * @param string $id - * @param string $hashKey - * @param string $hashIv + * @param string $id + * @param string $hashKey + * @param string $hashIv * @return $this */ - public function reset(string $id, string $hashKey, string $hashIv) + public function reset($id, $hashKey, $hashIv) { $this->id = $id; $this->hashKey = $hashKey; @@ -66,9 +67,9 @@ public function reset(string $id, string $hashKey, string $hashIv) * @return $this * @throws TradeInfoException */ - public function setRawData(array $rawData) + public function setRawData($rawData) { - if (! isset($rawData['TradeInfo']) || ! isset($rawData['TradeSha'])) { + if (!isset($rawData['TradeInfo']) || !isset($rawData['TradeSha'])) { throw new TradeInfoException('invalid data'); } @@ -87,10 +88,9 @@ public function setRawData(array $rawData) */ public function validateResponse() { - if (! $this->response) { - throw new \LogicException('set rawData first'); + if (!$this->response) { + throw new LogicException('set rawData first'); } - return $this->response->getTradeSha() === $this->countTradeSha($this->response->getTradeInfo()); } @@ -105,7 +105,7 @@ public function getId() /** * @return string */ - public function getHashKey(): string + public function getHashKey() { return $this->hashKey; } @@ -125,4 +125,12 @@ public function getResponse() { return $this->response; } + + /** + * @return string + */ + private function removeCtrlChr($str) + { + return preg_replace('/[\x00-\x1F\x7F]/', '', $str); + } } diff --git a/NewebPay.php b/NewebPay.php index eab3039..a75c1b9 100644 --- a/NewebPay.php +++ b/NewebPay.php @@ -5,7 +5,7 @@ use fall1600\Package\Newebpay\Contracts\OrderInterface; use fall1600\Package\Newebpay\Exceptions\TradeInfoException; use fall1600\Package\Newebpay\Info\Info; -use fall1600\Package\Newebpay\Info\Period\Info as PeriodInfo; +use LogicException; class NewebPay { @@ -13,67 +13,67 @@ class NewebPay * 藍新說是哪一版就是哪一版 * @var string */ - public const VERSION = '1.5'; + const VERSION = '1.6'; /** * 付款-測試環境 * @var string */ - public const CHECKOUT_URL_TEST = 'https://ccore.newebpay.com/MPG/mpg_gateway'; + const CHECKOUT_URL_TEST = 'https://ccore.newebpay.com/MPG/mpg_gateway'; /** * 付款-正式環境 * @var string */ - public const CHECKOUT_URL_PRODUCTION = 'https://core.newebpay.com/MPG/mpg_gateway'; + const CHECKOUT_URL_PRODUCTION = 'https://core.newebpay.com/MPG/mpg_gateway'; /** * 查詢付款資訊-測試環境 * @var string */ - public const QUERY_URL_TEST = 'https://ccore.newebpay.com/API/QueryTradeInfo'; + const QUERY_URL_TEST = 'https://ccore.newebpay.com/API/QueryTradeInfo'; /** * 查詢付款資訊-正式環境 * @var string */ - public const QUERY_URL_PRODUCTION = 'https://core.newebpay.com/API/QueryTradeInfo'; + const QUERY_URL_PRODUCTION = 'https://core.newebpay.com/API/QueryTradeInfo'; /** * 建立定期定額委託單-測試環境 * @var string */ - public const ISSUE_URL_TEST = 'https://ccore.newebpay.com/MPG/period'; + const ISSUE_URL_TEST = 'https://ccore.newebpay.com/MPG/period'; /** * 建立定期定額委託單-正式環境 * @var string */ - public const ISSUE_URL_PRODUCTION = 'https://core.newebpay.com/MPG/period'; + const ISSUE_URL_PRODUCTION = 'https://core.newebpay.com/MPG/period'; /** * 修改已建立委託單狀態-測試環境 * @var string */ - public const ALTER_STATUS_URL_TEST = 'https://ccore.newebpay.com/MPG/period/AlterStatus'; + const ALTER_STATUS_URL_TEST = 'https://ccore.newebpay.com/MPG/period/AlterStatus'; /** * 修改已建立委託單狀態-正式環境 * @var string */ - public const ALTER_STATUS_URL_PRODUCTION = 'https://core.newebpay.com/MPG/period/AlterStatus'; + const ALTER_STATUS_URL_PRODUCTION = 'https://core.newebpay.com/MPG/period/AlterStatus'; /** * 修改已建立委託單內容-測試環境 * @var string */ - public const ALTER_AMT_URL_TEST = 'https://ccore.newebpay.com/MPG/period/AlterAmt'; + const ALTER_AMT_URL_TEST = 'https://ccore.newebpay.com/MPG/period/AlterAmt'; /** * 修改已建立委託單內容-正式環境 * @var string */ - public const ALTER_AMT_URL_PRODUCTION = 'https://core.newebpay.com/MPG/period/AlterAmt'; + const ALTER_AMT_URL_PRODUCTION = 'https://core.newebpay.com/MPG/period/AlterAmt'; /** * 決定URL 要使用正式或測試機 @@ -101,17 +101,18 @@ class NewebPay * @throws TradeInfoException */ public function alterAmt( - string $orderNo, - string $periodNo, - string $periodType, - string $periodPoint, - int $alterAmt = null - ) { - if (! $this->merchant) { - throw new \LogicException('empty merchant'); + $orderNo, + $periodNo, + $periodType, + $periodPoint, + $alterAmt = null + ) + { + if (!$this->merchant) { + throw new LogicException('empty merchant'); } - $url = $this->isProduction? static::ALTER_AMT_URL_PRODUCTION: static::ALTER_AMT_URL_TEST; + $url = $this->isProduction ? static::ALTER_AMT_URL_PRODUCTION : static::ALTER_AMT_URL_TEST; $data = [ 'RespondType' => 'JSON', @@ -133,7 +134,7 @@ public function alterAmt( ]; $resp = $this->post($url, $payload); - if (! isset($resp['period'])) { + if (!isset($resp['period'])) { throw new TradeInfoException("interface change from Newebpay"); } @@ -147,13 +148,13 @@ public function alterAmt( * @return array * @throws TradeInfoException */ - public function alterStatus(string $orderNo, string $periodNo, string $alterType) + public function alterStatus($orderNo, $periodNo, $alterType) { - if (! $this->merchant) { - throw new \LogicException('empty merchant'); + if (!$this->merchant) { + throw new LogicException('empty merchant'); } - $url = $this->isProduction? static::ALTER_STATUS_URL_PRODUCTION: static::ALTER_STATUS_URL_TEST; + $url = $this->isProduction ? static::ALTER_STATUS_URL_PRODUCTION : static::ALTER_STATUS_URL_TEST; $payload = [ 'MerchantID_' => $this->merchant->getId(), @@ -170,14 +171,14 @@ public function alterStatus(string $orderNo, string $periodNo, string $alterType ]; $resp = $this->post($url, $payload); - if (! isset($resp['period'])) { + if (!isset($resp['period'])) { throw new TradeInfoException("interface change from Newebpay"); } return json_decode($this->merchant->decryptTradeInfo($resp['period']), true); } - public function issue(PeriodInfo $info) + public function issue($info) { echo << @@ -196,13 +197,13 @@ public function issue(PeriodInfo $info) EOT; } - public function generatePeriodForm(PeriodInfo $info) + public function generatePeriodForm($info) { - if (! $this->merchant) { - throw new \LogicException('empty merchant'); + if (!$this->merchant) { + throw new LogicException('empty merchant'); } - $url = $this->isProduction? static::ISSUE_URL_PRODUCTION: static::ISSUE_URL_TEST; + $url = $this->isProduction ? static::ISSUE_URL_PRODUCTION : static::ISSUE_URL_TEST; $tradeInfo = $this->merchant->countTradeInfo($info); @@ -214,7 +215,7 @@ public function generatePeriodForm(PeriodInfo $info) EOT; } - public function checkout(Info $info) + public function checkout($info) { echo << @@ -237,13 +238,13 @@ public function checkout(Info $info) * @param Info $info * @return string */ - public function generateForm(Info $info) + public function generateForm($info) { - if (! $this->merchant) { - throw new \LogicException('empty merchant'); + if (!$this->merchant) { + throw new LogicException('empty merchant'); } - $url = $this->isProduction ? static::CHECKOUT_URL_PRODUCTION: static::CHECKOUT_URL_TEST; + $url = $this->isProduction ? static::CHECKOUT_URL_PRODUCTION : static::CHECKOUT_URL_TEST; $tradeInfo = $this->merchant->countTradeInfo($info); @@ -261,34 +262,17 @@ public function generateForm(Info $info) EOT; } - public function checkoutForApi(Info $info) - { - if (! $this->merchant) { - throw new \LogicException('empty merchant'); - } - - return [ - 'url' => $this->isProduction ? static::CHECKOUT_URL_PRODUCTION: static::CHECKOUT_URL_TEST, - 'form_params' => [ - 'MerchantID' => $this->merchant->getId(), - 'TradeInfo' => $tradeInfo = $this->merchant->countTradeInfo($info), - 'TradeSha' => $this->merchant->countTradeSha($tradeInfo), - 'Version' => static::VERSION, - ], - ]; - } - /** - * @param OrderInterface $order + * @param OrderInterface $order * @return array */ - public function query(OrderInterface $order) + public function query($order) { - if (! $this->merchant) { - throw new \LogicException('empty merchant'); + if (!$this->merchant) { + throw new LogicException('empty merchant'); } - $url = $this->isProduction? static::QUERY_URL_PRODUCTION: static::QUERY_URL_TEST; + $url = $this->isProduction ? static::QUERY_URL_PRODUCTION : static::QUERY_URL_TEST; $payload = [ 'MerchantID' => $this->merchant->getId(), @@ -303,21 +287,21 @@ public function query(OrderInterface $order) return $this->post($url, $payload); } - public function setIsProduction(bool $isProduction) + public function setIsProduction($isProduction) { $this->isProduction = $isProduction; return $this; } - public function setFormId(string $formId) + public function setFormId($formId) { $this->formId = $formId; return $this; } - public function setMerchant(Merchant $merchant) + public function setMerchant($merchant) { $this->merchant = $merchant; @@ -325,10 +309,10 @@ public function setMerchant(Merchant $merchant) } /** - * @param OrderInterface $order + * @param OrderInterface $order * @return string */ - protected function countCheckValue(OrderInterface $order) + protected function countCheckValue($order) { $payload = [ 'IV' => $this->merchant->getHashIv(), @@ -341,7 +325,7 @@ protected function countCheckValue(OrderInterface $order) return strtoupper(hash('sha256', http_build_query($payload))); } - protected function post(string $url, array $payload) + protected function post($url, $payload) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); @@ -352,4 +336,12 @@ protected function post(string $url, array $payload) return json_decode($result, true); } + + /** + * @return Merchant + */ + public function getMerchant() + { + return $this->merchant; + } } diff --git a/Response.php b/Response.php index 2a8a0e1..ed07daa 100644 --- a/Response.php +++ b/Response.php @@ -19,7 +19,7 @@ class Response * @param string $tradeInfo * @param string $tradeSha */ - public function __construct(array $data, string $tradeInfo, string $tradeSha) + public function __construct($data, $tradeInfo, $tradeSha) { $this->data = $data; diff --git a/Tests/AliPayInfoTest.php b/Tests/AliPayInfoTest.php index d3ad848..80cc94b 100644 --- a/Tests/AliPayInfoTest.php +++ b/Tests/AliPayInfoTest.php @@ -6,8 +6,8 @@ use fall1600\Package\Newebpay\Contracts\AliPayProductInterface; use fall1600\Package\Newebpay\Contracts\OrderInterface; use fall1600\Package\Newebpay\Info\AliPayBasicInfo; -use fall1600\Package\Newebpay\Info\Decorator\EnableAliPay; use fall1600\Package\Newebpay\Info\Decorator\AliPayProduct; +use fall1600\Package\Newebpay\Info\Decorator\EnableAliPay; use fall1600\Package\Newebpay\Info\Decorator\EnableCredit; use fall1600\Package\Newebpay\Merchant; use fall1600\Package\Newebpay\NewebPay; @@ -23,7 +23,7 @@ public function test_getInfo() $order->expects($this->once()) ->method('getMerchantOrderNo') - ->willReturn($orderMerchantNo = (string) time()); + ->willReturn($orderMerchantNo = (string)time()); $order->expects($this->once()) ->method('getItemDesc') diff --git a/Tests/InfoTest.php b/Tests/InfoTest.php index ed9ff1a..21c9729 100644 --- a/Tests/InfoTest.php +++ b/Tests/InfoTest.php @@ -5,12 +5,12 @@ use fall1600\Package\Newebpay\Constants\LanguageType; use fall1600\Package\Newebpay\Contracts\OrderInterface; use fall1600\Package\Newebpay\Contracts\PayerInterface; +use fall1600\Package\Newebpay\Info\BasicInfo; use fall1600\Package\Newebpay\Info\Decorator\Language; use fall1600\Package\Newebpay\Info\Decorator\OfflinePay; +use fall1600\Package\Newebpay\Info\Decorator\PayCancel; use fall1600\Package\Newebpay\Info\Decorator\PayComplete; use fall1600\Package\Newebpay\Info\Decorator\PayerEmailEditable; -use fall1600\Package\Newebpay\Info\BasicInfo; -use fall1600\Package\Newebpay\Info\Decorator\PayCancel; use fall1600\Package\Newebpay\Merchant; use fall1600\Package\Newebpay\NewebPay; use PHPUnit\Framework\TestCase; @@ -20,9 +20,9 @@ class InfoTest extends TestCase public function test_info() { //arrange - $merchant = new Merchant('merchant.id.123','hash.key.123','hash.iv.4567890'); + $merchant = new Merchant('merchant.id.123', 'hash.key.123', 'hash.iv.4567890'); - $returnUrl= 'return.url'; + $returnUrl = 'return.url'; $notifyUrl = 'notify.url'; @@ -33,7 +33,7 @@ public function test_info() $order->expects($this->once()) ->method('getMerchantOrderNo') - ->willReturn($orderMerchantNo = (string) time()); + ->willReturn($orderMerchantNo = (string)time()); $order->expects($this->once()) ->method('getItemDesc') @@ -76,8 +76,7 @@ public function test_info() $ttl ), $returnUrl - ) - ; + ); $result = $info->getInfo(); //assert diff --git a/Tests/NewebPayTest.php b/Tests/NewebPayTest.php index 6220d44..7bf81fe 100644 --- a/Tests/NewebPayTest.php +++ b/Tests/NewebPayTest.php @@ -48,7 +48,7 @@ public function test_sendTransaction() $email = 'test@gg.cc'; - $returnUrl= 'return.url'; + $returnUrl = 'return.url'; $notifyUrl = 'notify.url'; @@ -90,79 +90,4 @@ public function test_sendTransaction() //assert $this->assertEquals($expected, $result); } - - public function test_checkoutByApi() - { - //arrange - $newebpay = new NewebPay(); - - $merchantId = 'merchant.id.123'; - - $hashKey = 'hash.key.234'; - - $hashIv = 'hash.iv.34567890'; - -// $merchant = new Merchant($merchantId, $hashKey, $hashIv); - - $merchant = $this->getMockBuilder(Merchant::class) - ->disableOriginalConstructor() - ->getMock(); - - $merchant->expects($this->atLeastOnce()) - ->method('getId') - ->willReturn($merchantId); - - $merchant->expects($this->once()) - ->method('countTradeInfo') - ->willReturn($tradeInfo = '12345'); - - $merchant->expects($this->once()) - ->method('countTradeSha') - ->willReturn($tradeSha = '22345'); - - $email = 'test@gg.cc'; - - $returnUrl= 'return.url'; - - $notifyUrl = 'notify.url'; - - $clientBackUrl = 'client.back.url'; - - $order = $this->getMockBuilder(OrderInterface::class) - ->getMock(); - - $payer = $this->getMockBuilder(PayerInterface::class) - ->getMock(); - - $ttl = 3; - - $customerUrl = 'customer.url'; - - $info = new BasicInfo($merchant->getId(), $notifyUrl, $order, $payer); - $info = new PayerEmailEditable($info, $email); - $info = new Language($info, LanguageType::EN); - $info = new PayCancel($info, $clientBackUrl); - $info = new OfflinePay($info, $customerUrl, $ttl); - $info = new PayComplete($info, $returnUrl); - - $expected = [ - 'url' => 'https://ccore.newebpay.com/MPG/mpg_gateway', - 'form_params' => [ - 'MerchantID' => $merchantId, - 'TradeInfo' => $tradeInfo, - 'TradeSha' => $tradeSha, - 'Version' => '1.5', - ], - ]; - - //act - $result = $newebpay - ->setIsProduction(false) - ->setMerchant($merchant) - ->checkoutForApi($info); - - //assert - $this->assertEquals($expected, $result); - - } } diff --git a/composer.json b/composer.json index a07cdfd..9b98651 100644 --- a/composer.json +++ b/composer.json @@ -1,28 +1,31 @@ { - "name": "fall1600/newebpay", - "description": "Payment solution of NewebPay(藍新金流), implementing by pure PHP", - "keywords": ["newebpay", "payment"], - "type": "library", - "license": "MIT", - "authors": [ - { - "name": "fall1600", - "email": "fall1600@gmail.com" - } - ], - "minimum-stability": "stable", - "require": { - "ext-openssl": "*", - "ext-mbstring": "*", - "ext-json": "*", - "ext-curl": "*" - }, - "autoload": { - "psr-4": { - "fall1600\\Package\\Newebpay\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] + "name": "fall1600/newebpay", + "description": "Payment solution of NewebPay(藍新金流), implementing by pure PHP", + "keywords": [ + "newebpay", + "payment" + ], + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "fall1600", + "email": "fall1600@gmail.com" } + ], + "minimum-stability": "stable", + "require": { + "ext-openssl": "*", + "ext-mbstring": "*", + "ext-json": "*", + "ext-curl": "*" + }, + "autoload": { + "psr-4": { + "fall1600\\Package\\Newebpay\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1fb1519..24ec7ae 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,7 @@ failOnWarning="true" > - + From 3a6ebd5b400f2354f7e9079c716b44bbe8b7e148 Mon Sep 17 00:00:00 2001 From: Johnny <> Date: Wed, 14 Jul 2021 08:14:50 +0800 Subject: [PATCH 3/3] implement period --- Cryption.php | 19 +++++ Merchant.php | 26 ++++--- PeriodResponse.php | 172 ++++++++++++++++++++++++++++++++++++++++++ Response.php | 125 +----------------------------- TradeInfoResponse.php | 141 ++++++++++++++++++++++++++++++++++ 5 files changed, 350 insertions(+), 133 deletions(-) create mode 100644 PeriodResponse.php create mode 100644 TradeInfoResponse.php diff --git a/Cryption.php b/Cryption.php index c961ac4..87fdaf9 100644 --- a/Cryption.php +++ b/Cryption.php @@ -92,6 +92,25 @@ public function decryptTradeInfo($tradeInfo) ); } + /** + * 從藍新回傳的加密交易資訊解成可讀的字串 + * @param string $period + * @return string + * @throws TradeInfoException + */ + public function decryptPeriod($period) + { + if (!$period) { + throw new LogicException('empty period'); + } + return openssl_decrypt( + hex2bin($period), + Cipher::METHOD, + $this->hashKey, + OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, + $this->hashIv + ); + } protected function addPadding($string, $blockSize = 32) { diff --git a/Merchant.php b/Merchant.php index 9009c24..21716fc 100644 --- a/Merchant.php +++ b/Merchant.php @@ -69,16 +69,19 @@ public function reset($id, $hashKey, $hashIv) */ public function setRawData($rawData) { - if (!isset($rawData['TradeInfo']) || !isset($rawData['TradeSha'])) { + if (isset($rawData['Period'])) { + $this->response = new PeriodResponse( + json_decode($this->removeCtrlChr($this->decryptPeriod($rawData['Period'])), true), + $rawData['Period']); + } else if (isset($rawData['TradeInfo'], $rawData['TradeSha'])) { + $this->response = new TradeInfoResponse( + json_decode($this->removeCtrlChr($this->decryptTradeInfo($rawData['TradeInfo'])), true), + $rawData['TradeInfo'], + $rawData['TradeSha'] + ); + } else { throw new TradeInfoException('invalid data'); } - - $this->response = new Response( - json_decode($this->decryptTradeInfo($rawData['TradeInfo']), true), - $rawData['TradeInfo'], - $rawData['TradeSha'] - ); - return $this; } @@ -91,7 +94,12 @@ public function validateResponse() if (!$this->response) { throw new LogicException('set rawData first'); } - return $this->response->getTradeSha() === $this->countTradeSha($this->response->getTradeInfo()); + if ($this->response instanceof TradeInfoResponse) { + return $this->response->getTradeSha() === $this->countTradeSha($this->response->getTradeInfo()); + } + if ($this->response instanceof PeriodResponse) { + return true; + } } /** diff --git a/PeriodResponse.php b/PeriodResponse.php new file mode 100644 index 0000000..cfcd21b --- /dev/null +++ b/PeriodResponse.php @@ -0,0 +1,172 @@ +period = $period; + } + + /** + * 商城代號 + * @return string|null + */ + public function getMerchantId() + { + return isset($this->data['Result']['MerchantID']) ? $this->data['Result']['MerchantID'] : null; + } + + /** + * 商店自訂的定期定額訂單編號 + * @return string|null + */ + public function getMerchantOrderNo() + { + return isset($this->data['Result']['MerchantOrderNo']) ? $this->data['Result']['MerchantOrderNo'] : null; + } + + /** + * 委託單週期 + * @return string|null + */ + public function getPeriodType() + { + return isset($this->data['Result']['PeriodType']) ? $this->data['Result']['PeriodType'] : null; + } + + /** + * 授權次數 + * @return int|null + */ + public function getAuthTimes() + { + $times = 0; + if (isset($this->data['Result']['AuthTimes'])) { + $times = (int)$this->data['Result']['AuthTimes']; + } + return $times; + } + + /** + * 授權時間 + * @return string|null + */ + public function getAuthTime() + { + return isset($this->data['Result']['AuthTime']) ? $this->data['Result']['AuthTime'] : null; + } + + /** + * 授權排程日期 + * @return string|null + */ + public function getDateArray() + { + return isset($this->data['Result']['DateArray']) ? $this->data['Result']['DateArray'] : null; + } + + /** + * 藍新金流交易序號 + * @return string|null + */ + public function getTradeNo() + { + return isset($this->data['Result']['TradeNo']) ? $this->data['Result']['TradeNo'] : null; + } + + /** + * 卡號前六後四碼 + * @return string|null + */ + public function getCardNo() + { + return isset($this->data['Result']['CardNo']) ? $this->data['Result']['CardNo'] : null; + } + + /** + * 每期金額 + * @return int|null + */ + public function getPeriodAmt() + { + $amt = 0; + if (isset($this->data['Result']['PeriodAmt'])) { + $amt = (int)$this->data['Result']['PeriodAmt']; + } + return $amt; + } + + /** + * 授權碼 + * @return string|null + */ + public function getAuthCode() + { + return isset($this->data['Result']['AuthCode']) ? $this->data['Result']['AuthCode'] : null; + } + + /** + * string // 銀行回應碼 + * @return string|null + */ + public function getRespondCode() + { + return isset($this->data['Result']['RespondCode']) ? $this->data['Result']['RespondCode'] : null; + } + + /** + * 款項保管銀行 + * @return string|null + */ + public function getEscrowBank() + { + return isset($this->data['Result']['EscrowBank']) ? $this->data['Result']['EscrowBank'] : null; + } + + /** + * 收單機構 + * @return string|null + */ + public function getAuthBank() + { + return isset($this->data['Result']['AuthBank']) ? $this->data['Result']['AuthBank'] : null; + } + + /** + * 交易類別 + * @return string|null + */ + public function getPaymentMethod() + { + return isset($this->data['Result']['PaymentMethod']) ? $this->data['Result']['PaymentMethod'] : null; + } + + /** + * 委託單號 + * @return string|null + */ + public function getPeriodNo() + { + return isset($this->data['Result']['PeriodNo']) ? $this->data['Result']['PeriodNo'] : null; + } + + /** + * 信用卡到期日 + * @return string|null + */ + public function getExtday() + { + return isset($this->data['Result']['Extday']) ? $this->data['Result']['Extday'] : null; + } +} diff --git a/Response.php b/Response.php index ed07daa..f95f8cd 100644 --- a/Response.php +++ b/Response.php @@ -7,25 +7,12 @@ class Response /** @var array */ protected $data; - /** @var string */ - protected $tradeInfo; - - /** @var string */ - protected $tradeSha; - /** - * 注入藍新來的回傳值 * @param array $data - * @param string $tradeInfo - * @param string $tradeSha */ - public function __construct($data, $tradeInfo, $tradeSha) + public function __construct($data) { $this->data = $data; - - $this->tradeInfo = $tradeInfo; - - $this->tradeSha = $tradeSha; } /** @@ -56,114 +43,4 @@ public function getResult() { return $this->data['Result']; } - - /** - * 商城代號 - * @return string|null - */ - public function getMerchantId() - { - return $this->data['Result']['MerchantID'] ?? null; - } - - /** - * 交易金額 - * @return int|null - */ - public function getAmt() - { - return ((int) $this->data['Result']['Amt']) ?? null; - } - - /** - * 交易序號 - * @return string|null - */ - public function getTradeNo() - { - return $this->data['Result']['TradeNo'] ?? null; - } - - /** - * 用來跟藍新溝通的訂單編號, 也就是OrderInterface 提供的MerchantOrderNo - * @return string|null - */ - public function getMerchantOrderNo() - { - return $this->data['Result']['MerchantOrderNo'] ?? null; - } - - /** - * 付款方式 - * @return string|null - */ - public function getPaymentType() - { - return $this->data['Result']['PaymentType'] ?? null; - } - - public function getResponseType() - { - return $this->data['Result']['ResponseType'] ?? null; - } - - /** - * 付款完成時間 - * @return string|null - */ - public function getPayTime() - { - return $this->data['Result']['PayTime'] ?? null; - } - - /** - * 付款人取號或交易時的ip - * @return string|null - */ - public function getIp() - { - return $this->data['Result']['IP'] ?? null; - } - - /** - * 款項保管銀行 - * @return string|null - */ - public function getEscrowBank() - { - return $this->data['Result']['EscrowBank'] ?? null; - } - - /** - * 離線金流會帶繳費到期日回來, 格式: yyyy-mm-dd - * @return string|null - */ - public function getExpireDate() - { - return $this->data['Result']['ExpireDate'] ?? null; - } - - /** - * @return string - */ - public function getTradeInfo() - { - return $this->tradeInfo; - } - - /** - * @return string - */ - public function getTradeSha() - { - return $this->tradeSha; - } - - /** - * @return array - */ - public function getData() - { - return $this->data; - } } diff --git a/TradeInfoResponse.php b/TradeInfoResponse.php new file mode 100644 index 0000000..7ce6c60 --- /dev/null +++ b/TradeInfoResponse.php @@ -0,0 +1,141 @@ +tradeInfo = $tradeInfo; + + $this->tradeSha = $tradeSha; + } + + /** + * /** + * 商城代號 + * @return string|null + */ + public function getMerchantId() + { + return isset($this->data['Result']['MerchantID']) ? $this->data['Result']['MerchantID'] : null; + } + + /** + * 交易金額 + * @return int|null + */ + public function getAmt() + { + $amt = 0; + if (isset($this->data['Result']['Amt'])) { + $amt = (int)$this->data['Result']['Amt']; + } + return $amt; + } + + /** + * 交易序號 + * @return string|null + */ + public function getTradeNo() + { + return isset($this->data['Result']['TradeNo']) ? $this->data['Result']['TradeNo'] : null; + } + + /** + * 用來跟藍新溝通的訂單編號, 也就是OrderInterface 提供的MerchantOrderNo + * @return string|null + */ + public function getMerchantOrderNo() + { + return isset($this->data['Result']['MerchantOrderNo']) ? $this->data['Result']['MerchantOrderNo'] : null; + } + + /** + * 付款方式 + * @return string|null + */ + public function getPaymentType() + { + return isset($this->data['Result']['PaymentType']) ? $this->data['Result']['PaymentType'] : null; + } + + public function getResponseType() + { + return isset($this->data['Result']['ResponseType']) ? $this->data['Result']['ResponseType'] : null; + } + + /** + * 付款完成時間 + * @return string|null + */ + public function getPayTime() + { + return isset($this->data['Result']['PayTime']) ? $this->data['Result']['PayTime'] : null; + } + + /** + * 付款人取號或交易時的ip + * @return string|null + */ + public function getIp() + { + return isset($this->data['Result']['IP']) ? $this->data['Result']['IP'] : null; + } + + /** + * 款項保管銀行 + * @return string|null + */ + public function getEscrowBank() + { + return isset($this->data['Result']['EscrowBank']) ? $this->data['Result']['EscrowBank'] : null; + } + + /** + * 離線金流會帶繳費到期日回來, 格式: yyyy-mm-dd + * @return string|null + */ + public function getExpireDate() + { + return isset($this->data['Result']['ExpireDate']) ? $this->data['Result']['ExpireDate'] : null; + } + + /** + * @return string + */ + public function getTradeInfo() + { + return $this->tradeInfo; + } + + /** + * @return string + */ + public function getTradeSha() + { + return $this->tradeSha; + } + + /** + * @return array + */ + public function getData() + { + return $this->data; + } +}