Usage examples for financial transactions

You can view examples of completing active financial transactions:

To work with these methods, you must create a private key (contact the account manager for instructions).

returnPayment

/**
 * Refunds a successful transfer to the Payer's account.
 * @param  string|int $invoiceId transaction number of the transfer being refunded
 * @param  string $amount        amount to refund to the Payer's account
 * @return string                response from Yandex.Money in XML format
 */
public function returnPayment($invoiceId, $amount) {
    $methodName = "returnPayment";
    $this->log->info("Start " . $methodName);
    $dateTime = Utils::formatDate(new \DateTime()) ;
    $requestParams = array(
        'clientOrderId' => mktime(),
        'requestDT' => $dateTime,
        'invoiceId' => $invoiceId,
        'shopId' => $this->settings->SHOP_ID,
        'amount' => number_format($amount, 2),
        'currency' => $this->settings->CURRENCY,
        'cause' => 'No product'
    );
    $result = $this->sendXmlRequest($methodName, $requestParams);
    $this->log->info($result);
    return $result;
}

confirmPayment

/**
 * Completes a successful transfer to the merchant's account. Used for deferred transfers.
 * @param  string|int $orderId transaction number of the transfer being confirmed
 * @param  string     $amount  amount to transfer
 * @return string              response from Yandex.Money in XML format
 */
public function confirmPayment($orderId, $amount) {
    $methodName = "confirmPayment";
    $this->log->info("Start " . $methodName);
    $dateTime = Utils::formatDate(new \DateTime()) ;
    $requestParams = array(
        'clientOrderId' => mktime(),
        'requestDT' => $dateTime,
        'orderId' => $orderId,
        'amount' => $amount,
        'currency' => 'RUB'
    );
    $result = $this->sendUrlEncodedRequest($methodName, $requestParams);
    $this->log->info($result);
    return $result;
}

cancelPayment

/**
 * Cancels a deferred payment.
 * @param  string|int $orderId transaction number of the deferred payment
 * @return string              response from Yandex.Money in XML format
 */
public function cancelPayment($orderId) {
    $methodName = "cancelPayment";
    $this->log->info("Start " . $methodName);
    $dateTime = Utils::formatDate(new \DateTime()) ;
    $requestParams = array(
        'requestDT' => $dateTime,
        'orderId' => $orderId
    );
    $result = $this->sendUrlEncodedRequest($methodName, $requestParams);
    $this->log->info($result);
    return $result;
}

repeatCardPayment

/**
 * Repeats a payment using the Payer's card data (with the Payer's consent) to pay for the store's
 * products or services.
 * @param  string|int $invoiceId transaction number of the transfer being repeated.
 * @param  string $amount        amount to make the payment
 * @return string                response from Yandex.Money in XML format
 */
public function repeatCardPayment($invoiceId, $amount) {
    $methodName = "repeatCardPayment";
    $this->log->info("Start " . $methodName);
    $requestParams = array(
        'clientOrderId' => mktime(),
        'invoiceId' => $invoiceId,
        'amount' => $amount
    );
    $result = $this->sendUrlEncodedRequest($methodName, $requestParams);
    $this->log->info($result);
    return $result;
}

Request signature

private function signData($data) {
    $descriptorspec = array(
        0 => array("pipe", "r"),
        1 => array("pipe", "w"),
    );
    $descriptorspec[2] = $descriptorspec[1];
    try {
        $opensslCommand = 'openssl smime -sign -signer ' . $this->settings->mws_cert .
            ' -inkey ' . $this->settings->mws_private_key .
            ' -nochain -nocerts -outform PEM -nodetach -passin pass:'.$this->settings->mws_cert_password;
        $this->log->info("opensslCommand: " . $opensslCommand);
        $process = proc_open($opensslCommand, $descriptorspec, $pipes);
        if (is_resource($process)) {
            fwrite($pipes[0], $data);
            fclose($pipes[0]);
            $pkcs7 = stream_get_contents($pipes[1]);
            $this->log->info($pkcs7);
            fclose($pipes[1]);
            $resCode = proc_close($process);
            if ($resCode != 0) {
                $errorMsg = 'OpenSSL call failed:' . $resCode . '\n' . $pkcs7;
                $this->log->info($errorMsg);
                throw new \Exception($errorMsg);
            }
            return $pkcs7;
        }
    } catch (\Exception $e) {
        $this->log->info($e);
        throw $e;
    }
}

See also

Financial transactions

Request to refund a successful transfer confirmPayment method

Request to confirm a deferred payment cancelPayment method

Request to cancel a deferred payment

Request to cancel a deferred payment

Request to repeat a bank card payment