mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Add subscriptions api logic to Single product (WIP)
This commit is contained in:
parent
999231fd01
commit
01ab5d6003
3 changed files with 47 additions and 1 deletions
|
@ -18,6 +18,34 @@ class SingleProductActionHandler {
|
|||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
subscriptionsConfiguration() {
|
||||
return {
|
||||
createSubscription: (data, actions) => {
|
||||
return actions.subscription.create({
|
||||
'plan_id': this.config.subscription_plan_id
|
||||
});
|
||||
},
|
||||
onApprove: (data, actions) => {
|
||||
fetch(this.config.ajax.approve_subscription.endpoint, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({
|
||||
nonce: this.config.ajax.approve_subscription.nonce,
|
||||
order_id: data.orderID,
|
||||
subscription_id: data.subscriptionID
|
||||
})
|
||||
}).then((res)=>{
|
||||
return res.json();
|
||||
}).then((data) => {
|
||||
location.href = this.config.redirect;
|
||||
});
|
||||
},
|
||||
onError: (err) => {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configuration()
|
||||
{
|
||||
return {
|
||||
|
|
|
@ -110,6 +110,14 @@ class SingleProductBootstap {
|
|||
this.errorHandler,
|
||||
);
|
||||
|
||||
if(
|
||||
PayPalCommerceGateway.data_client_id.has_subscriptions
|
||||
&& PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled
|
||||
) {
|
||||
this.renderer.render(actionHandler.subscriptionsConfiguration());
|
||||
return;
|
||||
}
|
||||
|
||||
this.renderer.render(
|
||||
actionHandler.configuration()
|
||||
);
|
||||
|
|
|
@ -1380,13 +1380,23 @@ class SmartButton implements SmartButtonInterface {
|
|||
* @return string
|
||||
*/
|
||||
private function paypal_subscription_id(): string {
|
||||
if($this->subscription_helper->current_product_is_subscription() ) {
|
||||
$product = wc_get_product();
|
||||
assert( $product instanceof WC_Product );
|
||||
|
||||
if ( $product->get_type() === 'subscription' && $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
return $product->get_meta( 'ppcp_subscription_plan' )['id'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
foreach ( $items as $item ) {
|
||||
$product = wc_get_product( $item['product_id'] );
|
||||
assert( $product instanceof WC_Product );
|
||||
|
||||
if ( $product->get_type() === 'subscription' && $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
return $product->get_meta( 'ppcp_subscription_plan' )->id;
|
||||
return $product->get_meta( 'ppcp_subscription_plan' )['id'];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue