付款成功后,Shopify将一个Webhook发送到我的订单管理系统,以更新我的客户已付款的本地数据库,并在订单上留下一条消息。
我对PHP不好,因此我愿意付钱给别人把它整理好。有什么好心可以帮助我吗?我认为那里使用的系统不相关,但是它是FreeBSD 12,nginx和PHP 7.3.4
public function mark_additionals_as_paid( )
{
define('SHOPIFY_APP_SECRET', 'MY_SECRET_KEY');
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = $this->_verify_webhook($data, $hmac_header);
if( $verified ) {
$returnedOrder = json_decode($data);
if( $returnedOrder->status == "completed" ) {
// search the database to see if this order is from a pending quote approval
$query = $this->order_details_model->get_order_via_shopify_id( $returnedOrder->id );
// if a result has been found, update it
if( $query->num_rows > 0 ) {
foreach ( $query->result() as $foundOrder ) {
$customer_has_paid = 0;
$customer_has_paid = $returnedOrder->total_price;
foreach ( $returnedOrder->line_items as $line_item ) {
// get the line item id contained in shopify response
$new_line_item_id = $line_item->id;
}
// update this order with the shopify data
$this->db->set( 'total_price_paid', 'total_price_paid+' . $customer_has_paid, FALSE );
$this->db->set( 'additional_order_id', $returnedOrder->order_id );
$this->db->where( 'order_id', $foundOrder->order_id );
$this->db->update('orders');
// add note to the order to show how much was paid
$data = array(
'order_id' => $foundOrder->order_id,
'note' => 'Customer paid £' . $returnedOrder->total_price . ' through Shopify',
'date_time' => date( 'Y-m-d H:i:s' ),
'user_id' => '1'
);
$this->timeline->insert( $data );
}
/*
once finished, query this order and see if the total_price_paid at least matches the total_price column,
being higher isn't the end of the world as a refund can be processed. whereas if it's less, the order cannot
be sent out
*/
$query = $this->order_details_model->get_order_via_shopify_id( $returnedOrder->id );
foreach ( $query->result() as $foundOrder ) {
$customer_paid = $foundOrder->total_price_paid;
$job_price = $foundOrder->total_price;
if( $customer_paid >= $job_price ){
// update this order with the shopify data
$this->db->set( 'pending_payment', 'n' );
$this->db->where( 'order_id', $foundOrder->order_id );
$this->db->update('orders');
// add note to the order to show outstanding balance has been paid
$data = array(
'order_id' => $foundOrder->order_id,
'note' => 'Outstanding balance has been paid (£' . $customer_has_paid . ')',
'date_time' => date( 'Y-m-d H:i:s' ),
'user_id' => '1'
);
$this->timeline->insert( $data );
}
}
}
}
// shopify keeps trying to send until an ok response is received
header('HTTP/1.0 200 OK');
} else {
header('HTTP/1.0 200 OK');
}
}