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 cf1f7c2
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 49 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
58 changes: 58 additions & 0 deletions src/Message/AcceptNotificationRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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, ['Amount' => (int) $this->getAmount()]);

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;
}
}
32 changes: 32 additions & 0 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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, ['Amount' => (int) $this->getAmount()]);

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'];
}
}
14 changes: 2 additions & 12 deletions src/Message/GetPaymentInfoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@ public function getData()
*/
public function sendData($data)
{
$data = array_merge($data, [
'CustomerId' => $this->getCustomerId(),
'Amount' => (int) $this->getAmount(),
'Str_Check' => $this->getStrCheck(),
]);
$data = array_merge($data, ['Amount' => (int) $this->getAmount()]);

$keys = ['result', 'e_orderno', 'CustomerId', 'Amount', 'OrderID', 'Str_Check'];
$plainText = '';
foreach ($keys as $key) {
$plainText .= $data[$key];
}
$hash = md5($plainText);
if (! hash_equals($hash, $data['str_check'])) {
if (! hash_equals($this->makeHash($data), $data['str_check'])) {
throw new InvalidResponseException('Invalid check');
}

Expand Down
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(),
]));
}
}
Loading

0 comments on commit cf1f7c2

Please sign in to comment.