composer.json000064400000000365150143411060007265 0ustar00{ "name": "4i4/vivawallet_client", "description": "Viva Wallet Client", "type": "drupal-module", "authors": [ { "name": "Petyo Stoyanov", "email": "pss@4i4.io" } ], "require": {} } src/Form/VivaWalletSettingsForm.php000064400000005712150143411060013372 0ustar00get('config.factory'), $container->get('config.typed') ); $instance->client = $container->get('vivawallet.client'); return $instance; } /** * {@inheritdoc} */ public function getFormId() { return 'vivawallet_config'; } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return [ static::SETTINGS, ]; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config(static::SETTINGS); $form['client_id'] = [ '#type' => 'textfield', '#title' => $this->t('Client ID'), '#default_value' => $config->get('client_id'), ]; $form['client_secret'] = [ '#type' => 'textfield', '#title' => $this->t('Client Secret'), '#default_value' => $config->get('client_secret'), ]; $form['source_code'] = [ '#type' => 'textfield', '#title' => $this->t('Source Code'), '#default_value' => $config->get('source_code'), ]; $form['demo'] = [ '#type' => 'checkbox', '#title' => $this->t('Demo mode'), '#default_value' => $config->get('demo'), ]; $form = parent::buildForm($form, $form_state); $form['actions']['test'] = [ '#type' => 'submit', '#value' => $this->t('Test'), '#button_type' => 'secondary', '#submit' => ['::test'], ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Retrieve the configuration. $this->config(static::SETTINGS) ->set('client_id', $form_state->getValue('client_id')) ->set('client_secret', $form_state->getValue('client_secret')) ->set('source_code', $form_state->getValue('source_code')) ->set('demo', $form_state->getValue('demo')) ->save(); parent::submitForm($form, $form_state); } /** * {@inheritdoc} */ public function test(array &$form, FormStateInterface $form_state) { $token = $this->client->getAccessToken(); if ($token) { $this->messenger()->addMessage($this->t('Access token received from VivaWallet.')); } else { $this->messenger()->addMessage($this->t('Something went wrong.')); } } } src/VivaWalletClient.php000064400000007334150143411060011263 0ustar00httpClientFactory = $httpClientFactory; $this->configFactory = $configFactory; $this->config = $this->configFactory->get(self::CONFIG)->getRawData(); } /** * API URL resolver. * * @param string $demo * Demo API subdomain. * @param string $live * Live API subdomain. * @param string $path * Path of API. * * @return string * Resolved API URL. */ public function resolveUrl(string $demo, string $live, string $path = ''): string { $url = $this->config['demo'] ? $demo : $live; return 'https://' . $url . '.vivapayments.com' . $path; } /** * To get authentication access token. */ public function getAccessToken() { $client_id = $this->config['client_id']; $client_secret = $this->config['client_secret']; $url = $this->resolveUrl('demo-accounts', 'accounts'); $client = $this->httpClientFactory->fromOptions([ 'base_uri' => $url, ]); $result = $client->post('/connect/token', [ RequestOptions::AUTH => [$client_id, $client_secret], RequestOptions::HEADERS => [ 'Content-Type' => 'application/x-www-form-urlencoded', ], RequestOptions::FORM_PARAMS => ['grant_type' => 'client_credentials'], ]); $response = Json::decode($result->getBody()->getContents()); return $response['access_token']; } /** * Generate order code for the order entity. * * @param array $data * The order data. * * @return string|null * The order code. * * @throws \GuzzleHttp\Exception\GuzzleException */ public function createOrder($data) { $data['sourceCode'] = $this->config['source_code']; $token = $this->getAccessToken(); $url = $this->resolveUrl('demo-api', 'api'); $client = $this->httpClientFactory->fromOptions([ 'base_uri' => $url, ]); $response = $client->post('/checkout/v2/orders', [ RequestOptions::HEADERS => [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ], RequestOptions::JSON => $data, ]); $order = Json::decode($response->getBody()->getContents()); return $order['orderCode'] ?: NULL; } /** * Retrieve transaction. * * @param $transaction_id * The transaction ID. * * @return array * The transaction raw object. * * @throws \GuzzleHttp\Exception\GuzzleException */ public function getTransaction($transaction_id) { $token = $this->getAccessToken(); $url = $this->resolveUrl('demo-api', 'api'); $client = $this->httpClientFactory->fromOptions([ 'base_uri' => $url, ]); $response = $client->get("/checkout/v2/transactions/$transaction_id", [ RequestOptions::HEADERS => [ 'Authorization' => 'Bearer ' . $token, ], ]); return Json::decode($response->getBody()->getContents()); } } vivawallet_client.info.yml000064400000000226150143411060011730 0ustar00name: 'Viva Wallet Client' type: 'module' description: 'API Client' package: 4i4 core_version_requirement: '^10 | ^11' configure: vivawallet.settings vivawallet_client.links.menu.yml000064400000000244150143411060013060 0ustar00vivawallet.settings: title: 'Viva Wallet' parent: system.admin_config_services description: "Configure the client settings" route_name: vivawallet.settings vivawallet_client.permissions.yml000064400000000177150143411060013355 0ustar00administer vivawallet client: title: 'Administer Viva Wallet' description: 'Client configurations' restrict access: true vivawallet_client.routing.yml000064400000000352150143411060012464 0ustar00vivawallet.settings: path: '/admin/config/services/vivawallet' defaults: _form: '\Drupal\vivawallet_client\Form\VivaWalletSettingsForm' _title: 'Viva Wallet' requirements: _permission: 'administer vivawallet client' vivawallet_client.services.yml000064400000000217150143411060012620 0ustar00services: vivawallet.client: class: Drupal\vivawallet_client\VivaWalletClient arguments: ['@http_client_factory', '@config.factory']