1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

locustでWooCommerceとStripeを使ったサイト(WordPress)での購入処理をするやり方

Posted at

はじめに

wordpress上でwoocommerceとstripeを使ったサイトへの購入処理をlocustで実装したので解説します.

woocommerceとは

wordpressで無料で使えるプラグインで本格的なサイトを作ることができます.
詳しい説明はここで見れます.

stripeとは

オンライン決済サービスのことです.
詳しい説明はここで見れます.

locustコード

最初にコードをのせておきます.

locustfile.py
from locust import HttpUser, TaskSet, task, constant
import random
import re

class UserBehavior(TaskSet):
    headers= {
        "Content_Type": "application/x-www-form-urlencoded"
    }

    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        #self.comment()

    @task(29)
    def index(self):
        self.client.get("/shop")
    
    @task(61)
    def addToCart(self):
        foo = ["36", "34", "45", "41", "32", "43"]
        num_item = random.randint(1, 10)
        data = {
            "product_sku": "",
            "product_id": f"{random.choice(foo)}", 
            "quantity": f"{num_item}"
        }
        self.res = self.client.post("/?wc-ajax=add_to_cart", data, headers=self.headers)
    
    @task(10)
    def checkout(self):
        self.addToCart()
        self.res2 = self.client.get("/checkout/", cookies=self.res.cookies)
        self.res2_nonce_list = re.findall("\"woocommerce-process-checkout-nonce\" value=\"\w+", self.res2.text)
        self.res2_nonce = self.res2_nonce_list[0]
        personal_data = {
            "billing_last_name": "○○",
            "billing_first_name": "○○",
            "billing_company": "",
            "billing_country": "JP",
            "billing_postcode": "123-4567",
            "billing_state": "JP13",
            "billing_city": "a",
            "billing_address_1": "11",
            "billing_address_2": "",
            "billing_phone": "1123456789",
            "billing_email": "hoge@○○.○○.○○.○○",
            "shipping_last_name": "○○",
            "shipping_first_name": "○○",
            "shipping_company": "",
            "shipping_country": "JP",
            "shipping_postcode": "123-4567",
            "shipping_state": "JP13",
            "shipping_city": "a",
            "shipping_address_1": "11",
            "shipping_address_2": "",
            "order_comments": "", 
            "shipping_method[0]": "free_shipping:1",
            "payment_method": "cod",
            "mailpoet_woocommerce_checkout_optin_present": "1",
            "woocommerce-process-checkout-nonce": f"{self.res2_nonce[-10:]}",
            "wp_http_referer": "http://○○/checkout/"
        }
        self.client.post("/?wc-ajax=checkout", personal_data, cookies=self.res.cookies)
class WebsiteUser(HttpUser):
    host = 'http://○○/'
    tasks = {UserBehavior}
    wait_time = constant(0)

購入処理

上のコードのなかで実際に購入処理を行っているのはこの部分です.

def checkout(self):
    self.addToCart()
    self.res2 = self.client.get("/checkout/", cookies=self.res.cookies)
    self.res2_nonce_list = re.findall("\"woocommerce-process-checkout-nonce\" value=\"\w+", self.res2.text)
    self.res2_nonce = self.res2_nonce_list[0]
    personal_data = {
            "billing_last_name": "○○",
            "billing_first_name": "○○",
            "billing_company": "",
            "billing_country": "JP",
            "billing_postcode": "123-4567",
            "billing_state": "JP13",
            "billing_city": "a",
            "billing_address_1": "11",
            "billing_address_2": "",
            "billing_phone": "1123456789",
            "billing_email": "hoge@○○.○○.○○.○○",
            "shipping_last_name": "○○",
            "shipping_first_name": "○○",
            "shipping_company": "",
            "shipping_country": "JP",
            "shipping_postcode": "123-4567",
            "shipping_state": "JP13",
            "shipping_city": "a",
            "shipping_address_1": "11",
            "shipping_address_2": "",
            "order_comments": "", 
            "shipping_method[0]": "free_shipping:1",
            "payment_method": "cod",
            "mailpoet_woocommerce_checkout_optin_present": "1",
            "woocommerce-process-checkout-nonce": f"{self.res2_nonce[-10:]}",
            "wp_http_referer": "http://○○/checkout/"
    }
    self.client.post("/?wc-ajax=checkout", personal_data, cookies=self.res.cookies)

ここでは商品情報をpostし,その後にチェックアウト画面をgetした時のレスポンスを変数に保存します.
そのレスポンス情報の中からnonceと呼ばれる10桁の数字を探します.
そしてその数字をチェックアウトする時のpostの引数のデータの中に埋め込みます.
そうすることで購入処理を行うことができます.

おわりに

今回はwordpress上でwoocommerceとstripeを使ったサイトへの購入処理をlocustで実装したので簡単に解説しました.

ここまで読んでいただき,ありがとうございました.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?