使用快速结账实现定期支付

4
TL;DR:我想在我的商店实现订阅,但是“paypalobjects.com/api/checkout.js”重定向到“sandbox.paypal.com/webapps/hermes/error”。常规付款按预期工作。我正在使用高级服务器整合的Express Checkout。

我的Paypal.Button:

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');

虽然不是很相关,但我的后端代码(包括测试数据)如下:

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返回了一个有效的响应,其中包括订单ID和状态为“CREATED”。(还有很多其他数据...)

{"id":"P-0SE01606VF925501Y2UAKG3Y","state":"CREATED","name":"Monthly subscription to Paypal","description":"Subscribtion.","type":"FIXED","payment_definitions":[{"id":"PD-35U317461H38251442UAKG4A","name":"Regular Payment Definition","type":"REGULAR","frequency":"Month","amount":{"currency":"NOK","value":"500"},"cycles":"12","charge_models":[{"id":"CHM-7T021625H451740052UAKG4A","type":"SHIPPING","amount":{"currency":"NOK","value":"0"}},{"id":"CHM-313690493W320615U2UAKG4A","type":"TAX","amount":{"currency":"NOK","value":"0"}}],"frequency_interval":"2"}],"merchant_preferences":{"setup_fee":{"currency":"NOK","value":"0"},"max_fail_attempts":"0","return_url":"http:\/\/www.vg.no","cancel_url":"http:\/\/www.yahoo.no","auto_bill_amount":"YES","initial_fail_amount_action":"CONTINUE"},"create_time":"2017-01-25T09:41:45.967Z","update_time":"2017-01-25T09:41:45.967Z","links":[{"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/billing-plans\/P-0SE01606VF925501Y2UAKG3Y","rel":"self","method":"GET"}]}

现在我的问题是,当我的PayPal按钮收到此响应时,它会处理信息并将我重定向到“sandbox.paypal.com/webapps/hermes/error”,这使得调试有些困难。
谢谢 :)

我们正在经历一个内部问题,我们的团队正在尽快修复它。- Paypal - LordMarty
你能否使用快速结账设置订阅? - Mike
是的,如果我没记错的话,上面的代码是有效的。 - LordMarty
1个回答

0
问题是Paypal的内部问题。现在已经解决了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接