使用Express Checkout重复付款

时间:2017-01-25 09:54:43

标签: paypal paypal-subscriptions

TL; DR:我正在尝试订阅我的商店,但“paypalobjects.com/api/checkout.js”重定向到“sandbox.paypal.com/webapps/hermes/error”。定期付款按预期工作。我正在使用Express Checkout高级服务器集成。

我的Paypal.Button:

template  <typename T, typename U> void * void_cast(U v) { static_cast<void *>(static_cast<T>(v)); }

不太相关,但我的后端看起来像这样(使用testdata):

paypal.Button.render({

        env: 'sandbox', // Optional: specify 'sandbox' environment

        payment: function(resolve, reject) {


            const token = localStorage.getItem('_token').split(' ')[1];
            if(!subscribe){
              var CREATE_PAYMENT_URL = `/store/${store}/paypal/create-payment/${orderId}?token=${token}`;
            }else{
              var CREATE_PAYMENT_URL = `/store/${store}/subscribe/paypal/create-payment/${orderId}?token=${token}`;
            }

            paypal.request.post(CREATE_PAYMENT_URL)
                .then(function(data) { resolve(data.id); })
                .catch(function(err) { console.log(err); });
        },

        onAuthorize: function(data) {

            const token = localStorage.getItem('_token').split(' ')[1];
              if(!subscribe){
                var EXECUTE_PAYMENT_URL = `/store/${store}/paypal/execute-payment?token=${token}`;
              }else{
                var EXECUTE_PAYMENT_URL = `/store/${store}/subscribe/paypal/execute-payment?token=${token}`;
              }


            paypal.request.post(EXECUTE_PAYMENT_URL,
                    { paymentID: data.paymentID, payerID: data.payerID })

                .then(function(data) {

                })
                .catch(function(err) { console.log("error " + err);});
        },
        onCancel: function(data){

          cancel();
          this.destroy();
        },
        onError: function (err) {
          console.log("ERROR OCCURRED!");
          console.log(err);
        }

  }, '#paypal-button');

我的后端从paypal返回一个有效的回复,其中包含订单的ID和状态“CREATED”。 (还有很多其他数据..)

public function createPaypalOrder(Request $request, $store, $orderId){

      $order = Order::with(['user', 'whitelabel'])->where('id', $orderId)->first();


      $amout = array(
        'value' => (string) $order->price/100,
        'currency' => 'NOK',
      );

      $shippingandtax = array(
        'value' => '0',
        'currency' => 'NOK',
      );

      $charge_models = array([
        'type'=> 'SHIPPING',
        'amount'=> $shippingandtax,
      ],
      [
        'type'=> 'TAX',
        'amount'=> $shippingandtax,
      ]);

      $payment_definitions_creation = array();
      array_push($payment_definitions_creation,[
        'name' => 'Regular Payment Definition',
        'type' => 'REGULAR',
        'frequency' => 'MONTH',
        'frequency_interval'=> '2',
        'amount' => $amout,
        'cycles' => '12',
        'charge_models' => $charge_models
      ]);

      $format = Config::get('constants.FRONTEND_URL')[env('APP_ENV')];
      $redirectBase = sprintf($format, $order->whitelabel->subdomain, 'orders/?order=' . $order->id);


      $merchant_preferences_temp = array(
        'value' => '0',
        'currency' => 'NOK'
      );
      $merchant_preferences = array(
          "setup_fee" => $merchant_preferences_temp,
          'return_url' => "http://www.vg.no",
          'cancel_url' => "http://www.yahoo.no",
          'auto_bill_amount' => 'YES',
          'initial_fail_amount_action' => 'CONTINUE',
          'max_fail_attempts' => '0'
      );

      $payment_definitions = array();
      array_push($payment_definitions, $payment_definitions_creation);

      $name = 'Monthly subscription to ' . (string)$order->whitelabel->title;
      $body = array(
        'name' => $name,
        'description' => 'Subscribtion.',
        'type' => 'fixed',
        'payment_definitions' => $payment_definitions_creation,
        "merchant_preferences"=> $merchant_preferences,
      );


      $token = $this->getPaypalToken($order);

      $client = new \GuzzleHttp\Client();
      $response = $client->post('https://api.sandbox.paypal.com/v1/payments/billing-plans', [
        'headers' => ['Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token],
        'json' => $body,
      ]);

      $paypalOrderCreation = json_decode($response->getBody());

      // add stuff to db
      $order->setTransactionId($paypalOrderCreation->id);
      return json_encode($paypalOrderCreation);

    }

现在我的问题是,当我的paypal.button收到此响应时,它处理信息并将我重定向到“sandbox.paypal.com/webapps/hermes/error”,这有点难以调试。

谢谢:)

1 个答案:

答案 0 :(得分:0)

问题是Paypal的内部问题。立即行动。