def make_payment(self, resource, mode, amount, customer, devise=None, order=None): # Auto incremental name for payments name = self.make_reference() payment_way = get_payment_way(self, mode) order_abspath = str(order.get_abspath()) if order else None # Devise if devise is None: shop = get_shop(resource) devise = shop.get_property('devise') # Payment configuration kw = {'amount': amount, 'customer_id': customer.name, 'devise': devise, 'order_abspath': order_abspath} # Create order cls = payment_way.payment_class return resource.make_resource(name, cls, **kw)
def POST(self, resource, context): # XXX TODO Check signature form = self._get_form(resource, context) # Set payment as paid if form['autorisation']: resource.update_payment_state(context, paid=True) else: resource.update_payment_state(context, paid=False) for key in ['transaction', 'autorisation', 'advanced_state']: resource.set_property(key, form[key]) # We check amount amount = form['amount'] / decimal('100') if resource.get_property('amount') != amount: raise ValueError, 'invalid payment amount' # We ensure that remote ip address belongs to Paybox authorized_ip = self.authorized_ip payment_way = get_payment_way(resource, 'paybox') if not payment_way.get_property('real_mode'): authorized_ip = authorized_ip + [None] if context.get_remote_ip() not in authorized_ip: resource.set_property('advanced_state', 'ip_not_authorized') # Return a blank page to payment context.set_content_type('text/plain')
def GET(self, resource, context): """This view load the paybox cgi. That script redirect on paybox server to show the payment form. """ # We get the paybox CGI path on server cgi_path = join(dirname(sys.executable), 'paybox.cgi') # Configuration kw = {} order = resource.parent kw['PBX_CMD'] = order.name kw['PBX_TOTAL'] = int(resource.get_property('amount') * 100) # Basic configuration kw['PBX_MODE'] = '4' kw['PBX_LANGUE'] = 'FRA' kw['PBX_TYPEPAIEMENT'] = 'CARTE' kw['PBX_WAIT'] = '0' kw['PBX_RUF1'] = 'POST' kw['PBX_RETOUR'] = "transaction:T;autorisation:A;amount:M;advanced_state:E;payment:P;carte:C;sign:K" # PBX Retour uri base_uri = context.uri.resolve(context.get_link(resource)) for option in PBXState.get_options(): key = option['pbx'] status = option['name'] uri = '%s/;end?status=%s' % (base_uri, status) kw[key] = '%s' % uri # PBX_REPONDRE_A (Url to call to set payment status) kw['PBX_REPONDRE_A'] = '%s/;callback' % base_uri # Configuration payment_way = get_payment_way(resource, 'paybox') for key in ['PBX_SITE', 'PBX_IDENTIFIANT', 'PBX_RANG', 'PBX_DIFF', 'PBX_AUTOSEULE']: kw[key] = payment_way.get_property(key) # Devise kw['PBX_DEVISE'] = resource.get_property('devise') # PBX_PORTEUR # XXX Allow to overide PBX_PORTEUR # (If someone call and give his card number ?) email = context.user.get_property('email') if Email.is_valid(email) is False: raise ValueError, 'PBX_PORTEUR should be a valid Email address' kw['PBX_PORTEUR'] = email # En mode test: if not payment_way.get_property('real_mode'): kw.update(payment_way.test_configuration) # Build cmd cmd = [cgi_path] + ['%s=%s' % (x[0], x[1]) for x in kw.iteritems()] log_debug("Calling Paybox: {0!r}".format(cmd)) # Call the CGI try: result = check_output(cmd) # Check if all is ok html = re.match ('.*?<HEAD>(.*?)</HTML>', result, re.DOTALL) if html is None: raise CalledProcessError except CalledProcessError, e: # Try do get error number num_error = re.match ('.*?NUMERR=(.*?)"', e.output, re.DOTALL) if num_error: num_error = num_error.group(1) error = PayboxCGIErrors.get_value(num_error) else: error = "Unknow reason" error = u"Error: payment module can't be loaded. (%s)" % error raise ValueError, error