_setInvoice($invoice); } parent::__construct($options); if ($this->getPaymentType() == self::SET_EXPRESS_CHECKOUT_TYPE) { $this->_setExpressCheckout(); } else if ($this->getPaymentType() == self::GET_EXPRESS_CHECKOUT_DETAILS_TYPE) { $this->_getExpressCheckoutDetails(); } else if ($this->getPaymentType() == self::DO_EXPRESS_CHECKOUT_PAYMENT_TYPE) { $this->_doExpressCheckoutPayment(); } else if ($this->getPaymentType() == self::REFUND_TRANSACTION) { $this->_refundTransaction(); } else if ($this->getPaymentType() == self::VERIFY_IPN_TYPE) { $this->_verifyIpn(); } return $this; } protected function _setExpressCheckout() { $data = $this->getOption('data'); $currencyCode = (!empty($data['currency'])) ? $data['currency'] : 'USD'; $params = [ 'TRXTYPE' => 'S', 'ACTION' => 'S', 'METHOD' => 'SetExpressCheckout', 'PAYMENTREQUEST_0_AMT' => $data['amount'], 'CURRENCYCODE' => $currencyCode, 'RETURNURL' => $data['returnUrl'], 'CANCELURL' => $data['cancelUrl'], ]; if ($data['subtotal']) { $params['PAYMENTREQUEST_0_ITEMAMT'] = $data['subtotal']; } if ($data['shipping']) { $params['PAYMENTREQUEST_0_SHIPPINGAMT'] = $data['shipping']; } if ($data['tax']) { $params['PAYMENTREQUEST_0_TAXAMT'] = $data['tax']; } if ($data['handling']) { $params['PAYMENTREQUEST_0_HANDLINGAMT'] = $data['handling']; } $discountSum = Zend_Locale_Math::Div(Zend_Locale_Math::Add($data['promo'], $data['giftCard'], 4), -1, 4); $discountSum = Zend_Locale_Math::round($discountSum, 2); if ($data['custom']) { $params = array_merge($params, $data['custom']); } $itemList = $this->_getItemList(); //додаєм дотанковий айтем з діскаунтом (сума мінусова) if ($discountSum < 0) { $itemList[] = [ 'L_PAYMENTREQUEST_0_AMT' => $discountSum, 'L_PAYMENTREQUEST_0_NAME' => 'Discount', 'L_PAYMENTREQUEST_0_QTY' => 1, ]; } foreach ($itemList as $itemKey => $item) { foreach ($item as $key => $value) { $params[$key . $itemKey] = $value; } } $response = $this->_sendRequest($params); if ($response['ACK'] == 'Failure' || !isset($response['TOKEN'])) { $this->_response = new App_Service_Payment_PayPalExpressCheckout_Response($response); } else { $location = $this->_getPaymentConfig('actionUrl') . '?cmd=_express-checkout&token=' . $response['TOKEN']; header('Location: ' . $location); die('FORWARD'); } return $this; } protected function _refundTransaction() { $data = $this->getOption('data'); $params = [ 'METHOD' => 'RefundTransaction', 'TRANSACTIONID' => $data['transactionId'], ]; $response = $this->_sendRequest($params); $this->_response = new App_Service_Payment_PayPalExpressCheckout_Response($response); return $this; } protected function _getExpressCheckoutDetails() { $data = $this->getOption('data'); $params = [ 'METHOD' => 'GetExpressCheckoutDetails', 'TOKEN' => $data['token'], ]; $response = $this->_sendRequest($params); $this->_response = new App_Service_Payment_PayPalExpressCheckout_Response($response); if ($response['ACK'] == 'Success') { $this->_expressCheckoutDetails = $response; } else { $this->_expressCheckoutDetails = [ 'status' => 'FAIL', 'msg' => $response['L_LONGMESSAGE0'], ]; } return $this; } protected function _doExpressCheckoutPayment() { $data = $this->getOption('data'); $params = [ 'METHOD' => 'DoExpressCheckoutPayment', 'TOKEN' => $data['token'], 'PAYERID' => $data['payerId'], 'PAYMENTREQUEST_0_AMT' => $data['amount'], 'PAYMENTREQUEST_0_CURRENCYCODE' => $data['currency'], 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', 'PAYMENTREQUEST_0_NOTIFYURL' => $this->_getPaymentConfig('notifyUrl'), ]; $response = $this->_sendRequest($params); $this->_response = new App_Service_Payment_PayPalExpressCheckout_Response($response); if ($response['ACK'] == 'Success') { $this->_expressCheckoutDetails = $response; } else { $this->_expressCheckoutDetails = [ 'status' => 'FAIL', 'msg' => $response['L_LONGMESSAGE0'], ]; } return $this; } protected function _sendRequest($params) { $params['SIGNATURE'] = $this->_getPaymentConfig('signature'); $params['VERSION'] = $this->_getPaymentConfig('version'); if ($this->_getPaymentConfig('writeLog')) { $logData = ['url' => $this->_getPaymentConfig('apiUri'), 'type' => $this->getPaymentType()] + $params; $this->_writeLog($logData); } $params['USER'] = $this->_getPaymentConfig('user'); $params['PWD'] = $this->_getPaymentConfig('pwd'); $c = curl_init(); curl_setopt_array($c, [ CURLOPT_URL => $this->_getPaymentConfig('apiUri'), CURLOPT_FAILONERROR => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($params), ]); $result = curl_exec($c); $resultArray = []; parse_str($result, $resultArray); if ($this->_getPaymentConfig('writeLog')) { $this->_writeLog(['type' => $this->getPaymentType()] + $resultArray); } if (!$result) { $resultArray = ['ERROR' => curl_error($c)]; } curl_close($c); return $resultArray; } public function verifyOrder($orderId, $orderTotal = null) { $orderConfig = $this->_getPaymentConfig('order'); $result = true; if ($orderConfig['enable']) { $orderClass = new $orderConfig['class'](); $checkout = new App_ECommerce_Checkout_Obj(); $transactionId = $checkout->getTransactionId(); $checkout->saveTransactionId($transactionId); if (!empty($transactionId)) { $checkout->saveTransactionId($transactionId); $verifyMethod = $orderConfig['verifyMethod']; $options = ['invoice' => $orderId, 'total' => $orderTotal, 'transactionId' => $transactionId]; $options += $this->getResponse()->getData(); $result = $orderClass->{$verifyMethod}($options); } else { $result = false; } } if ($this->_getPaymentConfig('writeLog')) { $this->_writeLog(['type' => 'verifyOrder', 'orderId' => $orderId, 'result' => $result]); } return $result; } public function updateOrder($orderId) { $orderConfig = $this->_getPaymentConfig('order'); $result = true; if ($orderConfig['enable']) { $orderClass = new $orderConfig['class'](); $checkout = new App_ECommerce_Checkout_Obj(); $transactionId = $checkout->getTransactionId(); $checkout->saveTransactionId($transactionId); $updateMethod = $orderConfig['updateMethod']; $options = ['invoice' => $orderId, 'transactionId' => $transactionId] + $this->getResponse()->getData(); $result = $orderClass->{$updateMethod}($options); } if ($this->_getPaymentConfig('writeLog')) { $this->_writeLog(['type' => 'updateOrder', 'orderId' => $orderId, 'result' => $result]); } return $result; } protected function _setInvoice($invoice) { $this->_invoice = $invoice; return $this; } /** @return App_Service_Payment_PayPalExpressCheckout_Response */ public function getResponse() { return $this->_response; } }