<?php
function createOrder($selectedGame_id, $quantityToBuy, $totalPrice, $msisdn) {
$consumer_key = ''; //intentionally removed
$consumer_secret = ''; //intentionally removed
$order_url = 'https://www.afrolottogh.com/wp-json/wc/v3/orders';
$auth = base64_encode($consumer_key . ":" . $consumer_secret);
$customerExists = checkAndCreateCustomer($msisdn); //function that checks for the existence of a customer based on the phone number that dialled the USSD code.
$customer_data = $customerExists ? $customerExists : null; //if customer exists then the details of the customer would be used to make the order
if ($customerExists) {
$order_data = array(
'payment_method' => 'bacs',
'payment_method_title' => 'Afro Lotto Checkout',
'set_paid' => true,
'billing' => array(
'first_name' => $customer_data['first_name'],
'last_name' => $customer_data['last_name'],
'address_1' => $customer_data['billing']['address_1'],
'address_2' => $customer_data['billing']['address_2'],
'city' => $customer_data['billing']['city'],
'state' => $customer_data['billing']['state'],
'postcode' => $customer_data['billing']['postcode'],
'country' => $customer_data['billing']['country'],
'email' => $customer_data['email'],
'phone' => $customer_data['billing']['phone']
),
'shipping' => array(
'first_name' => $customer_data['shipping']['first_name'],
'last_name' => $customer_data['shipping']['last_name'],
'address_1' => $customer_data['shipping']['address_1'],
'address_2' => $customer_data['shipping']['address_2'],
'city' => $customer_data['shipping']['city'],
'state' => $customer_data['shipping']['state'],
'postcode' => $customer_data['shipping']['postcode'],
'country' => $customer_data['shipping']['country']
),
'line_items' => array(
array(
'product_id' => $selectedGame_id,
'quantity' => $quantityToBuy
)
),
'shipping_lines' => array(
array(
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => $totalPrice
)
)
);
} else {
$order_data = array(
'payment_method' => 'bacs',
'payment_method_title' => 'Afro Lotto Checkout',
'set_paid' => true,
'billing' => array(
'first_name' => $msisdn,
'last_name' => 'USSD',
'address_1' => 'USSD',
'address_2' => '',
'city' => 'USSD',
'state' => 'AH',
'postcode' => '',
'country' => 'GH',
'email' => $msisdn . '@gmail.com',
'phone' => $msisdn
),
'shipping' => array(
'first_name' => $msisdn,
'last_name' => 'USSD',
'address_1' => 'USSD',
'address_2' => '',
'city' => 'USSD',
'state' => 'AH',
'postcode' => '',
'country' => 'GH'
),
'line_items' => array(
array(
'product_id' => $selectedGame_id,
'quantity' => $quantityToBuy
)
),
'shipping_lines' => array(
array(
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => $totalPrice
)
)
);
}
// Send request to create order
$ch = curl_init($order_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process response
$order_response = json_decode($response, true);
// Check if order creation was successful
if (isset($order_response['id'])) {
return "Order created successfully. Order ID: " . $order_response['id'];
} else {
return "Failed to create order. Error: " . $order_response['message'];
}
}
?>
我上面的代码没有按预期创建订单,我正在通过 USSD 系统使用 REST API 向 WordPress 网站发出订单请求。
因此,我使用 Postman 来测试与网站的 API 连接,方法是使用默认 API 端点购买实时产品来创建订单,并且它有效
我的挑战是如何让 PHP 文件能够做到这一点因为 PHP 代码中已经提供了所需的每个参数,但 WordPress 网站上没有显示任何订单
{
"payment_method": "bacs",
"payment_method_title": "Afro Lotto Checkout",
"set_paid": true,
"billing": {
"first_name": "John",
"last_name": "Doe",
"address_1": "123 Main St",
"city": "Anytown",
"state": "CA",
"postcode": "12345",
"country": "US",
"email": "[email protected]",
"phone": "123456789"
},
"shipping": {
"first_name": "John",
"last_name": "Doe",
"address_1": "123 Main St",
"city": "Anytown",
"state": "CA",
"postcode": "12345",
"country": "US"
},
"line_items": [
{
"product_id": 41660,
"quantity": 1
}
],
"shipping_lines": [
{
"method_id": "flat_rate",
"method_title": "Flat Rate",
"total": "5.00"
}
]
}
这是 $response 变量的值:
Order creation response: {
"code":"rest_invalid_param",
"message":"Invalid parameter(s): billing, shipping, shipping_lines",
"data":{
"status":400,
"params":{
"billing":"billing[first_name] is not of type string.",
"shipping":"shipping[first_name] is not of type string.",
"shipping_lines":"shipping_lines[0][total] is not of type string."
},
"details":{
"billing":{
"code":"rest_invalid_type",
"message":"billing[first_name] is not of type string.",
"data":{
"param":"billing[first_name]"
}
},
"shipping":{
"code":"rest_invalid_type",
"message":"shipping[first_name] is not of type string.",
"data":{
"param":"shipping[first_name]"
}
},
"shipping_lines":{
"code":"rest_invalid_type",
"message":"shipping_lines[0][total] is not of type string.",
"data":{
"param":"shipping_lines[0][total]"
}
}
}
}
}
所以我根据错误日志的响应对代码做了一些更改。我定义了计费和运输的默认值:
// Define default billing and shipping values
$default_billing_values = array(
'first_name' => (string)$msisdn,
'last_name' => 'USSD',
'address_1' => 'USSD',
'address_2' => '',
'city' => 'USSD',
'state' => 'AH',
'postcode' => '',
'country' => 'GH',
'email' => '[email protected]',
'phone' => (string)$msisdn
);
$default_shipping_values = array(
'first_name' => (string)$msisdn,
'last_name' => 'USSD',
'address_1' => 'USSD',
'address_2' => '',
'city' => 'USSD',
'state' => 'AH',
'postcode' => '',
'country' => 'GH'
);
然后我使用客户的信息(如果存在),否则我使用默认值:
// Check if customer exists, use customer data if available, otherwise use default values
if ($customerExists) {
$customer_id = isset($customer_data['id']) ? $customer_data['id'] : null;
$billing_data = isset($customer_data['billing']) ? $customer_data['billing'] : $default_billing_values;
$shipping_data = isset($customer_data['shipping']) ? $customer_data['shipping'] : $default_shipping_values;
} else {
$billing_data = $default_billing_values;
$shipping_data = $default_shipping_values;
}
然后我构建订单数据:
// Construct order data
$order_data = array(
'payment_method' => 'paystack',
'payment_method_title' => 'paystack',
'customer_id' => intval($customer_id),
'set_paid' => true,
'billing' => $billing_data,
'shipping' => $shipping_data,
'line_items' => array(
array(
'product_id' => intval($product_id),
'quantity' => intval($quantity)
)
),
'shipping_lines' => array(
array(
'method_id' => 'free_shipping',
'method_title' => 'Free Shipping',
'total' => '0.00'
)
)
);
其余代码基本保持不变:)
祝你好运,编码愉快!