Hi! I’d like to create subscription using Square payment link api. After this I’d like to log all charges on subscription and show them to the user, but I haven’t found a clear way on how to do this. This article How can I cancel subscription which was created by checkout api - #6 by supergodk mentions that subscription_id doesn’t sent in the response for createPaymentLink. In this case I have a lot of problems on how to match order, invoice, subscription together. Could you suggest me the correct way which allows to catch all successful and unsuccessful charges on subscription.
My current code using PHP SDK
function square_create_payment_link( $custom ): string|bool {
global $current_user, $square, $square_env_config;
$paymentLink = $square->checkout->paymentLinks->create(
new CreatePaymentLinkRequest( [
'idempotencyKey' => uniqid(),
'quickPay' => new QuickPay( [
'locationId' => $square_env_config['LOCATION_ID'],
'name' => 'Card protection',
'priceMoney' => new Money( [
'amount' => 100,
'currency' => Currency::Usd->value,
] ),
] ),
'checkoutOptions' => new CheckoutOptions( [
'subscriptionPlanId' => $square_env_config['SUBSCRIPTION_PLAN_VARIATION_DATA_ID'],
"redirectUrl" => get_pending_page()
] ),
'description' => 'Billed monthly',
] ),
);
if ( $paymentLink->getPaymentLink() ) {
return $paymentLink->getPaymentLink()->getUrl();
}
return false;
}
add_action( 'rest_api_init', 'square_init_rest_endpoints' );
function square_init_rest_endpoints(): void {
register_rest_route( 'payments/square', '/subscription-created', array(
'methods' => 'POST',
'permission_callback' => '__return_true',
'callback' => 'square_subscription_created',
) );
register_rest_route( 'payments/square', '/subscription-updated', array(
'methods' => 'POST',
'permission_callback' => '__return_true',
'callback' => 'square_subscription_updated',
) );
register_rest_route( 'payments/square', '/payment-updated', array(
'methods' => 'POST',
'permission_callback' => '__return_true',
'callback' => 'square_payment_updated',
) );
}