From db13274fb060f20b8c244395c93e1a100af35354 Mon Sep 17 00:00:00 2001
From: Rafael Monnerat <rafael@nexedi.com>
Date: Wed, 22 Jul 2020 22:10:08 +0000
Subject: [PATCH] erp5_wechat_secure_payment: Optinally, let the user configure
 spbill_create_ip

   Whenever used via activity nodes, it is not possible to discover IP of the server via portal absolute url.
---
 .../document.erp5.WechatService.py            |  10 +-
 .../wechat_spbill_create_ip_property.xml      |  38 +++
 .../WechatService_view.xml                    |   1 +
 .../my_wechat_spbill_create_ip.xml            | 264 ++++++++++++++++++
 4 files changed, 310 insertions(+), 3 deletions(-)
 create mode 100644 bt5/erp5_wechat_secure_payment/PropertySheetTemplateItem/portal_property_sheets/WechatService/wechat_spbill_create_ip_property.xml
 create mode 100644 bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view/my_wechat_spbill_create_ip.xml

diff --git a/bt5/erp5_wechat_secure_payment/DocumentTemplateItem/portal_components/document.erp5.WechatService.py b/bt5/erp5_wechat_secure_payment/DocumentTemplateItem/portal_components/document.erp5.WechatService.py
index 066c9a6a4d..f42119e8bc 100644
--- a/bt5/erp5_wechat_secure_payment/DocumentTemplateItem/portal_components/document.erp5.WechatService.py
+++ b/bt5/erp5_wechat_secure_payment/DocumentTemplateItem/portal_components/document.erp5.WechatService.py
@@ -118,7 +118,6 @@ class WechatService(XMLObject):
 
   def callWechatApi(self, URL, wechat_dict):
     portal = self.getPortalObject()
-    base_url = portal.absolute_url()
     wechat_url = self.getLinkUrlString()
     if self.getWechatMode() == "SANDBOX":
       key = self.getSandboxKey()
@@ -128,8 +127,13 @@ class WechatService(XMLObject):
       key = self.getServiceApiKey()
     nonce_str = self.generateRandomStr()
 
-    result = urlparse(base_url)
-    spbill_create_ip = socket.gethostbyname(result.netloc)
+    wechat_spbill_create_ip = self.getWechatSpbillCreateIp()
+    if not wechat_spbill_create_ip:
+      base_url = portal.absolute_url()
+      result = urlparse(base_url)
+      spbill_create_ip = socket.gethostbyname(result.netloc)
+    else:
+      spbill_create_ip = socket.gethostbyname(wechat_spbill_create_ip)
 
     # Construct parameter for calling the Wechat payment URL
     wechat_dict['appid'] = self.getServiceAppid()
diff --git a/bt5/erp5_wechat_secure_payment/PropertySheetTemplateItem/portal_property_sheets/WechatService/wechat_spbill_create_ip_property.xml b/bt5/erp5_wechat_secure_payment/PropertySheetTemplateItem/portal_property_sheets/WechatService/wechat_spbill_create_ip_property.xml
new file mode 100644
index 0000000000..2ad8e91f38
--- /dev/null
+++ b/bt5/erp5_wechat_secure_payment/PropertySheetTemplateItem/portal_property_sheets/WechatService/wechat_spbill_create_ip_property.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="Standard Property" module="erp5.portal_type"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>categories</string> </key>
+            <value>
+              <tuple>
+                <string>elementary_type/string</string>
+              </tuple>
+            </value>
+        </item>
+        <item>
+            <key> <string>description</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>wechat_spbill_create_ip_property</string> </value>
+        </item>
+        <item>
+            <key> <string>portal_type</string> </key>
+            <value> <string>Standard Property</string> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <string>wechat_mode</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view.xml b/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view.xml
index 62efb9777b..dadfcee814 100644
--- a/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view.xml
+++ b/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view.xml
@@ -99,6 +99,7 @@
                         <string>my_reference</string>
                         <string>my_wechat_mode</string>
                         <string>my_service_mch_id</string>
+                        <string>my_wechat_spbill_create_ip</string>
                       </list>
                     </value>
                 </item>
diff --git a/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view/my_wechat_spbill_create_ip.xml b/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view/my_wechat_spbill_create_ip.xml
new file mode 100644
index 0000000000..885b7f8fa7
--- /dev/null
+++ b/bt5/erp5_wechat_secure_payment/SkinTemplateItem/portal_skins/erp5_wechat_secure_payment/WechatService_view/my_wechat_spbill_create_ip.xml
@@ -0,0 +1,264 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="StringField" module="Products.Formulator.StandardFields"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>my_wechat_spbill_create_ip</string> </value>
+        </item>
+        <item>
+            <key> <string>message_values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>external_validator_failed</string> </key>
+                    <value> <string>The input failed the external validator.</string> </value>
+                </item>
+                <item>
+                    <key> <string>required_not_found</string> </key>
+                    <value> <string>Input is required but no input given.</string> </value>
+                </item>
+                <item>
+                    <key> <string>too_long</string> </key>
+                    <value> <string>Too much input was given.</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>overrides</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>alternate_name</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>css_class</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>default</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>description</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>display_maxwidth</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>display_width</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>editable</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>enabled</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>external_validator</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>extra</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>hidden</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>max_length</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>required</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>truncate</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>unicode</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>whitespace_preserve</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>tales</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>alternate_name</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>css_class</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>default</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>description</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>display_maxwidth</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>display_width</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>editable</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>enabled</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>external_validator</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>extra</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>hidden</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>max_length</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>required</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>truncate</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>unicode</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>whitespace_preserve</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>alternate_name</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>css_class</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>default</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>description</string> </key>
+                    <value> <string>Custom value, else it will use portal\'s ip.</string> </value>
+                </item>
+                <item>
+                    <key> <string>display_maxwidth</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>display_width</string> </key>
+                    <value> <int>20</int> </value>
+                </item>
+                <item>
+                    <key> <string>editable</string> </key>
+                    <value> <int>1</int> </value>
+                </item>
+                <item>
+                    <key> <string>enabled</string> </key>
+                    <value> <int>1</int> </value>
+                </item>
+                <item>
+                    <key> <string>external_validator</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>extra</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>hidden</string> </key>
+                    <value> <int>0</int> </value>
+                </item>
+                <item>
+                    <key> <string>input_type</string> </key>
+                    <value> <string>text</string> </value>
+                </item>
+                <item>
+                    <key> <string>max_length</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>required</string> </key>
+                    <value> <int>0</int> </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string>IP of your server</string> </value>
+                </item>
+                <item>
+                    <key> <string>truncate</string> </key>
+                    <value> <int>0</int> </value>
+                </item>
+                <item>
+                    <key> <string>unicode</string> </key>
+                    <value> <int>0</int> </value>
+                </item>
+                <item>
+                    <key> <string>whitespace_preserve</string> </key>
+                    <value> <int>0</int> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
-- 
2.30.9