본문 바로가기
Python

[python] paypal 결제 후 환불하기 예제

by 앗사비 2015. 12. 16.
728x90

사전 조건 : 파이썬+셀레늄 설치


---


* 결제하기


import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

my_id = "testuser@gmail.com"
my_pw = "qwer1234"
pay_id = "paymentuser@gmail.com"
pay_pw = "abc1122!"

class MyPaymentTest(unittest.TestCase):

    def setUp(self):
        #chrome 브라우저
        self.driver = webdriver.Chrome(executable_path=r'D:\webdriver\chromedriver.exe')
        
        #phantomjs
        #self.driver = webdriver.PhantomJS(executable_path=r'D:\webdriver\phantomjs.exe')

    def test_login(self):
        driver = self.driver
        driver.set_window_size(1280, 720) #브라우저 해상도
        
        #메인 페이지
        driver.get("https://test.mysite.com/")
        time.sleep(3) #3초 대기
        self.assertIn("My Site", driver.title) #브라우저 타이틀 검사

        #로그인
        driver.find_element_by_link_text("로그인").click() #하이퍼링크 문자열 클릭    
        elem_mail = driver.find_element_by_id("email_address")
        elem_pw = driver.find_element_by_id("password")
        elem_mail.send_keys(my_id)
        elem_pw.send_keys(my_pw)
        elem_pw.send_keys(Keys.RETURN) #엔터키
        time.sleep(3)
        
        #결제 페이지
        driver.get("https://test.mysite.com/pay")  
        driver.find_element_by_id('paypal').click() 
        driver.find_element_by_xpath("//*[@id='paypalpay']/input[12]").click()    
        time.sleep(7)  

        #페이팔 사이트에서 결제 진행
        driver.find_element_by_id('payment_type_paypal').click() 
        paypal_mail = driver.find_element_by_id("login_email")
        paypal_pw = driver.find_element_by_id("login_password")
        paypal_mail.send_keys(pay_id)
        paypal_pw.send_keys(pay_pw)
        paypal_pw.send_keys(Keys.RETURN)
        driver.find_element_by_name('submit.x').click() 

        #결제 완료 팝업창 표시될 때까지 기다림
        wait = WebDriverWait(driver, 100)
        element = wait.until(EC.visibility_of_element_located((By.ID,'popup_confirm_btn')))
        element.click() 

        #주문번호를 텍스트 파일에 저장
        pay_orderId = driver.find_element_by_id('orderId').text 
        f = open("D:/Python34/orderId.txt", 'w')
        f.write(pay_orderId)
        f.close()

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()
    
    


* 환불하기


import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

pay_admin_id = "paymentadmin@gmail.com"
pay_pw = "abc1122!"

#주문번호 가져오기
f = open("D:/Python34/orderId.txt", 'r') 
pay_orderId = f.readline() 
f.close()

class MyRefundTest(unittest.TestCase):

    def setUp(self):
        #chrome 브라우저
        self.driver = webdriver.Chrome(executable_path=r'D:\webdriver\chromedriver.exe')
        
        #phantomjs
        #self.driver = webdriver.PhantomJS(executable_path=r'D:\webdriver\phantomjs.exe')
        
    def test_login(self):
        driver = self.driver
        driver.set_window_size(1280, 720) #브라우저 해상도
        
        #메인 페이지
        driver.get("https://www.sandbox.paypal.com")
        time.sleep(3)
        
        #로그인
        driver.find_element_by_id('ul-btn').click() 
        elem_mail = driver.find_element_by_id("email")
        elem_pw = driver.find_element_by_id("password")
        elem_mail.send_keys(pay_admin_id)
        elem_pw.send_keys(pay_pw)
        elem_pw.send_keys(Keys.RETURN)
        time.sleep(10)
        
        #주문번호로 검색 후 환불
        driver.get("https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_history-search&nav=0%2e3%2e1") 
        driver.find_element_by_xpath("//*[@id='search_In']/option[2]").click()
        driver.find_element_by_id('search').send_keys(pay_orderId)
        driver.find_element_by_name('search').click() 
        time.sleep(5)       
        driver.find_element_by_link_text('Issue a refund').click() 
        time.sleep(1)  
        driver.find_element_by_name('confirm.x').click() 
        time.sleep(1)         
        driver.find_element_by_name('submit.x').click()
        time.sleep(1)         
        assert "Refund Complete" in driver.page_source

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

728x90