Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of SKU system #4869

Merged
merged 18 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void ExtensionRewardsServiceObserver::OnPendingContributionRemoved(
void ExtensionRewardsServiceObserver::OnReconcileComplete(
RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) {
auto* event_router = extensions::EventRouter::Get(profile_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnReconcileComplete(
RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) override;

Expand Down
4 changes: 2 additions & 2 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) override;
void OnPendingContributionSaved(
Expand Down Expand Up @@ -974,7 +974,7 @@ void RewardsDOMHandler::GetContributionAmount(const base::ListValue* args) {
void RewardsDOMHandler::OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) {
if (web_ui()->CanCallJavascript()) {
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class RewardsTipDOMHandler : public WebUIMessageHandler,
void OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) override;

Expand Down Expand Up @@ -499,7 +499,7 @@ void RewardsTipDOMHandler::OnlyAnonWallet(const base::ListValue* args) {
void RewardsTipDOMHandler::OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) {
if (!web_ui()->CanCallJavascript()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,30 @@ bool ParseJSON(base::StringPiece json,
],
"entries": [
// Brave
{ "name": "ledger.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "ledger-staging.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "balance.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "balance-staging.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "download.brave.com", "mode": "force-https", "policy": "custom", "pins": "brave"},
{ "name": "laptop-updates.brave.com", "mode": "force-https", "policy": "custom", "pins": "brave"},
{ "name": "sync.brave.com", "mode": "force-https", "policy": "custom", "pins": "brave"},
{ "name": "sync-staging.brave.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "publishers-distro.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "publishers-staging-distro.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "p3a.brave.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "p3a.bravesoftware.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "p3a-dev.bravesoftware.com", "policy": "custom", "mode": "force-https", "pins": "brave"}
{ "name": "p3a-dev.bravesoftware.com", "policy": "custom", "mode": "force-https", "pins": "brave"},

// Rewards
{ "name": "ledger.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "ledger-staging.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "ledger.rewards.brave.software", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "balance.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "balance-staging.mercury.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "balance.rewards.brave.software", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "publishers-distro.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "publishers-staging-distro.basicattentiontoken.org", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "creators-distro.brave.software", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "grant.rewards.brave.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "grant.rewards.bravesoftware.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "grant.rewards.brave.software", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "payment.rewards.brave.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "payment.rewards.bravesoftware.com", "policy": "custom", "mode": "force-https", "pins": "brave"},
{ "name": "payment.rewards.brave.software", "policy": "custom", "mode": "force-https", "pins": "brave"}
]})brave_json";

return ParseJSON_ChromiumImpl(brave_json, entries, pinsets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ void RewardsNotificationServiceImpl::OnPromotionFinished(
void RewardsNotificationServiceImpl::OnReconcileComplete(
RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) {
auto converted_result = static_cast<ledger::Result>(result);
Expand All @@ -384,15 +384,15 @@ void RewardsNotificationServiceImpl::OnReconcileComplete(
converted_result == ledger::Result::LEDGER_ERROR ||
converted_result == ledger::Result::TIP_ERROR) {
RewardsNotificationService::RewardsNotificationArgs args;
args.push_back(viewing_id);
args.push_back(contribution_id);
args.push_back(std::to_string(result));
args.push_back(std::to_string(type));
args.push_back(std::to_string(amount));

AddNotification(
RewardsNotificationService::REWARDS_NOTIFICATION_AUTO_CONTRIBUTE,
args,
"contribution_" + viewing_id);
"contribution_" + contribution_id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class RewardsNotificationServiceImpl
void OnReconcileComplete(
RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) override;

Expand Down
143 changes: 88 additions & 55 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "bat/ledger/internal/bat_helper.h"
#include "bat/ledger/internal/uphold/uphold_util.h"
#include "bat/ledger/internal/request/request_util.h"
#include "bat/ledger/internal/request/request_sku.h"
#include "bat/ledger/internal/static_values.h"
#include "bat/ledger/ledger.h"
#include "brave/browser/extensions/api/brave_action_api.h"
Expand Down Expand Up @@ -136,21 +137,14 @@ namespace brave_test_resp {
std::string verification_;
std::string promotions_;
std::string promotion_claim_;
std::string promotion_tokens_;
std::string creds_tokens_;
std::string creds_tokens_prod_;
std::string captcha_;
std::string wallet_properties_;
std::string wallet_properties_defaults_;
std::string uphold_auth_resp_;
std::string uphold_transactions_resp_;
std::string uphold_commit_resp_;

std::string contribution_;
std::string reconcile_;
std::string current_reconcile_;
std::string register_;
std::string register_credential_;
std::string surveyor_voting_;
std::string surveyor_voting_credential_;
} // namespace brave_test_resp

class BraveRewardsBrowserTest
Expand Down Expand Up @@ -271,6 +265,45 @@ class BraveRewardsBrowserTest
status.c_str());
}

std::string GetOrderCreateResponse() {
DCHECK(sku_order_);
std::string items;
for (const auto& item : sku_order_->items) {
items.append(base::StringPrintf(
R"({
"id": "%s",
"orderId": "%s",
"sku": "",
"createdAt": "2020-04-08T08:22:26.288974Z",
"updatedAt": "2020-04-08T08:22:26.288974Z",
"currency": "BAT",
"quantity": %d,
"price": "%g",
"description": "%s"
})",
item->order_item_id.c_str(),
sku_order_->order_id.c_str(),
item->quantity,
item->price,
item->description.c_str()));
}

return base::StringPrintf(
R"({
"id": "%s",
"createdAt": "2020-04-08T08:22:26.288974Z",
"currency": "BAT",
"updatedAt": "2020-04-08T08:22:26.288974Z",
"totalPrice": "%g",
"location": "brave.com",
"status": "pending",
"items": [%s]
})",
sku_order_->order_id.c_str(),
sku_order_->total_amount,
items.c_str());
}

std::vector<double> GetSiteBannerTipOptions(
content::WebContents* site_banner) {
rewards_service_browsertest_utils::WaitForElementToAppear(
Expand Down Expand Up @@ -351,7 +384,7 @@ class BraveRewardsBrowserTest
} else if (URLMatches(url, "/promotions/", PREFIX_V1,
ServerTypes::kPromotion)) {
if (url.find("claims") != std::string::npos) {
*response = brave_test_resp::promotion_tokens_;
*response = brave_test_resp::creds_tokens_;
} else {
*response = brave_test_resp::promotion_claim_;
}
Expand Down Expand Up @@ -409,30 +442,27 @@ class BraveRewardsBrowserTest
braveledger_uphold::GetAPIUrl("/v0/me"),
base::CompareCase::INSENSITIVE_ASCII)) {
*response = GetUpholdUser();
} else if (URLMatches(url, WALLET_PROPERTIES, PREFIX_V2,
ServerTypes::LEDGER)) {
GURL gurl(url);
if (gurl.has_query()) {
*response = brave_test_resp::reconcile_;
} else {
*response = brave_test_resp::current_reconcile_;
} else if (URLMatches(
url,
"/order",
PREFIX_V1,
ServerTypes::kPayments)) {
if (url.find("credentials") != std::string::npos) {
if (method == 0) {
#if defined(OFFICIAL_BUILD)
*response = brave_test_resp::creds_tokens_prod_;
#else
*response = brave_test_resp::creds_tokens_;
#endif

return;
}
return;
emerick marked this conversation as resolved.
Show resolved Hide resolved
} else if (url.find("transaction") == std::string::npos) {
*response = GetOrderCreateResponse();
}

} else if (URLMatches(url, RECONCILE_CONTRIBUTION, PREFIX_V2,
ServerTypes::LEDGER)) {
*response = brave_test_resp::contribution_;
} else if (URLMatches(url, REGISTER_VIEWING, PREFIX_V2,
ServerTypes::LEDGER)) {
if (url.find(REGISTER_VIEWING "/") != std::string::npos)
*response = brave_test_resp::register_credential_;
else
*response = brave_test_resp::register_;
} else if (URLMatches(url, SURVEYOR_BATCH_VOTING, PREFIX_V2,
ServerTypes::LEDGER)) {
if (url.find(SURVEYOR_BATCH_VOTING "/") != std::string::npos)
*response = brave_test_resp::surveyor_voting_credential_;
else
*response = brave_test_resp::surveyor_voting_;
*response_status_code = net::HTTP_CREATED;
}
}

Expand Down Expand Up @@ -650,8 +680,11 @@ class BraveRewardsBrowserTest
base::ReadFileToString(path.AppendASCII("promotion_claim_resp.json"),
&brave_test_resp::promotion_claim_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("promotion_tokens_resp.json"),
&brave_test_resp::promotion_tokens_));
base::ReadFileToString(path.AppendASCII("creds_tokens_resp.json"),
&brave_test_resp::creds_tokens_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("creds_tokens_prod_resp.json"),
&brave_test_resp::creds_tokens_prod_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("wallet_properties_resp.json"),
&brave_test_resp::wallet_properties_));
Expand All @@ -667,26 +700,6 @@ class BraveRewardsBrowserTest
ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("uphold_commit_resp.json"),
&brave_test_resp::uphold_commit_resp_));

ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("contribution_resp.json"),
&brave_test_resp::contribution_));
ASSERT_TRUE(base::ReadFileToString(path.AppendASCII("reconcile_resp.json"),
&brave_test_resp::reconcile_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("current_reconcile_resp.json"),
&brave_test_resp::current_reconcile_));
ASSERT_TRUE(base::ReadFileToString(path.AppendASCII("register_resp.json"),
&brave_test_resp::register_));
ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("register_credential_resp.json"),
&brave_test_resp::register_credential_));
ASSERT_TRUE(
base::ReadFileToString(path.AppendASCII("surveyor_voting_resp.json"),
&brave_test_resp::surveyor_voting_));
ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("surveyor_voting_credential_resp.json"),
&brave_test_resp::surveyor_voting_credential_));
}

void UpdateContributionBalance(double amount, bool verified = false) {
Expand Down Expand Up @@ -1174,7 +1187,7 @@ class BraveRewardsBrowserTest
void OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const int32_t type) {
const auto converted_result = static_cast<ledger::Result>(result);
Expand Down Expand Up @@ -1408,6 +1421,8 @@ class BraveRewardsBrowserTest

std::unique_ptr<base::RunLoop> wait_for_attestation_loop_;

ledger::SKUOrderPtr sku_order_ = nullptr;

bool last_publisher_added_ = false;
bool alter_publisher_list_ = false;
bool show_defaults_in_properties_ = false;
Expand Down Expand Up @@ -2716,6 +2731,24 @@ IN_PROC_BROWSER_TEST_F(
// 30 form unblinded and 20 from uphold
rewards_service()->SetContributionAmount(50.0);

ledger::SKUOrderItemList items;
auto item = ledger::SKUOrderItem::New();
item->order_item_id = "ed193339-e58c-483c-8d61-7decd3c24827";
item->order_id = "a38b211b-bf78-42c8-9479-b11e92e3a76c";
item->quantity = 80;
item->price = 0.25;
item->description = "description";
item->type = ledger::SKUOrderItemType::SINGLE_USE;
items.push_back(std::move(item));

auto order = ledger::SKUOrder::New();
order->order_id = "a38b211b-bf78-42c8-9479-b11e92e3a76c";
order->total_amount = 20;
order->merchant_id = "";
order->location = "brave.com";
order->items = std::move(items);
sku_order_ = std::move(order);

// Trigger contribution process
rewards_service()->StartMonthlyContributionForTest();

Expand Down
17 changes: 6 additions & 11 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -851,15 +851,8 @@ void RewardsServiceImpl::OnGetRewardsInternalsInfo(
rewards_internals_info->user_id = info->user_id;
rewards_internals_info->boot_stamp = info->boot_stamp;

for (const auto& item : info->current_reconciles) {
ReconcileInfo reconcile_info;
reconcile_info.viewing_id_ = item.second->viewing_id;
reconcile_info.amount_ = item.second->amount;
reconcile_info.retry_step_ =
static_cast<ContributionRetry>(item.second->retry_step);
reconcile_info.retry_level_ = item.second->retry_level;
rewards_internals_info->current_reconciles[item.first] = reconcile_info;
}
// TODO(https://github.com/brave/brave-browser/issues/8633)
// add active contributions

std::move(callback).Run(std::move(rewards_internals_info));
}
Expand All @@ -886,7 +879,7 @@ void RewardsServiceImpl::OnRecoverWallet(

void RewardsServiceImpl::OnReconcileComplete(
const ledger::Result result,
const std::string& viewing_id,
const std::string& contribution_id,
const double amount,
const ledger::RewardsType type) {
if (result == ledger::Result::LEDGER_OK &&
Expand All @@ -897,7 +890,7 @@ void RewardsServiceImpl::OnReconcileComplete(
for (auto& observer : observers_)
observer.OnReconcileComplete(this,
static_cast<int>(result),
viewing_id,
contribution_id,
amount,
static_cast<int>(type));
}
Expand Down Expand Up @@ -2871,6 +2864,8 @@ void RewardsServiceImpl::GetExternalWallets(
for (const auto& it : dict->DictItems()) {
ledger::ExternalWalletPtr wallet = ledger::ExternalWallet::New();

wallet->type = it.first;

auto* token = it.second.FindKey("token");
if (token && token->is_string()) {
wallet->token = token->GetString();
Expand Down
Loading