o
    ˷e                      @   sb   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZ d dlmZ G dd dZd	S )
    )HttpResponseRedirect)TemplateResponse)PayPalFailure)ConfirmFormPaymentForm)	PayPalWPPexpress_endpoint_for_token)warn_untestedc                
   @   s   e Zd ZdZddddZdededd	ddd
df
ddZdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& ZdS )'	PayPalProa[
  
    This class-based view takes care of PayPal WebsitePaymentsPro (WPP).
    PayPalPro has two separate flows - DirectPayment and ExpressPayFlow. In
    DirectPayment the user buys on your site. In ExpressPayFlow the user is
    direct to PayPal to confirm their purchase. PayPalPro implements both
    flows. To it create an instance using the these parameters:

    item: a dictionary that holds information about the item being purchased.

    For single item purchase (pay once):

        Required Keys:
            * amt: Float amount of the item.

        Optional Keys:
            * custom: You can set this to help you identify a transaction.
            * invnum: Unique ID that identifies this transaction.

    For recurring billing:

        Required Keys:
          * amt: Float amount for each billing cycle.
          * billingperiod: String unit of measure for the billing cycle (Day|Week|SemiMonth|Month|Year)
          * billingfrequency: Integer number of periods that make up a cycle.
          * profilestartdate: The date to begin billing. "2008-08-05T17:00:00Z" UTC/GMT
          * desc: Description of what you're billing for.

        Optional Keys:
          * trialbillingperiod: String unit of measure for trial cycle (Day|Week|SemiMonth|Month|Year)
          * trialbillingfrequency: Integer # of periods in a cycle.
          * trialamt: Float amount to bill for the trial period.
          * trialtotalbillingcycles: Integer # of cycles for the trial payment period.
          * failedinitamtaction: set to continue on failure (ContinueOnFailure / CancelOnFailure)
          * maxfailedpayments: number of payments before profile is suspended.
          * autobilloutamt: automatically bill outstanding amount.
          * subscribername: Full name of the person who paid.
          * profilereference: Unique reference or invoice number.
          * taxamt: How much tax.
          * initamt: Initial non-recurring payment due upon creation.
          * currencycode: defaults to USD
          * + a bunch of shipping fields

    payment_form_cls: form class that will be used to display the payment form.
    It should inherit from `paypal.pro.forms.PaymentForm` if you're adding more.

    payment_template: template used to ask the dude for monies. To comply with
    PayPal standards it must include a link to PayPal Express Checkout.

    confirm_form_cls: form class that will be used to display the confirmation form.
    It should inherit from `paypal.pro.forms.ConfirmForm`. It is only used in the Express flow.

    success_url / fail_url: URLs to be redirected to when the payment successful or fails.
    zQThere was an error processing your payment. Check your information and try again.z.Please correct the errors below and try again.z>There was a problem contacting PayPal. Please try again later.)
processingformpaypalNzpro/payment.htmlzpro/confirm.htmlz?successr   c                 C   sD   || _ || _|| _|| _|| _|| _|| _|pi | _|	| _|
| _	d S N)
itempayment_form_clspayment_templateconfirm_form_clsconfirm_templatesuccess_urlfail_urlcontextform_context_namenvp_handler)selfr   r   r   r   r   r   r   r   r   r    r   G/var/www/ideatree/venv/lib/python3.10/site-packages/paypal/pro/views.py__init__I   s   

zPayPalPro.__init__c                 C   sp   || _ |jdkr$|  r|  S |  r|  S |  r |  S |  S |  r,| 	 S | 
 r4|  S |  S )zAReturn the appropriate response for the state of the transaction.GET)requestmethodshould_redirect_to_expressredirect_to_expressshould_render_confirm_formrender_confirm_formshould_render_payment_formrender_payment_formshould_validate_confirm_formvalidate_confirm_formshould_validate_payment_formvalidate_payment_form)r   r   r   r   r   __call__a   s   
zPayPalPro.__call__c                 C   s   | j d uo	d| j v S )Nbillingperiod)r   r   r   r   r   is_recurringt   s   zPayPalPro.is_recurringc                 C   s   d| j jv S )Nexpressr   r   r,   r   r   r   r    w   s   z$PayPalPro.should_redirect_to_expressc                 C      d| j jv od| j jv S NtokenPayerIDr/   r,   r   r   r   r"   z      z$PayPalPro.should_render_confirm_formc                 C      dS NTr   r,   r   r   r   r$   }      z$PayPalPro.should_render_payment_formc                 C   r0   r1   )r   POSTr,   r   r   r   r&      r4   z&PayPalPro.should_validate_confirm_formc                 C   r5   r6   r   r,   r   r   r   r(      r7   z&PayPalPro.should_validate_payment_formc                 C   s"   |   | j| j< t| j| j| jS )z;Display the DirectPayment for entering payment information.)r   r   r   r   r   r   r,   r   r   r   r%      s   zPayPalPro.render_payment_formc                 C   s|   t   | | jj}| r%|| j| j}|rt| jS | j	d | j
d< || j
| j< | j
d| j	d  t| j| j| j
S )z8Try to validate and then process the DirectPayment form.r   errorsr   )r	   r   r   r8   is_validprocessr   r   r   r9   r   r   
setdefaultr   r   )r   r   successr   r   r   r)      s   
zPayPalPro.validate_payment_formc                 C   sZ   t | j}z|| j}W n ty%   t  | jd | jd< |   Y S w t	t
|jS )z
        First step of ExpressCheckout. Redirect the request to PayPal using the
        data returned from setExpressCheckout.
        r   r9   )r   r   setExpressCheckoutr   r   r	   r9   r   r%   r   r   r2   )r   wppnvp_objr   r   r   r!      s   
zPayPalPro.redirect_to_expressc                 C   sH   t   t| jjd | jjd d}| j|d| j| j< t| j| j| jS )z
        Second step of ExpressCheckout. Display an order confirmation form which
        contains hidden fields with the token / PayerID from PayPal.
        r2   r3   )r2   r3   )initial)	r	   dictr   r   r   r   r   r   r   )r   rA   r   r   r   r#      s   zPayPalPro.render_confirm_formc                 C   s   t | j}t| jjd | jjd d}| j| z|  r(t  || j}n|	| j}| 
| W n tyI   | jd | jd< |   Y S w t| jS )z
        Third and final step of ExpressCheckout. Request has pressed the confirmation but
        and we can send the final confirmation to PayPal using the data from the POST'ed form.
        r2   r3   )r2   payeridr   r9   )r   r   rB   r8   r   updater-   r	   createRecurringPaymentsProfiledoExpressCheckoutPayment
handle_nvpr   r9   r   r%   r   r   )r   r?   pp_datanvpr   r   r   r'      s   

zPayPalPro.validate_confirm_formc                 C   s   |  | d S r   )r   )r   rI   r   r   r   rG      s   zPayPalPro.handle_nvp)__name__
__module____qualname____doc__r9   r   r   r   r*   r-   r    r"   r$   r&   r(   r%   r)   r!   r#   r'   rG   r   r   r   r   r
      s<    7

r
   N)django.httpr   django.template.responser   paypal.pro.exceptionsr   paypal.pro.formsr   r   paypal.pro.helpersr   r   paypal.utilsr	   r
   r   r   r   r   <module>   s   