[gnome-software/wip/rancell/paid] Improve core payment API



commit da3145232e6aec0e805757a12dc336fcfe75f906
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Jul 22 10:03:05 2016 +0200

    Improve core payment API

 src/Makefile.am               |    2 +
 src/gs-page.c                 |    2 +-
 src/gs-payment-method.c       |  144 +++++++++++++++++++++++++++++++++++++++++
 src/gs-payment-method.h       |   49 ++++++++++++++
 src/gs-plugin-vfuncs.h        |   29 +++++++--
 src/gs-price.c                |   95 +++++++++------------------
 src/plugins/gs-plugin-dummy.c |   19 ++++++
 7 files changed, 269 insertions(+), 71 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 2ee4e58..f07a5ab 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -191,6 +191,8 @@ gnome_software_SOURCES =                            \
        gs-os-release.h                                 \
        gs-page.c                                       \
        gs-page.h                                       \
+       gs-payment-method.c                             \
+       gs-payment-method.h                             \
        gs-plugin.c                                     \
        gs-plugin.h                                     \
        gs-plugin-private.h                             \
diff --git a/src/gs-page.c b/src/gs-page.c
index 0a6804e..483d36b 100644
--- a/src/gs-page.c
+++ b/src/gs-page.c
@@ -360,7 +360,7 @@ gs_page_purchase_app (GsPage *page, GsApp *app)
        }
        escaped = g_markup_escape_text (gs_app_get_name (app), -1);
        gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog),
-                                                   /* TRANSLATORS: longer dialog text */
+                                                   /* TRANSLATORS: Describing what will be purchased */
                                                     _("You will be charged %s and %s will be installed."),
                                                     price_text,
                                                     escaped);
diff --git a/src/gs-payment-method.c b/src/gs-payment-method.c
new file mode 100644
index 0000000..92b7a38
--- /dev/null
+++ b/src/gs-payment-method.c
@@ -0,0 +1,144 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2016 Canonical Ltd.
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include "gs-payment-method.h"
+
+struct _GsPaymentMethod
+{
+       GObject                  parent_instance;
+
+       gchar                   *description;
+       GHashTable              *metadata;
+};
+
+G_DEFINE_TYPE (GsPaymentMethod, gs_payment_method, G_TYPE_OBJECT)
+
+/**
+ * gs_payment_method_get_description:
+ * @method: a #GsPaymentMethod
+ *
+ * Gets the payment method description.
+ *
+ * Returns: a one line description for this payment method, e.g. "**** **** **** 1111 (exp 23/2020)"
+ */
+const gchar *
+gs_payment_method_get_description (GsPaymentMethod *method)
+{
+       g_return_val_if_fail (GS_IS_PAYMENT_METHOD (method), NULL);
+       return method->description;
+}
+
+/**
+ * gs_payment_method_set_description:
+ * @method: a #GsPaymentMethod
+ * @description: Human readable description for this payment method, e.g. "**** **** **** 1111 (exp 23/2020)"
+ *
+ * Sets the one line description that may be displayed for this payment method.
+ */
+void
+gs_payment_method_set_description (GsPaymentMethod *method, const gchar *description)
+{
+       g_return_if_fail (GS_IS_PAYMENT_METHOD (method));
+       g_free (method->description);
+       method->description = g_strdup (description);
+}
+
+/**
+ * gs_payment_method_get_metadata_item:
+ * @method: a #GsPaymentMethod
+ * @key: a string
+ *
+ * Gets some metadata from a payment method object.
+ * It is left for the the plugin to use this method as required, but a
+ * typical use would be to store an ID for this payment, or payment information.
+ *
+ * Returns: A string value, or %NULL for not found
+ */
+const gchar *
+gs_payment_method_get_metadata_item (GsPaymentMethod *method, const gchar *key)
+{
+       g_return_val_if_fail (GS_IS_PAYMENT_METHOD (method), NULL);
+       g_return_val_if_fail (key != NULL, NULL);
+       return g_hash_table_lookup (method->metadata, key);
+}
+
+/**
+ * gs_payment_method_add_metadata:
+ * @method: a #GsPaymentMethod
+ * @key: a string
+ * @value: a string
+ *
+ * Adds metadata to the review object.
+ * It is left for the the plugin to use this method as required, but a
+ * typical use would be to store an ID for this payment, or payment information.
+ */
+void
+gs_payment_method_add_metadata (GsPaymentMethod *method, const gchar *key, const gchar *value)
+{
+       g_return_if_fail (GS_IS_PAYMENT_METHOD (method));
+       g_hash_table_insert (method->metadata, g_strdup (key), g_strdup (value));
+}
+
+static void
+gs_payment_method_finalize (GObject *object)
+{
+       GsPaymentMethod *method = GS_PAYMENT_METHOD (object);
+
+       g_free (method->description);
+       g_hash_table_unref (method->metadata);
+
+       G_OBJECT_CLASS (gs_payment_method_parent_class)->finalize (object);
+}
+
+static void
+gs_payment_method_class_init (GsPaymentMethodClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       object_class->finalize = gs_payment_method_finalize;
+}
+
+static void
+gs_payment_method_init (GsPaymentMethod *method)
+{
+       method->metadata = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                 g_free, g_free);
+}
+
+/**
+ * gs_payment_method_new:
+ *
+ * Creates a new payment method object.
+ *
+ * Return value: a new #GsPaymentMethod object.
+ **/
+GsPaymentMethod *
+gs_payment_method_new (void)
+{
+       GsPaymentMethod *method;
+       method = g_object_new (GS_TYPE_PAYMENT_METHOD, NULL);
+       return GS_PAYMENT_METHOD (method);
+}
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-payment-method.h b/src/gs-payment-method.h
new file mode 100644
index 0000000..d1a51d4
--- /dev/null
+++ b/src/gs-payment-method.h
@@ -0,0 +1,49 @@
+ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2016 Canonical Ltd.
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __GS_PAYMENT_METHOD_H
+#define __GS_PAYMENT_METHOD_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_PAYMENT_METHOD (gs_payment_method_get_type ())
+
+G_DECLARE_FINAL_TYPE (GsPaymentMethod, gs_payment_method, GS, PAYMENT_METHOD, GObject)
+
+GsPaymentMethod        *gs_payment_method_new                  (void);
+
+const gchar    *gs_payment_method_get_description      (GsPaymentMethod        *method);
+void            gs_payment_method_set_description      (GsPaymentMethod        *method,
+                                                        const gchar            *description);
+
+const gchar    *gs_payment_method_get_metadata_item    (GsPaymentMethod        *method,
+                                                        const gchar            *key);
+void            gs_payment_method_add_metadata         (GsPaymentMethod        *method,
+                                                        const gchar            *key,
+                                                        const gchar            *value);
+
+G_END_DECLS
+
+#endif /* __GS_PAYMENT_METHOD_H */
+
+/* vim: set noexpandtab: */
diff --git a/src/gs-plugin-vfuncs.h b/src/gs-plugin-vfuncs.h
index fa02838..ea50c41 100644
--- a/src/gs-plugin-vfuncs.h
+++ b/src/gs-plugin-vfuncs.h
@@ -40,6 +40,7 @@
 #include "gs-app.h"
 #include "gs-app-list.h"
 #include "gs-category.h"
+#include "gs-payment-method.h"
 #include "gs-price.h"
 
 G_BEGIN_DECLS
@@ -498,10 +499,27 @@ gboolean   gs_plugin_update_cancel                (GsPlugin       *plugin,
                                                         GError         **error);
 
 /**
+ * gs_plugin_get_payment_methods:
+ * @plugin: a #GsPlugin
+ * @payment_methods: list to add payment methods to.
+ * @cancellable: a #GCancellable, or %NULL
+ * @error: a #GError, or %NULL
+ *
+ * Get the available payment methods for purchasing apps.
+ *
+ * Returns: %TRUE for success or if not relevant
+ **/
+gboolean        gs_plugin_get_payment_methods          (GsPlugin       *plugin,
+                                                        GList          **payment_methods,
+                                                        GCancellable   *cancellable,
+                                                        GError         **error);
+
+/**
  * gs_plugin_app_purchase:
  * @plugin: a #GsPlugin
  * @app: a #GsApp
  * @price: a #GsPrice
+ * @payment_method: (allow-none): a #GsPaymentMethod, or %NULL
  * @cancellable: a #GCancellable, or %NULL
  * @error: a #GError, or %NULL
  *
@@ -512,11 +530,12 @@ gboolean   gs_plugin_update_cancel                (GsPlugin       *plugin,
  *
  * Returns: %TRUE for success or if not relevant
  **/
-gboolean        gs_plugin_app_purchase                 (GsPlugin       *plugin,
-                                                        GsApp          *app,
-                                                        GsPrice        *price,
-                                                        GCancellable   *cancellable,
-                                                        GError         **error);
+gboolean        gs_plugin_app_purchase                 (GsPlugin               *plugin,
+                                                        GsApp                  *app,
+                                                        GsPrice                *price,
+                                                        GsPaymentMethod        *payment_method,
+                                                        GCancellable           *cancellable,
+                                                        GError                 **error);
 
 /**
  * gs_plugin_app_install:
diff --git a/src/gs-price.c b/src/gs-price.c
index 69ac267..17f29a7 100644
--- a/src/gs-price.c
+++ b/src/gs-price.c
@@ -33,17 +33,15 @@ struct _GsPrice
        gchar                   *currency;
 };
 
-enum {
-       PROP_0,
-       PROP_AMOUNT,
-       PROP_CURRENCY,
-       PROP_LAST
-};
-
 G_DEFINE_TYPE (GsPrice, gs_price, G_TYPE_OBJECT)
 
 /**
  * gs_price_get_amount:
+ * @price: a #GsPrice
+ *
+ * Get the amount of money in this price.
+ *
+ * Returns: The amount of money in this price, e.g. 0.99
  */
 gdouble
 gs_price_get_amount (GsPrice *price)
@@ -54,6 +52,10 @@ gs_price_get_amount (GsPrice *price)
 
 /**
  * gs_price_set_amount:
+ * @price: a #GsPrice
+ * @amount: The amount of this price, e.g. 0.99
+ *
+ * Set the amount of money in this price.
  */
 void
 gs_price_set_amount (GsPrice *price, gdouble amount)
@@ -64,6 +66,11 @@ gs_price_set_amount (GsPrice *price, gdouble amount)
 
 /**
  * gs_price_get_currency:
+ * @price: a #GsPrice
+ *
+ * Get the currency a price is using.
+ *
+ * Returns: an ISO 4217 currency code for this price, e.g. "USD"
  */
 const gchar *
 gs_price_get_currency (GsPrice *price)
@@ -74,6 +81,10 @@ gs_price_get_currency (GsPrice *price)
 
 /**
  * gs_price_set_currency:
+ * @price: a #GsPrice
+ * @currency: An ISO 4217 currency code, e.g. "USD"
+ *
+ * Set the currency this price is using.
  */
 void
 gs_price_set_currency (GsPrice *price, const gchar *currency)
@@ -85,6 +96,11 @@ gs_price_set_currency (GsPrice *price, const gchar *currency)
 
 /**
  * gs_price_to_string:
+ * @price: a #GsPrice
+ *
+ * Convert a price object to a human readable string.
+ *
+ * Returns: A human readable string for this price, e.g. "US$0.99"
  */
 gchar *
 gs_price_to_string (GsPrice *price)
@@ -115,44 +131,6 @@ gs_price_to_string (GsPrice *price)
 }
 
 static void
-gs_price_get_property (GObject *object, guint prop_id,
-                       GValue *value, GParamSpec *pspec)
-{
-       GsPrice *price = GS_PRICE (object);
-
-       switch (prop_id) {
-       case PROP_AMOUNT:
-               g_value_set_double (value, price->amount);
-               break;
-       case PROP_CURRENCY:
-               g_value_set_string (value, price->currency);
-               break;
-       default:
-               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-               break;
-       }
-}
-
-static void
-gs_price_set_property (GObject *object, guint prop_id,
-                       const GValue *value, GParamSpec *pspec)
-{
-       GsPrice *price = GS_PRICE (object);
-
-       switch (prop_id) {
-       case PROP_AMOUNT:
-               gs_price_set_amount (price, g_value_get_double (value));
-               break;
-       case PROP_CURRENCY:
-               gs_price_set_currency (price, g_value_get_string (value));
-               break;
-       default:
-               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-               break;
-       }
-}
-
-static void
 gs_price_finalize (GObject *object)
 {
        GsPrice *price = GS_PRICE (object);
@@ -165,27 +143,8 @@ gs_price_finalize (GObject *object)
 static void
 gs_price_class_init (GsPriceClass *klass)
 {
-       GParamSpec *pspec;
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        object_class->finalize = gs_price_finalize;
-       object_class->get_property = gs_price_get_property;
-       object_class->set_property = gs_price_set_property;
-
-       /**
-        * GsApp:amount:
-        */
-       pspec = g_param_spec_double ("amount", NULL, NULL,
-                                    0.0, G_MAXDOUBLE, 0.0,
-                                    G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-       g_object_class_install_property (object_class, PROP_AMOUNT, pspec);
-
-       /**
-        * GsApp:currency:
-        */
-       pspec = g_param_spec_string ("currency", NULL, NULL,
-                                    NULL,
-                                    G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-       g_object_class_install_property (object_class, PROP_CURRENCY, pspec);
 }
 
 static void
@@ -195,6 +154,10 @@ gs_price_init (GsPrice *price)
 
 /**
  * gs_price_new:
+ * @amount: The amount of this price, e.g. 0.99
+ * @currency: An ISO 4217 currency code, e.g. "USD"
+ *
+ * Creates a new price object.
  *
  * Return value: a new #GsPrice object.
  **/
@@ -202,7 +165,9 @@ GsPrice *
 gs_price_new (gdouble amount, const gchar *currency)
 {
        GsPrice *price;
-       price = g_object_new (GS_TYPE_PRICE, "amount", amount, "currency", currency, NULL);
+       price = g_object_new (GS_TYPE_PRICE, NULL);
+       price->amount = amount;
+       price->currency = currency;
        return GS_PRICE (price);
 }
 
diff --git a/src/plugins/gs-plugin-dummy.c b/src/plugins/gs-plugin-dummy.c
index cfdfa1b..864e162 100644
--- a/src/plugins/gs-plugin-dummy.c
+++ b/src/plugins/gs-plugin-dummy.c
@@ -652,12 +652,31 @@ gs_plugin_update_cancel (GsPlugin *plugin, GsApp *app,
 }
 
 /**
+ * gs_plugin_get_payment_methods:
+ */
+gboolean
+gs_plugin_get_payment_methods (GsPlugin *plugin,
+                              GList **payment_methods,
+                              GCancellable *cancellable,
+                              GError **error)
+{
+       GsPaymentMethod *method;
+       g_debug ("Getting payment methods");
+       method = gs_payment_method_new ();
+       gs_payment_method_set_description (method, "Test Payment Method");
+       gs_payment_method_add_metadata (method, "card-number", "0000 0000 0000 0000");
+       *payment_methods = g_list_append (*payment_methods, method);
+       return TRUE;
+}
+
+/**
  * gs_plugin_app_purchase:
  */
 gboolean
 gs_plugin_app_purchase (GsPlugin *plugin,
                        GsApp *app,
                        GsPrice *price,
+                       GsPaymentMethod *payment_method,
                        GCancellable *cancellable,
                        GError **error)
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]