Skip to content

Instantly share code, notes, and snippets.

@tvaliasek
Created January 16, 2017 14:31
Show Gist options
  • Save tvaliasek/c5a8e72a826af73624d4f25f4f610263 to your computer and use it in GitHub Desktop.
Save tvaliasek/c5a8e72a826af73624d4f25f4f610263 to your computer and use it in GitHub Desktop.
parameters:
gopay:
testConfig: {'goid': 8540000425, 'clientId': 1834419554, 'clientSecret': 'sgdgsdfgsdfgsdfgs', 'isProductionMode': false, 'timeout': 600}
productionConfig: {'goid': 8174868614, 'clientId': 1577708517, 'clientSecret': 'sgsdgsdfgsdfgsd', 'isProductionMode': true, 'timeout': 600}
enableProduction: true
returnUrl: 'https://www.xfgsdgsfg.cz/gopay-return/'
notifyUrl: 'https://www.sdgsdgsdfgsdfg.cz/gopay-notify/'
<?php
namespace App\Model;
use Nette,
Nette\Utils\Strings,
Nette\Database\Connection;
class GopayAPI extends Nette\Object {
private $testConfig = array();
private $productionConfig = array();
private $activeConfig = array();
private $isProduction = false;
private $returnUrl;
private $notifyUrl;
private $gopay;
private $database;
public static $constPaymentStatus = [
'CREATED' => 0,
'PAYMENT_METHOD_CHOSEN' => 1,
'PAID' => 2,
'AUTHORIZED' => 3,
'CANCELED' => 4,
'TIMEOUTED' => 5,
'REFUNDED' => 6,
'PARTIALLY_REFUNDED' => 7];
public static $humanPaymentStatus = ['Založena',
'Metoda vybrána',
'Zaplacena',
'Předautorizována',
'Zrušena',
'Vypršela platnost',
'Refundována',
'Částečně refundována'];
public function __construct(Nette\Database\Context $database, $testConfig, $productionConfig, $returnUrl, $notifyUrl, $isProduction=false) {
$this->database = $database;
$this->testConfig = $testConfig;
$this->productionConfig = $productionConfig;
$this->returnUrl = $returnUrl;
$this->notifyUrl = $notifyUrl;
$this->testConfig['language'] = $this->productionConfig['language'] = \GoPay\Definition\Language::CZECH;
$this->isProduction = $isProduction;
$this->activeConfig = ($this->isProduction!==true) ? $this->testConfig : $this->productionConfig;
$this->gopay = \GoPay\Api::payments($this->activeConfig);
}
public function createPayment($order) {
$payment = array(
'payer'=>$order['payer'],
'target' => array('type' => 'ACCOUNT',
'goid' => $this->activeConfig['goid']),
'amount' => $order['totalPrice'],
'currency' => 'CZK',
'order_number' => $order['id'],
'order_description' => $order['desc'],
'items' => $order['items'],
'callback' => array(
'return_url' => $this->returnUrl,
'notification_url' => $this->notifyUrl
),
'lang' => \GoPay\Definition\Language::CZECH
);
$response = $this->gopay->createPayment($payment);
if($response->hasSucceed()){
$this->savePaymentGopayId($order['id'], $response->json['id'], $response->json['state']);
return $response->json['gw_url'];
} else {
return false;
}
}
private function savePaymentGopayId($orderId, $gopayId, $gopayStatus){
$payment = $this->database->table('platby')->where('(gopayPaymentId IS NULL) AND objednavky_id = ?', $orderId)->limit(1)->fetch();
if($payment!=false){
$status = (key_exists($gopayStatus, self::$constPaymentStatus)) ? self::$constPaymentStatus[$gopayStatus] : null;
$payment->update(array('gopayPaymentId'=>$gopayId, 'stav'=>$status));
}
}
public function checkUpdatePayment($gopayId){
$paymentRec = $this->database->table('platby')->where('gopayPaymentId = ?', $gopayId)->limit(1)->fetch();
//\Tracy\Debugger::log('Předáno ID: '.$gopayId);
if($paymentRec!=false){
//\Tracy\Debugger::log('Nalezen záznam platby ID: '.$paymentRec->id);
$response = $this->gopay->getStatus($gopayId);
//\Tracy\Debugger::log('Odpověď GOPAY: '.json_encode($response));
if($response->hasSucceed()){
$status = $response->json['state'];
if(key_exists($status, self::$constPaymentStatus)){
$paymentRec->update(array('stav'=>self::$constPaymentStatus[$status]));
return self::$constPaymentStatus[$status];
}
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment