interface InterfacePurchase {
public function getId();
public function getItemId();
public function setItemId ($val);
public function getItemType();
public function setItemType ($val);
public function getPrice();
public function setPrice ($val);
public function getUserId();
public function setUserId($val);
public function getStatus();
public function setStatus($val);
public function save ();
/**
*
*/
public function callbackPayment ();
/**
* -. ,
*/
public function getItem ();
}
class CPurchase {
protected $_mPurchase = null ;
/**
* @return InterfacePurchase
**/
public static function createPurchaseByType ($type) {
$purchase = null ;
switch ($type){
case PURCHASE_SHOP: $purchase = new CPurchaseShop(); break;
case PURCHASE_ACCOUNT: $purchase = new CPurchaseAccount(); break;
case PURCHASE_RAIT: $purchase = new CPurchaseRait(); break;
// ...
default : throw new ExceptionUnknownPurchaseType (__CLASS__);
}
$purchase->_mPurchase = new CPurchaseItem ();
return $purchase;
}
/**
* @return InterfacePurchase
**/
public static function loadPurchaseById($id){
$purchase_item = CPurchaseItem::getById($id);
$purchase = self::createPurchaseByType($purchase_item->getType());
$purchase->_mPurchase = $purchase_item;
}
public function getId() { return $ this ->_mPurchase->getId(); }
public function getItemId() { return $ this ->_mPurchase->getItemId();}
public function setItemId ($val) { return $ this ->_mPurchase->setItemId( $val ); }
public function getItemType() { return $ this ->_mPurchase->getItemType(); }
public function setItemType ($val) { return $ this ->_mPurchase->setItemType( $val ); }
public function getPrice() { return $ this ->_mPurchase->getPrice (); }
public function setPrice ($val) { return $ this ->_mPurchase->setPrice ( $val ); }
public function getUserId() { return $ this ->_mPurchase->getUserId(); }
public function setUserId($val) { return $ this ->_mPurchase->setUserId($val); }
public function getStatus() { return $ this ->_mPurchase->getStatus(); }
public function setStatus($val) { return $ this ->_mPurchase->setStatus($val); }
public function save () { $ this ->_mPurchase->save(); }
}
Class CPurchaseAccount extends CPurchase implements InterfacePurchase {
public function getItem (){
$item = null ;
If ($item_id = $ this ->getItemId()) {
$item = CMembership::getById($item_id);
}
return $item;
}
public function callbackPayment () {
$ this ->setStatus(PURCHASE_STATUS_OK);
ServiceAccount::setMembership($ this ->getUserId(), $ this ->getItemId());
}
}
* This source code was highlighted with Source Code Highlighter .
interface InterfaceBilling {
public function getId();
public function getPurchaseId();
public function setPurchaseId ($val);
public function getBillingType();
public function setBillingType ($val);
public function getStatus();
public function setStatus($val);
public function save ();
/**
*
*/
public function redirectToBilling ();
/**
* , ,
*/
public static function checkResponseFormat ($data);
/**
*
*/
public function checkResult ($data);
/**
* . , .
*/
public function addResultInView ($view, $results);
}
class CBilling {
protected $_mBilling = null ;
/**
* @return InterfaceBilling
**/
public static function createBillingByType( $type ) {
switch ($type){
case BILLING_ROBOX: $billing = new CBillingRobox(); break;
case BILLING_WM: $billing = new CBillingWM(); break;
// ...
default : throw new ExceptionUnknownBillingType (__CLASS__);
}
$billing->_mBilling = new CBillingItem();
$ this ->setBillingType($type);
}
public static function getBillingTypeByRequest($response_data) {
$billing_type = null ;
if (CBillingRobox::checkResponseFormat($response_data)) {
$billing_type = self::BILLING_ROBOX;
}
if (CBillingWM::checkResponseFormat($response_data)) {
$billing_type = self::BILLING_WM;
}
return $billing_type;
}
public function getId() { return $ this ->_mBilling->getId(); }
public function getPurchaseId() { return $ this ->_mBilling->getPurchaseId(); }
public function setPurchaseId ($val) { return $ this ->_mBilling->setPurchaseId($val); }
public function getBillingType() { return $ this ->_mBilling->getBillingType(); }
public function setBillingType ($val) { return $ this ->_mBilling->setBillingType($val); }
public function getStatus() { return $ this ->_mBilling->getStatus(); }
public function setStatus($val) { return $ this ->_mBilling->setStatus($val); }
public function save () { $ this ->_mBilling->save(); }
public function checkSumm($summ) {
$purchase = CPurchaseItem::getById($ this ->getPurchaseId());
return intval($purchase->getPrice()) == intval($summ);
}
public function checkStatusNotFinish() {
$purchase = CPurchaseItem::getById($ this ->getPurchaseId());
return PURCHASE_STATUS_OK != $purchase->getStatus();
}
}
class CBillingRobox extends CBilling implements InterfaceBilling {
public function redirectToBilling () {
$redirect_uri = Config::getKey( 'pay_uri' , 'robox' );
$purchase = CPurchaseItem::getById($ this ->getPurchaseId());
$hash = array(
'MrchLogin' => Config::getKey( 'merchant_login' , 'robox' ),
'OutSum' => $purchase->getPrice(),
'InvId' => $ this ->getId(),
'SignatureValue' => $ this ->_getSignatureValue()
);
MyApplication::redirect($redirect_uri, $hash);
}
public static function checkResponseFormat ($data) {
$is_id = isset($data[ 'InvId' ]);
$is_summ = isset($data[ 'OutSum' ]);
$is_resp_crc = isset($data[ 'SignatureValue' ]);
$result = $is_id && $is_summ && $is_resp_crc;
return $result;
}
public function checkResult ($data) {
$billing_item_id = isset($data[ 'InvId' ])? $data[ 'InvId' ]:0;
$summ = isset($data[ 'OutSum' ])? $data[ 'OutSum' ]:0;
$result = FALSE;
$purchase = null ;
try {
$ this ->_mBilling = CBillingItem::sgetById($billing_item_id);
$purchase = CPurchase::loadPurchaseById($ this ->getPurchaseId());
} catch (ExObjectNotFound $e) {}
if ($ this ->_mBilling && $purchase) {
$is_valid_control_summ = $ this ->_checkControlSumm($data);
$is_valid_summ = $ this ->_checkSumm($summ);
$is_valid_status = $ this ->_checkStatusNotFinish();
if ($is_valid_control_summ && $is_valid_summ && $is_valid_status) {
$result = TRUE;
$ this ->callbackPayment();
$purchase->callbackPayment();
}
}
return $result;
}
public function addResultInView ($view, $result) {
if ($result && $ this ->getId()) {
$view->addText( "OK" );
$view->addText($ this ->getId());
} else {
$view->addText( "ERROR" );
}
}
private function _getSignatureValue() {
$purchase = CPurchaseItem::getById($ this ->getPurchaseId());
$hash = array(
Config::getKey( 'merchant_login' , 'robox' ) ,
$purchase->getPrice(),
$ this ->getId(),
Config::getKey( 'merchant_password1' , 'robox' )
);
return md5(join( ':' , $hash));
}
private function checkControlSumm($data) {
$resp_crc = isset($data[ 'SignatureValue' ])? $data[ 'SignatureValue' ]:0;
return strtoupper(self::getControlSumm($data)) == strtoupper($resp_crc);
}
static public function getControlSumm($data) {
$hash = array(
isset($data[ 'OutSum' ])? $data[ 'OutSum' ]: '' ,
isset($data[ 'InvId' ])? $data[ 'InvId' ]: '' ,
Config::getKey( 'merchant_password2' , 'robox' )
);
return md5(join( ':' , $hash));
}
}
* This source code was highlighted with Source Code Highlighter .
class ModuleBilling {
private function _createResponse(){
// ,
}
// , :
public function actionResultPage () {
$response = $ this ->_createResponse();
$response_data = $_REQUEST;
$view = new View();
if ( $billing_type = CBilling::getBillingTypeByRequest( $response_data ) ) {
$billing = CBilling::createBillingByType($billing_type);
$result = $billing->checkResult($response_data);
if ($result){
$response->setStatus(CResponse::STATUS_OK);
} else {
$response->setStatus(CResponse::STATUS_ERROR);
}
$response->save();
$billing->addResultInView($view, $result);
}
return $view;
}
// :
public function actionBilling($req = array()){
$user = ServiceUser::checkAccess();
$billing_type = Request::getQueryVar( 'type' );
$purchase_id = Request::getQueryVar( 'purchase' );
$purchase = CPurchase::loadPurchaseById($purchase_id);
$purchase->setStatus(PURCHASE_STATUS_WAITMONEY);
$purchase->save();
$billing = CBilling::createBillingByType($billing_type);
$billing->setPurchaseId($purchase_id);
$billing->setStatus(BILLING_STATUS_WAITMONEY);
$billing->save();
$billing->redirectToBilling();
}
}
// :
...
$action = new ModuleBilling ();
$action->actionResultPage();
...
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/76742/
All Articles