Sha256: 9c21c71ba0406f367db970c5c403408aca0ffd1464e3815b9a85c00d16f58928

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

<script src="https://www.paypal.com/sdk/js?client-id=<%= payment_method.preferred_client_id %>"></script>

<div id="paypal-button-container"></div>

<script>
  paypal.Buttons({
    createOrder: async function(data, actions) {
      try {
        const response = await fetch('/paypal/create_order', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'X-CSRF-Token': '<%= form_authenticity_token %>'
          },
          body: JSON.stringify({
            payment_method_id: '<%= payment_method.id %>'
          })
        });
    
        if (!response.ok) {
          throw new Error(`Server error: ${response.statusText}`);
        }
    
        const orderData = await response.json();
    
        if (!orderData.orderID) {
          throw new Error('Order ID not found in response');
        }
    
        return orderData.orderID;
    
      } catch (error) {
        console.error('Error creating PayPal order:', error);
        alert('There was an issue creating the PayPal order. Please try again later.');
        return null;
      }
    },
    onApprove: function(data, actions) {
      return fetch('/paypal/capture_order', {
        method: 'post',
        headers: {
          'Content-Type': 'application/json',
          'X-CSRF-Token': '<%= form_authenticity_token %>'
        },
        body: JSON.stringify({
          orderID: data.orderID,
          payment_method_id: '<%= payment_method.id %>'
        })
      }).then(function(res) {
        return res.json();
      }).then(function(details) {
        window.location.href = '<%= spree.order_path(@order) %>';
      });
    }
  }).render('#paypal-button-container');
</script>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_paypal-1.0.0 app/views/spree/checkout/payment/_paypal.html.erb
spree_paypal-0.1.2 app/views/spree/checkout/payment/_paypal.html.erb
spree_paypal-0.1.1 app/views/spree/checkout/payment/_paypal.html.erb
spree_paypal-0.1.0 app/views/spree/checkout/payment/_paypal.html.erb