Skip to content

Commit

Permalink
complete purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Oct 14, 2024
1 parent 1431f9e commit 691b377
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Omnipay\Gomypay;

use Omnipay\Common\AbstractGateway;
use Omnipay\Gomypay\Message\AcceptNotificationRequest;
use Omnipay\Gomypay\Message\CompletePurchaseRequest;
use Omnipay\Gomypay\Message\PurchaseRequest;
use Omnipay\Gomypay\Traits\HasGomypay;

Expand Down Expand Up @@ -32,6 +34,16 @@ public function purchase(array $options = [])
return $this->createRequest(PurchaseRequest::class, $options);
}

public function completePurchase(array $options = [])
{
return $this->createRequest(CompletePurchaseRequest::class, $options);
}

public function acceptNotification(array $options = [])
{
return $this->createRequest(AcceptNotificationRequest::class, $options);
}

public function getPaymentInfo(array $options = [])
{
return $this->createRequest(GetPaymentInfoRequest::class, $options);
Expand Down
3 changes: 0 additions & 3 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;

/**
* Abstract Request
*/
abstract class AbstractRequest extends BaseAbstractRequest
{
protected $liveEndpoint = 'https://n.gomypay.asia/ShuntClass.aspx';
Expand Down
62 changes: 62 additions & 0 deletions src/Message/AcceptNotificationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Omnipay\Gomypay\Message;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Common\Message\NotificationInterface;
use Omnipay\Gomypay\Traits\HasGomypay;

class AcceptNotificationRequest extends AbstractRequest implements NotificationInterface
{
use HasGomypay;

public function getData()
{
return $this->httpRequest->request->all();
}

/**
* @throws InvalidRequestException
* @throws InvalidResponseException
*/
public function sendData($data)
{
$data = array_merge($data, [
'CustomerId' => $this->getCustomerId(),
'Amount' => (int) $this->getAmount(),
'Str_Check' => $this->getStrCheck(),
]);

if (! hash_equals($this->makeHash($data), $data['str_check'])) {
throw new InvalidResponseException('Invalid hash');
}

return $this->response = new AcceptNotificationResponse($this, $data);
}

public function getTransactionId()
{
return $this->getNotificationResponse()->getTransactionId();
}

public function getTransactionReference()
{
return $this->getNotificationResponse()->getTransactionReference();
}

public function getTransactionStatus()
{
return $this->getNotificationResponse()->getTransactionStatus();
}

public function getMessage()
{
return $this->getNotificationResponse()->getMessage();
}

private function getNotificationResponse()
{
return ! $this->response ? $this->send() : $this->response;
}
}
13 changes: 13 additions & 0 deletions src/Message/AcceptNotificationResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Omnipay\Gomypay\Message;

use Omnipay\Common\Message\NotificationInterface;

class AcceptNotificationResponse extends CompletePurchaseResponse implements NotificationInterface
{
public function getTransactionStatus()
{
return $this->isSuccessful() ? self::STATUS_COMPLETED : self::STATUS_FAILED;
}
}
36 changes: 36 additions & 0 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Omnipay\Gomypay\Message;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Exception\InvalidResponseException;
use Omnipay\Gomypay\Traits\HasGomypay;

class CompletePurchaseRequest extends AbstractRequest
{
use HasGomypay;

public function getData()
{
return $this->httpRequest->query->all();
}

/**
* @throws InvalidRequestException
* @throws InvalidResponseException
*/
public function sendData($data)
{
$data = array_merge($data, [
'CustomerId' => $this->getCustomerId(),
'Amount' => (int) $this->getAmount(),
'Str_Check' => $this->getStrCheck(),
]);

if (! hash_equals($this->makeHash($data), $data['str_check'])) {
throw new InvalidResponseException('Invalid hash');
}

return $this->response = new CompletePurchaseResponse($this, $data);
}
}
31 changes: 31 additions & 0 deletions src/Message/CompletePurchaseResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Omnipay\Gomypay\Message;

class CompletePurchaseResponse extends AbstractResponse
{
public function isSuccessful()
{
return (int) $this->getCode() === 1;
}

public function getCode()
{
return $this->data['result'];
}

public function getMessage()
{
return $this->data['ret_msg'];
}

public function getTransactionId()
{
return $this->data['e_orderno'];
}

public function getTransactionReference()
{
return $this->data['OrderID'];
}
}
5 changes: 0 additions & 5 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
use Omnipay\Gomypay\PaymentMethod;
use Omnipay\Gomypay\Traits\HasGomypay;

/**
* Authorize Request
*
* @method PurchaseResponse send()
*/
class PurchaseRequest extends AbstractRequest
{
use HasGomypay;
Expand Down
21 changes: 21 additions & 0 deletions src/Traits/HasGomypay.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,25 @@ public function setStr_Check($value)
{
return $this->setStrCheck($value);
}

public function makeHash(array $data)
{
$amount = 0;
if (array_key_exists('PayAmount', $data)) {
$amount = $data['PayAmount'];
} elseif (array_key_exists('e_money', $data)) {
$amount = $data['e_money'];
} elseif (array_key_exists('Amount', $data)) {
$amount = $data['Amount'];
}

return md5(implode('', [
$data['result'],
$data['e_orderno'],
$this->getCustomerId(),
$amount,
$data['OrderID'],
$this->getStrCheck(),
]));
}
}
169 changes: 169 additions & 0 deletions tests/Message/AcceptNotificationRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Omnipay\Gomypay\Tests\Message;

use Omnipay\Common\Message\NotificationInterface;
use Omnipay\Gomypay\Message\AcceptNotificationRequest;
use Omnipay\Tests\TestCase;

class AcceptNotificationRequestTest extends TestCase
{
/**
* @var AcceptNotificationRequest
*/
private $request;

public function setUp(): void
{
parent::setUp();

$this->request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize([
'CustomerId' => '80013554',
'Str_Check' => '2b1bef9d8ab6a81e9a2739c6ecc64ef8',
]);
}

public function testGetCreditCardData()
{
$this->getHttpRequest()->request->replace([
'Send_Type' => '0',
'result' => '1',
'ret_msg' => '授權成功',
'OrderID' => '2020050700000000001',
'e_Cur' => 'NT',
'e_money' => '50',
'e_date' => '20200507',
'e_time' => '12:30:59',
'e_orderno' => '2020050701',
'e_no' => '80013554',
'e_outlay' => '2',
'avcode' => '012345',
'str_check' => 'bf577c7a76d440a797c1716aff9c01c9',
'Invoice_No' => '12345',
'CardLastNum' => '2222',
]);

$this->assertEquals(NotificationInterface::STATUS_COMPLETED, $this->request->getTransactionStatus());
$this->assertEquals('授權成功', $this->request->getMessage());
$this->assertEquals('2020050701', $this->request->getTransactionId());
$this->assertEquals('2020050700000000001', $this->request->getTransactionReference());
}

public function testGetUnionPayData()
{
$this->getHttpRequest()->request->replace([
'Send_Type' => '1',
'result' => '1',
'ret_msg' => '授權成功',
'OrderID' => '2020050700000000001',
'e_Cur' => 'NT',
'e_money' => '50',
'e_date' => '20200507',
'e_time' => '12:30:59',
'e_orderno' => '2020050701',
'e_no' => '80013554',
'e_outlay' => '2',
'str_check' => 'bf577c7a76d440a797c1716aff9c01c9',
]);

$this->assertEquals(NotificationInterface::STATUS_COMPLETED, $this->request->getTransactionStatus());
$this->assertEquals('授權成功', $this->request->getMessage());
$this->assertEquals('2020050701', $this->request->getTransactionId());
$this->assertEquals('2020050700000000001', $this->request->getTransactionReference());
}

public function testGetBarcodeData()
{
$this->getHttpRequest()->request->replace([
'Send_Type' => '2',
'result' => '1',
'ret_msg' => '授權成功',
'OrderID' => '2020050700000000001',
'e_money' => '100',
'PayAmount' => '50',
'e_date' => '20200507',
'e_time' => '12:30:59',
'e_orderno' => '2020050701',
'e_payaccount' => '0055600701508856',
'str_check' => 'bf577c7a76d440a797c1716aff9c01c9',
]);

$this->assertEquals(NotificationInterface::STATUS_COMPLETED, $this->request->getTransactionStatus());
$this->assertEquals('授權成功', $this->request->getMessage());
$this->assertEquals('2020050701', $this->request->getTransactionId());
$this->assertEquals('2020050700000000001', $this->request->getTransactionReference());
}

public function testGetVirtualAccountData()
{
$this->getHttpRequest()->request->replace([
'Send_Type' => '4',
'result' => '1',
'ret_msg' => '授權成功',
'OrderID' => '2020050700000000001',
'e_money' => '100',
'PayAmount' => '50',
'e_date' => '20200507',
'e_time' => '12:30:59',
'e_orderno' => '2020050701',
'e_payaccount' => '0055600701508856',
'e_PayInfo' => '013,08856',
'str_check' => 'bf577c7a76d440a797c1716aff9c01c9',
]);

$this->assertEquals(NotificationInterface::STATUS_COMPLETED, $this->request->getTransactionStatus());
$this->assertEquals('授權成功', $this->request->getMessage());
$this->assertEquals('2020050701', $this->request->getTransactionId());
$this->assertEquals('2020050700000000001', $this->request->getTransactionReference());
}

public function testGetWebAtmData()
{
$this->getHttpRequest()->request->replace([
'Send_Type' => '3',
'result' => '1',
'ret_msg' => '授權成功',
'OrderID' => '2020050700000000001',
'e_money' => '100',
'PayAmount' => '50',
'e_date' => '20200507',
'e_time' => '12:30:59',
'e_orderno' => '2020050701',
'e_payaccount' => '0055600701508856',
'e_PayInfo' => '013,08856',
'str_check' => 'bf577c7a76d440a797c1716aff9c01c9',
]);

$this->assertEquals(NotificationInterface::STATUS_COMPLETED, $this->request->getTransactionStatus());
$this->assertEquals('授權成功', $this->request->getMessage());
$this->assertEquals('2020050701', $this->request->getTransactionId());
$this->assertEquals('2020050700000000001', $this->request->getTransactionReference());
}

public function testGetCvsData()
{
$this->getHttpRequest()->request->replace([
'Send_Type' => '6',
'StoreType' => '3',
'result' => '1',
'ret_msg' => '授權成功',
'OrderID' => '2020050700000000001',
'e_money' => '100',
'PayAmount' => '50',
'e_date' => '20200507',
'e_time' => '12:30:59',
'e_orderno' => '2020050701',
'PinCode' => 'GMPA2018383076',
'Barcode2' => '123456',
'Market_ID' => 'SE',
'Shop_Store_Name' => '7-11(地址)',
'str_check' => 'bf577c7a76d440a797c1716aff9c01c9',
]);

$this->assertEquals(NotificationInterface::STATUS_COMPLETED, $this->request->getTransactionStatus());
$this->assertEquals('授權成功', $this->request->getMessage());
$this->assertEquals('2020050701', $this->request->getTransactionId());
$this->assertEquals('2020050700000000001', $this->request->getTransactionReference());
}
}
Loading

0 comments on commit 691b377

Please sign in to comment.