示例#1
0
    def pay(self, amount, preauth=False, **kwargs):
        session_timeout = self.merchant.get('session_timeout', self.__default_session_timeout)
        currency = self.merchant.get('currency', self.__default_currency_code)
        fail_url = kwargs.get('fail_url', self.merchant.get('fail_url'))
        success_url = kwargs.get('success_url', self.merchant.get('success_url'))
        client_id = kwargs.get('client_id')
        page_view = kwargs.get('page_view', 'DESKTOP')
        details = kwargs.get('details', {})
        description = kwargs.get('description')
        method = 'rest/register' if not preauth else 'rest/registerPreAuth'

        if success_url is None:
            raise ValueError("success_url is not set")

        try:
            amount = Decimal(str(amount))
        except (ValueError, DecimalException):
            raise TypeError(
                "Wrong amount type, passed {} ({}) instead of decimal".format(amount, type(amount)))

        payment = Payment(amount=amount, client_id=client_id, method=Method.WEB, details={
            'username': self.merchant.get("username"),
            'currency': currency,
            'success_url': success_url,
            'fail_url': fail_url,
            'session_timeout': session_timeout,
            'client_id': client_id
        })

        payment.details.update(details)
        payment.save()

        data = {
            'orderNumber': payment.uid.hex,
            'amount': int(amount * 100),
            'returnUrl': success_url,
            'failUrl': fail_url,
            'sessionTimeoutSecs': session_timeout,
            'pageView': page_view,
        }
        if kwargs.get('params'):
            data.update({'jsonParams': json.dumps(kwargs.get('params'))})
        if kwargs.get('client_id'):
            data.update({'clientId': client_id})
        if kwargs.get('description'):
            data.update({'description': description})

        response = self.execute_request(data, method, payment)

        payment.bank_id = response.get('orderId')
        payment.status = Status.PENDING
        payment.details.update({'redirect_url': response.get('formUrl')})
        if kwargs.get('params'):
            payment.details.update(kwargs.get('params'))
        payment.save()

        return payment, payment.details.get("redirect_url")
示例#2
0
    def mobile_pay(self, amount, token, ip, **kwargs):
        currency = self.merchant.get('currency', self.__default_currency_code)
        client_id = kwargs.get('client_id')
        details = kwargs.get('details', {})
        fail_url = kwargs.get('fail_url', self.merchant.get('fail_url'))
        success_url = kwargs.get('success_url',
                                 self.merchant.get('success_url'))
        method = "applepay/payment"
        db_method = Method.APPLE

        try:
            decoded_token = json.loads(base64.b64decode(token).decode())
            if "signedMessage" in decoded_token:
                method = "google/payment"
                db_method = Method.GOOGLE

        except:
            raise TypeError("Failed to decode payment token")

        try:
            amount = Decimal(str(amount))
        except (ValueError, DecimalException):
            raise TypeError(
                "Wrong amount type, passed {} ({}) instead of decimal".format(
                    amount, type(amount)))

        payment = Payment(amount=amount,
                          client_id=client_id,
                          method=db_method,
                          details={
                              'username': self.merchant.get("username"),
                              'currency': currency
                          })

        if kwargs.get('params'):
            payment.details.update(kwargs.get('params'))

        payment.details.update(details)
        payment.status = Status.PENDING
        payment.save()

        data = {
            'merchant': self.merchant_id,
            'orderNumber': payment.uid.hex,
            'paymentToken': token,
            'ip': ip
        }
        if method == "google/payment":
            data.update({
                'amount': int(amount * 100),
                'returnUrl': success_url,
                'failUrl': fail_url
            })
        if kwargs.get('params'):
            data.update({'additionalParameters': kwargs.get('params')})
        if kwargs.get('description'):
            data.update({'description': kwargs.get('description')})

        response = self.execute_request(data, method, payment)

        if response.get('success'):
            payment.bank_id = response.get('data').get('orderId')
            if 'orderStatus' in response:
                payment.details.update(
                    {"pan": response['orderStatus']['cardAuthInfo']['pan']})
        else:
            payment.status = Status.FAILED

        payment.save()
        if payment.status != Status.FAILED:
            payment = self.check_status(payment.uid)
        return payment, response