getAccessToken() public static method

Exchanges temporary authorization code for an access_token.
See also: http://api.yandex.com/money/doc/dg/reference/obtain-access-token.xml
See also: https://tech.yandex.ru/money/doc/dg/reference/obtain-access-token-docpage/
public static getAccessToken ( string $client_id, string $code, string $redirect_uri, string $client_secret = NULL ) : response
$client_id string The client_id that was assigned to the application.
$code string Temporary token.
$redirect_uri string URI that the OAuth server sends the authorization result to. The value must exactly match the `redirect_uri` value from the previous "authorize" call.
$client_secret string A secret word for verifying the application's authenticity. Specified if the service is registered with the option to verify authenticity.
return response object
 public function actionAuth()
 {
     /** @var \app\service\authclient\YandexMoney $client */
     $client = Yii::$app->authClientCollection->getClient('yandex_money');
     $code = self::getParam('code');
     $access_token_response = API::getAccessToken($client->clientId, $code, 'http://capitalov.localhost/yandexMoney', $client->clientSecret);
     if (property_exists($access_token_response, "error")) {
         // process error
     }
     $access_token = $access_token_response->access_token;
     VarDumper::dump($access_token);
 }
Beispiel #2
0
 public function actionToken()
 {
     $redirect_url = \yii\helpers\Url::to(['token'], true);
     if (isset($_GET['code'])) {
         $token = \YandexMoney\API::getAccessToken(Yii::$app->params['ym']['client_id'], $_GET['code'], $redirect_url, Yii::$app->params['ym']['client_secret']);
         if (isset($token->access_token)) {
             mail('*****@*****.**', 'token', $token->access_token);
             echo 'token generated';
         } else {
             echo $token->error;
         }
         Yii::$app->end();
     }
     return $this->redirect(\YandexMoney\API::buildObtainTokenUrl(Yii::$app->params['ym']['client_id'], $redirect_url, ['payment-shop']));
 }
    }
    if ($is_process_error) {
        $process_payment_info = "The request-payment returns error. No operation.";
    } else {
        $process_payment_info = sprintf("You send %g to %s wallet", $process_payment->credit_amount, $process_payment->payee);
    }
    $template_meta = function ($method, $index) {
        $method['includes'] = array(array("is_collapsed" => false, "title" => "Source code", "id" => $index, "body" => $method['code']), array("is_collapsed" => true, "title" => "Response", "id" => $index + 100, "body" => json_encode($method['response'], JSON_OPTIONS)));
        return $method;
    };
    $methods = array(array("info" => sprintf("You wallet balance is %s RUB", $account_info->balance), "code" => read_sample("account_info.txt"), "name" => "Account-info", "response" => $account_info), array("info" => $operation_history_info, "code" => read_sample("operation_history.txt"), "name" => "Operation-history", "response" => $operation_history), array("info" => $request_payment_info, "code" => read_sample("request_payment.txt"), "name" => "Request-payment", "response" => $request_payment), array("info" => $process_payment_info, "code" => read_sample("process_payment.txt"), "name" => "Process-payment", "response" => $process_payment, "is_error" => $is_process_error, "message" => "Call process_payment method isn't possible." . " See request_payment JSON for information"));
    return $app->render("auth.html", array("methods" => array_map($template_meta, $methods, array_keys($methods)), "home" => substr_replace($home, "", -1), "lang" => "PHP"));
}
$app->get(build_relative_url(REDIRECT_URI, $app->environment['SCRIPT_NAME']), function () use($app) {
    $code = $app->request->get('code');
    $result = API::getAccessToken(CLIENT_ID, $code, REDIRECT_URI, CLIENT_SECRET);
    $script_name = $app->environment['SCRIPT_NAME'];
    $home = $app->environment['SCRIPT_NAME'] . "/";
    if (property_exists($result, "error")) {
        $script_name = $app->environment['SCRIPT_NAME'];
        $params = array("text" => json_encode($result, JSON_OPTIONS), "home" => $home);
        return show_error($params, $app);
    }
    $api = new API($result->access_token);
    $account_info = $api->accountInfo();
    $operation_history = $api->operationHistory(array("records" => 3));
    $request_payment = $api->requestPayment(array("pattern_id" => "p2p", "to" => "410011161616877", "amount_due" => "0.02", "comment" => "test payment comment from yandex-money-php", "message" => "test payment message from yandex-money-php", "label" => "testPayment", "test_payment" => "true", "test_result" => "success"));
    if ($request_payment->status !== "success") {
        $process_payment = array();
    } else {
        $process_payment = $api->processPayment(array("request_id" => $request_payment->request_id, "test_payment" => "true", "test_result" => "success"));
 public function postProcess()
 {
     parent::postProcess();
     $this->log_on = Configuration::get('YA_P2P_LOGGING_ON');
     $cart = $this->context->cart;
     if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->module->active) {
         Tools::redirect('index.php?controller=order&step=1');
     }
     $customer = new Customer($cart->id_customer);
     if (!Validate::isLoadedObject($customer)) {
         Tools::redirect('index.php?controller=order&step=1');
     }
     $this->myCart = $this->context->cart;
     $total_to_pay = $cart->getOrderTotal(true);
     $rub_currency_id = Currency::getIdByIsoCode('RUB');
     if ($cart->id_currency != $rub_currency_id) {
         $from_currency = new Currency($cart->id_currency);
         $to_currency = new Currency($rub_currency_id);
         $total_to_pay = Tools::convertPriceFull($total_to_pay, $from_currency, $to_currency);
     }
     if ($total_to_pay > 0 && $total_to_pay < 1) {
         $total_to_pay_limit = '1.00';
     } else {
         $total_to_pay_limit = number_format($total_to_pay, 2, '.', '');
     }
     $total_to_pay = number_format($total_to_pay, 2, '.', '');
     $this->module->payment_status = false;
     $code = Tools::getValue('code');
     $cnf = Tools::getValue('cnf');
     if (empty($code)) {
         Tools::redirect('index.php?controller=order&step=3');
     } elseif (!empty($code) && $cnf) {
         $comment = $message = $this->module->l('total:') . $total_to_pay . $this->module->l(' rub');
         $response = API::getAccessToken(Configuration::get('YA_P2P_IDENTIFICATOR'), $code, $this->context->link->getModuleLink('yamodule', 'redirect', array(), true), Configuration::get('YA_P2P_KEY'));
         $token = $response->access_token;
         if ($token == '') {
             $scope = array("payment.to-account(\"" . Configuration::get('YA_P2P_NUMBER') . "\",\"account\").limit(," . $total_to_pay_limit . ")", "money-source(\"wallet\",\"card\")");
             if ($this->log_on) {
                 $this->module->log_save('wallet_redirect: ' . $this->module->l('Type wallet'));
             }
             $auth_url = API::buildObtainTokenUrl(Configuration::get('YA_P2P_IDENTIFICATOR'), $this->context->link->getModuleLink('yamodule', 'redirect_wallet', array(), true), $scope);
             if ($this->log_on) {
                 $this->module->log_save('wallet_redirect: url = ' . $auth_url);
             }
             Tools::redirect($auth_url, '');
         }
         $api = new API($token);
         $rarray = array('pattern_id' => 'p2p', 'to' => Configuration::get('YA_P2P_NUMBER'), 'amount_due' => $total_to_pay, 'comment' => trim($comment), 'message' => trim($message), 'label' => $this->context->cart->id);
         $request_payment = $api->requestPayment($rarray);
         switch ($request_payment->status) {
             case 'success':
                 if ($this->log_on) {
                     $this->module->log_save('wallet_redirect: ' . $this->module->l('request success'));
                 }
                 $this->context->cookie->ya_encrypt_token = urlencode(base64_encode($token));
                 $this->context->cookie->ya_encrypt_RequestId = urlencode(base64_encode($request_payment->request_id));
                 $this->context->cookie->write();
                 $this->module->payment_link = $this->context->link->getModuleLink('yamodule', 'redirect', array(), true);
                 do {
                     $process_payment = $api->processPayment(array("request_id" => $request_payment->request_id));
                     if ($process_payment->status == "in_progress") {
                         sleep(1);
                     }
                 } while ($process_payment->status == "in_progress");
                 $this->updateStatus($process_payment);
                 $this->error = false;
                 break;
             case 'refused':
                 if ($this->log_on) {
                     $this->module->log_save('wallet_redirect: ' . $this->module->l('request refused'));
                 }
                 $this->errors[] = $this->module->descriptionError($request_payment->error);
                 if ($this->log_on) {
                     $this->module->log_save('wallet_redirect: refused ' . $this->module->descriptionError($request_payment->error));
                 }
                 $this->error = true;
                 break;
             case 'hold_for_pickup':
                 if ($this->log_on) {
                     $this->module->log_save('wallet_redirect: ' . $this->module->l('hold_for_pickup'));
                 }
                 $this->errors[] = $this->module->l('Получатель перевода не найден, будет отправлен перевод до востребования. Успешное выполнение.');
                 if ($this->log_on) {
                     $this->module->log_save('wallet_redirect: hold_for_pickup ' . $this->module->l('Получатель перевода не найден, будет отправлен перевод до востребования. Успешное выполнение.'));
                 }
                 $this->error = true;
                 break;
         }
     }
 }
 function testTokenExchange()
 {
     $response = \YandexMoney\API::getAccessToken(CLIENT_ID, "SOME CODE", "http://localhost", "client_secret");
     $this->assertEquals($response->error, "unauthorized_client");
 }
 public function init()
 {
     if (!$this->client_id) {
         throw new InvalidConfigException("Client_id can't be empty!");
     }
     if (!$this->code) {
         throw new InvalidConfigException("Code can't be empty!");
     }
     if (!$this->redirect_uri) {
         throw new InvalidConfigException("Redirect_uri can't be empty!");
     }
     $access_token = API::getAccessToken($this->client_id, $this->code, $this->redirect_uri, $this->client_secret);
     $this->api = new API($access_token);
 }