mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +01:00
PayZen REST API wrapper ruby + live test keys while configuring
This commit is contained in:
parent
69352c07df
commit
30830b56fd
17
app/controllers/api/payzen_controller.rb
Normal file
17
app/controllers/api/payzen_controller.rb
Normal file
@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# API Controller for accessing PayZen API endpoints through the front-end app
|
||||
class API::PayzenController < API::ApiController
|
||||
before_action :authenticate_user!
|
||||
require 'pay_zen/charge'
|
||||
|
||||
def sdk_test
|
||||
str = 'fab-manager'
|
||||
|
||||
client = PayZen::Charge.new(base_url: params[:base_url], username: params[:username], password: params[:password])
|
||||
res = client.sdk_test(str)
|
||||
|
||||
puts res
|
||||
@status = res&.answer&.value == str
|
||||
end
|
||||
end
|
10
app/frontend/src/javascript/api/payzen.ts
Normal file
10
app/frontend/src/javascript/api/payzen.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import apiClient from './api-client';
|
||||
import { AxiosResponse } from 'axios';
|
||||
|
||||
export default class PayzenAPI {
|
||||
|
||||
static async chargeSDKTest(baseURL: string, username: string, password: string): Promise<any> {
|
||||
const res: AxiosResponse = await apiClient.post('/api/payzen/sdk_test', { base_url: baseURL, username, password });
|
||||
return res?.data;
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ import { SettingName } from '../models/setting';
|
||||
import { FabInput } from './fab-input';
|
||||
import { enableMapSet } from 'immer';
|
||||
import { useImmer } from 'use-immer';
|
||||
import PayzenAPI from '../api/payzen';
|
||||
|
||||
enableMapSet();
|
||||
|
||||
@ -38,7 +39,7 @@ const PayZenKeysFormComponent: React.FC<PayZenKeysFormProps> = ({ onValidKeys })
|
||||
if (publicKeyAddOnClassName === validClassName && restApiAddOnClassName === validClassName) {
|
||||
onValidKeys(settings);
|
||||
}
|
||||
}, [publicKeyAddOnClassName, restApiAddOnClassName]);
|
||||
}, [publicKeyAddOnClassName, restApiAddOnClassName, settings]);
|
||||
|
||||
/**
|
||||
* Check if the inputted public key is valid and assign it to the settings if the key is valid
|
||||
@ -68,24 +69,23 @@ const PayZenKeysFormComponent: React.FC<PayZenKeysFormProps> = ({ onValidKeys })
|
||||
}
|
||||
}
|
||||
if (valid) {
|
||||
setRestApiAddOn(<i className="fa fa-check" />);
|
||||
setRestApiAddOnClassName('key-valid');
|
||||
PayzenAPI.chargeSDKTest(
|
||||
settings.get(SettingName.PayZenEndpoint),
|
||||
settings.get(SettingName.PayZenUsername),
|
||||
settings.get(SettingName.PayZenPassword)
|
||||
).then(result => {
|
||||
if (result.success) {
|
||||
setRestApiAddOn(<i className="fa fa-check" />);
|
||||
setRestApiAddOnClassName('key-valid');
|
||||
} else {
|
||||
setRestApiAddOn(<i className="fa fa-times" />);
|
||||
setRestApiAddOnClassName('key-invalid');
|
||||
}
|
||||
}, () => {
|
||||
setRestApiAddOn(<i className="fa fa-times" />);
|
||||
setRestApiAddOnClassName('key-invalid');
|
||||
});
|
||||
}
|
||||
// if (!key.match(/^sk_/)) {
|
||||
// setRestApiAddOn(<i className="fa fa-times" />);
|
||||
// setRestApiAddOnClassName('key-invalid');
|
||||
// return;
|
||||
// }
|
||||
// StripeAPI.listAllCharges(key).then(() => {
|
||||
// setSecretKey(key);
|
||||
// setSecretKeyAddOn(<i className="fa fa-check" />);
|
||||
// setSecretKeyAddOnClassName('key-valid');
|
||||
// }, reason => {
|
||||
// if (reason.response.status === 401) {
|
||||
// setSecretKeyAddOn(<i className="fa fa-times" />);
|
||||
// setSecretKeyAddOnClassName('key-invalid');
|
||||
// }
|
||||
// });
|
||||
};
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ const PayZenKeysFormComponent: React.FC<PayZenKeysFormProps> = ({ onValidKeys })
|
||||
<label htmlFor="payzen_endpoint">{ t('app.admin.invoices.payment.endpoint') } *</label>
|
||||
<FabInput id="payzen_endpoint"
|
||||
type="url"
|
||||
icon={<i className="fas fa-anchor" />}
|
||||
icon={<i className="fas fa-link" />}
|
||||
value={settings.get(SettingName.PayZenEndpoint)}
|
||||
onChange={testRestApi(SettingName.PayZenEndpoint)}
|
||||
debounce={200}
|
||||
|
8
app/policies/payzen_policy.rb
Normal file
8
app/policies/payzen_policy.rb
Normal file
@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Check the access policies for API::PayzenController
|
||||
class PayzenPolicy < ApplicationPolicy
|
||||
def sdk_test?
|
||||
user.admin?
|
||||
end
|
||||
end
|
3
app/views/api/payzen/sdk_test.json.jbuilder
Normal file
3
app/views/api/payzen/sdk_test.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
json.success @status
|
@ -183,6 +183,9 @@ Rails.application.routes.draw do
|
||||
|
||||
# test MIME type
|
||||
post 'files/mime_type' => 'files#mime'
|
||||
|
||||
# PayZen special endpoint
|
||||
post 'payzen/sdk_test' => 'payzen#sdk_test'
|
||||
end
|
||||
|
||||
# rss
|
||||
|
18
lib/pay_zen/charge.rb
Normal file
18
lib/pay_zen/charge.rb
Normal file
@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'pay_zen/client'
|
||||
|
||||
# Charge/* endpoints of the PayZen REST API
|
||||
class PayZen::Charge < PayZen::Client
|
||||
def initialize(base_url: nil, username: nil, password: nil)
|
||||
super(base_url: base_url, username: username, password: password)
|
||||
end
|
||||
|
||||
##
|
||||
# @see https://payzen.io/fr-FR/rest/V4.0/api/playground/Charge/SDKTest/
|
||||
##
|
||||
def sdk_test(value)
|
||||
post('/Charge/SDKTest', value: value)
|
||||
end
|
||||
end
|
||||
|
44
lib/pay_zen/client.rb
Normal file
44
lib/pay_zen/client.rb
Normal file
@ -0,0 +1,44 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# PayZen payement gateway
|
||||
module PayZen; end
|
||||
|
||||
API_PATH = '/api-payment/V4'
|
||||
|
||||
# Client for connecting to the PayZen REST API
|
||||
class PayZen::Client
|
||||
def initialize(base_url: nil, username: nil, password: nil)
|
||||
@base_url = base_url
|
||||
@username = username
|
||||
@password = password
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def post(rel_url, payload, tmp_base_url: nil, tmp_username: nil, tmp_password: nil)
|
||||
require 'uri'
|
||||
require 'net/http'
|
||||
require 'json'
|
||||
|
||||
uri = URI.join(tmp_base_url || base_url, API_PATH, rel_url)
|
||||
headers = {
|
||||
'Authorization' => authorization_header(tmp_username, tmp_password),
|
||||
'Content-Type' => 'application/json'
|
||||
}
|
||||
|
||||
res = Net::HTTP.post(uri, payload.to_json, headers)
|
||||
JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)
|
||||
end
|
||||
|
||||
def base_url
|
||||
Setting.get('payzen_endpoint')
|
||||
end
|
||||
|
||||
def authorization_header(user, passwd)
|
||||
username = user || Setting.get('payzen_username')
|
||||
password = passwd || Setting.get('payzen_password')
|
||||
|
||||
credentials = Base64.strict_encode64("#{username}:#{password}")
|
||||
"Basic #{credentials}"
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user