Пример #1
0
class TestAdaptivePayments(TestCase):
    notification_url = "http://notify.me"
    return_url = "http://return.me"
    cancel_url = "http://cancel.me"

    def setUp(self):
        self.paypal = AdaptivePayments(
            API_USERNAME, API_PASSWORD, API_SIGNATURE, API_APPLICATION_ID, API_EMAIL, sandbox=True
        )
        self.paypal.debug = False

    def test_pay(self):
        response = self.paypal.pay(
            actionType="PAY",
            cancelUrl=self.cancel_url,
            currencyCode="USD",
            senderEmail=API_EMAIL,
            feesPayer="EACHRECEIVER",
            memo="Simple payment example",
            receiverList={"receiver": [{"amount": "1.0", "email": EMAIL_ACCOUNT}]},
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url,
        )
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")
        self.assertEquals(response["paymentExecStatus"], "COMPLETED")
        self.assertTrue(response.has_key("payKey"))

    def test_set_payments_options(self):
        payKey = self.paypal.pay(
            actionType="CREATE",
            cancelUrl=self.cancel_url,
            currencyCode="USD",
            senderEmail=API_EMAIL,
            feesPayer="EACHRECEIVER",
            receiverList={"receiver": [{"amount": "1.0", "email": EMAIL_ACCOUNT}]},
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url,
        )["payKey"]
        response = self.paypal.set_payments_options(
            initiatingEntitity={
                "institutionCustomer": {
                    "countryCode": "US",
                    "displayName": "Sesame Street",
                    "email": "*****@*****.**",
                    "firstName": "Elmo",
                    "institutionCustomer": "1",
                    "institutionCustomer": "2",
                    "lastName": "Baby Monster",
                }
            },
            payKey=payKey,
        )
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")

    def test_execute_payment(self):
        payKey = self.paypal.pay(
            actionType="CREATE",
            cancelUrl=self.cancel_url,
            currencyCode="USD",
            senderEmail=API_EMAIL,
            feesPayer="EACHRECEIVER",
            receiverList={"receiver": [{"amount": "1.0", "email": EMAIL_ACCOUNT}]},
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url,
        )["payKey"]
        response = self.paypal.execute_payment(payKey=payKey)
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")
        self.assertEquals(response["paymentExecStatus"], "COMPLETED")

    def test_payment_details(self):
        payKey = self.paypal.pay(
            actionType="CREATE",
            cancelUrl=self.cancel_url,
            currencyCode="USD",
            senderEmail=API_EMAIL,
            feesPayer="EACHRECEIVER",
            receiverList={"receiver": [{"amount": "1.0", "email": EMAIL_ACCOUNT}]},
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url,
        )["payKey"]
        response = self.paypal.payment_details(payKey=payKey)
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")
        self.assertEquals(response["payKey"], payKey)
        self.assertEquals(response["actionType"], "CREATE")

    def test_get_payment_options(self):
        payKey = self.paypal.pay(
            actionType="CREATE",
            cancelUrl=self.cancel_url,
            currencyCode="USD",
            senderEmail=API_EMAIL,
            feesPayer="EACHRECEIVER",
            receiverList={"receiver": [{"amount": "1.0", "email": EMAIL_ACCOUNT}]},
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url,
        )["payKey"]
        response = self.paypal.get_payment_options(payKey=payKey)
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")

    def test_preapproval(self):
        response = self.paypal.preapproval(
            clientDetails={"ipAddress": "127.0.0.1"},
            startingDate=datetime.now(),
            endingDate=datetime.now() + timedelta(days=2),
            maxTotalAmountOfAllPayments="10.00",
            currencyCode="USD",
            cancelUrl=self.cancel_url,
            returnUrl=self.return_url,
        )
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")
        self.assertTrue(response.has_key("preapprovalKey"))

    def test_preapproval_details(self):
        preapprovalKey = self.paypal.preapproval(
            clientDetails={"ipAddress": "127.0.0.1"},
            startingDate=datetime.now(),
            endingDate=datetime.now() + timedelta(days=2),
            maxTotalAmountOfAllPayments="10.00",
            currencyCode="USD",
            cancelUrl=self.cancel_url,
            returnUrl=self.return_url,
        )["preapprovalKey"]
        response = self.paypal.preapproval_details(preapprovalKey=preapprovalKey)
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")
        self.assertEquals(response["returnUrl"], self.return_url)
        self.assertEquals(response["cancelUrl"], self.cancel_url)
        self.assertEquals(response["currencyCode"], "USD")

    def test_cancel_preapproval(self):
        preapprovalKey = self.paypal.preapproval(
            clientDetails={"ipAddress": "127.0.0.1"},
            startingDate=datetime.now(),
            endingDate=datetime.now() + timedelta(days=2),
            maxTotalAmountOfAllPayments="10.00",
            currencyCode="USD",
            cancelUrl=self.cancel_url,
            returnUrl=self.return_url,
        )["preapprovalKey"]
        response = self.paypal.cancel_preapproval(preapprovalKey=preapprovalKey)
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")

    def test_refund(self):
        payKey = self.paypal.pay(
            actionType="PAY",
            cancelUrl=self.cancel_url,
            currencyCode="USD",
            senderEmail=API_EMAIL,
            feesPayer="EACHRECEIVER",
            receiverList={"receiver": [{"amount": "1.0", "email": EMAIL_ACCOUNT}]},
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url,
        )["payKey"]
        response = self.paypal.refund(currencyCode="USD", payKey=payKey)
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")

    def test_convert_currency(self):
        response = self.paypal.convert_currency(
            baseAmountList={"currency": [{"amount": "10.00", "code": "USD"}]},
            convertToCurrencyList=[{"currencyCode": "EUR"}],
        )
        self.assertEquals(response["responseEnvelope"]["ack"], "Success")
        self.assertTrue(response.has_key("estimatedAmountTable"))
Пример #2
0
class TestAdaptivePayments(TestCase):
    notification_url = "http://notify.me"
    return_url = "http://return.me"
    cancel_url = "http://cancel.me"

    def setUp(self):
        self.paypal = AdaptivePayments(API_USERNAME,
                                       API_PASSWORD,
                                       API_SIGNATURE,
                                       API_APPLICATION_ID,
                                       API_EMAIL,
                                       sandbox=True)
        self.paypal.debug = False

    def test_pay(self):
        response = self.paypal.pay(actionType='PAY',
                                   cancelUrl=self.cancel_url,
                                   currencyCode='USD',
                                   senderEmail=API_EMAIL,
                                   feesPayer='EACHRECEIVER',
                                   memo='Simple payment example',
                                   receiverList={
                                       'receiver': [{
                                           'amount': "1.0",
                                           'email': EMAIL_ACCOUNT,
                                       }]
                                   },
                                   returnUrl=self.return_url,
                                   ipnNotificationUrl=self.notification_url)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")
        self.assertEquals(response['paymentExecStatus'], "COMPLETED")
        self.assertTrue(response.has_key('payKey'))

    def test_set_payments_options(self):
        payKey = self.paypal.pay(
            actionType='CREATE',
            cancelUrl=self.cancel_url,
            currencyCode='USD',
            senderEmail=API_EMAIL,
            feesPayer='EACHRECEIVER',
            receiverList={
                'receiver': [{
                    'amount': "1.0",
                    'email': EMAIL_ACCOUNT,
                }]
            },
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url)['payKey']
        response = self.paypal.set_payments_options(initiatingEntitity={
            'institutionCustomer': {
                'countryCode': 'US',
                'displayName': 'Sesame Street',
                'email': '*****@*****.**',
                'firstName': 'Elmo',
                'institutionCustomer': '1',
                'institutionCustomer': '2',
                'lastName': 'Baby Monster'
            }
        },
                                                    payKey=payKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")

    def test_execute_payment(self):
        payKey = self.paypal.pay(
            actionType='CREATE',
            cancelUrl=self.cancel_url,
            currencyCode='USD',
            senderEmail=API_EMAIL,
            feesPayer='EACHRECEIVER',
            receiverList={
                'receiver': [{
                    'amount': "1.0",
                    'email': EMAIL_ACCOUNT,
                }]
            },
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url)['payKey']
        response = self.paypal.execute_payment(payKey=payKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")
        self.assertEquals(response['paymentExecStatus'], 'COMPLETED')

    def test_payment_details(self):
        payKey = self.paypal.pay(
            actionType='CREATE',
            cancelUrl=self.cancel_url,
            currencyCode='USD',
            senderEmail=API_EMAIL,
            feesPayer='EACHRECEIVER',
            receiverList={
                'receiver': [{
                    'amount': "1.0",
                    'email': EMAIL_ACCOUNT,
                }]
            },
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url)['payKey']
        response = self.paypal.payment_details(payKey=payKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")
        self.assertEquals(response['payKey'], payKey)
        self.assertEquals(response['actionType'], 'CREATE')

    def test_get_payment_options(self):
        payKey = self.paypal.pay(
            actionType='CREATE',
            cancelUrl=self.cancel_url,
            currencyCode='USD',
            senderEmail=API_EMAIL,
            feesPayer='EACHRECEIVER',
            receiverList={
                'receiver': [{
                    'amount': "1.0",
                    'email': EMAIL_ACCOUNT,
                }]
            },
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url)['payKey']
        response = self.paypal.get_payment_options(payKey=payKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")

    def test_preapproval(self):
        response = self.paypal.preapproval(
            clientDetails={
                'ipAddress': '127.0.0.1',
            },
            startingDate=datetime.now(),
            endingDate=datetime.now() + timedelta(days=2),
            maxTotalAmountOfAllPayments="10.00",
            currencyCode='USD',
            cancelUrl=self.cancel_url,
            returnUrl=self.return_url,
        )
        self.assertEquals(response['responseEnvelope']['ack'], "Success")
        self.assertTrue(response.has_key('preapprovalKey'))

    def test_preapproval_details(self):
        preapprovalKey = self.paypal.preapproval(
            clientDetails={
                'ipAddress': '127.0.0.1',
            },
            startingDate=datetime.now(),
            endingDate=datetime.now() + timedelta(days=2),
            maxTotalAmountOfAllPayments="10.00",
            currencyCode='USD',
            cancelUrl=self.cancel_url,
            returnUrl=self.return_url,
        )['preapprovalKey']
        response = self.paypal.preapproval_details(
            preapprovalKey=preapprovalKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")
        self.assertEquals(response['returnUrl'], self.return_url)
        self.assertEquals(response['cancelUrl'], self.cancel_url)
        self.assertEquals(response['currencyCode'], 'USD')

    def test_cancel_preapproval(self):
        preapprovalKey = self.paypal.preapproval(
            clientDetails={
                'ipAddress': '127.0.0.1',
            },
            startingDate=datetime.now(),
            endingDate=datetime.now() + timedelta(days=2),
            maxTotalAmountOfAllPayments="10.00",
            currencyCode='USD',
            cancelUrl=self.cancel_url,
            returnUrl=self.return_url,
        )['preapprovalKey']
        response = self.paypal.cancel_preapproval(
            preapprovalKey=preapprovalKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")

    def test_refund(self):
        payKey = self.paypal.pay(
            actionType='PAY',
            cancelUrl=self.cancel_url,
            currencyCode='USD',
            senderEmail=API_EMAIL,
            feesPayer='EACHRECEIVER',
            receiverList={
                'receiver': [{
                    'amount': "1.0",
                    'email': EMAIL_ACCOUNT,
                }]
            },
            returnUrl=self.return_url,
            ipnNotificationUrl=self.notification_url)['payKey']
        response = self.paypal.refund(currencyCode='USD', payKey=payKey)
        self.assertEquals(response['responseEnvelope']['ack'], "Success")

    def test_convert_currency(self):
        response = self.paypal.convert_currency(
            baseAmountList={'currency': [{
                'amount': "10.00",
                'code': 'USD'
            }]},
            convertToCurrencyList=[{
                'currencyCode': 'EUR'
            }])
        self.assertEquals(response['responseEnvelope']['ack'], "Success")
        self.assertTrue(response.has_key('estimatedAmountTable'))
Пример #3
0
class AdaptivePapalPayments(AbstractPayment):

    NOT_NEED_IPN = False

    def __init__(self, *args, **kwargs):

        self.payments = AdaptivePayments(settings.PAYPAL_API_USERNAME,
                                         settings.PAYPAL_API_PASSWORD,
                                         settings.PAYPAL_API_SIGNATURE,
                                         settings.PAYPAL_API_APPLICATION_ID,
                                         settings.PAYPAL_API_EMAIL,
                                         sandbox=settings.USE_PAYPAL_SANDBOX)
        super(AdaptivePapalPayments, self).__init__()

    def preapproval(self, cancel_url, return_url, ip_address):

        now = datetime.datetime.now()
        domain = Site.objects.get_current().domain
        protocol = "https"
        total_amount = None
        if settings.CONVERT_XPF:
            total_amount = convert_from_xpf(self.booking.total_amount)
        else:
            total_amount = self.booking.total_amount
        response = self.payments.preapproval(
            startingDate=now,
            endingDate=now + datetime.timedelta(days=360),
            currencyCode=self.booking._currency,
            maxTotalAmountOfAllPayments=str(total_amount.quantize(D(".00"), ROUND_CEILING)),
            cancelUrl=cancel_url,
            returnUrl=return_url,
            ipnNotificationUrl='http://www.postbin.org/1fi02go' if settings.USE_PAYPAL_SANDBOX \
                else urljoin("%s://%s" % (protocol, domain), reverse('preapproval_ipn')),
            client_details={
                'ipAddress': ip_address,
                'partnerName': 'e-loue',
                'customerType': 'Business' if self.booking.borrower.is_professional else 'Personnal',
                'customerId': str(self.booking.borrower.pk)
            }
        )
        self.preapproval_key = response['preapprovalKey']

    def pay(self, cancel_url, return_url):
        # debit borrower
        domain = Site.objects.get_current().domain
        protocol = "https"

        total_amount = None
        net_price = None
        if settings.CONVERT_XPF:
            total_amount = convert_from_xpf(self.booking.total_amount)
            net_price = convert_from_xpf(self.booking.net_price)
        else:
            total_amount = self.booking.total_amount
            net_price = self.booking.net_price
        response = self.payments.pay(
            actionType='PAY_PRIMARY',
            senderEmail=self.booking.borrower.paypal_email,
            feesPayer='PRIMARYRECEIVER',
            cancelUrl=cancel_url,
            returnUrl=return_url,
            currencyCode=self.booking._currency,
            preapprovalKey=self.preapproval_key,
            ipnNotificationUrl='http://www.postbin.org/1fi02go' if settings.USE_PAYPAL_SANDBOX \
                else urljoin("%s://%s" % (protocol, domain), reverse('pay_ipn')),
            receiverList={'receiver': [
                {'primary':True, 'amount':str(total_amount.quantize(D(".00"), ROUND_CEILING)), 'email':settings.PAYPAL_API_EMAIL},
                {'primary':False, 'amount':str(net_price.quantize(D(".00"), ROUND_FLOOR)), 'email':self.booking.owner.paypal_email}
            ]}
        )
        if 'ERROR' in response.get('paymentExecStatus', None):
            raise PaypalError('paymentExecStatus',
                              response.get('paymentExecStatus', None),
                              response)
        self.pay_key = response['payKey']

    def refund(self):
        response = self.payments.refund(payKey=self.booking.pay_key,
                                        currencyCode=self.booking._currency)

    def execute_payment(self):
        # pay owner
        response = self.payments.execute_payment(
            payKey=self.booking.payment.pay_key)

    def cancel_preapproval(self):
        response = self.payments.cancel_preapproval(
            preapprovalKey=self.booking.preapproval_key, )

    def give_caution(self, amount, cancel_url, return_url):
        if not amount or amount > self.booking.deposit_amount:
            amount = self.booking.deposit_amount

        domain = Site.objects.get_current().domain
        protocol = "https"

        response = self.payments.pay(
            actionType='PAY',
            senderEmail=self.booking.borrower.paypal_email,
            preapprovalKey=self.booking.preapproval_key,
            cancelUrl=cancel_url,
            returnUrl=return_url,
            currencyCode=self.booking._currency,
            ipnNotificationUrl='http://www.postbin.org/1fi02go' if settings.USE_PAYPAL_SANDBOX \
                else urljoin("%s://%s" % (protocol, domain), reverse('pay_ipn')),
            receiverList={'receiver': [
                {'amount':str(amount.quantize(D('.00'), ROUND_FLOOR)), 'email':self.booking.owner.paypal_email},
            ]}
        )