Creates a transaction for an order

I want to add a payment to an unpaid order, but unfortunately the documentation does not match the behavior of the API. When I want to create a payment for the order, it asks me for a parent_id, which is not mandatory according to the documentation, and I don’t know where to get it from. I basically just want to create a payment for the order and mark the order as paid, but it doesn’t work.

Can someone please help me or explain my error?

Here is the code (PHP Curl)

$transactionData = [
‘transaction’ => [
“currency”=>“USD”,
“amount”=>“10.00”,
“kind”=>“capture”,
“test”=>true
]
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url . "/orders/$payItem[OrderId]/transactions.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($transactionData));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-Shopify-Access-Token: $access_token",
    "Content-Type: application/json"
]);

$response = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1 Like

Hi! Looking at the docs the problem seems to be the way you are using “capture”. I believe capture relates to already having the payment pre-authorized, and as such requires the parent_id. However, setting “kind” => “sale” should do the whole process in a single step. see below:

$transactionData = [
‘transaction’ => [
“currency”=>“USD”,
“amount”=>“10.00”,
“kind”=>“sale”,
“test”=>true
]
];

Here’s the full documentation about the kind key according to shopify

  • authorization: Money that the customer has agreed to pay. The authorization period can be between 7 and 30 days (depending on your payment service) while a store waits for a payment to be captured.
  • capture: A transfer of money that was reserved during the authorization of a shop.
  • sale: The authorization and capture of a payment performed in one single step.
  • void: The cancellation of a pending authorization or capture.
  • refund: The partial or full return of captured money to the customer.

To follow up on Garrett’s reply, make sure to also set the source if there is no parent

"source":"external"